github.com/fibonacci-chain/fbc@v0.0.0-20231124064014-c7636198c1e9/x/staking/keeper/keeper.go (about) 1 package keeper 2 3 import ( 4 "fmt" 5 "strings" 6 7 "github.com/fibonacci-chain/fbc/libs/tendermint/libs/log" 8 "github.com/fibonacci-chain/fbc/x/staking/exported" 9 10 "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/codec" 11 sdk "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/types" 12 "github.com/fibonacci-chain/fbc/x/params" 13 "github.com/fibonacci-chain/fbc/x/staking/types" 14 ) 15 16 // Implements ValidatorSet interface 17 var _ types.ValidatorSet = Keeper{} 18 19 // Keeper is the keeper struct of the staking store 20 type Keeper struct { 21 storeKey sdk.StoreKey 22 cdcMarshl *codec.CodecProxy 23 supplyKeeper types.SupplyKeeper 24 hooks types.StakingHooks 25 paramstore params.Subspace 26 } 27 28 // NewKeeper creates a new staking Keeper instance 29 func NewKeeper(cdcMarshl *codec.CodecProxy, key sdk.StoreKey, supplyKeeper types.SupplyKeeper, 30 paramstore params.Subspace) Keeper { 31 // set KeyTable if it has not already been set 32 if !paramstore.HasKeyTable() { 33 paramstore = paramstore.WithKeyTable(ParamKeyTable()) 34 } 35 // ensure bonded and not bonded module accounts are set 36 if addr := supplyKeeper.GetModuleAddress(types.BondedPoolName); addr == nil { 37 panic(fmt.Sprintf("%s module account has not been set", types.BondedPoolName)) 38 } 39 40 if addr := supplyKeeper.GetModuleAddress(types.NotBondedPoolName); addr == nil { 41 panic(fmt.Sprintf("%s module account has not been set", types.NotBondedPoolName)) 42 } 43 44 return Keeper{ 45 storeKey: key, 46 cdcMarshl: cdcMarshl, 47 supplyKeeper: supplyKeeper, 48 paramstore: paramstore, 49 hooks: nil, 50 } 51 } 52 53 func NewKeeperWithNoParam(cdcMarshl *codec.CodecProxy, key sdk.StoreKey, supplyKeeper types.SupplyKeeper, 54 paramstore params.Subspace) Keeper { 55 56 return Keeper{ 57 storeKey: key, 58 cdcMarshl: cdcMarshl, 59 supplyKeeper: supplyKeeper, 60 paramstore: paramstore, 61 hooks: nil, 62 } 63 } 64 65 // Logger returns a module-specific logger 66 func (k Keeper) Logger(ctx sdk.Context) log.Logger { 67 return ctx.Logger().With("module", types.ModuleName) 68 } 69 70 // SetHooks sets the validator hooks 71 func (k *Keeper) SetHooks(sh types.StakingHooks) *Keeper { 72 if k.hooks != nil { 73 panic("cannot set validator hooks twice") 74 } 75 k.hooks = sh 76 return k 77 } 78 79 // Codespace returns the codespace 80 func (k Keeper) Codespace() string { 81 return types.ModuleName 82 } 83 84 // GetLastTotalPower loads the last total validator power 85 func (k Keeper) GetLastTotalPower(ctx sdk.Context) (power sdk.Int) { 86 store := ctx.KVStore(k.storeKey) 87 b := store.Get(types.LastTotalPowerKey) 88 if b == nil { 89 return sdk.ZeroInt() 90 } 91 k.cdcMarshl.GetCdc().MustUnmarshalBinaryLengthPrefixed(b, &power) 92 return 93 } 94 95 // SetLastTotalPower sets the last total validator power 96 func (k Keeper) SetLastTotalPower(ctx sdk.Context, power sdk.Int) { 97 store := ctx.KVStore(k.storeKey) 98 b := k.cdcMarshl.GetCdc().MustMarshalBinaryLengthPrefixed(power) 99 store.Set(types.LastTotalPowerKey, b) 100 } 101 102 // IsValidator tells whether a validator is in bonded status 103 func (k Keeper) IsValidator(ctx sdk.Context, addr sdk.AccAddress) bool { 104 var curValidators []string 105 // fetch all the bonded validators, insert them into currValidators 106 k.IterateBondedValidatorsByPower(ctx, func(index int64, validator exported.ValidatorI) (stop bool) { 107 curValidators = append(curValidators, validator.GetOperator().String()) 108 return false 109 }) 110 111 valStr := sdk.ValAddress(addr).String() 112 for _, val := range curValidators { 113 if valStr == val { 114 return true 115 } 116 } 117 return false 118 } 119 120 // GetOperAddrFromValidatorAddr returns the validator address according to the consensus pubkey 121 // the validator has to exist 122 func (k Keeper) GetOperAddrFromValidatorAddr(ctx sdk.Context, va string) (sdk.ValAddress, bool) { 123 validators := k.GetAllValidators(ctx) 124 125 for _, validator := range validators { 126 if strings.Compare(strings.ToUpper(va), validator.ConsPubKey.Address().String()) == 0 { 127 return validator.OperatorAddress, true 128 } 129 } 130 return nil, false 131 } 132 133 // GetOperAndValidatorAddr returns the operator addresses and consensus pubkeys of all the validators 134 func (k Keeper) GetOperAndValidatorAddr(ctx sdk.Context) types.OVPairs { 135 validators := k.GetAllValidators(ctx) 136 var ovPairs types.OVPairs 137 138 for _, validator := range validators { 139 ovPair := types.OVPair{OperAddr: validator.OperatorAddress, ValAddr: validator.ConsPubKey.Address().String()} 140 ovPairs = append(ovPairs, ovPair) 141 } 142 return ovPairs 143 }