test: e2e/staking to system tests (#21882)

This commit is contained in:
Julián Toledano 2024-09-25 09:04:10 +02:00 committed by GitHub
parent 71f18076d6
commit c971f75acf
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 14 additions and 147 deletions

View File

@ -1,20 +0,0 @@
//go:build e2e
// +build e2e
package testutil
import (
"testing"
"github.com/stretchr/testify/suite"
"cosmossdk.io/simapp"
"github.com/cosmos/cosmos-sdk/testutil/network"
)
func TestE2ETestSuite(t *testing.T) {
cfg := network.DefaultConfig(simapp.NewTestNetworkFixture)
cfg.NumValidators = 2
suite.Run(t, NewE2ETestSuite(cfg))
}

View File

@ -1,122 +0,0 @@
package testutil
import (
"context"
"errors"
"testing"
"github.com/cometbft/cometbft/rpc/client/http"
"github.com/stretchr/testify/suite"
"cosmossdk.io/math"
banktypes "cosmossdk.io/x/bank/types"
stakingtypes "cosmossdk.io/x/staking/types"
"github.com/cosmos/cosmos-sdk/crypto/hd"
"github.com/cosmos/cosmos-sdk/crypto/keyring"
clitestutil "github.com/cosmos/cosmos-sdk/testutil/cli"
"github.com/cosmos/cosmos-sdk/testutil/network"
sdk "github.com/cosmos/cosmos-sdk/types"
)
type E2ETestSuite struct {
suite.Suite
cfg network.Config
network network.NetworkI
}
func NewE2ETestSuite(cfg network.Config) *E2ETestSuite {
return &E2ETestSuite{cfg: cfg}
}
func (s *E2ETestSuite) SetupSuite() {
s.T().Log("setting up e2e test suite")
if testing.Short() {
s.T().Skip("skipping test in unit-tests mode.")
}
var err error
s.network, err = network.New(s.T(), s.T().TempDir(), s.cfg)
s.Require().NoError(err)
}
func (s *E2ETestSuite) TearDownSuite() {
s.T().Log("tearing down e2e test suite")
s.network.Cleanup()
}
// TestBlockResults tests that the validator updates correctly show when
// calling the /block_results RPC endpoint.
// ref: https://github.com/cosmos/cosmos-sdk/issues/7401.
func (s *E2ETestSuite) TestBlockResults() {
require := s.Require()
val := s.network.GetValidators()[0]
// Create new account in the keyring.
k, _, err := val.GetClientCtx().Keyring.NewMnemonic("NewDelegator", keyring.English, sdk.FullFundraiserPath, keyring.DefaultBIP39Passphrase, hd.Secp256k1)
require.NoError(err)
pub, err := k.GetPubKey()
require.NoError(err)
newAddr := sdk.AccAddress(pub.Address())
msgSend := &banktypes.MsgSend{
FromAddress: val.GetAddress().String(),
ToAddress: newAddr.String(),
Amount: sdk.NewCoins(sdk.NewCoin(s.cfg.BondDenom, math.NewInt(200))),
}
// Send some funds to the new account.
_, err = clitestutil.SubmitTestTx(
val.GetClientCtx(),
msgSend,
val.GetAddress(),
clitestutil.TestTxConfig{},
)
require.NoError(err)
require.NoError(s.network.WaitForNextBlock())
msgDel := &stakingtypes.MsgDelegate{
DelegatorAddress: newAddr.String(),
ValidatorAddress: val.GetValAddress().String(),
Amount: sdk.NewCoin(s.cfg.BondDenom, math.NewInt(150)),
}
// create a delegation from the new account to validator `val`.
_, err = clitestutil.SubmitTestTx(
val.GetClientCtx(),
msgDel,
newAddr,
clitestutil.TestTxConfig{},
)
require.NoError(err)
require.NoError(s.network.WaitForNextBlock())
// Create a HTTP rpc client.
rpcClient, err := http.New(val.GetRPCAddress())
require.NoError(err)
// Loop until we find a block result with the correct validator updates.
// By experience, it happens around 2 blocks after `delHeight`.
_ = s.network.RetryForBlocks(func() error {
latestHeight, err := s.network.LatestHeight()
require.NoError(err)
res, err := rpcClient.BlockResults(context.Background(), &latestHeight)
if err != nil {
return err
}
if len(res.ValidatorUpdates) == 0 {
return errors.New("validator update not found yet")
}
valUpdate := res.ValidatorUpdates[0]
require.Equal(
valUpdate.PubKeyBytes,
val.GetPubKey().Bytes(),
)
return nil
}, 10)
}

View File

@ -182,7 +182,7 @@ go test -mod=readonly -tags='system_test' -v ./... --run TestQueryTotalSupply -
## Part 4: Set state via TX
Complexer workflows and tests require modifying state on a running chain. This works only with builtin logic and operations.
If we want to burn some our new tokens, we need to submit a bank burn message to do this.
If we want to burn some of our new tokens, we need to submit a bank burn message to do this.
The CLI wrapper works similar to the query. Just pass the parameters. It uses the `node0` key as *default*:
```go

View File

@ -12,6 +12,7 @@ import (
func TestStakeUnstake(t *testing.T) {
// Scenario:
// delegate tokens to validator
// check validator has been updated
// undelegate some tokens
sut.ResetChain(t)
@ -29,16 +30,24 @@ func TestStakeUnstake(t *testing.T) {
// query validator address to delegate tokens
rsp := cli.CustomQuery("q", "staking", "validators")
valAddr := gjson.Get(rsp, "validators.#.operator_address").Array()[0].String()
valPk := gjson.Get(rsp, "validators.#.consensus_pubkey.value").Array()[0].String()
// stake tokens
rsp = cli.RunAndWait("tx", "staking", "delegate", valAddr, "10000stake", "--from="+account1Addr, "--fees=1stake")
rsp = cli.RunAndWait("tx", "staking", "delegate", valAddr, "1000000stake", "--from="+account1Addr, "--fees=1stake")
RequireTxSuccess(t, rsp)
t.Log(cli.QueryBalance(account1Addr, "stake"))
assert.Equal(t, int64(9989999), cli.QueryBalance(account1Addr, "stake"))
assert.Equal(t, int64(8999999), cli.QueryBalance(account1Addr, "stake"))
// check validator has been updated
rsp = cli.CustomQuery("q", "block-results", gjson.Get(rsp, "height").String())
validatorUpdates := gjson.Get(rsp, "validator_updates").Array()
assert.NotEmpty(t, validatorUpdates)
vpk := gjson.Get(validatorUpdates[0].String(), "pub_key_bytes").String()
assert.Equal(t, vpk, valPk)
rsp = cli.CustomQuery("q", "staking", "delegation", account1Addr, valAddr)
assert.Equal(t, "10000", gjson.Get(rsp, "delegation_response.balance.amount").String(), rsp)
assert.Equal(t, "1000000", gjson.Get(rsp, "delegation_response.balance.amount").String(), rsp)
assert.Equal(t, "stake", gjson.Get(rsp, "delegation_response.balance.denom").String(), rsp)
// unstake tokens
@ -46,7 +55,7 @@ func TestStakeUnstake(t *testing.T) {
RequireTxSuccess(t, rsp)
rsp = cli.CustomQuery("q", "staking", "delegation", account1Addr, valAddr)
assert.Equal(t, "5000", gjson.Get(rsp, "delegation_response.balance.amount").String(), rsp)
assert.Equal(t, "995000", gjson.Get(rsp, "delegation_response.balance.amount").String(), rsp)
assert.Equal(t, "stake", gjson.Get(rsp, "delegation_response.balance.denom").String(), rsp)
rsp = cli.CustomQuery("q", "staking", "unbonding-delegation", account1Addr, valAddr)