github.com/fibonacci-chain/fbc@v0.0.0-20231124064014-c7636198c1e9/libs/cosmos-sdk/x/staking/keeper/keeper.go (about) 1 package keeper 2 3 import ( 4 "container/list" 5 "fmt" 6 7 "github.com/fibonacci-chain/fbc/libs/tendermint/libs/log" 8 9 "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/codec" 10 sdk "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/types" 11 "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/x/params" 12 "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/x/staking/types" 13 ) 14 15 const aminoCacheSize = 500 16 17 // Implements ValidatorSet interface 18 //var _ types.ValidatorSet = Keeper{} 19 20 // Implements DelegationSet interface 21 var _ types.DelegationSet = Keeper{} 22 23 // keeper of the staking store 24 type Keeper struct { 25 storeKey sdk.StoreKey 26 cdc *codec.Codec 27 supplyKeeper types.SupplyKeeper 28 hooks types.StakingHooks 29 paramstore params.Subspace 30 validatorCache map[string]cachedValidator 31 validatorCacheList *list.List 32 } 33 34 // NewKeeper creates a new staking Keeper instance 35 func NewKeeper( 36 cdc *codec.Codec, key sdk.StoreKey, supplyKeeper types.SupplyKeeper, paramstore params.Subspace, 37 ) Keeper { 38 39 // ensure bonded and not bonded module accounts are set 40 if addr := supplyKeeper.GetModuleAddress(types.BondedPoolName); addr == nil { 41 panic(fmt.Sprintf("%s module account has not been set", types.BondedPoolName)) 42 } 43 44 if addr := supplyKeeper.GetModuleAddress(types.NotBondedPoolName); addr == nil { 45 panic(fmt.Sprintf("%s module account has not been set", types.NotBondedPoolName)) 46 } 47 48 return Keeper{ 49 storeKey: key, 50 cdc: cdc, 51 supplyKeeper: supplyKeeper, 52 paramstore: paramstore.WithKeyTable(ParamKeyTable()), 53 hooks: nil, 54 validatorCache: make(map[string]cachedValidator, aminoCacheSize), 55 validatorCacheList: list.New(), 56 } 57 } 58 59 // Logger returns a module-specific logger. 60 func (k Keeper) Logger(ctx sdk.Context) log.Logger { 61 return ctx.Logger().With("module", fmt.Sprintf("x/%s", types.ModuleName)) 62 } 63 64 // Set the validator hooks 65 func (k *Keeper) SetHooks(sh types.StakingHooks) *Keeper { 66 if k.hooks != nil { 67 panic("cannot set validator hooks twice") 68 } 69 k.hooks = sh 70 return k 71 } 72 73 // Load the last total validator power. 74 func (k Keeper) GetLastTotalPower(ctx sdk.Context) (power sdk.Int) { 75 store := ctx.KVStore(k.storeKey) 76 b := store.Get(types.LastTotalPowerKey) 77 if b == nil { 78 return sdk.ZeroInt() 79 } 80 k.cdc.MustUnmarshalBinaryLengthPrefixed(b, &power) 81 return 82 } 83 84 // Set the last total validator power. 85 func (k Keeper) SetLastTotalPower(ctx sdk.Context, power sdk.Int) { 86 store := ctx.KVStore(k.storeKey) 87 b := k.cdc.MustMarshalBinaryLengthPrefixed(power) 88 store.Set(types.LastTotalPowerKey, b) 89 }