github.com/fibonacci-chain/fbc@v0.0.0-20231124064014-c7636198c1e9/x/staking/keeper/params.go (about) 1 package keeper 2 3 import ( 4 "time" 5 6 sdk "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/types" 7 "github.com/fibonacci-chain/fbc/x/params" 8 "github.com/fibonacci-chain/fbc/x/staking/types" 9 ) 10 11 // Default parameter namespace 12 const ( 13 DefaultParamspace = types.ModuleName 14 ) 15 16 // ParamKeyTable returns param table for staking module 17 func ParamKeyTable() params.KeyTable { 18 return params.NewKeyTable().RegisterParamSet(&types.Params{}) 19 } 20 21 // UnbondingTime returns the param UnbondingTime 22 func (k Keeper) UnbondingTime(ctx sdk.Context) (res time.Duration) { 23 k.paramstore.Get(ctx, types.KeyUnbondingTime, &res) 24 return 25 } 26 27 // MaxValidators returns the param Maximum number of validators 28 func (k Keeper) MaxValidators(ctx sdk.Context) (res uint16) { 29 k.paramstore.Get(ctx, types.KeyMaxValidators, &res) 30 return 31 } 32 33 // BondDenom returns the default the denomination of staking token 34 func (k Keeper) BondDenom(_ sdk.Context) string { 35 return sdk.DefaultBondDenom 36 } 37 38 // GetParams gets all params as types.Params 39 func (k Keeper) GetParams(ctx sdk.Context) types.Params { 40 return types.NewParams( 41 k.UnbondingTime(ctx), 42 k.MaxValidators(ctx), 43 k.ParamsEpoch(ctx), 44 k.ParamsMaxValsToAddShares(ctx), 45 k.ParamsMinDelegation(ctx), 46 k.ParamsMinSelfDelegation(ctx), 47 ) 48 } 49 50 // SetParams sets the params 51 func (k Keeper) SetParams(ctx sdk.Context, params types.Params) { 52 k.paramstore.SetParamSet(ctx, ¶ms) 53 } 54 55 // ParamsEpoch returns epoch from paramstore, only update the KeyEpoch after last epoch ends 56 func (k Keeper) ParamsEpoch(ctx sdk.Context) (res uint16) { 57 k.paramstore.Get(ctx, types.KeyEpoch, &res) 58 return 59 } 60 61 // GetEpoch returns the epoch for validators updates 62 func (k Keeper) GetEpoch(ctx sdk.Context) (epoch uint16) { 63 store := ctx.KVStore(k.storeKey) 64 b := store.Get(types.KeyEpoch) 65 if b == nil { 66 return types.DefaultEpoch 67 } 68 k.cdcMarshl.GetCdc().MustUnmarshalBinaryLengthPrefixed(b, &epoch) 69 return 70 } 71 72 // SetEpoch set epoch into keystore 73 func (k Keeper) SetEpoch(ctx sdk.Context, epoch uint16) { 74 store := ctx.KVStore(k.storeKey) 75 b := k.cdcMarshl.GetCdc().MustMarshalBinaryLengthPrefixed(epoch) 76 store.Set(types.KeyEpoch, b) 77 } 78 79 // IsEndOfEpoch checks whether an epoch is end 80 func (k Keeper) IsEndOfEpoch(ctx sdk.Context) bool { 81 blockInterval := ctx.BlockHeight() - k.GetTheEndOfLastEpoch(ctx) 82 return blockInterval%int64(k.GetEpoch(ctx)) == 0 83 } 84 85 // GetTheEndOfLastEpoch returns the deadline of the current epoch 86 func (k Keeper) GetTheEndOfLastEpoch(ctx sdk.Context) (height int64) { 87 store := ctx.KVStore(k.storeKey) 88 b := store.Get(types.KeyTheEndOfLastEpoch) 89 if b == nil { 90 return int64(0) 91 } 92 k.cdcMarshl.GetCdc().MustUnmarshalBinaryLengthPrefixed(b, &height) 93 return 94 } 95 96 // SetTheEndOfLastEpoch sets the deadline of the current epoch 97 func (k Keeper) SetTheEndOfLastEpoch(ctx sdk.Context) { 98 store := ctx.KVStore(k.storeKey) 99 b := k.cdcMarshl.GetCdc().MustMarshalBinaryLengthPrefixed(ctx.BlockHeight()) 100 store.Set(types.KeyTheEndOfLastEpoch, b) 101 } 102 103 // ParamsMaxValsToAddShares returns the param MaxValsToAddShares 104 func (k Keeper) ParamsMaxValsToAddShares(ctx sdk.Context) (num uint16) { 105 k.paramstore.Get(ctx, types.KeyMaxValsToAddShares, &num) 106 return 107 } 108 109 // ParamsMinDelegation returns the param MinDelegateAmount 110 func (k Keeper) ParamsMinDelegation(ctx sdk.Context) (num sdk.Dec) { 111 k.paramstore.Get(ctx, types.KeyMinDelegation, &num) 112 return 113 } 114 115 // ParamsMinSelfDelegation returns the param MinSelfDelegateAmount 116 func (k Keeper) ParamsMinSelfDelegation(ctx sdk.Context) (num sdk.Dec) { 117 k.paramstore.Get(ctx, types.KeyMinSelfDelegation, &num) 118 return 119 }