2018-06-26 19:00:12 -07:00
|
|
|
package keeper
|
|
|
|
|
|
|
|
import (
|
2018-12-10 14:14:52 -08:00
|
|
|
"container/list"
|
2019-06-26 09:41:14 -07:00
|
|
|
"fmt"
|
2018-12-10 14:14:52 -08:00
|
|
|
|
2019-04-15 20:58:06 -07:00
|
|
|
"github.com/tendermint/tendermint/libs/log"
|
|
|
|
|
2018-09-13 11:17:32 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/codec"
|
2018-06-26 19:00:12 -07:00
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
2020-02-19 03:52:20 -08:00
|
|
|
paramtypes "github.com/cosmos/cosmos-sdk/x/params/types"
|
2019-01-11 12:08:01 -08:00
|
|
|
"github.com/cosmos/cosmos-sdk/x/staking/types"
|
2018-06-26 19:00:12 -07:00
|
|
|
)
|
|
|
|
|
2018-12-11 13:10:42 -08:00
|
|
|
const aminoCacheSize = 500
|
|
|
|
|
2019-06-04 15:06:58 -07:00
|
|
|
// Implements ValidatorSet interface
|
|
|
|
var _ types.ValidatorSet = Keeper{}
|
|
|
|
|
|
|
|
// Implements DelegationSet interface
|
|
|
|
var _ types.DelegationSet = Keeper{}
|
|
|
|
|
2019-01-11 12:08:01 -08:00
|
|
|
// keeper of the staking store
|
2018-06-26 19:00:12 -07:00
|
|
|
type Keeper struct {
|
2018-12-10 14:14:52 -08:00
|
|
|
storeKey sdk.StoreKey
|
2020-02-06 11:21:02 -08:00
|
|
|
cdc codec.Marshaler
|
2020-01-30 13:31:16 -08:00
|
|
|
bankKeeper types.BankKeeper
|
2019-06-28 13:11:27 -07:00
|
|
|
supplyKeeper types.SupplyKeeper
|
2019-06-04 15:06:58 -07:00
|
|
|
hooks types.StakingHooks
|
2020-02-19 03:52:20 -08:00
|
|
|
paramstore paramtypes.Subspace
|
2018-12-10 14:14:52 -08:00
|
|
|
validatorCache map[string]cachedValidator
|
|
|
|
validatorCacheList *list.List
|
2018-06-26 19:00:12 -07:00
|
|
|
}
|
|
|
|
|
2019-06-28 13:11:27 -07:00
|
|
|
// NewKeeper creates a new staking Keeper instance
|
2019-12-27 09:57:54 -08:00
|
|
|
func NewKeeper(
|
2020-02-19 03:52:20 -08:00
|
|
|
cdc codec.Marshaler, key sdk.StoreKey, bk types.BankKeeper, sk types.SupplyKeeper, ps paramtypes.Subspace,
|
2019-12-27 09:57:54 -08:00
|
|
|
) Keeper {
|
2020-02-28 09:59:06 -08:00
|
|
|
|
2020-02-21 09:34:03 -08:00
|
|
|
if !ps.HasKeyTable() {
|
|
|
|
ps = ps.WithKeyTable(ParamKeyTable())
|
|
|
|
}
|
2019-02-08 15:57:33 -08:00
|
|
|
|
2019-06-28 13:11:27 -07:00
|
|
|
// ensure bonded and not bonded module accounts are set
|
2020-01-30 13:31:16 -08:00
|
|
|
if addr := sk.GetModuleAddress(types.BondedPoolName); addr == nil {
|
2019-06-28 13:11:27 -07:00
|
|
|
panic(fmt.Sprintf("%s module account has not been set", types.BondedPoolName))
|
|
|
|
}
|
|
|
|
|
2020-01-30 13:31:16 -08:00
|
|
|
if addr := sk.GetModuleAddress(types.NotBondedPoolName); addr == nil {
|
2019-06-28 13:11:27 -07:00
|
|
|
panic(fmt.Sprintf("%s module account has not been set", types.NotBondedPoolName))
|
|
|
|
}
|
|
|
|
|
|
|
|
return Keeper{
|
2018-12-10 14:14:52 -08:00
|
|
|
storeKey: key,
|
|
|
|
cdc: cdc,
|
2020-01-30 13:31:16 -08:00
|
|
|
bankKeeper: bk,
|
|
|
|
supplyKeeper: sk,
|
2020-02-21 09:34:03 -08:00
|
|
|
paramstore: ps,
|
2018-12-10 14:14:52 -08:00
|
|
|
hooks: nil,
|
2018-12-11 13:10:42 -08:00
|
|
|
validatorCache: make(map[string]cachedValidator, aminoCacheSize),
|
2018-12-10 14:14:52 -08:00
|
|
|
validatorCacheList: list.New(),
|
2018-06-26 19:00:12 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-15 20:58:06 -07:00
|
|
|
// Logger returns a module-specific logger.
|
2019-06-26 09:41:14 -07:00
|
|
|
func (k Keeper) Logger(ctx sdk.Context) log.Logger {
|
|
|
|
return ctx.Logger().With("module", fmt.Sprintf("x/%s", types.ModuleName))
|
|
|
|
}
|
2019-04-15 20:58:06 -07:00
|
|
|
|
2018-08-31 17:01:23 -07:00
|
|
|
// Set the validator hooks
|
2019-06-04 15:06:58 -07:00
|
|
|
func (k *Keeper) SetHooks(sh types.StakingHooks) *Keeper {
|
2018-09-24 21:09:31 -07:00
|
|
|
if k.hooks != nil {
|
2018-08-31 17:01:23 -07:00
|
|
|
panic("cannot set validator hooks twice")
|
|
|
|
}
|
2018-09-24 21:09:31 -07:00
|
|
|
k.hooks = sh
|
2018-08-31 17:01:23 -07:00
|
|
|
return k
|
|
|
|
}
|
|
|
|
|
2018-10-22 01:18:10 -07:00
|
|
|
// Load the last total validator power.
|
2020-02-06 11:21:02 -08:00
|
|
|
func (k Keeper) GetLastTotalPower(ctx sdk.Context) sdk.Int {
|
2018-06-26 19:00:12 -07:00
|
|
|
store := ctx.KVStore(k.storeKey)
|
2020-02-06 11:21:02 -08:00
|
|
|
bz := store.Get(types.LastTotalPowerKey)
|
|
|
|
if bz == nil {
|
2018-10-23 11:48:50 -07:00
|
|
|
return sdk.ZeroInt()
|
2018-06-26 19:00:12 -07:00
|
|
|
}
|
2020-02-06 11:21:02 -08:00
|
|
|
|
|
|
|
ip := sdk.IntProto{}
|
2020-03-13 12:58:43 -07:00
|
|
|
k.cdc.MustUnmarshalBinaryBare(bz, &ip)
|
2020-02-06 11:21:02 -08:00
|
|
|
return ip.Int
|
2018-06-26 19:00:12 -07:00
|
|
|
}
|
|
|
|
|
2018-10-22 01:18:10 -07:00
|
|
|
// Set the last total validator power.
|
2018-10-23 11:48:50 -07:00
|
|
|
func (k Keeper) SetLastTotalPower(ctx sdk.Context, power sdk.Int) {
|
2018-10-22 01:18:10 -07:00
|
|
|
store := ctx.KVStore(k.storeKey)
|
2020-03-13 12:58:43 -07:00
|
|
|
bz := k.cdc.MustMarshalBinaryBare(&sdk.IntProto{Int: power})
|
2020-02-06 11:21:02 -08:00
|
|
|
store.Set(types.LastTotalPowerKey, bz)
|
2018-10-22 01:18:10 -07:00
|
|
|
}
|