Skip to content

Commit

Permalink
fix: fix the cli test for cancel unbond (#11579)
Browse files Browse the repository at this point in the history
## 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)
  • Loading branch information
gsk967 authored Apr 9, 2022
1 parent 83f5ecf commit 398583a
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 8 deletions.
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)
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

0 comments on commit 398583a

Please sign in to comment.