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

feat: Cancel unbonding delegation entry #10885

Merged
merged 56 commits into from
Apr 5, 2022
Merged
Show file tree
Hide file tree
Changes from 50 commits
Commits
Show all changes
56 commits
Select commit Hold shift + click to select a range
b18cbc2
feat(WIP): add msg service for cancel unbonding delegation
Jan 5, 2022
276d33b
WIP: add simulation for canceling unbonding delegation
Jan 5, 2022
5f197f1
Merge the master
Jan 5, 2022
759fff3
WIP: update the initial balance in ubd entry
Jan 5, 2022
bf10855
WIP: update the initial balance and balance in ubd entry
Jan 6, 2022
bcee445
Merge remote-tracking branch 'upstream/master' into sai/577_cancel_un…
Jan 6, 2022
ff8be0f
revert the proto-gen for gov,staking
Jan 6, 2022
ee7079e
Merge branch 'master' into sai/577_cancel_unbond
gsk967 Jan 6, 2022
98a6126
test: add cli test for cancel unbond delegation
Jan 7, 2022
8d3a82b
Merge branch 'sai/577_cancel_unbond' of github.com:gsk967/cosmos-sdk …
Jan 7, 2022
78042e6
test: add simulation test for cancel-unbond
Jan 8, 2022
5147003
chore: check the validator is jailed or not for cancel unbond
Jan 11, 2022
d93705f
chore: try to fix cancel unbond simulation
Jan 11, 2022
389bd1f
Merge branch 'master' into sai/577_cancel_unbond
gsk967 Jan 11, 2022
f909286
chore: update the consensus version and changelog
Jan 19, 2022
07a4822
chore: update the consensus version and changelog
Jan 19, 2022
a8cd5dd
Merge branch 'master' of https://github.com/cosmos/cosmos-sdk into sa…
Jan 20, 2022
31c54a1
test: add more test cases for cancel unbond delegation
Jan 20, 2022
927e3b9
Merge remote-tracking branch 'upstream/master' into sai/577_cancel_un…
Jan 21, 2022
550e27d
fix: fix the issue of docs bug
Jan 27, 2022
65b8527
Merge the upstream/master
Jan 27, 2022
2d26ab3
Merge branch 'master' into sai/577_cancel_unbond
gsk967 Jan 27, 2022
816cb9c
Merge branch 'master' into sai/577_cancel_unbond
gsk967 Jan 28, 2022
e27b990
Merge remote upstream master
Jan 29, 2022
61c4818
Merge branch 'master' into sai/577_cancel_unbond
gsk967 Jan 31, 2022
be0b721
Merge branch 'master' into sai/577_cancel_unbond
gsk967 Feb 1, 2022
ad66dc4
Merge branch 'master' into sai/577_cancel_unbond
Mar 21, 2022
ef9f21e
fix: fix the build issue
Mar 21, 2022
15d215e
Merge branch 'master' into sai/577_cancel_unbond
gsk967 Mar 23, 2022
b21d5a0
chore: address the pr comments
Mar 23, 2022
563db02
Merge branch 'master' into sai/577_cancel_unbond
gsk967 Mar 23, 2022
f288ecd
Update CHANGELOG.md
alexanderbez Mar 23, 2022
70a2b53
Merge branch 'master' into sai/577_cancel_unbond
gsk967 Mar 24, 2022
e9bd4bb
Merge branch 'master' into sai/577_cancel_unbond
gsk967 Mar 24, 2022
971dc0a
chore: remove commented code
Mar 24, 2022
8b9fee6
Merge branch 'master' into sai/577_cancel_unbond
gsk967 Mar 25, 2022
ea69358
Merge branch 'master' into sai/577_cancel_unbond
gsk967 Mar 25, 2022
f7cafe2
Merge branch 'master' into sai/577_cancel_unbond
gsk967 Mar 26, 2022
28243eb
Merge branch 'master' into sai/577_cancel_unbond
gsk967 Mar 28, 2022
6c3aa6f
Merge branch 'master' into sai/577_cancel_unbond
gsk967 Mar 29, 2022
4c67387
Merge branch 'master' into sai/577_cancel_unbond
gsk967 Mar 30, 2022
793ce7f
test: add test cases for cancel unbond
Mar 30, 2022
8c1e20a
chore: covert creation_height from uint64 to int64
Mar 30, 2022
de22757
Merge branch 'master' into sai/577_cancel_unbond
gsk967 Mar 30, 2022
597aa0b
Merge branch 'master' into sai/577_cancel_unbond
gsk967 Mar 30, 2022
5390f78
Merge branch 'master' into sai/577_cancel_unbond
gsk967 Mar 31, 2022
b468491
Merge branch 'master' into sai/577_cancel_unbond
gsk967 Apr 1, 2022
b8b643e
Merge branch 'master' into sai/577_cancel_unbond
anilcse Apr 1, 2022
0bc4085
Merge branch 'master' into sai/577_cancel_unbond
gsk967 Apr 3, 2022
069101a
Merge branch 'master' into sai/577_cancel_unbond
gsk967 Apr 4, 2022
ebaa959
Merge branch 'master' into sai/577_cancel_unbond
gsk967 Apr 4, 2022
78725ee
Merge branch 'master' into sai/577_cancel_unbond
gsk967 Apr 5, 2022
d4bba93
Merge branch 'master' into sai/577_cancel_unbond
gsk967 Apr 5, 2022
376024d
chore: address the review comments
Apr 5, 2022
21ce078
Merge branch 'sai/577_cancel_unbond' of github.com:gsk967/cosmos-sdk …
Apr 5, 2022
bb70c1c
Merge branch 'master' into sai/577_cancel_unbond
gsk967 Apr 5, 2022
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,8 @@ Ref: https://keepachangelog.com/en/1.0.0/
* [\#10868](https://github.com/cosmos/cosmos-sdk/pull/10868) Bump gov to v1beta2. Both v1beta1 and v1beta2 queries and Msgs are accepted.
* [\#11011](https://github.com/cosmos/cosmos-sdk/pull/11011) Remove burning of deposits when qourum is not reached on a governance proposal and when the deposit is not fully met.
* [\#11019](https://github.com/cosmos/cosmos-sdk/pull/11019) Add `MsgCreatePermanentLockedAccount` and CLI method for creating permanent locked account
* (x/staking) [\#10885] (https://github.com/cosmos/cosmos-sdk/pull/10885) Add new `CancelUnbondingDelegation`
transaction to `x/staking` module. Delegators can now cancel unbonding delegation entry and re-delegate back to validator.
* (x/feegrant) [\#10830](https://github.com/cosmos/cosmos-sdk/pull/10830) Expired allowances will be pruned from state.
* (x/authz,x/feegrant) [\#11214](https://github.com/cosmos/cosmos-sdk/pull/11214) Fix Amino JSON encoding of authz and feegrant Msgs to be consistent with other modules.
* (authz)[\#11060](https://github.com/cosmos/cosmos-sdk/pull/11060) Support grant with no expire time.
Expand Down
1,294 changes: 1,205 additions & 89 deletions api/cosmos/staking/v1beta1/tx.pulsar.go

Large diffs are not rendered by default.

40 changes: 40 additions & 0 deletions api/cosmos/staking/v1beta1/tx_grpc.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions proto/cosmos/staking/v1beta1/tx.proto
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ service Msg {
// Undelegate defines a method for performing an undelegation from a
// delegate and a validator.
rpc Undelegate(MsgUndelegate) returns (MsgUndelegateResponse);

// CancelUnbondingDelegation defines a method for performing canceling the unbonding delegation
// and delegate back to previous validator.
rpc CancelUnbondingDelegation(MsgCancelUnbondingDelegation) returns (MsgCancelUnbondingDelegationResponse);
gsk967 marked this conversation as resolved.
Show resolved Hide resolved
}

// MsgCreateValidator defines a SDK message for creating a new validator.
Expand Down Expand Up @@ -136,3 +140,20 @@ message MsgUndelegate {
message MsgUndelegateResponse {
google.protobuf.Timestamp completion_time = 1 [(gogoproto.nullable) = false, (gogoproto.stdtime) = true];
}

// MsgCancelUnbondingDelegation defines the SDK message for performing a cancel unbonding delegation for delegator
message MsgCancelUnbondingDelegation{
option (cosmos.msg.v1.signer) = "delegator_address";
option (gogoproto.equal) = false;
gsk967 marked this conversation as resolved.
Show resolved Hide resolved
option (gogoproto.goproto_getters) = false;

string delegator_address = 1 [(cosmos_proto.scalar) = "cosmos.AddressString"];
string validator_address = 2 [(cosmos_proto.scalar) = "cosmos.AddressString"];
// amount is always less than or equal to unbonding delegation entry balance
cosmos.base.v1beta1.Coin amount = 3 [(gogoproto.nullable) = false];
amaury1093 marked this conversation as resolved.
Show resolved Hide resolved
// creation_height is the height which the unbonding took place.
int64 creation_height = 4;
}

// MsgCancelUnbondingDelegationResponse
message MsgCancelUnbondingDelegationResponse{}
1 change: 1 addition & 0 deletions simapp/params/weights.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const (
DefaultWeightMsgDelegate int = 100
DefaultWeightMsgUndelegate int = 100
DefaultWeightMsgBeginRedelegate int = 100
DefaultWeightMsgCancelUnbondingDelegation int = 100

DefaultWeightCommunitySpendProposal int = 5
DefaultWeightTextProposal int = 5
Expand Down
52 changes: 52 additions & 0 deletions x/staking/client/cli/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cli
import (
"fmt"
"os"
"strconv"
"strings"

"github.com/spf13/cobra"
Expand Down Expand Up @@ -44,6 +45,7 @@ func NewTxCmd() *cobra.Command {
NewDelegateCmd(),
NewRedelegateCmd(),
NewUnbondCmd(),
NewCancelUnbondingDelegation(),
)

return stakingTxCmd
Expand Down Expand Up @@ -277,6 +279,56 @@ $ %s tx staking unbond %s1gghjut3ccd8ay0zduzj64hwre2fxs9ldmqhffj 100stake --from
return cmd
}

func NewCancelUnbondingDelegation() *cobra.Command {
bech32PrefixValAddr := sdk.GetConfig().GetBech32ValidatorAddrPrefix()

cmd := &cobra.Command{
Use: "cancel-unbond [validator-addr] [amount] [creation-height]",
Short: "Cancel unbonding delegation and delegate back to validator",
Args: cobra.ExactArgs(3),
Long: strings.TrimSpace(
fmt.Sprintf(`Cancel Unbonding Delegation and delegate back to validator.

Example:
$ %s tx staking cancel-unbond %s1gghjut3ccd8ay0zduzj64hwre2fxs9ldmqhffj 100stake 2 --from mykey
`,
version.AppName, bech32PrefixValAddr,
),
),
Example: fmt.Sprintf(`$ %s tx staking cancel-unbond %s1gghjut3ccd8ay0zduzj64hwre2fxs9ldmqhffj 100stake 2 --from mykey`,
version.AppName, bech32PrefixValAddr),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}
delAddr := clientCtx.GetFromAddress()
valAddr, err := sdk.ValAddressFromBech32(args[0])
if err != nil {
return err
}

amount, err := sdk.ParseCoinNormalized(args[1])
if err != nil {
return err
}

creationHeight, err := strconv.ParseInt(args[2], 10, 64)
if err != nil {
return sdkerrors.Wrap(fmt.Errorf("invalid height: %d", creationHeight), "invalid height")
}

msg := types.NewMsgCancelUnbondingDelegation(delAddr, valAddr, creationHeight, amount)

return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), msg)
},
}

flags.AddTxFlagsToCmd(cmd)

return cmd
}

func newBuildCreateValidatorMsg(clientCtx client.Context, txf tx.Factory, fs *flag.FlagSet) (tx.Factory, *types.MsgCreateValidator, error) {
fAmount, _ := fs.GetString(FlagAmount)
amount, err := sdk.ParseCoinNormalized(fAmount)
Expand Down
111 changes: 110 additions & 1 deletion x/staking/client/testutil/suite.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package testutil
import (
"context"
"fmt"
sdkerr "github.com/cosmos/cosmos-sdk/types/errors"
gsk967 marked this conversation as resolved.
Show resolved Hide resolved
"strings"
"testing"

Expand Down Expand Up @@ -72,8 +73,11 @@ func (s *IntegrationTestSuite) SetupSuite() {
_, err = s.network.WaitForHeight(1)
s.Require().NoError(err)
// unbonding
_, err = MsgUnbondExec(val.ClientCtx, val.Address, val.ValAddress, unbond)
out, err = MsgUnbondExec(val.ClientCtx, val.Address, val.ValAddress, unbond)
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)
s.Require().NoError(err)
Expand Down Expand Up @@ -1297,6 +1301,111 @@ func (s *IntegrationTestSuite) TestNewUnbondCmd() {
}
}

func (s *IntegrationTestSuite) TestNewCancelUnbondingDelegationCmd() {
val := s.network.Validators[0]

testCases := []struct {
name string
args []string
expectErr bool
expectedCode uint32
respType proto.Message
}{
{
"Without validator address",
[]string{
fmt.Sprintf("--%s=%s", flags.FlagFrom, val.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(s.cfg.BondDenom, sdk.NewInt(10))).String()),
},
true, 0, nil,
},
{
"Without canceling unbond delegation amount",
[]string{
val.ValAddress.String(),
fmt.Sprintf("--%s=%s", flags.FlagFrom, val.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(s.cfg.BondDenom, sdk.NewInt(10))).String()),
},
true, 0, nil,
},
{
"Without unbond creation height",
[]string{
val.ValAddress.String(),
sdk.NewCoin(s.cfg.BondDenom, sdk.NewInt(150)).String(),
fmt.Sprintf("--%s=%s", flags.FlagFrom, val.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(s.cfg.BondDenom, sdk.NewInt(10))).String()),
},
true, 0, nil,
},
{
"Wrong unbonding creation height",
[]string{
val.ValAddress.String(),
sdk.NewCoin(s.cfg.BondDenom, sdk.NewInt(10)).String(),
sdk.NewInt(10000).String(),
fmt.Sprintf("--%s=%s", flags.FlagFrom, val.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(s.cfg.BondDenom, sdk.NewInt(10))).String()),
},
false, sdkerr.ErrNotFound.ABCICode(), &sdk.TxResponse{},
},
{
"Invalid unbonding amount (higher than the unbonding amount)",
[]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),
fmt.Sprintf("--%s=%s", flags.FlagFees, sdk.NewCoins(sdk.NewCoin(s.cfg.BondDenom, sdk.NewInt(10))).String()),
},
false, sdkerr.ErrInvalidRequest.ABCICode(), &sdk.TxResponse{},
},
{
"valid transaction of canceling unbonding delegation",
[]string{
val.ValAddress.String(),
sdk.NewCoin(s.cfg.BondDenom, sdk.NewInt(10)).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),
fmt.Sprintf("--%s=%s", flags.FlagFees, sdk.NewCoins(sdk.NewCoin(s.cfg.BondDenom, sdk.NewInt(10))).String()),
},
false, 0, &sdk.TxResponse{},
},
gsk967 marked this conversation as resolved.
Show resolved Hide resolved
}

