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" paramtypes "github.com/cosmos/cosmos-sdk/x/params/types" "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.Marshaler bankKeeper types.BankKeeper supplyKeeper types.SupplyKeeper hooks types.StakingHooks paramstore paramtypes.Subspace validatorCache map[string]cachedValidator validatorCacheList *list.List } // NewKeeper creates a new staking Keeper instance func NewKeeper( cdc codec.Marshaler, key sdk.StoreKey, bk types.BankKeeper, sk types.SupplyKeeper, ps paramtypes.Subspace, ) Keeper { if !ps.HasKeyTable() { ps = ps.WithKeyTable(ParamKeyTable()) } // ensure bonded and not bonded module accounts are set if addr := sk.GetModuleAddress(types.BondedPoolName); addr == nil { panic(fmt.Sprintf("%s module account has not been set", types.BondedPoolName)) } if addr := sk.GetModuleAddress(types.NotBondedPoolName); addr == nil { panic(fmt.Sprintf("%s module account has not been set", types.NotBondedPoolName)) } return Keeper{ storeKey: key, cdc: cdc, bankKeeper: bk, supplyKeeper: sk, paramstore: ps, hooks: nil, validatorCache: make(map[string]cachedValidator, aminoCacheSize), validatorCacheList: list.New(), } } // 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 } // Load the last total validator power. func (k Keeper) GetLastTotalPower(ctx sdk.Context) sdk.Int { store := ctx.KVStore(k.storeKey) bz := store.Get(types.LastTotalPowerKey) if bz == nil { return sdk.ZeroInt() } ip := sdk.IntProto{} k.cdc.MustUnmarshalBinaryLengthPrefixed(bz, &ip) return ip.Int } // Set the last total validator power. func (k Keeper) SetLastTotalPower(ctx sdk.Context, power sdk.Int) { store := ctx.KVStore(k.storeKey) bz := k.cdc.MustMarshalBinaryLengthPrefixed(&sdk.IntProto{Int: power}) store.Set(types.LastTotalPowerKey, bz) }