2019-05-16 08:25:32 -07:00
|
|
|
package staking
|
|
|
|
|
|
|
|
import (
|
2020-08-25 08:44:13 -07:00
|
|
|
"context"
|
2019-05-16 08:25:32 -07:00
|
|
|
"encoding/json"
|
2020-01-09 12:14:23 -08:00
|
|
|
"fmt"
|
2019-08-28 07:58:25 -07:00
|
|
|
"math/rand"
|
2019-05-16 08:25:32 -07:00
|
|
|
|
2020-08-25 08:44:13 -07:00
|
|
|
"github.com/grpc-ecosystem/grpc-gateway/runtime"
|
2019-06-05 16:26:17 -07:00
|
|
|
"github.com/spf13/cobra"
|
2019-05-28 01:44:04 -07:00
|
|
|
abci "github.com/tendermint/tendermint/abci/types"
|
|
|
|
|
2020-06-01 05:46:03 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/client"
|
2019-05-16 08:25:32 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/codec"
|
2020-07-23 19:45:34 -07:00
|
|
|
cdctypes "github.com/cosmos/cosmos-sdk/codec/types"
|
2019-05-16 08:25:32 -07:00
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
2019-06-05 16:26:17 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/types/module"
|
2020-03-23 04:55:44 -07:00
|
|
|
simtypes "github.com/cosmos/cosmos-sdk/types/simulation"
|
2019-05-28 01:44:04 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/x/staking/client/cli"
|
2020-06-12 07:54:37 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/x/staking/keeper"
|
2019-08-13 15:16:03 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/x/staking/simulation"
|
2019-05-16 08:25:32 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/x/staking/types"
|
|
|
|
)
|
|
|
|
|
2021-10-08 19:02:16 -07:00
|
|
|
const (
|
feat: Cancel unbonding delegation entry (#10885)
## Description
Closes: #577
This pull request contains `Canceling unbonding delegation entry` and `delegate back to previous validator`
### `Msg` Service
```protobuf=
package cosmos.staking.v1beta1;
service Msg {
// CancelUnbondingDelegation
rpc CancelUnbondingDelegation(MsgCancelUnbondingDelegation) returns (MsgCancelUnbondingDelegationResponse);
}
// MsgCancelUnbondingDelegation
message MsgCancelUnbondingDelegation {
option (gogoproto.equal) = false;
option (gogoproto.goproto_getters) = false;
string delegator_address = 1 [(cosmos_proto.scalar) = "cosmos.AddressString"];
string validator_address = 2 [(cosmos_proto.scalar) = "cosmos.AddressString"];
cosmos.base.v1beta1.Coin amount = 3 [(gogoproto.nullable) = false];
// creation_height is the height which the unbonding took place.
int64 creation_height = 4;
}
// MsgCancelUnbondingDelegationResponse
message MsgCancelUnbondingDelegationResponse{
}
```
### `Msg` Method Implementation
```go=
func (k msgServer) CancelUnbondingDelegation(goCtx context.Context, msg *types.MsgCancelUnbondingDelegation) (*types.MsgCancelUnbondingDelegationResponse, error) {
/*
// checking the unbonding delegation at creation_height
// get the unbonding delegations of delegatorAddress
if ubdEntry balance is equal to msg.Amount
remove the entry from ubd
else
update the specific entry with new balance
if len(ubd.Entries) == 0 {
k.RemoveUnbondingDelegation(ctx, ubd)
} else {
k.SetUnbondingDelegation(ctx, ubd)
}
*/
// update the delegation back to validator
// get validator
validator, found := k.GetValidator(ctx, valAddr)
if !found {
return nil, types.ErrNoValidatorFound
}
// delegate the unbonding amount to validator back
_, err = k.Keeper.Delegate(ctx, delegatorAddress, msg.Amount.Amount, types.Unbonding, validator, false)
if err != nil {
return nil, err
}
_, err := ms.Keeper.CancelUnbondingDelegation(ctx,delegatorAddress,validatorAddress,amount,creation_height)
if err != nil {
return nil, err
}
return &types.MsgCancelUnbondingDelegationResponse{}, nil
}
```
#### `cli tx` Method
```bash=
simd tx staking cancel-unbond [validator-address] [amount] [creation_height] --from [user] --chain-id [chain-id]
Example:
simd tx staking cancel-unbond cosmosvaloper1mqtyv4qux68r26mql2hjhgrvz72snjwpulq22m 100000stake 10280 --from test1 --chain-id test-chain
```
---
### 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)
2022-04-05 03:01:13 -07:00
|
|
|
consensusVersion uint64 = 4
|
2021-10-08 19:02:16 -07:00
|
|
|
)
|
|
|
|
|
2019-05-16 08:25:32 -07:00
|
|
|
var (
|
2019-08-13 15:16:03 -07:00
|
|
|
_ module.AppModule = AppModule{}
|
|
|
|
_ module.AppModuleBasic = AppModuleBasic{}
|
2019-12-05 01:29:54 -08:00
|
|
|
_ module.AppModuleSimulation = AppModule{}
|
2019-05-16 08:25:32 -07:00
|
|
|
)
|
|
|
|
|
2019-08-13 15:16:03 -07:00
|
|
|
// AppModuleBasic defines the basic application module used by the staking module.
|
2020-04-21 14:33:56 -07:00
|
|
|
type AppModuleBasic struct {
|
2021-04-29 03:46:22 -07:00
|
|
|
cdc codec.Codec
|
2020-04-21 14:33:56 -07:00
|
|
|
}
|
2019-05-16 08:25:32 -07:00
|
|
|
|
2019-06-05 16:26:17 -07:00
|
|
|
var _ module.AppModuleBasic = AppModuleBasic{}
|
2019-05-16 08:25:32 -07:00
|
|
|
|
2019-08-13 15:16:03 -07:00
|
|
|
// Name returns the staking module's name.
|
2019-05-16 08:25:32 -07:00
|
|
|
func (AppModuleBasic) Name() string {
|
2020-06-12 07:54:37 -07:00
|
|
|
return types.ModuleName
|
2019-05-16 08:25:32 -07:00
|
|
|
}
|
|
|
|
|
2020-09-07 07:47:12 -07:00
|
|
|
// RegisterLegacyAminoCodec registers the staking module's types on the given LegacyAmino codec.
|
|
|
|
func (AppModuleBasic) RegisterLegacyAminoCodec(cdc *codec.LegacyAmino) {
|
|
|
|
types.RegisterLegacyAminoCodec(cdc)
|
2019-05-16 08:25:32 -07:00
|
|
|
}
|
|
|
|
|
2020-07-23 19:45:34 -07:00
|
|
|
// RegisterInterfaces registers the module's interface types
|
|
|
|
func (b AppModuleBasic) RegisterInterfaces(registry cdctypes.InterfaceRegistry) {
|
|
|
|
types.RegisterInterfaces(registry)
|
|
|
|
}
|
|
|
|
|
2019-08-13 15:16:03 -07:00
|
|
|
// DefaultGenesis returns default genesis state as raw bytes for the staking
|
|
|
|
// module.
|
2021-04-29 03:46:22 -07:00
|
|
|
func (AppModuleBasic) DefaultGenesis(cdc codec.JSONCodec) json.RawMessage {
|
2020-06-12 07:54:37 -07:00
|
|
|
return cdc.MustMarshalJSON(types.DefaultGenesisState())
|
2019-05-16 08:25:32 -07:00
|
|
|
}
|
|
|
|
|
2019-08-13 15:16:03 -07:00
|
|
|
// ValidateGenesis performs genesis state validation for the staking module.
|
2021-04-29 03:46:22 -07:00
|
|
|
func (AppModuleBasic) ValidateGenesis(cdc codec.JSONCodec, config client.TxEncodingConfig, bz json.RawMessage) error {
|
2020-06-12 07:54:37 -07:00
|
|
|
var data types.GenesisState
|
2020-02-18 04:50:13 -08:00
|
|
|
if err := cdc.UnmarshalJSON(bz, &data); err != nil {
|
2020-06-12 07:54:37 -07:00
|
|
|
return fmt.Errorf("failed to unmarshal %s genesis state: %w", types.ModuleName, err)
|
2019-05-16 08:25:32 -07:00
|
|
|
}
|
2020-01-09 12:14:23 -08:00
|
|
|
|
2020-08-11 07:22:30 -07:00
|
|
|
return ValidateGenesis(&data)
|
2019-05-16 08:25:32 -07:00
|
|
|
}
|
|
|
|
|
2020-10-28 04:39:49 -07:00
|
|
|
// RegisterGRPCGatewayRoutes registers the gRPC Gateway routes for the staking module.
|
|
|
|
func (AppModuleBasic) RegisterGRPCGatewayRoutes(clientCtx client.Context, mux *runtime.ServeMux) {
|
2021-10-28 04:01:25 -07:00
|
|
|
if err := types.RegisterQueryHandlerClient(context.Background(), mux, types.NewQueryClient(clientCtx)); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2020-08-25 08:44:13 -07:00
|
|
|
}
|
|
|
|
|
2019-08-13 15:16:03 -07:00
|
|
|
// GetTxCmd returns the root tx command for the staking module.
|
2020-07-21 08:28:43 -07:00
|
|
|
func (AppModuleBasic) GetTxCmd() *cobra.Command {
|
2020-07-21 06:54:07 -07:00
|
|
|
return cli.NewTxCmd()
|
2019-06-05 16:26:17 -07:00
|
|
|
}
|
|
|
|
|
2019-08-13 15:16:03 -07:00
|
|
|
// GetQueryCmd returns no root query command for the staking module.
|
2020-07-21 08:28:43 -07:00
|
|
|
func (AppModuleBasic) GetQueryCmd() *cobra.Command {
|
2020-07-17 08:23:42 -07:00
|
|
|
return cli.GetQueryCmd()
|
2019-06-05 16:26:17 -07:00
|
|
|
}
|
|
|
|
|
2019-08-13 15:16:03 -07:00
|
|
|
// AppModule implements an application module for the staking module.
|
2019-05-16 08:25:32 -07:00
|
|
|
type AppModule struct {
|
|
|
|
AppModuleBasic
|
2019-08-13 15:16:03 -07:00
|
|
|
|
2020-06-12 07:54:37 -07:00
|
|
|
keeper keeper.Keeper
|
2019-08-26 07:57:21 -07:00
|
|
|
accountKeeper types.AccountKeeper
|
2020-01-30 13:31:16 -08:00
|
|
|
bankKeeper types.BankKeeper
|
2019-05-16 08:25:32 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewAppModule creates a new AppModule object
|
2021-04-29 03:46:22 -07:00
|
|
|
func NewAppModule(cdc codec.Codec, keeper keeper.Keeper, ak types.AccountKeeper, bk types.BankKeeper) AppModule {
|
2019-05-16 08:25:32 -07:00
|
|
|
return AppModule{
|
2020-04-21 14:33:56 -07:00
|
|
|
AppModuleBasic: AppModuleBasic{cdc: cdc},
|
2019-12-05 01:29:54 -08:00
|
|
|
keeper: keeper,
|
2020-01-30 13:31:16 -08:00
|
|
|
accountKeeper: ak,
|
|
|
|
bankKeeper: bk,
|
2019-05-16 08:25:32 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-13 15:16:03 -07:00
|
|
|
// Name returns the staking module's name.
|
2019-05-16 08:25:32 -07:00
|
|
|
func (AppModule) Name() string {
|
2020-06-12 07:54:37 -07:00
|
|
|
return types.ModuleName
|
2019-05-16 08:25:32 -07:00
|
|
|
}
|
|
|
|
|
2019-08-13 15:16:03 -07:00
|
|
|
// RegisterInvariants registers the staking module invariants.
|
2019-06-06 13:32:38 -07:00
|
|
|
func (am AppModule) RegisterInvariants(ir sdk.InvariantRegistry) {
|
2020-06-12 07:54:37 -07:00
|
|
|
keeper.RegisterInvariants(ir, am.keeper)
|
2019-05-16 08:25:32 -07:00
|
|
|
}
|
|
|
|
|
2021-07-19 07:49:42 -07:00
|
|
|
// Deprecated: Route returns the message routing key for the staking module.
|
2020-06-11 08:37:23 -07:00
|
|
|
func (am AppModule) Route() sdk.Route {
|
2021-07-19 07:49:42 -07:00
|
|
|
return sdk.Route{}
|
2019-05-16 08:25:32 -07:00
|
|
|
}
|
|
|
|
|
2019-08-13 15:16:03 -07:00
|
|
|
// QuerierRoute returns the staking module's querier route name.
|
2019-05-16 08:25:32 -07:00
|
|
|
func (AppModule) QuerierRoute() string {
|
2020-06-12 07:54:37 -07:00
|
|
|
return types.QuerierRoute
|
2019-05-16 08:25:32 -07:00
|
|
|
}
|
|
|
|
|
2020-07-24 12:04:29 -07:00
|
|
|
// LegacyQuerierHandler returns the staking module sdk.Querier.
|
2020-08-26 02:39:38 -07:00
|
|
|
func (am AppModule) LegacyQuerierHandler(legacyQuerierCdc *codec.LegacyAmino) sdk.Querier {
|
2020-07-25 01:03:58 -07:00
|
|
|
return keeper.NewQuerier(am.keeper, legacyQuerierCdc)
|
2019-05-16 08:25:32 -07:00
|
|
|
}
|
|
|
|
|
2020-10-19 10:46:10 -07:00
|
|
|
// RegisterServices registers module services.
|
2020-10-12 09:31:51 -07:00
|
|
|
func (am AppModule) RegisterServices(cfg module.Configurator) {
|
2020-10-19 10:46:10 -07:00
|
|
|
types.RegisterMsgServer(cfg.MsgServer(), keeper.NewMsgServerImpl(am.keeper))
|
2020-07-28 05:53:35 -07:00
|
|
|
querier := keeper.Querier{Keeper: am.keeper}
|
2020-10-12 09:31:51 -07:00
|
|
|
types.RegisterQueryServer(cfg.QueryServer(), querier)
|
2021-02-25 02:43:31 -08:00
|
|
|
|
|
|
|
m := keeper.NewMigrator(am.keeper)
|
|
|
|
cfg.RegisterMigration(types.ModuleName, 1, m.Migrate1to2)
|
2021-10-08 19:02:16 -07:00
|
|
|
cfg.RegisterMigration(types.ModuleName, 2, m.Migrate2to3)
|
2020-07-28 05:53:35 -07:00
|
|
|
}
|
2020-06-06 00:59:57 -07:00
|
|
|
|
2019-08-13 15:16:03 -07:00
|
|
|
// InitGenesis performs genesis initialization for the staking module. It returns
|
|
|
|
// no validator updates.
|
2021-04-29 03:46:22 -07:00
|
|
|
func (am AppModule) InitGenesis(ctx sdk.Context, cdc codec.JSONCodec, data json.RawMessage) []abci.ValidatorUpdate {
|
2020-06-12 07:54:37 -07:00
|
|
|
var genesisState types.GenesisState
|
2020-05-02 12:26:59 -07:00
|
|
|
|
2020-02-18 04:50:13 -08:00
|
|
|
cdc.MustUnmarshalJSON(data, &genesisState)
|
2020-05-02 12:26:59 -07:00
|
|
|
|
2020-08-11 07:22:30 -07:00
|
|
|
return InitGenesis(ctx, am.keeper, am.accountKeeper, am.bankKeeper, &genesisState)
|
2019-05-16 08:25:32 -07:00
|
|
|
}
|
|
|
|
|
2019-08-13 15:16:03 -07:00
|
|
|
// ExportGenesis returns the exported genesis state as raw bytes for the staking
|
|
|
|
// module.
|
2021-04-29 03:46:22 -07:00
|
|
|
func (am AppModule) ExportGenesis(ctx sdk.Context, cdc codec.JSONCodec) json.RawMessage {
|
2019-05-16 08:25:32 -07:00
|
|
|
gs := ExportGenesis(ctx, am.keeper)
|
2020-02-18 04:50:13 -08:00
|
|
|
return cdc.MustMarshalJSON(gs)
|
2019-05-16 08:25:32 -07:00
|
|
|
}
|
|
|
|
|
2021-02-10 09:49:31 -08:00
|
|
|
// ConsensusVersion implements AppModule/ConsensusVersion.
|
2021-10-08 19:02:16 -07:00
|
|
|
func (AppModule) ConsensusVersion() uint64 { return consensusVersion }
|
2021-02-10 09:49:31 -08:00
|
|
|
|
2019-08-13 15:16:03 -07:00
|
|
|
// BeginBlock returns the begin blocker for the staking module.
|
2019-12-18 05:20:02 -08:00
|
|
|
func (am AppModule) BeginBlock(ctx sdk.Context, _ abci.RequestBeginBlock) {
|
|
|
|
BeginBlocker(ctx, am.keeper)
|
|
|
|
}
|
2019-05-16 08:25:32 -07:00
|
|
|
|
2019-08-13 15:16:03 -07:00
|
|
|
// EndBlock returns the end blocker for the staking module. It returns no validator
|
|
|
|
// updates.
|
2019-06-26 09:03:25 -07:00
|
|
|
func (am AppModule) EndBlock(ctx sdk.Context, _ abci.RequestEndBlock) []abci.ValidatorUpdate {
|
2019-05-16 08:25:32 -07:00
|
|
|
return EndBlocker(ctx, am.keeper)
|
|
|
|
}
|
2019-12-05 01:29:54 -08:00
|
|
|
|
|
|
|
// AppModuleSimulation functions
|
|
|
|
|
|
|
|
// GenerateGenesisState creates a randomized GenState of the staking module.
|
|
|
|
func (AppModule) GenerateGenesisState(simState *module.SimulationState) {
|
|
|
|
simulation.RandomizedGenState(simState)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ProposalContents doesn't return any content functions for governance proposals.
|
2020-03-23 04:55:44 -07:00
|
|
|
func (AppModule) ProposalContents(simState module.SimulationState) []simtypes.WeightedProposalContent {
|
2019-12-05 01:29:54 -08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// RandomizedParams creates randomized staking param changes for the simulator.
|
2020-03-23 04:55:44 -07:00
|
|
|
func (AppModule) RandomizedParams(r *rand.Rand) []simtypes.ParamChange {
|
2019-12-05 01:29:54 -08:00
|
|
|
return simulation.ParamChanges(r)
|
|
|
|
}
|
|
|
|
|
|
|
|
// RegisterStoreDecoder registers a decoder for staking module's types
|
2020-04-21 14:33:56 -07:00
|
|
|
func (am AppModule) RegisterStoreDecoder(sdr sdk.StoreDecoderRegistry) {
|
2020-06-12 07:54:37 -07:00
|
|
|
sdr[types.StoreKey] = simulation.NewDecodeStore(am.cdc)
|
2019-12-05 01:29:54 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
// WeightedOperations returns the all the staking module operations with their respective weights.
|
2020-03-23 04:55:44 -07:00
|
|
|
func (am AppModule) WeightedOperations(simState module.SimulationState) []simtypes.WeightedOperation {
|
2020-01-30 13:31:16 -08:00
|
|
|
return simulation.WeightedOperations(
|
|
|
|
simState.AppParams, simState.Cdc, am.accountKeeper, am.bankKeeper, am.keeper,
|
|
|
|
)
|
2019-12-05 01:29:54 -08:00
|
|
|
}
|