From 398583a0402ad4d7755cd6002d061e658fb1d910 Mon Sep 17 00:00:00 2001 From: Sai Kumar <17549398+gsk967@users.noreply.github.com> Date: Sat, 9 Apr 2022 16:46:09 +0530 Subject: [PATCH] fix: fix the cli test for cancel unbond (#11579) ## Description Closes: #XXXX --- ### Author Checklist *All items are required. Please add a note to the item if the item is not applicable and please add links to any relevant follow up issues.* I have... - [ ] included the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title - [ ] added `!` to the type prefix if API or client breaking change - [ ] targeted the correct branch (see [PR Targeting](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#pr-targeting)) - [ ] provided a link to the relevant issue or specification - [ ] followed the guidelines for [building modules](https://github.com/cosmos/cosmos-sdk/blob/master/docs/building-modules) - [ ] included the necessary unit and integration [tests](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#testing) - [ ] added a changelog entry to `CHANGELOG.md` - [ ] included comments for [documenting Go code](https://blog.golang.org/godoc) - [ ] updated the relevant documentation or specification - [ ] reviewed "Files changed" and left comments if necessary - [ ] confirmed all CI checks have passed ### Reviewers Checklist *All items are required. Please add a note if the item is not applicable and please add your handle next to the items reviewed if you only reviewed selected items.* I have... - [ ] confirmed the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title - [ ] confirmed `!` in the type prefix if API or client breaking change - [ ] confirmed all author checklist items have been addressed - [ ] reviewed state machine logic - [ ] reviewed API design and naming - [ ] reviewed documentation is accurate - [ ] reviewed tests and test coverage - [ ] manually tested (if applicable) --- x/staking/client/testutil/grpc.go | 2 +- x/staking/client/testutil/suite.go | 36 ++++++++++++++++++++++++------ x/staking/simulation/operations.go | 4 ++++ 3 files changed, 34 insertions(+), 8 deletions(-) diff --git a/x/staking/client/testutil/grpc.go b/x/staking/client/testutil/grpc.go index 78c39f857b59..26093aa15bc3 100644 --- a/x/staking/client/testutil/grpc.go +++ b/x/staking/client/testutil/grpc.go @@ -348,7 +348,7 @@ func (s *IntegrationTestSuite) TestGRPCQueryUnbondingDelegation() { s.Require().NoError(err) s.Require().Equal(ubd.Unbond.DelegatorAddress, val.Address.String()) s.Require().Equal(ubd.Unbond.ValidatorAddress, val.ValAddress.String()) - s.Require().Len(ubd.Unbond.Entries, 1) + s.Require().Len(ubd.Unbond.Entries, 2) } }) } diff --git a/x/staking/client/testutil/suite.go b/x/staking/client/testutil/suite.go index e5ddab70b86a..879808868eb9 100644 --- a/x/staking/client/testutil/suite.go +++ b/x/staking/client/testutil/suite.go @@ -18,6 +18,7 @@ import ( "github.com/cosmos/cosmos-sdk/crypto/keys/ed25519" clitestutil "github.com/cosmos/cosmos-sdk/testutil/cli" "github.com/cosmos/cosmos-sdk/testutil/network" + "github.com/cosmos/cosmos-sdk/testutil/rest" sdk "github.com/cosmos/cosmos-sdk/types" sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" "github.com/cosmos/cosmos-sdk/types/query" @@ -72,14 +73,21 @@ func (s *IntegrationTestSuite) SetupSuite() { s.Require().Equal(uint32(0), txRes.Code) _, err = s.network.WaitForHeight(1) s.Require().NoError(err) - // unbonding - out, err = MsgUnbondExec(val.ClientCtx, val.Address, val.ValAddress, unbond) + + unbondingAmount := sdk.NewCoin(sdk.DefaultBondDenom, sdk.NewInt(5)) + // unbonding the amount + out, err = MsgUnbondExec(val.ClientCtx, val.Address, val.ValAddress, unbondingAmount) + s.Require().NoError(err) + s.Require().NoError(val.ClientCtx.Codec.UnmarshalJSON(out.Bytes(), &txRes)) + s.Require().Equal(uint32(0), txRes.Code) + // unbonding the amount + out, err = MsgUnbondExec(val.ClientCtx, val.Address, val.ValAddress, unbondingAmount) s.Require().NoError(err) s.Require().NoError(err) s.Require().NoError(val.ClientCtx.Codec.UnmarshalJSON(out.Bytes(), &txRes)) s.Require().Equal(uint32(0), txRes.Code) - _, err = s.network.WaitForHeight(1) + err = s.network.WaitForNextBlock() s.Require().NoError(err) } @@ -595,7 +603,7 @@ func (s *IntegrationTestSuite) TestGetCmdQueryUnbondingDelegation() { s.Require().NoError(err) s.Require().Equal(ubd.DelegatorAddress, val.Address.String()) s.Require().Equal(ubd.ValidatorAddress, val.ValAddress.String()) - s.Require().Len(ubd.Entries, 1) + s.Require().Len(ubd.Entries, 2) } }) } @@ -1362,7 +1370,6 @@ func (s *IntegrationTestSuite) TestNewCancelUnbondingDelegationCmd() { []string{ val.ValAddress.String(), sdk.NewCoin(s.cfg.BondDenom, sdk.NewInt(10000)).String(), - sdk.NewInt(3).String(), fmt.Sprintf("--%s=%s", flags.FlagFrom, val.Address.String()), fmt.Sprintf("--%s=true", flags.FlagSkipConfirmation), fmt.Sprintf("--%s=%s", flags.FlagBroadcastMode, flags.BroadcastBlock), @@ -1374,8 +1381,7 @@ func (s *IntegrationTestSuite) TestNewCancelUnbondingDelegationCmd() { "valid transaction of canceling unbonding delegation", []string{ val.ValAddress.String(), - sdk.NewCoin(s.cfg.BondDenom, sdk.NewInt(10)).String(), - sdk.NewInt(3).String(), + sdk.NewCoin(s.cfg.BondDenom, sdk.NewInt(5)).String(), fmt.Sprintf("--%s=%s", flags.FlagFrom, val.Address.String()), fmt.Sprintf("--%s=true", flags.FlagSkipConfirmation), fmt.Sprintf("--%s=%s", flags.FlagBroadcastMode, flags.BroadcastBlock), @@ -1391,6 +1397,22 @@ func (s *IntegrationTestSuite) TestNewCancelUnbondingDelegationCmd() { s.Run(tc.name, func() { cmd := cli.NewCancelUnbondingDelegation() clientCtx := val.ClientCtx + if !tc.expectErr && tc.expectedCode != sdkerrors.ErrNotFound.ABCICode() { + getCreationHeight := func() int64 { + // fethichg the unbonding delegations + resp, err := rest.GetRequest(fmt.Sprintf("%s/cosmos/staking/v1beta1/delegators/%s/unbonding_delegations", val.APIAddress, val.Address.String())) + s.Require().NoError(err) + + var ubds types.QueryDelegatorUnbondingDelegationsResponse + + err = val.ClientCtx.Codec.UnmarshalJSON(resp, &ubds) + s.Require().NoError(err) + s.Require().Len(ubds.UnbondingResponses, 1) + s.Require().Equal(ubds.UnbondingResponses[0].DelegatorAddress, val.Address.String()) + return ubds.UnbondingResponses[0].Entries[1].CreationHeight + } + tc.args = append(tc.args, fmt.Sprint(getCreationHeight())) + } out, err := clitestutil.ExecTestCLICmd(clientCtx, cmd, tc.args) if tc.expectErr { diff --git a/x/staking/simulation/operations.go b/x/staking/simulation/operations.go index 8b3ce6e65337..1a702b150c35 100644 --- a/x/staking/simulation/operations.go +++ b/x/staking/simulation/operations.go @@ -411,6 +411,10 @@ func SimulateMsgCancelUnbondingDelegate(ak types.AccountKeeper, bk types.BankKee // get random unbonding delegation entry at block height unbondingDelegationEntry := unbondingDelegation.Entries[r.Intn(len(unbondingDelegation.Entries))] + if unbondingDelegationEntry.CompletionTime.Before(ctx.BlockTime()) { + return simtypes.NoOpMsg(types.ModuleName, types.TypeMsgCancelUnbondingDelegation, "unbonding delegation is already processed"), nil, nil + } + if !unbondingDelegationEntry.Balance.IsPositive() { return simtypes.NoOpMsg(types.ModuleName, types.TypeMsgCancelUnbondingDelegation, "delegator receiving balance is negative"), nil, nil }