cosmos-sdk/x/distribution/keeper/keeper.go

158 lines
4.7 KiB
Go
Raw Normal View History

2018-09-03 17:46:33 -07:00
package keeper
import (
2018-09-19 16:00:21 -07:00
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/distribution/types"
2018-09-18 21:42:05 -07:00
"github.com/cosmos/cosmos-sdk/x/params"
)
2019-01-11 12:08:01 -08:00
// keeper of the staking store
type Keeper struct {
2018-09-19 19:13:12 -07:00
storeKey sdk.StoreKey
cdc *codec.Codec
paramSpace params.Subspace
2018-09-19 19:13:12 -07:00
bankKeeper types.BankKeeper
2019-01-11 12:08:01 -08:00
stakingKeeper types.StakingKeeper
2018-09-19 19:13:12 -07:00
feeCollectionKeeper types.FeeCollectionKeeper
// codespace
codespace sdk.CodespaceType
}
func NewKeeper(cdc *codec.Codec, key sdk.StoreKey, paramSpace params.Subspace, ck types.BankKeeper,
2019-01-11 12:08:01 -08:00
sk types.StakingKeeper, fck types.FeeCollectionKeeper, codespace sdk.CodespaceType) Keeper {
keeper := Keeper{
2018-09-19 19:13:12 -07:00
storeKey: key,
cdc: cdc,
paramSpace: paramSpace.WithTypeTable(ParamTypeTable()),
2018-09-19 19:13:12 -07:00
bankKeeper: ck,
2019-01-11 12:08:01 -08:00
stakingKeeper: sk,
2018-09-19 19:13:12 -07:00
feeCollectionKeeper: fck,
codespace: codespace,
}
return keeper
}
//______________________________________________________________________
2018-09-04 23:41:17 -07:00
// get the global fee pool distribution info
2018-09-05 16:15:15 -07:00
func (k Keeper) GetFeePool(ctx sdk.Context) (feePool types.FeePool) {
store := ctx.KVStore(k.storeKey)
2018-09-13 00:03:30 -07:00
b := store.Get(FeePoolKey)
if b == nil {
2018-09-04 23:41:17 -07:00
panic("Stored fee pool should not have been nil")
}
k.cdc.MustUnmarshalBinaryLengthPrefixed(b, &feePool)
return
}
2018-09-04 23:41:17 -07:00
// set the global fee pool distribution info
2018-09-05 16:15:15 -07:00
func (k Keeper) SetFeePool(ctx sdk.Context, feePool types.FeePool) {
store := ctx.KVStore(k.storeKey)
b := k.cdc.MustMarshalBinaryLengthPrefixed(feePool)
2018-09-13 00:03:30 -07:00
store.Set(FeePoolKey, b)
}
// get the total validator accum for the ctx height
// in the fee pool
func (k Keeper) GetFeePoolValAccum(ctx sdk.Context) sdk.Dec {
// withdraw self-delegation
height := ctx.BlockHeight()
2019-01-11 12:08:01 -08:00
totalPower := sdk.NewDecFromInt(k.stakingKeeper.GetLastTotalPower(ctx))
fp := k.GetFeePool(ctx)
return fp.GetTotalValAccum(height, totalPower)
}
2018-09-13 00:03:30 -07:00
//______________________________________________________________________
2018-09-13 23:35:02 -07:00
// set the proposer public key for this block
func (k Keeper) GetPreviousProposerConsAddr(ctx sdk.Context) (consAddr sdk.ConsAddress) {
store := ctx.KVStore(k.storeKey)
2018-09-13 23:35:02 -07:00
b := store.Get(ProposerKey)
2018-09-13 23:35:02 -07:00
if b == nil {
panic("Previous proposer not set")
2018-09-13 23:35:02 -07:00
}
k.cdc.MustUnmarshalBinaryLengthPrefixed(b, &consAddr)
2018-09-13 23:35:02 -07:00
return
}
// get the proposer public key for this block
func (k Keeper) SetPreviousProposerConsAddr(ctx sdk.Context, consAddr sdk.ConsAddress) {
store := ctx.KVStore(k.storeKey)
b := k.cdc.MustMarshalBinaryLengthPrefixed(consAddr)
store.Set(ProposerKey, b)
}
//______________________________________________________________________
// get context required for withdraw operations
func (k Keeper) GetWithdrawContext(ctx sdk.Context,
valOperatorAddr sdk.ValAddress) types.WithdrawContext {
feePool := k.GetFeePool(ctx)
height := ctx.BlockHeight()
2019-01-11 12:08:01 -08:00
validator := k.stakingKeeper.Validator(ctx, valOperatorAddr)
lastValPower := k.stakingKeeper.GetLastValidatorPower(ctx, valOperatorAddr)
lastTotalPower := sdk.NewDecFromInt(k.stakingKeeper.GetLastTotalPower(ctx))
return types.NewWithdrawContext(
feePool, height, lastTotalPower, sdk.NewDecFromInt(lastValPower),
validator.GetCommission())
}
//______________________________________________________________________
// PARAM STORE
// Type declaration for parameters
func ParamTypeTable() params.TypeTable {
return params.NewTypeTable(
ParamStoreKeyCommunityTax, sdk.Dec{},
2018-10-15 13:12:42 -07:00
ParamStoreKeyBaseProposerReward, sdk.Dec{},
ParamStoreKeyBonusProposerReward, sdk.Dec{},
)
}
// Returns the current CommunityTax rate from the global param store
2018-09-18 21:42:05 -07:00
// nolint: errcheck
func (k Keeper) GetCommunityTax(ctx sdk.Context) sdk.Dec {
2018-10-15 13:12:42 -07:00
var percent sdk.Dec
k.paramSpace.Get(ctx, ParamStoreKeyCommunityTax, &percent)
return percent
2018-09-18 21:42:05 -07:00
}
// nolint: errcheck
2018-10-15 13:12:42 -07:00
func (k Keeper) SetCommunityTax(ctx sdk.Context, percent sdk.Dec) {
k.paramSpace.Set(ctx, ParamStoreKeyCommunityTax, &percent)
}
// Returns the current BaseProposerReward rate from the global param store
// nolint: errcheck
func (k Keeper) GetBaseProposerReward(ctx sdk.Context) sdk.Dec {
var percent sdk.Dec
k.paramSpace.Get(ctx, ParamStoreKeyBaseProposerReward, &percent)
return percent
}
// nolint: errcheck
func (k Keeper) SetBaseProposerReward(ctx sdk.Context, percent sdk.Dec) {
k.paramSpace.Set(ctx, ParamStoreKeyBaseProposerReward, &percent)
}
// Returns the current BaseProposerReward rate from the global param store
// nolint: errcheck
func (k Keeper) GetBonusProposerReward(ctx sdk.Context) sdk.Dec {
var percent sdk.Dec
k.paramSpace.Get(ctx, ParamStoreKeyBonusProposerReward, &percent)
return percent
}
// nolint: errcheck
func (k Keeper) SetBonusProposerReward(ctx sdk.Context, percent sdk.Dec) {
k.paramSpace.Set(ctx, ParamStoreKeyBonusProposerReward, &percent)
2018-09-18 21:42:05 -07:00
}