Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: refactor remaining E2E tests for SDK v47 #2744

Merged
merged 11 commits into from
Sep 26, 2023
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 57 additions & 18 deletions tests/e2e/e2e_bank_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,50 +9,89 @@ import (

func (s *IntegrationTestSuite) testBankTokenTransfer() {
s.Run("send_photon_between_accounts", func() {
var err error
senderAddress, _ := s.chainA.validators[0].keyInfo.GetAddress()
sender := senderAddress.String()
var (
err error
valIdx = 0
c = s.chainA
chainEndpoint = fmt.Sprintf("http://%s", s.valResources[c.id][valIdx].GetHostPort("1317/tcp"))
)

recipientAddress, _ := s.chainA.validators[1].keyInfo.GetAddress()
recipient := recipientAddress.String()
// define one sender and two recipient accounts
alice, _ := c.genesisAccounts[1].keyInfo.GetAddress()
bob, _ := c.genesisAccounts[2].keyInfo.GetAddress()
charlie, _ := c.genesisAccounts[3].keyInfo.GetAddress()

chainAAPIEndpoint := fmt.Sprintf("http://%s", s.valResources[s.chainA.id][0].GetHostPort("1317/tcp"))
var beforeAliceUAtomBalance,
beforeBobUAtomBalance,
beforeCharlieUAtomBalance,
afterAliceUAtomBalance,
afterBobUAtomBalance,
afterCharlieUAtomBalance sdk.Coin

var (
beforeSenderUAtomBalance sdk.Coin
beforeRecipientUAtomBalance sdk.Coin
// get balances of sender and recipient accounts
s.Require().Eventually(
func() bool {
beforeAliceUAtomBalance, err = getSpecificBalance(chainEndpoint, alice.String(), uatomDenom)
s.Require().NoError(err)

beforeBobUAtomBalance, err = getSpecificBalance(chainEndpoint, bob.String(), uatomDenom)
s.Require().NoError(err)

beforeCharlieUAtomBalance, err = getSpecificBalance(chainEndpoint, charlie.String(), uatomDenom)
s.Require().NoError(err)

return beforeAliceUAtomBalance.IsValid() && beforeBobUAtomBalance.IsValid() && beforeCharlieUAtomBalance.IsValid()
},
10*time.Second,
5*time.Second,
)

// alice sends tokens to bob
s.execBankSend(s.chainA, valIdx, alice.String(), bob.String(), tokenAmount.String(), standardFees.String(), false)

// check that the transfer was successful
s.Require().Eventually(
func() bool {
beforeSenderUAtomBalance, err = getSpecificBalance(chainAAPIEndpoint, sender, uatomDenom)
afterAliceUAtomBalance, err = getSpecificBalance(chainEndpoint, alice.String(), uatomDenom)
s.Require().NoError(err)

beforeRecipientUAtomBalance, err = getSpecificBalance(chainAAPIEndpoint, recipient, uatomDenom)
afterBobUAtomBalance, err = getSpecificBalance(chainEndpoint, bob.String(), uatomDenom)
s.Require().NoError(err)

return beforeSenderUAtomBalance.IsValid() && beforeRecipientUAtomBalance.IsValid()
decremented := beforeAliceUAtomBalance.Sub(tokenAmount).Sub(standardFees).IsEqual(afterAliceUAtomBalance)
incremented := beforeBobUAtomBalance.Add(tokenAmount).IsEqual(afterBobUAtomBalance)

return decremented && incremented
},
10*time.Second,
5*time.Second,
)

s.execBankSend(s.chainA, 0, sender, recipient, tokenAmount.String(), standardFees.String(), false)
// save the updated account balances of alice and bob
beforeAliceUAtomBalance, beforeBobUAtomBalance = afterAliceUAtomBalance, afterBobUAtomBalance

// alice sends tokens to bob and charlie, at once
s.execBankMultiSend(s.chainA, valIdx, alice.String(), []string{bob.String(), charlie.String()}, tokenAmount.String(), standardFees.String(), false)

s.Require().Eventually(
func() bool {
afterSenderUAtomBalance, err := getSpecificBalance(chainAAPIEndpoint, sender, uatomDenom)
afterAliceUAtomBalance, err = getSpecificBalance(chainEndpoint, alice.String(), uatomDenom)
s.Require().NoError(err)

afterRecipientUAtomBalance, err := getSpecificBalance(chainAAPIEndpoint, recipient, uatomDenom)
afterBobUAtomBalance, err = getSpecificBalance(chainEndpoint, bob.String(), uatomDenom)
s.Require().NoError(err)

decremented := beforeSenderUAtomBalance.Sub(tokenAmount).Sub(standardFees).IsEqual(afterSenderUAtomBalance)
incremented := beforeRecipientUAtomBalance.Add(tokenAmount).IsEqual(afterRecipientUAtomBalance)
afterCharlieUAtomBalance, err = getSpecificBalance(chainEndpoint, charlie.String(), uatomDenom)
s.Require().NoError(err)

// assert alice's account gets decremented the amount of tokens twice
decremented := beforeAliceUAtomBalance.Sub(tokenAmount).Sub(tokenAmount).Sub(standardFees).IsEqual(afterAliceUAtomBalance)
incremented := beforeBobUAtomBalance.Add(tokenAmount).IsEqual(afterBobUAtomBalance) &&
beforeCharlieUAtomBalance.Add(tokenAmount).IsEqual(afterCharlieUAtomBalance)

return decremented && incremented
},
time.Minute,
10*time.Second,
5*time.Second,
)
})
Expand Down
94 changes: 92 additions & 2 deletions tests/e2e/e2e_exec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,44 @@ func (s *IntegrationTestSuite) execBankSend(
s.executeGaiaTxCommand(ctx, c, gaiaCommand, valIdx, s.expectErrExecValidation(c, valIdx, expectErr))
}

func (s *IntegrationTestSuite) execBankMultiSend(
c *chain,
valIdx int,
from string,
to []string,
amt string,
fees string,
expectErr bool,
opt ...flagOption,
) {
// TODO remove the hardcode opt after refactor, all methods should accept custom flags
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TODO copied from ExecBankSend and will be addressed in another refactoring.

opt = append(opt, withKeyValue(flagFees, fees))
opt = append(opt, withKeyValue(flagFrom, from))
opts := applyOptions(c.id, opt)

ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
defer cancel()

s.T().Logf("sending %s tokens from %s to %s on chain %s", amt, from, to, c.id)

gaiaCommand := []string{
gaiadBinary,
txCommand,
banktypes.ModuleName,
"multi-send",
from,
}

gaiaCommand = append(gaiaCommand, to...)
gaiaCommand = append(gaiaCommand, amt, "-y")

for flag, value := range opts {
gaiaCommand = append(gaiaCommand, fmt.Sprintf("--%s=%v", flag, value))
}

s.executeGaiaTxCommand(ctx, c, gaiaCommand, valIdx, s.expectErrExecValidation(c, valIdx, expectErr))
}

type txBankSend struct {
from string
to string
Expand Down Expand Up @@ -428,7 +466,7 @@ func (s *IntegrationTestSuite) runGovExec(c *chain, valIdx int, submitterAddr, g
// })
// }

func (s *IntegrationTestSuite) executeDelegate(c *chain, valIdx int, amount, valOperAddress, delegatorAddr, home, delegateFees string) { //nolint:unparam
func (s *IntegrationTestSuite) execDelegate(c *chain, valIdx int, amount, valOperAddress, delegatorAddr, home, delegateFees string) { //nolint:unparam

ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
defer cancel()
Expand All @@ -455,8 +493,60 @@ func (s *IntegrationTestSuite) executeDelegate(c *chain, valIdx int, amount, val
s.T().Logf("%s successfully delegated %s to %s", delegatorAddr, amount, valOperAddress)
}

func (s *IntegrationTestSuite) executeRedelegate(c *chain, valIdx int, amount, originalValOperAddress,
func (s *IntegrationTestSuite) execUnbondDelegation(c *chain, valIdx int, amount, valOperAddress, delegatorAddr, home, delegateFees string) {
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
defer cancel()

s.T().Logf("Executing gaiad tx staking unbond %s", c.id)

gaiaCommand := []string{
gaiadBinary,
txCommand,
stakingtypes.ModuleName,
"unbond",
valOperAddress,
amount,
fmt.Sprintf("--%s=%s", flags.FlagFrom, delegatorAddr),
fmt.Sprintf("--%s=%s", flags.FlagChainID, c.id),
fmt.Sprintf("--%s=%s", flags.FlagGasPrices, delegateFees),
"--keyring-backend=test",
fmt.Sprintf("--%s=%s", flags.FlagHome, home),
"--output=json",
"-y",
}

s.executeGaiaTxCommand(ctx, c, gaiaCommand, valIdx, s.defaultExecValidation(c, valIdx))
s.T().Logf("%s successfully undelegated %s to %s", delegatorAddr, amount, valOperAddress)
}

func (s *IntegrationTestSuite) execCancelUnbondingDelegation(c *chain, valIdx int, amount, valOperAddress, creationHeight, delegatorAddr, home, delegateFees string) {
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
defer cancel()

s.T().Logf("Executing gaiad tx staking cancel-unbond %s", c.id)

gaiaCommand := []string{
gaiadBinary,
txCommand,
stakingtypes.ModuleName,
"cancel-unbond",
valOperAddress,
amount,
creationHeight,
fmt.Sprintf("--%s=%s", flags.FlagFrom, delegatorAddr),
fmt.Sprintf("--%s=%s", flags.FlagChainID, c.id),
fmt.Sprintf("--%s=%s", flags.FlagGasPrices, delegateFees),
"--keyring-backend=test",
fmt.Sprintf("--%s=%s", flags.FlagHome, home),
"--output=json",
"-y",
}

s.executeGaiaTxCommand(ctx, c, gaiaCommand, valIdx, s.defaultExecValidation(c, valIdx))
s.T().Logf("%s successfully canceled unbonding %s to %s", delegatorAddr, amount, valOperAddress)
}

func (s *IntegrationTestSuite) execRedelegate(c *chain, valIdx int, amount, originalValOperAddress,
newValOperAddress, delegatorAddr, home, delegateFees string,
) {
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
Expand Down
85 changes: 81 additions & 4 deletions tests/e2e/e2e_staking_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@ package e2e

import (
"fmt"
"strconv"
"time"

"cosmossdk.io/math"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/staking/types"
)

func (s *IntegrationTestSuite) testStaking() {
Expand All @@ -26,7 +29,7 @@ func (s *IntegrationTestSuite) testStaking() {
delegation := sdk.NewCoin(uatomDenom, delegationAmount) // 500 atom

// Alice delegate uatom to Validator A
s.executeDelegate(s.chainA, 0, delegation.String(), validatorAddressA, delegatorAddress.String(), gaiaHomePath, fees.String())
s.execDelegate(s.chainA, 0, delegation.String(), validatorAddressA, delegatorAddress.String(), gaiaHomePath, fees.String())

// Validate delegation successful
s.Require().Eventually(
Expand All @@ -41,8 +44,11 @@ func (s *IntegrationTestSuite) testStaking() {
5*time.Second,
)

// Alice re-delegate uatom from Validator A to Validator B
s.executeRedelegate(s.chainA, 0, delegation.String(), validatorAddressA, validatorAddressB, delegatorAddress.String(), gaiaHomePath, fees.String())
redelegationAmount := delegationAmount.Quo(sdk.NewInt(2))
redelegation := sdk.NewCoin(uatomDenom, redelegationAmount) // 250 atom

// Alice re-delegate half of her uatom delegation from Validator A to Validator B
s.execRedelegate(s.chainA, 0, redelegation.String(), validatorAddressA, validatorAddressB, delegatorAddress.String(), gaiaHomePath, fees.String())

// Validate re-delegation successful
s.Require().Eventually(
Expand All @@ -51,7 +57,78 @@ func (s *IntegrationTestSuite) testStaking() {
amt := res.GetDelegationResponse().GetDelegation().GetShares()
s.Require().NoError(err)

return amt.Equal(sdk.NewDecFromInt(delegationAmount))
return amt.Equal(sdk.NewDecFromInt(redelegationAmount))
},
20*time.Second,
5*time.Second,
)

var (
currDelegation sdk.Coin
currDelegationAmount math.Int
)

// query alice's current delegation from validator A
s.Require().Eventually(
func() bool {
res, err := queryDelegation(chainEndpoint, validatorAddressA, delegatorAddress.String())
amt := res.GetDelegationResponse().GetDelegation().GetShares()
s.Require().NoError(err)

currDelegationAmount = amt.TruncateInt()
currDelegation = sdk.NewCoin(uatomDenom, currDelegationAmount)

return currDelegation.IsValid()
},
20*time.Second,
5*time.Second,
)

// Alice unbonds all her uatom delegation from Validator A
s.execUnbondDelegation(s.chainA, 0, currDelegation.String(), validatorAddressA, delegatorAddress.String(), gaiaHomePath, fees.String())

var ubdDelegationEntry types.UnbondingDelegationEntry

// validate unbonding delegations
s.Require().Eventually(
func() bool {
res, err := queryUnbondingDelegation(chainEndpoint, validatorAddressA, delegatorAddress.String())
s.Require().NoError(err)

s.Require().Len(res.GetUnbond().Entries, 1)
ubdDelegationEntry = res.GetUnbond().Entries[0]

return ubdDelegationEntry.Balance.Equal(currDelegationAmount)
},
20*time.Second,
5*time.Second,
)

// cancel the full amount of unbonding delegations from Validator A
s.execCancelUnbondingDelegation(
s.chainA,
0,
currDelegation.String(),
validatorAddressA,
strconv.Itoa(int(ubdDelegationEntry.CreationHeight)),
delegatorAddress.String(),
gaiaHomePath,
fees.String(),
)

// validate that unbonding delegation was successfully canceled
s.Require().Eventually(
func() bool {
resDel, err := queryDelegation(chainEndpoint, validatorAddressA, delegatorAddress.String())
amt := resDel.GetDelegationResponse().GetDelegation().GetShares()
s.Require().NoError(err)

// expect that no unbonding delegations are found for validator A
_, err = queryUnbondingDelegation(chainEndpoint, validatorAddressA, delegatorAddress.String())
s.Require().Error(err)

// expect to get the delegation back
return amt.Equal(sdk.NewDecFromInt(currDelegationAmount))
},
20*time.Second,
5*time.Second,
Expand Down
6 changes: 3 additions & 3 deletions tests/e2e/e2e_vesting_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func (s *IntegrationTestSuite) testDelayedVestingAccount(api string) {
s.Require().Equal(vestingBalance.AmountOf(uatomDenom), balance.Amount)

// Delegate coins should succeed
s.executeDelegate(chain, valIdx, vestingDelegationAmount.String(), valOpAddr,
s.execDelegate(chain, valIdx, vestingDelegationAmount.String(), valOpAddr,
vestingDelayedAcc.String(), gaiaHomePath, vestingDelegationFees.String())

// Validate delegation successful
Expand Down Expand Up @@ -128,7 +128,7 @@ func (s *IntegrationTestSuite) testContinuousVestingAccount(api string) {
s.Require().Equal(vestingBalance.AmountOf(uatomDenom), balance.Amount)

// Delegate coins should succeed
s.executeDelegate(chain, valIdx, vestingDelegationAmount.String(),
s.execDelegate(chain, valIdx, vestingDelegationAmount.String(),
valOpAddr, continuousVestingAcc.String(), gaiaHomePath, vestingDelegationFees.String())

// Validate delegation successful
Expand Down Expand Up @@ -267,7 +267,7 @@ func (s *IntegrationTestSuite) testPeriodicVestingAccount(api string) { //nolint
}

// Delegate coins should succeed
s.executeDelegate(chain, valIdx, vestingDelegationAmount.String(), valOpAddr,
s.execDelegate(chain, valIdx, vestingDelegationAmount.String(), valOpAddr,
periodicVestingAddr, gaiaHomePath, vestingDelegationFees.String())

// Validate delegation successful
Expand Down
Loading
Loading