Skip to content

Commit

Permalink
Merge Pr #5506: Refactor use of parameters in x/distribution to match…
Browse files Browse the repository at this point in the history
… module spec
  • Loading branch information
alexanderbez authored Jan 10, 2020
1 parent 6024115 commit bf41dea
Show file tree
Hide file tree
Showing 25 changed files with 605 additions and 615 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ Ref: https://keepachangelog.com/en/1.0.0/

### State Machine Breaking

* (genesis) [\#5506](https://github.com/cosmos/cosmos-sdk/pull/5506) The `x/distribution` genesis state
now includes `params` instead of individual parameters.
* (genesis) [\#5017](https://github.com/cosmos/cosmos-sdk/pull/5017) The `x/genaccounts` module has been
deprecated and all components removed except the `legacy/` package. This requires changes to the
genesis state. Namely, `accounts` now exist under `app_state.auth.accounts`. The corresponding migration
Expand All @@ -50,6 +52,8 @@ logic has been implemented for v0.38 target version. Applications can migrate vi

### API Breaking Changes

* (modules) [\#5506](https://github.com/cosmos/cosmos-sdk/pull/5506) Remove individual setters of `x/distribution` parameters.
Instead, follow the module spec in getting parameters, setting new value(s) and finally calling `SetParams`.
* (types) [\#5495](https://github.com/cosmos/cosmos-sdk/pull/5495) Remove redundant `(Must)Bech32ify*` and `(Must)Get*KeyBech32`
functions in favor of `(Must)Bech32ifyPubKey` and `(Must)GetPubKeyFromBech32` respectively, both of
which take a `Bech32PubKeyType` (string).
Expand Down Expand Up @@ -245,6 +249,8 @@ to detail this new feature and how state transitions occur.
* (docs/interfaces/) Add documentation on building interfaces for the Cosmos SDK.
* Redesigned user interface that features new dynamically generated sidebar, build-time code embedding from GitHub, new homepage as well as many other improvements.
* (types) [\#5428](https://github.com/cosmos/cosmos-sdk/pull/5428) Add `Mod` (modulo) method and `RelativePow` (exponentation) function for `Uint`.
* (modules) [\#5506](https://github.com/cosmos/cosmos-sdk/pull/5506) Remove redundancy in `x/distribution`s use of parameters. There
now exists a single `Params` type with a getter and setter along with a getter for each individual parameter.

### Bug Fixes

Expand Down
71 changes: 34 additions & 37 deletions x/distribution/alias.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
// nolint

const (
DefaultParamspace = keeper.DefaultParamspace
ModuleName = types.ModuleName
StoreKey = types.StoreKey
RouterKey = types.RouterKey
Expand All @@ -24,10 +23,7 @@ const (
QueryDelegatorValidators = types.QueryDelegatorValidators
QueryWithdrawAddr = types.QueryWithdrawAddr
QueryCommunityPool = types.QueryCommunityPool
ParamCommunityTax = types.ParamCommunityTax
ParamBaseProposerReward = types.ParamBaseProposerReward
ParamBonusProposerReward = types.ParamBonusProposerReward
ParamWithdrawAddrEnabled = types.ParamWithdrawAddrEnabled
DefaultParamspace = types.DefaultParamspace
TypeMsgFundCommunityPool = types.TypeMsgFundCommunityPool
)

Expand All @@ -40,29 +36,30 @@ var (
ReferenceCountInvariant = keeper.ReferenceCountInvariant
ModuleAccountInvariant = keeper.ModuleAccountInvariant
NewKeeper = keeper.NewKeeper
GetValidatorOutstandingRewardsAddress = keeper.GetValidatorOutstandingRewardsAddress
GetDelegatorWithdrawInfoAddress = keeper.GetDelegatorWithdrawInfoAddress
GetDelegatorStartingInfoAddresses = keeper.GetDelegatorStartingInfoAddresses
GetValidatorHistoricalRewardsAddressPeriod = keeper.GetValidatorHistoricalRewardsAddressPeriod
GetValidatorCurrentRewardsAddress = keeper.GetValidatorCurrentRewardsAddress
GetValidatorAccumulatedCommissionAddress = keeper.GetValidatorAccumulatedCommissionAddress
GetValidatorSlashEventAddressHeight = keeper.GetValidatorSlashEventAddressHeight
GetValidatorOutstandingRewardsKey = keeper.GetValidatorOutstandingRewardsKey
GetDelegatorWithdrawAddrKey = keeper.GetDelegatorWithdrawAddrKey
GetDelegatorStartingInfoKey = keeper.GetDelegatorStartingInfoKey
GetValidatorHistoricalRewardsPrefix = keeper.GetValidatorHistoricalRewardsPrefix
GetValidatorHistoricalRewardsKey = keeper.GetValidatorHistoricalRewardsKey
GetValidatorCurrentRewardsKey = keeper.GetValidatorCurrentRewardsKey
GetValidatorAccumulatedCommissionKey = keeper.GetValidatorAccumulatedCommissionKey
GetValidatorSlashEventPrefix = keeper.GetValidatorSlashEventPrefix
GetValidatorSlashEventKeyPrefix = keeper.GetValidatorSlashEventKeyPrefix
GetValidatorSlashEventKey = keeper.GetValidatorSlashEventKey
ParamKeyTable = keeper.ParamKeyTable
GetValidatorOutstandingRewardsAddress = types.GetValidatorOutstandingRewardsAddress
GetDelegatorWithdrawInfoAddress = types.GetDelegatorWithdrawInfoAddress
GetDelegatorStartingInfoAddresses = types.GetDelegatorStartingInfoAddresses
GetValidatorHistoricalRewardsAddressPeriod = types.GetValidatorHistoricalRewardsAddressPeriod
GetValidatorCurrentRewardsAddress = types.GetValidatorCurrentRewardsAddress
GetValidatorAccumulatedCommissionAddress = types.GetValidatorAccumulatedCommissionAddress
GetValidatorSlashEventAddressHeight = types.GetValidatorSlashEventAddressHeight
GetValidatorOutstandingRewardsKey = types.GetValidatorOutstandingRewardsKey
GetDelegatorWithdrawAddrKey = types.GetDelegatorWithdrawAddrKey
GetDelegatorStartingInfoKey = types.GetDelegatorStartingInfoKey
GetValidatorHistoricalRewardsPrefix = types.GetValidatorHistoricalRewardsPrefix
GetValidatorHistoricalRewardsKey = types.GetValidatorHistoricalRewardsKey
GetValidatorCurrentRewardsKey = types.GetValidatorCurrentRewardsKey
GetValidatorAccumulatedCommissionKey = types.GetValidatorAccumulatedCommissionKey
GetValidatorSlashEventPrefix = types.GetValidatorSlashEventPrefix
GetValidatorSlashEventKeyPrefix = types.GetValidatorSlashEventKeyPrefix
GetValidatorSlashEventKey = types.GetValidatorSlashEventKey
HandleCommunityPoolSpendProposal = keeper.HandleCommunityPoolSpendProposal
NewQuerier = keeper.NewQuerier
MakeTestCodec = keeper.MakeTestCodec
CreateTestInputDefault = keeper.CreateTestInputDefault
CreateTestInputAdvanced = keeper.CreateTestInputAdvanced
ParamKeyTable = types.ParamKeyTable
DefaultParams = types.DefaultParams
RegisterCodec = types.RegisterCodec
NewDelegatorStartingInfo = types.NewDelegatorStartingInfo
ErrEmptyDelegatorAddr = types.ErrEmptyDelegatorAddr
Expand Down Expand Up @@ -100,20 +97,19 @@ var (
NewValidatorSlashEvent = types.NewValidatorSlashEvent

// variable aliases
FeePoolKey = keeper.FeePoolKey
ProposerKey = keeper.ProposerKey
ValidatorOutstandingRewardsPrefix = keeper.ValidatorOutstandingRewardsPrefix
DelegatorWithdrawAddrPrefix = keeper.DelegatorWithdrawAddrPrefix
DelegatorStartingInfoPrefix = keeper.DelegatorStartingInfoPrefix
ValidatorHistoricalRewardsPrefix = keeper.ValidatorHistoricalRewardsPrefix
ValidatorCurrentRewardsPrefix = keeper.ValidatorCurrentRewardsPrefix
ValidatorAccumulatedCommissionPrefix = keeper.ValidatorAccumulatedCommissionPrefix
ValidatorSlashEventPrefix = keeper.ValidatorSlashEventPrefix
ParamStoreKeyCommunityTax = keeper.ParamStoreKeyCommunityTax
ParamStoreKeyBaseProposerReward = keeper.ParamStoreKeyBaseProposerReward
ParamStoreKeyBonusProposerReward = keeper.ParamStoreKeyBonusProposerReward
ParamStoreKeyWithdrawAddrEnabled = keeper.ParamStoreKeyWithdrawAddrEnabled
TestAddrs = keeper.TestAddrs
FeePoolKey = types.FeePoolKey
ProposerKey = types.ProposerKey
ValidatorOutstandingRewardsPrefix = types.ValidatorOutstandingRewardsPrefix
DelegatorWithdrawAddrPrefix = types.DelegatorWithdrawAddrPrefix
DelegatorStartingInfoPrefix = types.DelegatorStartingInfoPrefix
ValidatorHistoricalRewardsPrefix = types.ValidatorHistoricalRewardsPrefix
ValidatorCurrentRewardsPrefix = types.ValidatorCurrentRewardsPrefix
ValidatorAccumulatedCommissionPrefix = types.ValidatorAccumulatedCommissionPrefix
ValidatorSlashEventPrefix = types.ValidatorSlashEventPrefix
ParamStoreKeyCommunityTax = types.ParamStoreKeyCommunityTax
ParamStoreKeyBaseProposerReward = types.ParamStoreKeyBaseProposerReward
ParamStoreKeyBonusProposerReward = types.ParamStoreKeyBonusProposerReward
ParamStoreKeyWithdrawAddrEnabled = types.ParamStoreKeyWithdrawAddrEnabled
ModuleCdc = types.ModuleCdc
EventTypeSetWithdrawAddress = types.EventTypeSetWithdrawAddress
EventTypeRewards = types.EventTypeRewards
Expand All @@ -139,6 +135,7 @@ type (
ValidatorCurrentRewardsRecord = types.ValidatorCurrentRewardsRecord
DelegatorStartingInfoRecord = types.DelegatorStartingInfoRecord
ValidatorSlashEventRecord = types.ValidatorSlashEventRecord
Params = types.Params
GenesisState = types.GenesisState
MsgSetWithdrawAddress = types.MsgSetWithdrawAddress
MsgWithdrawDelegatorReward = types.MsgWithdrawDelegatorReward
Expand Down
10 changes: 9 additions & 1 deletion x/distribution/client/cli/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,18 @@ func GetCmdQueryParams(queryRoute string, cdc *codec.Codec) *cobra.Command {
Short: "Query distribution params",
RunE: func(cmd *cobra.Command, args []string) error {
cliCtx := context.NewCLIContext().WithCodec(cdc)
params, err := common.QueryParams(cliCtx, queryRoute)

route := fmt.Sprintf("custom/%s/%s", types.QuerierRoute, types.QueryParams)
res, _, err := cliCtx.QueryWithData(route, nil)
if err != nil {
return err
}

var params types.Params
if err := cdc.UnmarshalJSON(res, &params); err != nil {
return fmt.Errorf("failed to unmarshal params: %w", err)
}

return cliCtx.PrintOutput(params)
},
}
Expand Down
32 changes: 0 additions & 32 deletions x/distribution/client/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,38 +8,6 @@ import (
"github.com/cosmos/cosmos-sdk/x/distribution/types"
)

// QueryParams actually queries distribution params.
func QueryParams(cliCtx context.CLIContext, queryRoute string) (PrettyParams, error) {
route := fmt.Sprintf("custom/%s/params/%s", queryRoute, types.ParamCommunityTax)

retCommunityTax, _, err := cliCtx.QueryWithData(route, []byte{})
if err != nil {
return PrettyParams{}, err
}

route = fmt.Sprintf("custom/%s/params/%s", queryRoute, types.ParamBaseProposerReward)
retBaseProposerReward, _, err := cliCtx.QueryWithData(route, []byte{})
if err != nil {
return PrettyParams{}, err
}

route = fmt.Sprintf("custom/%s/params/%s", queryRoute, types.ParamBonusProposerReward)
retBonusProposerReward, _, err := cliCtx.QueryWithData(route, []byte{})
if err != nil {
return PrettyParams{}, err
}

route = fmt.Sprintf("custom/%s/params/%s", queryRoute, types.ParamWithdrawAddrEnabled)
retWithdrawAddrEnabled, _, err := cliCtx.QueryWithData(route, []byte{})
if err != nil {
return PrettyParams{}, err
}

return NewPrettyParams(
retCommunityTax, retBaseProposerReward, retBonusProposerReward, retWithdrawAddrEnabled,
), nil
}

// QueryDelegatorTotalRewards queries delegator total rewards.
func QueryDelegatorTotalRewards(cliCtx context.CLIContext, queryRoute, delAddr string) ([]byte, error) {
delegatorAddr, err := sdk.AccAddressFromBech32(delAddr)
Expand Down
34 changes: 0 additions & 34 deletions x/distribution/client/common/pretty_params.go

This file was deleted.

6 changes: 4 additions & 2 deletions x/distribution/client/rest/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,13 +216,15 @@ func paramsHandlerFn(cliCtx context.CLIContext, queryRoute string) http.HandlerF
return
}

params, err := common.QueryParams(cliCtx, queryRoute)
route := fmt.Sprintf("custom/%s/%s", types.QuerierRoute, types.QueryParams)
res, height, err := cliCtx.QueryWithData(route, nil)
if err != nil {
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
return
}

rest.PostProcessResponse(w, cliCtx, params)
cliCtx = cliCtx.WithHeight(height)
rest.PostProcessResponse(w, cliCtx, res)
}
}

Expand Down
20 changes: 10 additions & 10 deletions x/distribution/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,7 @@ func InitGenesis(ctx sdk.Context, keeper Keeper, supplyKeeper types.SupplyKeeper
var moduleHoldings sdk.DecCoins

keeper.SetFeePool(ctx, data.FeePool)
keeper.SetCommunityTax(ctx, data.CommunityTax)
keeper.SetBaseProposerReward(ctx, data.BaseProposerReward)
keeper.SetBonusProposerReward(ctx, data.BonusProposerReward)
keeper.SetWithdrawAddrEnabled(ctx, data.WithdrawAddrEnabled)
keeper.SetParams(ctx, data.Params)

for _, dwi := range data.DelegatorWithdrawInfos {
keeper.SetDelegatorWithdrawAddr(ctx, dwi.DelegatorAddress, dwi.WithdrawAddress)
Expand Down Expand Up @@ -61,10 +58,8 @@ func InitGenesis(ctx sdk.Context, keeper Keeper, supplyKeeper types.SupplyKeeper
// ExportGenesis returns a GenesisState for a given context and keeper.
func ExportGenesis(ctx sdk.Context, keeper Keeper) types.GenesisState {
feePool := keeper.GetFeePool(ctx)
communityTax := keeper.GetCommunityTax(ctx)
baseProposerRewards := keeper.GetBaseProposerReward(ctx)
bonusProposerRewards := keeper.GetBonusProposerReward(ctx)
withdrawAddrEnabled := keeper.GetWithdrawAddrEnabled(ctx)
params := keeper.GetParams(ctx)

dwi := make([]types.DelegatorWithdrawInfo, 0)
keeper.IterateDelegatorWithdrawAddrs(ctx, func(del sdk.AccAddress, addr sdk.AccAddress) (stop bool) {
dwi = append(dwi, types.DelegatorWithdrawInfo{
Expand All @@ -73,6 +68,7 @@ func ExportGenesis(ctx sdk.Context, keeper Keeper) types.GenesisState {
})
return false
})

pp := keeper.GetPreviousProposerConsAddr(ctx)
outstanding := make([]types.ValidatorOutstandingRewardsRecord, 0)
keeper.IterateValidatorOutstandingRewards(ctx,
Expand All @@ -84,6 +80,7 @@ func ExportGenesis(ctx sdk.Context, keeper Keeper) types.GenesisState {
return false
},
)

acc := make([]types.ValidatorAccumulatedCommissionRecord, 0)
keeper.IterateValidatorAccumulatedCommissions(ctx,
func(addr sdk.ValAddress, commission types.ValidatorAccumulatedCommission) (stop bool) {
Expand All @@ -94,6 +91,7 @@ func ExportGenesis(ctx sdk.Context, keeper Keeper) types.GenesisState {
return false
},
)

his := make([]types.ValidatorHistoricalRewardsRecord, 0)
keeper.IterateValidatorHistoricalRewards(ctx,
func(val sdk.ValAddress, period uint64, rewards types.ValidatorHistoricalRewards) (stop bool) {
Expand All @@ -105,6 +103,7 @@ func ExportGenesis(ctx sdk.Context, keeper Keeper) types.GenesisState {
return false
},
)

cur := make([]types.ValidatorCurrentRewardsRecord, 0)
keeper.IterateValidatorCurrentRewards(ctx,
func(val sdk.ValAddress, rewards types.ValidatorCurrentRewards) (stop bool) {
Expand All @@ -126,6 +125,7 @@ func ExportGenesis(ctx sdk.Context, keeper Keeper) types.GenesisState {
return false
},
)

slashes := make([]types.ValidatorSlashEventRecord, 0)
keeper.IterateValidatorSlashEvents(ctx,
func(val sdk.ValAddress, height uint64, event types.ValidatorSlashEvent) (stop bool) {
Expand All @@ -138,6 +138,6 @@ func ExportGenesis(ctx sdk.Context, keeper Keeper) types.GenesisState {
return false
},
)
return types.NewGenesisState(feePool, communityTax, baseProposerRewards, bonusProposerRewards, withdrawAddrEnabled,
dwi, pp, outstanding, acc, his, cur, dels, slashes)

return types.NewGenesisState(params, feePool, dwi, pp, outstanding, acc, his, cur, dels, slashes)
}
7 changes: 6 additions & 1 deletion x/distribution/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,15 @@ func NewKeeper(
panic(fmt.Sprintf("%s module account has not been set", types.ModuleName))
}

// set KeyTable if it has not already been set
if !paramSpace.HasKeyTable() {
paramSpace = paramSpace.WithKeyTable(types.ParamKeyTable())
}

return Keeper{
storeKey: key,
cdc: cdc,
paramSpace: paramSpace.WithKeyTable(ParamKeyTable()),
paramSpace: paramSpace,
stakingKeeper: sk,
supplyKeeper: supplyKeeper,
feeCollectorName: feeCollectorName,
Expand Down
7 changes: 5 additions & 2 deletions x/distribution/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,15 @@ import (
func TestSetWithdrawAddr(t *testing.T) {
ctx, _, keeper, _, _ := CreateTestInputDefault(t, false, 1000)

keeper.SetWithdrawAddrEnabled(ctx, false)
params := keeper.GetParams(ctx)
params.WithdrawAddrEnabled = false
keeper.SetParams(ctx, params)

err := keeper.SetWithdrawAddr(ctx, delAddr1, delAddr2)
require.NotNil(t, err)

keeper.SetWithdrawAddrEnabled(ctx, true)
params.WithdrawAddrEnabled = true
keeper.SetParams(ctx, params)

err = keeper.SetWithdrawAddr(ctx, delAddr1, delAddr2)
require.Nil(t, err)
Expand Down
Loading

0 comments on commit bf41dea

Please sign in to comment.