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

fix: fix the cli test for cancel unbond #11579

Merged
merged 9 commits into from
Apr 9, 2022
2 changes: 1 addition & 1 deletion x/staking/client/testutil/grpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
})
}
Expand Down
36 changes: 29 additions & 7 deletions x/staking/client/testutil/suite.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Copy link
Contributor

@atheeshp atheeshp Apr 9, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
s.Require().NoError(err)

this is not related to the changes you made, but can you address this.

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)
}

Expand Down Expand Up @@ -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)
}
})
}
Expand Down Expand Up @@ -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),
Expand All @@ -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),
Expand All @@ -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 {
Expand Down
4 changes: 4 additions & 0 deletions x/staking/simulation/operations.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down