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

test: integration tests for creating and impeaching validator #86

Merged
merged 5 commits into from
Feb 16, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
18 changes: 17 additions & 1 deletion x/slashing/client/testutil/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,29 @@ package testutil

import (
"testing"
"time"

"github.com/cosmos/cosmos-sdk/testutil/network"
"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"
)

func TestIntegrationTestSuite(t *testing.T) {
cfg := network.DefaultConfig()
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(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))
}
219 changes: 219 additions & 0 deletions x/slashing/client/testutil/impeach_validator.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,219 @@
package testutil

import (
"encoding/base64"
"fmt"
"os"

"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"
govcli "github.com/cosmos/cosmos-sdk/x/gov/client/cli"
v1 "github.com/cosmos/cosmos-sdk/x/gov/types/v1"
"github.com/cosmos/cosmos-sdk/x/staking/client/cli"
"github.com/cosmos/cosmos-sdk/x/staking/types"
)

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

cfg network.Config
network *network.Network
proposalIDs []string
}

func NewImpeachValidatorTestSuite(cfg network.Config) *ImpeachValidatorTestSuite {
return &ImpeachValidatorTestSuite{cfg: cfg}
}

func (s *ImpeachValidatorTestSuite) SetupSuite() {
s.T().Log("setting up test suite")

var err error
s.network, err = network.New(s.T(), s.T().TempDir(), s.cfg)
s.Require().NoError(err)

_, err = s.network.WaitForHeight(1)
s.Require().NoError(err)

for _, testcase := range testcases {
s.submitProposal(testcase)
s.voteProposal(testcase.proposalID)
s.proposalIDs = append(s.proposalIDs, testcase.proposalID)
}
}

func (s *ImpeachValidatorTestSuite) SetupNewSuite() {
s.T().Log("setting up new test suite")

var err error
s.network, err = network.New(s.T(), s.T().TempDir(), s.cfg)
s.Require().NoError(err)

_, err = s.network.WaitForHeight(1)
s.Require().NoError(err)
}

func (s *ImpeachValidatorTestSuite) TearDownSuite() {
s.T().Log("tearing down test suite")
s.network.Cleanup()
}

func (s *ImpeachValidatorTestSuite) TestQuerySuccessfulImpeachedValidator() {
proposalID := s.proposalIDs[0]
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)}
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{targetVal.Address.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().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) TestQueryDoubleImpeachedValidator() {
proposalID := s.proposalIDs[1]
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)}
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{targetVal.Address.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().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) 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(`
{
"messages": [
{
"@type": "/cosmos.slashing.v1beta1.MsgImpeach",
"from":"%s",
"validator_address":"%s"
}
],
"metadata": "%s",
"deposit": "%s"
}`,
from,
valAddr.String(),
base64.StdEncoding.EncodeToString(propMetadata),
sdk.NewCoin(s.cfg.BondDenom, v1.DefaultMinDepositTokens),
)

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)
}
}
6 changes: 4 additions & 2 deletions x/staking/client/testutil/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@ 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) {
Expand All @@ -27,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))
}
Loading