From 0684a42d9f2edfa5e105c5d010c02a2334bb8209 Mon Sep 17 00:00:00 2001 From: Christopher Goes Date: Wed, 16 Jan 2019 23:17:56 +0100 Subject: [PATCH] Merge PR #3292: Enable/disable withdraw addresses with a parameter --- PENDING.md | 5 +++-- cmd/gaia/cmd/gaiad/main.go | 2 -- x/distribution/genesis.go | 6 ++++-- x/distribution/handler.go | 5 ++++- x/distribution/keeper/keeper.go | 11 +++++++++++ x/distribution/keeper/keeper_test.go | 14 ++++++++++++++ x/distribution/keeper/key.go | 1 + x/distribution/keeper/params.go | 14 ++++++++++++++ x/distribution/types/errors.go | 12 ++++++++---- x/distribution/types/genesis.go | 5 ++++- 10 files changed, 63 insertions(+), 12 deletions(-) diff --git a/PENDING.md b/PENDING.md index 9e53e61d48..5ab7497bd2 100644 --- a/PENDING.md +++ b/PENDING.md @@ -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 diff --git a/cmd/gaia/cmd/gaiad/main.go b/cmd/gaia/cmd/gaiad/main.go index f1b2160975..dbeb6afa3e 100644 --- a/cmd/gaia/cmd/gaiad/main.go +++ b/cmd/gaia/cmd/gaiad/main.go @@ -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" diff --git a/x/distribution/genesis.go b/x/distribution/genesis.go index cfebd03816..9212a5aabd 100644 --- a/x/distribution/genesis.go +++ b/x/distribution/genesis.go @@ -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) } diff --git a/x/distribution/handler.go b/x/distribution/handler.go index 5b29cb8456..e5e2c5aa84 100644 --- a/x/distribution/handler.go +++ b/x/distribution/handler.go @@ -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()), diff --git a/x/distribution/keeper/keeper.go b/x/distribution/keeper/keeper.go index 46d749f748..a40e37380d 100644 --- a/x/distribution/keeper/keeper.go +++ b/x/distribution/keeper/keeper.go @@ -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) diff --git a/x/distribution/keeper/keeper_test.go b/x/distribution/keeper/keeper_test.go index 99e96e7861..7029469182 100644 --- a/x/distribution/keeper/keeper_test.go +++ b/x/distribution/keeper/keeper_test.go @@ -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) diff --git a/x/distribution/keeper/key.go b/x/distribution/keeper/key.go index 5e98155972..05bd7bf5cd 100644 --- a/x/distribution/keeper/key.go +++ b/x/distribution/keeper/key.go @@ -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 diff --git a/x/distribution/keeper/params.go b/x/distribution/keeper/params.go index d0faf266b4..fb1b23ed58 100644 --- a/x/distribution/keeper/params.go +++ b/x/distribution/keeper/params.go @@ -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) +} diff --git a/x/distribution/types/errors.go b/x/distribution/types/errors.go index cdb6a7233d..907ad226e2 100644 --- a/x/distribution/types/errors.go +++ b/x/distribution/types/errors.go @@ -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") +} diff --git a/x/distribution/types/genesis.go b/x/distribution/types/genesis.go index 63dd2d252a..2ae3bd6c61 100644 --- a/x/distribution/types/genesis.go +++ b/x/distribution/types/genesis.go @@ -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{},