2020-02-18 04:42:53 -08:00
|
|
|
package keeper_test
|
2018-06-26 19:00:12 -07:00
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2018-06-29 18:10:15 -07:00
|
|
|
"github.com/stretchr/testify/require"
|
2020-07-14 10:41:30 -07:00
|
|
|
"github.com/stretchr/testify/suite"
|
2018-06-26 19:00:12 -07:00
|
|
|
|
2020-08-14 10:58:53 -07:00
|
|
|
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
|
2020-03-20 10:14:14 -07:00
|
|
|
|
2020-07-14 10:41:30 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/baseapp"
|
2020-02-18 04:42:53 -08:00
|
|
|
"github.com/cosmos/cosmos-sdk/simapp"
|
2020-07-14 10:41:30 -07:00
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
|
|
"github.com/cosmos/cosmos-sdk/x/staking/keeper"
|
2019-01-11 12:08:01 -08:00
|
|
|
"github.com/cosmos/cosmos-sdk/x/staking/types"
|
2018-06-26 19:00:12 -07:00
|
|
|
)
|
|
|
|
|
2020-07-14 10:41:30 -07:00
|
|
|
type KeeperTestSuite struct {
|
|
|
|
suite.Suite
|
|
|
|
|
|
|
|
app *simapp.SimApp
|
|
|
|
ctx sdk.Context
|
|
|
|
addrs []sdk.AccAddress
|
|
|
|
vals []types.Validator
|
|
|
|
queryClient types.QueryClient
|
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
|
|
|
msgServer types.MsgServer
|
2020-07-14 10:41:30 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func (suite *KeeperTestSuite) SetupTest() {
|
2021-08-16 17:52:06 -07:00
|
|
|
app := simapp.Setup(suite.T(), false)
|
2020-08-14 10:58:53 -07:00
|
|
|
ctx := app.BaseApp.NewContext(false, tmproto.Header{})
|
2020-07-14 10:41:30 -07:00
|
|
|
|
|
|
|
querier := keeper.Querier{Keeper: app.StakingKeeper}
|
|
|
|
|
|
|
|
queryHelper := baseapp.NewQueryServerTestHelper(ctx, app.InterfaceRegistry())
|
|
|
|
types.RegisterQueryServer(queryHelper, querier)
|
|
|
|
queryClient := types.NewQueryClient(queryHelper)
|
|
|
|
|
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
|
|
|
suite.msgServer = keeper.NewMsgServerImpl(app.StakingKeeper)
|
|
|
|
|
2020-10-23 05:07:52 -07:00
|
|
|
addrs, _, validators := createValidators(suite.T(), ctx, app, []int64{9, 8, 7})
|
2020-08-14 10:58:53 -07:00
|
|
|
header := tmproto.Header{
|
2020-07-14 10:41:30 -07:00
|
|
|
ChainID: "HelloChain",
|
|
|
|
Height: 5,
|
|
|
|
}
|
|
|
|
|
2020-10-29 05:17:37 -07:00
|
|
|
// sort a copy of the validators, so that original validators does not
|
|
|
|
// have its order changed
|
|
|
|
sortedVals := make([]types.Validator, len(validators))
|
|
|
|
copy(sortedVals, validators)
|
2021-01-28 05:54:39 -08:00
|
|
|
hi := types.NewHistoricalInfo(header, sortedVals, app.StakingKeeper.PowerReduction(ctx))
|
2020-10-23 05:07:52 -07:00
|
|
|
app.StakingKeeper.SetHistoricalInfo(ctx, 5, &hi)
|
2020-07-14 10:41:30 -07:00
|
|
|
|
|
|
|
suite.app, suite.ctx, suite.queryClient, suite.addrs, suite.vals = app, ctx, queryClient, addrs, validators
|
|
|
|
}
|
2018-06-26 19:00:12 -07:00
|
|
|
func TestParams(t *testing.T) {
|
2021-08-16 17:52:06 -07:00
|
|
|
app := simapp.Setup(t, false)
|
2020-08-14 10:58:53 -07:00
|
|
|
ctx := app.BaseApp.NewContext(false, tmproto.Header{})
|
2020-02-18 04:42:53 -08:00
|
|
|
|
2018-06-26 19:00:12 -07:00
|
|
|
expParams := types.DefaultParams()
|
|
|
|
|
|
|
|
//check that the empty keeper loads the default
|
2020-02-18 04:42:53 -08:00
|
|
|
resParams := app.StakingKeeper.GetParams(ctx)
|
2018-06-29 18:10:15 -07:00
|
|
|
require.True(t, expParams.Equal(resParams))
|
2018-06-26 19:00:12 -07:00
|
|
|
|
|
|
|
//modify a params, save, and retrieve
|
|
|
|
expParams.MaxValidators = 777
|
2020-02-18 04:42:53 -08:00
|
|
|
app.StakingKeeper.SetParams(ctx, expParams)
|
|
|
|
resParams = app.StakingKeeper.GetParams(ctx)
|
2018-06-29 18:10:15 -07:00
|
|
|
require.True(t, expParams.Equal(resParams))
|
2018-06-26 19:00:12 -07:00
|
|
|
}
|
2020-07-14 10:41:30 -07:00
|
|
|
|
|
|
|
func TestKeeperTestSuite(t *testing.T) {
|
|
|
|
suite.Run(t, new(KeeperTestSuite))
|
|
|
|
}
|