Merge PR #3292: Enable/disable withdraw addresses with a parameter

This commit is contained in:
Christopher Goes 2019-01-16 23:17:56 +01:00 committed by GitHub
parent 2942f83ff5
commit 0684a42d9f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 63 additions and 12 deletions

View File

@ -27,8 +27,9 @@ BREAKING CHANGES
* [\#3064](https://github.com/cosmos/cosmos-sdk/issues/3064) Sanitize `sdk.Coin` denom. Coins denoms are now case insensitive, i.e. 100fooToken equals to 100FOOTOKEN.
* [\#3195](https://github.com/cosmos/cosmos-sdk/issues/3195) Allows custom configuration for syncable strategy
* [\#3242](https://github.com/cosmos/cosmos-sdk/issues/3242) Fix infinite gas
meter utilization during aborted ante handler executions.
* [staking] \#2222 `/stake` -> `/staking` module rename
meter utilization during aborted ante handler executions.
* [\#2222] [x/staking] `/stake` -> `/staking` module rename
* \#3292 [x/distribution] Enable or disable withdraw addresses with a parameter in the param store
* [staking] \#1402 Redelegation and unbonding-delegation structs changed to include multiple an array of entries
* Tendermint

View File

@ -4,8 +4,6 @@ import (
"encoding/json"
"io"
"github.com/cosmos/cosmos-sdk/store"
"github.com/cosmos/cosmos-sdk/baseapp"
"github.com/cosmos/cosmos-sdk/store"

View File

@ -11,6 +11,7 @@ func InitGenesis(ctx sdk.Context, keeper Keeper, data types.GenesisState) {
keeper.SetCommunityTax(ctx, data.CommunityTax)
keeper.SetBaseProposerReward(ctx, data.BaseProposerReward)
keeper.SetBonusProposerReward(ctx, data.BonusProposerReward)
keeper.SetWithdrawAddrEnabled(ctx, data.WithdrawAddrEnabled)
for _, dwi := range data.DelegatorWithdrawInfos {
keeper.SetDelegatorWithdrawAddr(ctx, dwi.DelegatorAddr, dwi.WithdrawAddr)
}
@ -39,6 +40,7 @@ func ExportGenesis(ctx sdk.Context, keeper Keeper) types.GenesisState {
communityTax := keeper.GetCommunityTax(ctx)
baseProposerRewards := keeper.GetBaseProposerReward(ctx)
bonusProposerRewards := keeper.GetBonusProposerReward(ctx)
withdrawAddrEnabled := keeper.GetWithdrawAddrEnabled(ctx)
dwi := make([]types.DelegatorWithdrawInfo, 0)
keeper.IterateDelegatorWithdrawAddrs(ctx, func(del sdk.AccAddress, addr sdk.AccAddress) (stop bool) {
dwi = append(dwi, types.DelegatorWithdrawInfo{
@ -102,6 +104,6 @@ func ExportGenesis(ctx sdk.Context, keeper Keeper) types.GenesisState {
return false
},
)
return types.NewGenesisState(feePool, communityTax, baseProposerRewards, bonusProposerRewards, dwi, pp, outstanding,
acc, his, cur, dels, slashes)
return types.NewGenesisState(feePool, communityTax, baseProposerRewards, bonusProposerRewards, withdrawAddrEnabled,
dwi, pp, outstanding, acc, his, cur, dels, slashes)
}

View File

@ -27,7 +27,10 @@ func NewHandler(k keeper.Keeper) sdk.Handler {
func handleMsgModifyWithdrawAddress(ctx sdk.Context, msg types.MsgSetWithdrawAddress, k keeper.Keeper) sdk.Result {
k.SetDelegatorWithdrawAddr(ctx, msg.DelegatorAddr, msg.WithdrawAddr)
err := k.SetWithdrawAddr(ctx, msg.DelegatorAddr, msg.WithdrawAddr)
if err != nil {
return err.Result()
}
tags := sdk.NewTags(
tags.Delegator, []byte(msg.DelegatorAddr.String()),

View File

@ -35,6 +35,17 @@ func NewKeeper(cdc *codec.Codec, key sdk.StoreKey, paramSpace params.Subspace, c
return keeper
}
// set withdraw address
func (k Keeper) SetWithdrawAddr(ctx sdk.Context, delegatorAddr sdk.AccAddress, withdrawAddr sdk.AccAddress) sdk.Error {
if !k.GetWithdrawAddrEnabled(ctx) {
return types.ErrSetWithdrawAddrDisabled(k.codespace)
}
k.SetDelegatorWithdrawAddr(ctx, delegatorAddr, withdrawAddr)
return nil
}
// withdraw rewards from a delegation
func (k Keeper) WithdrawDelegationRewards(ctx sdk.Context, delAddr sdk.AccAddress, valAddr sdk.ValAddress) sdk.Error {
val := k.stakingKeeper.Validator(ctx, valAddr)

View File

@ -9,6 +9,20 @@ import (
"github.com/cosmos/cosmos-sdk/x/distribution/types"
)
func TestSetWithdrawAddr(t *testing.T) {
ctx, _, keeper, _, _ := CreateTestInputDefault(t, false, 1000)
keeper.SetWithdrawAddrEnabled(ctx, false)
err := keeper.SetWithdrawAddr(ctx, delAddr1, delAddr2)
require.NotNil(t, err)
keeper.SetWithdrawAddrEnabled(ctx, true)
err = keeper.SetWithdrawAddr(ctx, delAddr1, delAddr2)
require.Nil(t, err)
}
func TestWithdrawValidatorCommission(t *testing.T) {
ctx, ak, keeper, _, _ := CreateTestInputDefault(t, false, 1000)

View File

@ -27,6 +27,7 @@ var (
ParamStoreKeyCommunityTax = []byte("communitytax")
ParamStoreKeyBaseProposerReward = []byte("baseproposerreward")
ParamStoreKeyBonusProposerReward = []byte("bonusproposerreward")
ParamStoreKeyWithdrawAddrEnabled = []byte("withdrawaddrenabled")
)
// gets an address from a delegator's withdraw info key

View File

@ -11,6 +11,7 @@ func ParamTypeTable() params.TypeTable {
ParamStoreKeyCommunityTax, sdk.Dec{},
ParamStoreKeyBaseProposerReward, sdk.Dec{},
ParamStoreKeyBonusProposerReward, sdk.Dec{},
ParamStoreKeyWithdrawAddrEnabled, false,
)
}
@ -52,3 +53,16 @@ func (k Keeper) GetBonusProposerReward(ctx sdk.Context) sdk.Dec {
func (k Keeper) SetBonusProposerReward(ctx sdk.Context, percent sdk.Dec) {
k.paramSpace.Set(ctx, ParamStoreKeyBonusProposerReward, &percent)
}
// returns the current WithdrawAddrEnabled
// nolint: errcheck
func (k Keeper) GetWithdrawAddrEnabled(ctx sdk.Context) bool {
var enabled bool
k.paramSpace.Get(ctx, ParamStoreKeyWithdrawAddrEnabled, &enabled)
return enabled
}
// nolint: errcheck
func (k Keeper) SetWithdrawAddrEnabled(ctx sdk.Context, enabled bool) {
k.paramSpace.Set(ctx, ParamStoreKeyWithdrawAddrEnabled, &enabled)
}

View File

@ -8,10 +8,11 @@ import (
type CodeType = sdk.CodeType
const (
DefaultCodespace sdk.CodespaceType = "DISTR"
CodeInvalidInput CodeType = 103
CodeNoDistributionInfo CodeType = 104
CodeNoValidatorCommission CodeType = 105
DefaultCodespace sdk.CodespaceType = "DISTR"
CodeInvalidInput CodeType = 103
CodeNoDistributionInfo CodeType = 104
CodeNoValidatorCommission CodeType = 105
CodeSetWithdrawAddrDisabled CodeType = 106
)
func ErrNilDelegatorAddr(codespace sdk.CodespaceType) sdk.Error {
@ -32,3 +33,6 @@ func ErrNoValidatorDistInfo(codespace sdk.CodespaceType) sdk.Error {
func ErrNoValidatorCommission(codespace sdk.CodespaceType) sdk.Error {
return sdk.NewError(codespace, CodeNoValidatorCommission, "no validator commission to withdraw")
}
func ErrSetWithdrawAddrDisabled(codespace sdk.CodespaceType) sdk.Error {
return sdk.NewError(codespace, CodeSetWithdrawAddrDisabled, "set withdraw address disabled")
}

View File

@ -52,6 +52,7 @@ type GenesisState struct {
CommunityTax sdk.Dec `json:"community_tax"`
BaseProposerReward sdk.Dec `json:"base_proposer_reward"`
BonusProposerReward sdk.Dec `json:"bonus_proposer_reward"`
WithdrawAddrEnabled bool `json:"withdraw_addr_enabled"`
DelegatorWithdrawInfos []DelegatorWithdrawInfo `json:"delegator_withdraw_infos"`
PreviousProposer sdk.ConsAddress `json:"previous_proposer"`
OutstandingRewards sdk.DecCoins `json:"outstanding_rewards"`
@ -63,7 +64,7 @@ type GenesisState struct {
}
func NewGenesisState(feePool FeePool, communityTax, baseProposerReward, bonusProposerReward sdk.Dec,
dwis []DelegatorWithdrawInfo, pp sdk.ConsAddress, r OutstandingRewards,
withdrawAddrEnabled bool, dwis []DelegatorWithdrawInfo, pp sdk.ConsAddress, r OutstandingRewards,
acc []ValidatorAccumulatedCommissionRecord, historical []ValidatorHistoricalRewardsRecord,
cur []ValidatorCurrentRewardsRecord, dels []DelegatorStartingInfoRecord,
slashes []ValidatorSlashEventRecord) GenesisState {
@ -73,6 +74,7 @@ func NewGenesisState(feePool FeePool, communityTax, baseProposerReward, bonusPro
CommunityTax: communityTax,
BaseProposerReward: baseProposerReward,
BonusProposerReward: bonusProposerReward,
WithdrawAddrEnabled: withdrawAddrEnabled,
DelegatorWithdrawInfos: dwis,
PreviousProposer: pp,
OutstandingRewards: r,
@ -91,6 +93,7 @@ func DefaultGenesisState() GenesisState {
CommunityTax: sdk.NewDecWithPrec(2, 2), // 2%
BaseProposerReward: sdk.NewDecWithPrec(1, 2), // 1%
BonusProposerReward: sdk.NewDecWithPrec(4, 2), // 4%
WithdrawAddrEnabled: true,
DelegatorWithdrawInfos: []DelegatorWithdrawInfo{},
PreviousProposer: nil,
OutstandingRewards: sdk.DecCoins{},