for _, tc := range testCases {
tc := tc

s.Run(tc.name, func() {
cmd := cli.NewCancelUnbondingDelegation()
clientCtx := val.ClientCtx

out, err := clitestutil.ExecTestCLICmd(clientCtx, cmd, tc.args)
if tc.expectErr {
s.Require().Error(err)
} else {
s.Require().NoError(err, out.String())
s.Require().NoError(clientCtx.Codec.UnmarshalJSON(out.Bytes(), tc.respType), out.String())

txResp := tc.respType.(*sdk.TxResponse)
s.Require().Equal(tc.expectedCode, txResp.Code, out.String())
}
})
}
}

// TestBlockResults tests that the validator updates correctly show when
// calling the /block_results RPC endpoint.
// ref: https://github.com/cosmos/cosmos-sdk/issues/7401.
Expand Down
2 changes: 1 addition & 1 deletion x/staking/keeper/delegation.go
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ func (k Keeper) DequeueAllMatureUBDQueue(ctx sdk.Context, currTime time.Time) (m
store := ctx.KVStore(k.storeKey)

// gets an iterator for all timeslices from time 0 until the current Blockheader time
unbondingTimesliceIterator := k.UBDQueueIterator(ctx, ctx.BlockHeader().Time)
unbondingTimesliceIterator := k.UBDQueueIterator(ctx, currTime)
defer unbondingTimesliceIterator.Close()

for ; unbondingTimesliceIterator.Valid(); unbondingTimesliceIterator.Next() {
Expand Down
3 changes: 3 additions & 0 deletions x/staking/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ type KeeperTestSuite struct {
addrs []sdk.AccAddress
vals []types.Validator
queryClient types.QueryClient
msgServer types.MsgServer
}

func (suite *KeeperTestSuite) SetupTest() {
Expand All @@ -35,6 +36,8 @@ func (suite *KeeperTestSuite) SetupTest() {
types.RegisterQueryServer(queryHelper, querier)
queryClient := types.NewQueryClient(queryHelper)

suite.msgServer = keeper.NewMsgServerImpl(app.StakingKeeper)

addrs, _, validators := createValidators(suite.T(), ctx, app, []int64{9, 8, 7})
header := tmproto.Header{
ChainID: "HelloChain",
Expand Down
Loading