From 7297ef8378fe9601465caa27e89f609b25fcef3f Mon Sep 17 00:00:00 2001 From: Julian Toledano Date: Tue, 24 Sep 2024 16:33:00 +0200 Subject: [PATCH 1/2] update: staking system test checks . del: e2e staking tests --- tests/e2e/staking/cli_test.go | 20 ----- tests/e2e/staking/suite.go | 122 --------------------------- tests/systemtests/getting_started.md | 2 +- tests/systemtests/staking_test.go | 17 +++- 4 files changed, 14 insertions(+), 147 deletions(-) delete mode 100644 tests/e2e/staking/cli_test.go delete mode 100644 tests/e2e/staking/suite.go diff --git a/tests/e2e/staking/cli_test.go b/tests/e2e/staking/cli_test.go deleted file mode 100644 index 0705c5645e63..000000000000 --- a/tests/e2e/staking/cli_test.go +++ /dev/null @@ -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)) -} diff --git a/tests/e2e/staking/suite.go b/tests/e2e/staking/suite.go deleted file mode 100644 index 1d08429f3b09..000000000000 --- a/tests/e2e/staking/suite.go +++ /dev/null @@ -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) -} diff --git a/tests/systemtests/getting_started.md b/tests/systemtests/getting_started.md index 47bee4dafa5f..7adc98f1b3e9 100644 --- a/tests/systemtests/getting_started.md +++ b/tests/systemtests/getting_started.md @@ -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 diff --git a/tests/systemtests/staking_test.go b/tests/systemtests/staking_test.go index 5f8f911191bc..20ed7ee44513 100644 --- a/tests/systemtests/staking_test.go +++ b/tests/systemtests/staking_test.go @@ -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() + valAddrPk := 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 + endBlockRsp := cli.CustomQuery("q", "block-results", gjson.Get(rsp, "height").String()) + validatorUpdates := gjson.Get(endBlockRsp, "validator_updates").Array() + assert.NotEmpty(t, validatorUpdates) + pubKey := gjson.Get(validatorUpdates[0].String(), "pub_key_bytes").String() + assert.Equal(t, pubKey, valAddrPk) 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) From 12e621945034809821998356de5cbd6e64679fee Mon Sep 17 00:00:00 2001 From: Julian Toledano Date: Tue, 24 Sep 2024 16:40:43 +0200 Subject: [PATCH 2/2] nit --- tests/systemtests/staking_test.go | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/tests/systemtests/staking_test.go b/tests/systemtests/staking_test.go index 20ed7ee44513..58b6c863a744 100644 --- a/tests/systemtests/staking_test.go +++ b/tests/systemtests/staking_test.go @@ -30,7 +30,7 @@ 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() - valAddrPk := gjson.Get(rsp, "validators.#.consensus_pubkey.value").Array()[0].String() + valPk := gjson.Get(rsp, "validators.#.consensus_pubkey.value").Array()[0].String() // stake tokens rsp = cli.RunAndWait("tx", "staking", "delegate", valAddr, "1000000stake", "--from="+account1Addr, "--fees=1stake") @@ -40,11 +40,11 @@ func TestStakeUnstake(t *testing.T) { assert.Equal(t, int64(8999999), cli.QueryBalance(account1Addr, "stake")) // check validator has been updated - endBlockRsp := cli.CustomQuery("q", "block-results", gjson.Get(rsp, "height").String()) - validatorUpdates := gjson.Get(endBlockRsp, "validator_updates").Array() + rsp = cli.CustomQuery("q", "block-results", gjson.Get(rsp, "height").String()) + validatorUpdates := gjson.Get(rsp, "validator_updates").Array() assert.NotEmpty(t, validatorUpdates) - pubKey := gjson.Get(validatorUpdates[0].String(), "pub_key_bytes").String() - assert.Equal(t, pubKey, valAddrPk) + 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, "1000000", gjson.Get(rsp, "delegation_response.balance.amount").String(), rsp)