From d44ae7141ae4098eac531490a4aae09bfb31be74 Mon Sep 17 00:00:00 2001 From: Keefe Liu Date: Tue, 14 Feb 2023 16:12:25 +0800 Subject: [PATCH] fix bugs --- x/slashing/client/testutil/cli_test.go | 11 +- .../client/testutil/impeach_validator.go | 189 ++++++----- x/staking/client/testutil/cli_test.go | 7 +- x/staking/client/testutil/create_validator.go | 303 ++++++++++-------- x/staking/client/testutil/test_helpers.go | 6 +- 5 files changed, 290 insertions(+), 226 deletions(-) diff --git a/x/slashing/client/testutil/cli_test.go b/x/slashing/client/testutil/cli_test.go index f62c81bcc6..9c2c747f40 100644 --- a/x/slashing/client/testutil/cli_test.go +++ b/x/slashing/client/testutil/cli_test.go @@ -7,24 +7,27 @@ import ( "testing" "time" + "github.com/stretchr/testify/require" + "github.com/stretchr/testify/suite" + "github.com/cosmos/cosmos-sdk/testutil/network" sdk "github.com/cosmos/cosmos-sdk/types" v1 "github.com/cosmos/cosmos-sdk/x/gov/types/v1" - "github.com/stretchr/testify/require" - "github.com/stretchr/testify/suite" ) func TestIntegrationTestSuite(t *testing.T) { cfg := network.DefaultConfig() - cfg.NumValidators = 3 + cfg.NumValidators = 1 + suite.Run(t, NewIntegrationTestSuite(cfg)) dp := v1.NewDepositParams(sdk.NewCoins(sdk.NewCoin(cfg.BondDenom, v1.DefaultMinDepositTokens)), time.Duration(15)*time.Second) - vp := v1.NewVotingParams(time.Duration(10) * time.Second) + vp := v1.NewVotingParams(time.Duration(5) * time.Second) genesisState := v1.DefaultGenesisState() genesisState.DepositParams = &dp genesisState.VotingParams = &vp bz, err := cfg.Codec.MarshalJSON(genesisState) require.NoError(t, err) cfg.GenesisState["gov"] = bz + cfg.NumValidators = 3 suite.Run(t, NewImpeachValidatorTestSuite(cfg)) } diff --git a/x/slashing/client/testutil/impeach_validator.go b/x/slashing/client/testutil/impeach_validator.go index 6c76db2af2..b2d7e913ee 100644 --- a/x/slashing/client/testutil/impeach_validator.go +++ b/x/slashing/client/testutil/impeach_validator.go @@ -4,25 +4,45 @@ import ( "encoding/base64" "fmt" "os" - "strings" - "time" + + "github.com/stretchr/testify/suite" + tmcli "github.com/tendermint/tendermint/libs/cli" "github.com/cosmos/cosmos-sdk/client/flags" "github.com/cosmos/cosmos-sdk/testutil" clitestutil "github.com/cosmos/cosmos-sdk/testutil/cli" "github.com/cosmos/cosmos-sdk/testutil/network" sdk "github.com/cosmos/cosmos-sdk/types" - authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" govcli "github.com/cosmos/cosmos-sdk/x/gov/client/cli" - gov "github.com/cosmos/cosmos-sdk/x/gov/types" v1 "github.com/cosmos/cosmos-sdk/x/gov/types/v1" "github.com/cosmos/cosmos-sdk/x/staking/client/cli" - stakingtestutil "github.com/cosmos/cosmos-sdk/x/staking/client/testutil" "github.com/cosmos/cosmos-sdk/x/staking/types" - "github.com/stretchr/testify/suite" - tmcli "github.com/tendermint/tendermint/libs/cli" ) +type testParams struct { + discription string + proposalID string + from string +} + +var testcases = []testParams{ + { + discription: "impeach the last validator", + proposalID: "1", + from: "0x7b5Fe22B5446f7C62Ea27B8BD71CeF94e03f3dF2", + }, + { + discription: "impeach the last validator again", + proposalID: "2", + from: "0x7b5Fe22B5446f7C62Ea27B8BD71CeF94e03f3dF2", + }, + { + discription: "impeach the last validator, but invalid from", + proposalID: "3", + from: "0x8b70dC9B691fCeB4e1c69dF8cbF8c077AD4b5853", + }, +} + type ImpeachValidatorTestSuite struct { suite.Suite @@ -45,29 +65,11 @@ func (s *ImpeachValidatorTestSuite) SetupSuite() { _, err = s.network.WaitForHeight(1) s.Require().NoError(err) - // Invalid proposal (--from address not gov) - s.submitProposal(s.network.Validators[0].ValAddress.String(), false) - - // Valid proposal - s.submitProposal(gov.ModuleName, true) - proposalID := fmt.Sprintf("%d", 1) - s.proposalIDs = append(s.proposalIDs, proposalID) - - // Valid proposal to impeach the same validator that was already impeached - s.submitProposal(gov.ModuleName, true) - proposalIDInvalidImpeached := fmt.Sprintf("%d", 2) - s.proposalIDs = append(s.proposalIDs, proposalIDInvalidImpeached) - - for _, proposal := range s.proposalIDs { - s.voteProposal(proposal, s.network.Validators[0], "yes") - s.voteProposal(proposal, s.network.Validators[1], "yes") - s.voteProposal(proposal, s.network.Validators[2], "no") + for _, testcase := range testcases { + s.submitProposal(testcase) + s.voteProposal(testcase.proposalID) + s.proposalIDs = append(s.proposalIDs, testcase.proposalID) } - - time.Sleep(10 * time.Second) - - s.TestQuerySuccessfulImpeachedValidator() - s.TestQueryInvalidAlreadyImpeached() } func (s *ImpeachValidatorTestSuite) SetupNewSuite() { @@ -81,59 +83,15 @@ func (s *ImpeachValidatorTestSuite) SetupNewSuite() { s.Require().NoError(err) } -func (s *ImpeachValidatorTestSuite) submitProposal(from string, isValidTestCase bool) { - val := s.network.Validators[0] - clientCtx := val.ClientCtx - - kickedVal := s.network.Validators[2] - - args := append([]string{ - s.ImpeachValidatorProposal(kickedVal.Address, from).Name(), - fmt.Sprintf("--%s=%s", flags.FlagFrom, val.ValAddress.String()), - fmt.Sprintf("--gas=%s", fmt.Sprintf("%d", flags.DefaultGasLimit+100000)), - }, stakingtestutil.CommonArgs...) - - response, err := clitestutil.ExecTestCLICmd(clientCtx, govcli.NewCmdSubmitProposal(), args) - - if err == nil { - splits := strings.Split(response.String(), ",") - splits = strings.Split(splits[2], ":") - codespace := strings.Trim(splits[1], "\"") - if string(codespace) != "" { - err = gov.ErrInvalidProposalMsg - } - } - - if isValidTestCase { - s.Require().NoError(err) - } else { - s.Require().Error(err) - } -} - -func (s *ImpeachValidatorTestSuite) voteProposal(proposalID string, val *network.Validator, voteOption string) { - clientCtx := val.ClientCtx - clientCtx.Client = s.network.Validators[0].RPCClient - - args := append([]string{ - proposalID, - voteOption, - fmt.Sprintf("--%s=%s", flags.FlagFrom, val.Address.String()), - }, stakingtestutil.CommonArgs...) - _, err := clitestutil.ExecTestCLICmd(clientCtx, govcli.NewCmdVote(), args) - s.Require().NoError(err) -} - func (s *ImpeachValidatorTestSuite) TearDownSuite() { s.T().Log("tearing down test suite") s.network.Cleanup() } func (s *ImpeachValidatorTestSuite) TestQuerySuccessfulImpeachedValidator() { - val := s.network.Validators[0] - clientCtx := val.ClientCtx proposalID := s.proposalIDs[0] - kickedVal := s.network.Validators[2] + targetVal := s.network.Validators[len(s.network.Validators)-1] + clientCtx := s.network.Validators[0].ClientCtx // query proposal args := []string{proposalID, fmt.Sprintf("--%s=json", tmcli.OutputFlag)} @@ -146,21 +104,20 @@ func (s *ImpeachValidatorTestSuite) TestQuerySuccessfulImpeachedValidator() { // query validator queryCmd := cli.GetCmdQueryValidator() res, err := clitestutil.ExecTestCLICmd( - val.ClientCtx, queryCmd, - []string{kickedVal.Address.String(), fmt.Sprintf("--%s=json", tmcli.OutputFlag)}, + clientCtx, queryCmd, + []string{targetVal.Address.String(), fmt.Sprintf("--%s=json", tmcli.OutputFlag)}, ) s.Require().NoError(err) var result types.Validator - s.Require().NoError(val.ClientCtx.Codec.UnmarshalJSON(res.Bytes(), &result)) - s.Require().NotEqual(result.GetStatus(), types.Bonded, fmt.Sprintf("validator %s not in bonded status", kickedVal.Address.String())) + s.Require().NoError(clientCtx.Codec.UnmarshalJSON(res.Bytes(), &result)) + s.Require().NotEqual(result.GetStatus(), types.Bonded, fmt.Sprintf("validator %s not in bonded status", targetVal.Address.String())) s.Require().Equal(result.Jailed, true) } -func (s *ImpeachValidatorTestSuite) TestQueryInvalidAlreadyImpeached() { - val := s.network.Validators[0] - clientCtx := val.ClientCtx +func (s *ImpeachValidatorTestSuite) TestQueryDoubleImpeachedValidator() { proposalID := s.proposalIDs[1] - kickedVal := s.network.Validators[2] + targetVal := s.network.Validators[len(s.network.Validators)-1] + clientCtx := s.network.Validators[0].ClientCtx // query proposal args := []string{proposalID, fmt.Sprintf("--%s=json", tmcli.OutputFlag)} @@ -173,17 +130,46 @@ func (s *ImpeachValidatorTestSuite) TestQueryInvalidAlreadyImpeached() { // query validator queryCmd := cli.GetCmdQueryValidator() res, err := clitestutil.ExecTestCLICmd( - val.ClientCtx, queryCmd, - []string{kickedVal.Address.String(), fmt.Sprintf("--%s=json", tmcli.OutputFlag)}, + clientCtx, queryCmd, + []string{targetVal.Address.String(), fmt.Sprintf("--%s=json", tmcli.OutputFlag)}, ) s.Require().NoError(err) var result types.Validator - s.Require().NoError(val.ClientCtx.Codec.UnmarshalJSON(res.Bytes(), &result)) - s.Require().NotEqual(result.GetStatus(), types.Bonded, fmt.Sprintf("validator %s not in bonded status", kickedVal.Address.String())) + s.Require().NoError(clientCtx.Codec.UnmarshalJSON(res.Bytes(), &result)) + s.Require().NotEqual(result.GetStatus(), types.Bonded, fmt.Sprintf("validator %s not in bonded status", targetVal.Address.String())) s.Require().Equal(result.Jailed, true) } -func (s *ImpeachValidatorTestSuite) ImpeachValidatorProposal(valAddr sdk.AccAddress, from string) *os.File { +func (s *ImpeachValidatorTestSuite) TestQueryInvalidFromAddress() { + proposalID := s.proposalIDs[2] + clientCtx := s.network.Validators[0].ClientCtx + + // query proposal, should not be found because of invalid from the proposal will be rejected. + args := []string{proposalID, fmt.Sprintf("--%s=json", tmcli.OutputFlag)} + _, err := clitestutil.ExecTestCLICmd(clientCtx, govcli.GetCmdQueryProposal(), args) + s.Require().Error(err) +} + +func (s *ImpeachValidatorTestSuite) submitProposal(params testParams) { + val := s.network.Validators[0] + clientCtx := val.ClientCtx + + // Always impeach the last validator. + targetVal := s.network.Validators[len(s.network.Validators)-1] + args := []string{ + s.impeachValidatorProposal(targetVal.Address, params.from).Name(), + fmt.Sprintf("--%s=%s", flags.FlagFrom, val.ValAddress.String()), + fmt.Sprintf("--gas=%s", fmt.Sprintf("%d", flags.DefaultGasLimit+100000)), + fmt.Sprintf("--%s=true", flags.FlagSkipConfirmation), + fmt.Sprintf("--%s=%s", flags.FlagBroadcastMode, flags.BroadcastBlock), + fmt.Sprintf("--%s=%s", flags.FlagFees, sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, sdk.NewInt(10))).String()), + } + + _, err := clitestutil.ExecTestCLICmd(clientCtx, govcli.NewCmdSubmitProposal(), args) + s.Require().NoError(err) +} + +func (s *ImpeachValidatorTestSuite) impeachValidatorProposal(valAddr sdk.AccAddress, from string) *os.File { propMetadata := []byte{42} proposal := fmt.Sprintf(` { @@ -197,7 +183,7 @@ func (s *ImpeachValidatorTestSuite) ImpeachValidatorProposal(valAddr sdk.AccAddr "metadata": "%s", "deposit": "%s" }`, - authtypes.NewModuleAddress(from), + from, valAddr.String(), base64.StdEncoding.EncodeToString(propMetadata), sdk.NewCoin(s.cfg.BondDenom, v1.DefaultMinDepositTokens), @@ -206,3 +192,28 @@ func (s *ImpeachValidatorTestSuite) ImpeachValidatorProposal(valAddr sdk.AccAddr proposalFile := testutil.WriteToNewTempFile(s.T(), proposal) return proposalFile } + +func (s *ImpeachValidatorTestSuite) voteProposal(proposalID string) { + for i := 0; i < len(s.network.Validators); i++ { + clientCtx := s.network.Validators[i].ClientCtx + clientCtx.Client = s.network.Validators[0].RPCClient + + // The last validator vote no, others vote yes. + voteOption := "yes" + if i == len(s.network.Validators)-1 { + voteOption = "no" + } + + args := []string{ + proposalID, + voteOption, + fmt.Sprintf("--%s=%s", flags.FlagFrom, s.network.Validators[i].Address.String()), + fmt.Sprintf("--%s=true", flags.FlagSkipConfirmation), + fmt.Sprintf("--%s=%s", flags.FlagBroadcastMode, flags.BroadcastBlock), + fmt.Sprintf("--%s=%s", flags.FlagFees, sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, sdk.NewInt(10))).String()), + } + + _, err := clitestutil.ExecTestCLICmd(clientCtx, govcli.NewCmdVote(), args) + s.Require().NoError(err) + } +} diff --git a/x/staking/client/testutil/cli_test.go b/x/staking/client/testutil/cli_test.go index 641e1688a2..434af15456 100644 --- a/x/staking/client/testutil/cli_test.go +++ b/x/staking/client/testutil/cli_test.go @@ -7,16 +7,18 @@ import ( "testing" "time" + "github.com/stretchr/testify/require" + "github.com/stretchr/testify/suite" + "github.com/cosmos/cosmos-sdk/testutil/network" sdk "github.com/cosmos/cosmos-sdk/types" v1 "github.com/cosmos/cosmos-sdk/x/gov/types/v1" - "github.com/stretchr/testify/require" - "github.com/stretchr/testify/suite" ) func TestIntegrationTestSuite(t *testing.T) { cfg := network.DefaultConfig() cfg.NumValidators = 2 + suite.Run(t, NewIntegrationTestSuite(cfg)) dp := v1.NewDepositParams(sdk.NewCoins(sdk.NewCoin(cfg.BondDenom, v1.DefaultMinDepositTokens)), time.Duration(15)*time.Second) vp := v1.NewVotingParams(time.Duration(5) * time.Second) @@ -26,5 +28,6 @@ func TestIntegrationTestSuite(t *testing.T) { bz, err := cfg.Codec.MarshalJSON(genesisState) require.NoError(t, err) cfg.GenesisState["gov"] = bz + cfg.NumValidators = 2 suite.Run(t, NewCreateValidatorTestSuite(cfg)) } diff --git a/x/staking/client/testutil/create_validator.go b/x/staking/client/testutil/create_validator.go index 6e920f2ddb..bc64a8fc54 100644 --- a/x/staking/client/testutil/create_validator.go +++ b/x/staking/client/testutil/create_validator.go @@ -2,12 +2,15 @@ package testutil import ( "encoding/base64" - "encoding/hex" "fmt" "os" - "strings" "time" + authztestutil "github.com/cosmos/cosmos-sdk/x/authz/client/testutil" + + "github.com/stretchr/testify/suite" + tmcli "github.com/tendermint/tendermint/libs/cli" + "github.com/cosmos/cosmos-sdk/client/flags" "github.com/cosmos/cosmos-sdk/crypto/keyring" "github.com/cosmos/cosmos-sdk/crypto/keys/ed25519" @@ -16,7 +19,6 @@ import ( "github.com/cosmos/cosmos-sdk/testutil/network" sdk "github.com/cosmos/cosmos-sdk/types" authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" - authztestutil "github.com/cosmos/cosmos-sdk/x/authz/client/testutil" banktestutil "github.com/cosmos/cosmos-sdk/x/bank/client/testutil" govcli "github.com/cosmos/cosmos-sdk/x/gov/client/cli" gov "github.com/cosmos/cosmos-sdk/x/gov/types" @@ -24,11 +26,60 @@ import ( "github.com/cosmos/cosmos-sdk/x/staking/client/cli" "github.com/cosmos/cosmos-sdk/x/staking/types" ethHd "github.com/evmos/ethermint/crypto/hd" - "github.com/prysmaticlabs/prysm/crypto/bls" - "github.com/stretchr/testify/suite" - tmcli "github.com/tendermint/tendermint/libs/cli" ) +type testParams struct { + discription string + proposalID string + enableGrant bool + from string + relayerAddress string + relayerBlsKey string +} + +var testcases = []testParams{ + { + discription: "crate success", + proposalID: "1", + enableGrant: true, + from: "0x7b5Fe22B5446f7C62Ea27B8BD71CeF94e03f3dF2", + relayerAddress: "0x8b70dC9B691fCeB4e1c69dF8cbF8c077AD4b5853", + relayerBlsKey: "926f1853304b482634d1ca8ef9652524228202d2a3ca376f3e5ab040430c42703f9de038a41917f944cf4e653cbed45a", + }, + { + discription: "no grant delegate authorization to gov module account", + proposalID: "2", + enableGrant: false, + from: "0x7b5Fe22B5446f7C62Ea27B8BD71CeF94e03f3dF2", + relayerAddress: "0x36B810C7E246b1c042335bAF1933Da71C5B5AeC5", + relayerBlsKey: "a4c392f233e140b124c35a509d8880c52bd9bc3d13042fec3d84c1d55cc4d5be30fe3003d096bc33bc924af563a8de9f", + }, + { + discription: "duplicated relayer address", + proposalID: "3", + enableGrant: true, + from: "0x7b5Fe22B5446f7C62Ea27B8BD71CeF94e03f3dF2", + relayerAddress: "0x8b70dC9B691fCeB4e1c69dF8cbF8c077AD4b5853", + relayerBlsKey: "819a55435aed37ff4f917d3644c74da1f6354c0d4fbc7ca425ebe6b98a0416fc4ef5eb13fceffb3a109eec4af12bba0a", + }, + { + discription: "duplicated relayer bls pub key", + proposalID: "4", + enableGrant: true, + from: "0x7b5Fe22B5446f7C62Ea27B8BD71CeF94e03f3dF2", + relayerAddress: "0x9E6de7BF11C459E8dA3a5f36c1A87A6FfaDCbA9d", + relayerBlsKey: "926f1853304b482634d1ca8ef9652524228202d2a3ca376f3e5ab040430c42703f9de038a41917f944cf4e653cbed45a", + }, + { + discription: "invalid from", + proposalID: "5", + enableGrant: true, + from: "0x8b70dC9B691fCeB4e1c69dF8cbF8c077AD4b5853", + relayerAddress: "0x73Fd0b049bBF30b3A165e5b2eAf9895B015A2515", + relayerBlsKey: "b700697ccca38c0d56cde325571dc5412de99a0fa2c28eeb1078cb4eeaa31c238720bee9cce47422e986a7af4f3b19c8", + }, +} + type CreateValidatorTestSuite struct { suite.Suite @@ -52,45 +103,12 @@ func (s *CreateValidatorTestSuite) SetupSuite() { _, err = s.network.WaitForHeight(1) s.Require().NoError(err) - // Valid case - blsSecretKey, _ := bls.RandKey() - blsPubKey := hex.EncodeToString(blsSecretKey.PublicKey().Marshal()) - - newVal := s.getCoin() - s.grantDelegator(newVal) - s.submitProposal(newVal, gov.ModuleName, blsPubKey, true) - s.validators = append(s.validators, newVal) - proposalID := fmt.Sprintf("%d", 1) - s.proposalIDs = append(s.proposalIDs, proposalID) - s.voteProposal(proposalID) - - // Invalid case (No grant) - blsSecretKey2, _ := bls.RandKey() - blsPubKey2 := hex.EncodeToString(blsSecretKey2.PublicKey().Marshal()) - - newVal2 := s.getCoin() - s.submitProposal(newVal2, gov.ModuleName, blsPubKey2, false) - - // Invalid case (--from account not gov) - blsSecretKey3, _ := bls.RandKey() - blsPubKey3 := hex.EncodeToString(blsSecretKey3.PublicKey().Marshal()) - - newVal3 := s.getCoin() - s.grantDelegator(newVal3) - s.submitProposal(newVal3, newVal3.String(), blsPubKey3, false) - - // Invalid case (Repeated relayer address) - blsSecretKey4, _ := bls.RandKey() - blsPubKey4 := hex.EncodeToString(blsSecretKey4.PublicKey().Marshal()) - - newVal4 := s.getCoin() - s.grantDelegator(newVal4) - s.submitProposal(newVal, gov.ModuleName, blsPubKey4, false) - - // Invalid case (Repeated relayer key) - newVal5 := s.getCoin() - s.grantDelegator(newVal5) - s.submitProposal(newVal5, gov.ModuleName, blsPubKey, false) + for _, testcase := range testcases { + newVal := s.submitProposal(testcase) + s.voteProposal(testcase.proposalID) + s.validators = append(s.validators, newVal) + s.proposalIDs = append(s.proposalIDs, testcase.proposalID) + } } func (s *CreateValidatorTestSuite) SetupNewSuite() { @@ -104,11 +122,94 @@ func (s *CreateValidatorTestSuite) SetupNewSuite() { s.Require().NoError(err) } -func (s *CreateValidatorTestSuite) getCoin() sdk.AccAddress { +func (s *CreateValidatorTestSuite) TearDownSuite() { + s.T().Log("tearing down test suite") + s.network.Cleanup() +} + +func (s *CreateValidatorTestSuite) TestQuerySuccessfulCreatedValidator() { + clientCtx := s.network.Validators[0].ClientCtx + proposalID := s.proposalIDs[0] + newVal := s.validators[0] + + // waiting for voting period to end + time.Sleep(10 * time.Second) + + // query proposal + args := []string{proposalID, fmt.Sprintf("--%s=json", tmcli.OutputFlag)} + out, err := clitestutil.ExecTestCLICmd(clientCtx, govcli.GetCmdQueryProposal(), args) + s.Require().NoError(err) + var proposal v1.Proposal + s.Require().NoError(clientCtx.Codec.UnmarshalJSON(out.Bytes(), &proposal), out.String()) + s.Require().Equal(v1.ProposalStatus_PROPOSAL_STATUS_PASSED, proposal.Status, out.String()) + + // query validator + queryCmd := cli.GetCmdQueryValidator() + res, err := clitestutil.ExecTestCLICmd( + clientCtx, queryCmd, + []string{newVal.String(), fmt.Sprintf("--%s=json", tmcli.OutputFlag)}, + ) + s.Require().NoError(err) + var result types.Validator + s.Require().NoError(clientCtx.Codec.UnmarshalJSON(res.Bytes(), &result)) + s.Require().Equal(result.GetStatus(), types.Bonded, fmt.Sprintf("validator %s not in bonded status", newVal.String())) +} + +func (s *CreateValidatorTestSuite) TestQueryNoGrantAuthorization() { + clientCtx := s.network.Validators[0].ClientCtx + proposalID := s.proposalIDs[1] + + // query proposal + args := []string{proposalID, fmt.Sprintf("--%s=json", tmcli.OutputFlag)} + out, err := clitestutil.ExecTestCLICmd(clientCtx, govcli.GetCmdQueryProposal(), args) + s.Require().NoError(err) + var proposal v1.Proposal + s.Require().NoError(clientCtx.Codec.UnmarshalJSON(out.Bytes(), &proposal), out.String()) + s.Require().Equal(v1.ProposalStatus_PROPOSAL_STATUS_FAILED, proposal.Status, out.String()) +} + +func (s *CreateValidatorTestSuite) TestQueryDuplicatedRelayerAddress() { + clientCtx := s.network.Validators[0].ClientCtx + proposalID := s.proposalIDs[2] + + // query proposal + args := []string{proposalID, fmt.Sprintf("--%s=json", tmcli.OutputFlag)} + out, err := clitestutil.ExecTestCLICmd(clientCtx, govcli.GetCmdQueryProposal(), args) + s.Require().NoError(err) + var proposal v1.Proposal + s.Require().NoError(clientCtx.Codec.UnmarshalJSON(out.Bytes(), &proposal), out.String()) + s.Require().Equal(v1.ProposalStatus_PROPOSAL_STATUS_FAILED, proposal.Status, out.String()) +} + +func (s *CreateValidatorTestSuite) TestQueryDuplicatedRelayerBlsKey() { + clientCtx := s.network.Validators[0].ClientCtx + proposalID := s.proposalIDs[3] + + // query proposal + args := []string{proposalID, fmt.Sprintf("--%s=json", tmcli.OutputFlag)} + out, err := clitestutil.ExecTestCLICmd(clientCtx, govcli.GetCmdQueryProposal(), args) + s.Require().NoError(err) + var proposal v1.Proposal + s.Require().NoError(clientCtx.Codec.UnmarshalJSON(out.Bytes(), &proposal), out.String()) + s.Require().Equal(v1.ProposalStatus_PROPOSAL_STATUS_FAILED, proposal.Status, out.String()) +} + +func (s *CreateValidatorTestSuite) TestQueryInvalidFromAddress() { + clientCtx := s.network.Validators[0].ClientCtx + proposalID := s.proposalIDs[4] + + // query proposal, should not be found because of invalid from the proposal will be rejected. + args := []string{proposalID, fmt.Sprintf("--%s=json", tmcli.OutputFlag)} + _, err := clitestutil.ExecTestCLICmd(clientCtx, govcli.GetCmdQueryProposal(), args) + s.Require().Error(err) +} + +func (s *CreateValidatorTestSuite) submitProposal(params testParams) sdk.AccAddress { val := s.network.Validators[0] + clientCtx := val.ClientCtx // Get coin from current validator - k, _, err := val.ClientCtx.Keyring.NewMnemonic("NewAccount", keyring.English, sdk.FullFundraiserPath, keyring.DefaultBIP39Passphrase, ethHd.EthSecp256k1) + k, _, err := clientCtx.Keyring.NewMnemonic("NewAccount", keyring.English, sdk.FullFundraiserPath, keyring.DefaultBIP39Passphrase, ethHd.EthSecp256k1) s.Require().NoError(err) pub, err := k.GetPubKey() @@ -116,63 +217,44 @@ func (s *CreateValidatorTestSuite) getCoin() sdk.AccAddress { newVal := sdk.AccAddress(pub.Address()) _, err = banktestutil.MsgSendExec( - val.ClientCtx, + clientCtx, val.Address, newVal, - sdk.NewCoins(sdk.NewCoin(s.cfg.BondDenom, sdk.NewInt(200000000))), + sdk.NewCoins(sdk.NewCoin(s.cfg.BondDenom, sdk.NewInt(20000000))), fmt.Sprintf("--%s=true", flags.FlagSkipConfirmation), fmt.Sprintf("--%s=%s", flags.FlagBroadcastMode, flags.BroadcastBlock), fmt.Sprintf("--%s=%s", flags.FlagFees, sdk.NewCoins(sdk.NewCoin(s.cfg.BondDenom, sdk.NewInt(10))).String()), ) s.Require().NoError(err) - return newVal -} - -func (s *CreateValidatorTestSuite) grantDelegator(newVal sdk.AccAddress) { - val := s.network.Validators[0] - _, err := val.ClientCtx.Keyring.KeyByAddress(newVal) - // Grant delegate authorization - _, err = authztestutil.CreateGrant(val, []string{ - authtypes.NewModuleAddress(gov.ModuleName).String(), - "delegate", - fmt.Sprintf("--spend-limit=100000000%s", s.cfg.BondDenom), - fmt.Sprintf("--allowed-validators=%s", newVal.String()), - fmt.Sprintf("--%s=%s", flags.FlagFrom, newVal.String()), - fmt.Sprintf("--%s=true", flags.FlagSkipConfirmation), - fmt.Sprintf("--%s=%s", flags.FlagBroadcastMode, flags.BroadcastBlock), - fmt.Sprintf("--%s=%s", flags.FlagFees, sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, sdk.NewInt(10))).String()), - }) + _, err = s.network.WaitForHeight(1) s.Require().NoError(err) -} -func (s *CreateValidatorTestSuite) submitProposal(newVal sdk.AccAddress, from string, blsPubKey string, isValidTestCase bool) { - val := s.network.Validators[0] - clientCtx := val.ClientCtx + // Grant delegate authorization + if params.enableGrant { + _, err = authztestutil.CreateGrant(val, []string{ + authtypes.NewModuleAddress(gov.ModuleName).String(), + "delegate", + fmt.Sprintf("--spend-limit=100000000%s", s.cfg.BondDenom), + fmt.Sprintf("--allowed-validators=%s", newVal.String()), + fmt.Sprintf("--%s=%s", flags.FlagFrom, newVal.String()), + fmt.Sprintf("--%s=true", flags.FlagSkipConfirmation), + fmt.Sprintf("--%s=%s", flags.FlagBroadcastMode, flags.BroadcastBlock), + fmt.Sprintf("--%s=%s", flags.FlagFees, sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, sdk.NewInt(10))).String()), + }) + s.Require().NoError(err) + } args := append([]string{ - s.createValidatorProposal(newVal, blsPubKey, from).Name(), + s.createValidatorProposal(newVal, params.from, params.relayerAddress, params.relayerBlsKey).Name(), fmt.Sprintf("--%s=%s", flags.FlagFrom, newVal.String()), fmt.Sprintf("--gas=%s", fmt.Sprintf("%d", flags.DefaultGasLimit+100000)), - }, CommonArgs...) - - response, err := clitestutil.ExecTestCLICmd(clientCtx, govcli.NewCmdSubmitProposal(), args) + }, commonArgs...) - // For testcases with errors found in the response instead (Invalid --from account testcase) - if err == nil { - splits := strings.Split(response.String(), ",") - splits = strings.Split(splits[2], ":") - codespace := strings.Trim(splits[1], "\"") - if string(codespace) != "" { - err = gov.ErrInvalidProposalMsg - } - } + _, err = clitestutil.ExecTestCLICmd(clientCtx, govcli.NewCmdSubmitProposal(), args) + s.Require().NoError(err) - if isValidTestCase { - s.Require().NoError(err) - } else { - s.Require().Error(err) - } + return newVal } func (s *CreateValidatorTestSuite) voteProposal(proposalID string) { @@ -183,48 +265,13 @@ func (s *CreateValidatorTestSuite) voteProposal(proposalID string) { proposalID, "yes", fmt.Sprintf("--%s=%s", flags.FlagFrom, val.Address.String()), - }, CommonArgs...) + }, commonArgs...) _, err := clitestutil.ExecTestCLICmd(clientCtx, govcli.NewCmdVote(), args) s.Require().NoError(err) } -func (s *CreateValidatorTestSuite) TearDownSuite() { - s.T().Log("tearing down test suite") - s.network.Cleanup() -} - -func (s *CreateValidatorTestSuite) TestQuerySuccessfulCreatedValidator() { - val := s.network.Validators[0] - clientCtx := val.ClientCtx - proposalID := s.proposalIDs[0] - newVal := s.validators[0] - - // waiting for voting period to end - time.Sleep(10 * time.Second) - - // query proposal - args := []string{proposalID, fmt.Sprintf("--%s=json", tmcli.OutputFlag)} - out, err := clitestutil.ExecTestCLICmd(clientCtx, govcli.GetCmdQueryProposal(), args) - s.Require().NoError(err) - var proposal v1.Proposal - s.Require().NoError(clientCtx.Codec.UnmarshalJSON(out.Bytes(), &proposal), out.String()) - s.Require().Equal(v1.ProposalStatus_PROPOSAL_STATUS_PASSED, proposal.Status, out.String()) - - // query validator - queryCmd := cli.GetCmdQueryValidator() - res, err := clitestutil.ExecTestCLICmd( - val.ClientCtx, queryCmd, - []string{newVal.String(), fmt.Sprintf("--%s=json", tmcli.OutputFlag)}, - ) - s.Require().NoError(err) - var result types.Validator - s.Require().NoError(val.ClientCtx.Codec.UnmarshalJSON(res.Bytes(), &result)) - s.Require().Equal(result.GetStatus(), types.Bonded, fmt.Sprintf("validator %s not in bonded status", newVal.String())) -} - -func (s *CreateValidatorTestSuite) createValidatorProposal(valAddr sdk.AccAddress, blsPubKey string, from string) *os.File { +func (s *CreateValidatorTestSuite) createValidatorProposal(valAddr sdk.AccAddress, from string, relayerAddress string, relayerBlsKey string) *os.File { pubKey := base64.StdEncoding.EncodeToString(ed25519.GenPrivKey().PubKey().Bytes()) - propMetadata := []byte{42} proposal := fmt.Sprintf(` { @@ -265,9 +312,9 @@ func (s *CreateValidatorTestSuite) createValidatorProposal(valAddr sdk.AccAddres valAddr.String(), valAddr.String(), pubKey, - authtypes.NewModuleAddress(from), - valAddr.String(), - blsPubKey, + from, + relayerAddress, + relayerBlsKey, base64.StdEncoding.EncodeToString(propMetadata), sdk.NewCoin(s.cfg.BondDenom, v1.DefaultMinDepositTokens), ) diff --git a/x/staking/client/testutil/test_helpers.go b/x/staking/client/testutil/test_helpers.go index 4794d2a508..1416d4c7ea 100644 --- a/x/staking/client/testutil/test_helpers.go +++ b/x/staking/client/testutil/test_helpers.go @@ -11,7 +11,7 @@ import ( stakingcli "github.com/cosmos/cosmos-sdk/x/staking/client/cli" ) -var CommonArgs = []string{ +var commonArgs = []string{ fmt.Sprintf("--%s=true", flags.FlagSkipConfirmation), fmt.Sprintf("--%s=%s", flags.FlagBroadcastMode, flags.BroadcastBlock), fmt.Sprintf("--%s=%s", flags.FlagFees, sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, sdk.NewInt(10))).String()), @@ -28,7 +28,7 @@ func MsgRedelegateExec(clientCtx client.Context, from, src, dst sdk.AccAddress, } args = append(args, extraArgs...) - args = append(args, CommonArgs...) + args = append(args, commonArgs...) return clitestutil.ExecTestCLICmd(clientCtx, stakingcli.NewRedelegateCmd(), args) } @@ -42,7 +42,7 @@ func MsgUnbondExec(clientCtx client.Context, from, valAddress sdk.AccAddress, fmt.Sprintf("--%s=%s", flags.FlagFrom, from.String()), } - args = append(args, CommonArgs...) + args = append(args, commonArgs...) args = append(args, extraArgs...) return clitestutil.ExecTestCLICmd(clientCtx, stakingcli.NewUnbondCmd(), args) }