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

98 lines
2.6 KiB
Go

package keeper
import (
"container/list"
"fmt"
"github.com/tendermint/tendermint/libs/log"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/params"
"github.com/cosmos/cosmos-sdk/x/staking/types"
)
const aminoCacheSize = 500
// Implements ValidatorSet interface
var _ types.ValidatorSet = Keeper{}
// Implements DelegationSet interface
var _ types.DelegationSet = Keeper{}
// keeper of the staking store
type Keeper struct {
storeKey sdk.StoreKey
cdc *codec.Codec
supplyKeeper types.SupplyKeeper
hooks types.StakingHooks
paramstore params.Subspace
validatorCache map[string]cachedValidator
validatorCacheList *list.List
// codespace
codespace sdk.CodespaceType
}
// NewKeeper creates a new staking Keeper instance
func NewKeeper(cdc *codec.Codec, key sdk.StoreKey, supplyKeeper types.SupplyKeeper,
paramstore params.Subspace, codespace sdk.CodespaceType) Keeper {
// ensure bonded and not bonded module accounts are set
if addr := supplyKeeper.GetModuleAddress(types.BondedPoolName); addr == nil {
panic(fmt.Sprintf("%s module account has not been set", types.BondedPoolName))
}
if addr := supplyKeeper.GetModuleAddress(types.NotBondedPoolName); addr == nil {
panic(fmt.Sprintf("%s module account has not been set", types.NotBondedPoolName))
}
return Keeper{
storeKey: key,
cdc: cdc,
supplyKeeper: supplyKeeper,
paramstore: paramstore.WithKeyTable(ParamKeyTable()),
hooks: nil,
validatorCache: make(map[string]cachedValidator, aminoCacheSize),
validatorCacheList: list.New(),
codespace: codespace,
}
}
// Logger returns a module-specific logger.
func (k Keeper) Logger(ctx sdk.Context) log.Logger {
return ctx.Logger().With("module", fmt.Sprintf("x/%s", types.ModuleName))
}
// Set the validator hooks
func (k *Keeper) SetHooks(sh types.StakingHooks) *Keeper {
if k.hooks != nil {
panic("cannot set validator hooks twice")
}
k.hooks = sh
return k
}
// return the codespace
func (k Keeper) Codespace() sdk.CodespaceType {
return k.codespace
}
// Load the last total validator power.
func (k Keeper) GetLastTotalPower(ctx sdk.Context) (power sdk.Int) {
store := ctx.KVStore(k.storeKey)
b := store.Get(types.LastTotalPowerKey)
if b == nil {
return sdk.ZeroInt()
}
k.cdc.MustUnmarshalBinaryLengthPrefixed(b, &power)
return
}
// Set the last total validator power.
func (k Keeper) SetLastTotalPower(ctx sdk.Context, power sdk.Int) {
store := ctx.KVStore(k.storeKey)
b := k.cdc.MustMarshalBinaryLengthPrefixed(power)
store.Set(types.LastTotalPowerKey, b)
}