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"
|
2018-08-31 02:03:43 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/x/params"
|
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
|
|
|
|
storeTKey sdk.StoreKey
|
|
|
|
cdc *codec.Codec
|
2019-06-28 13:11:27 -07:00
|
|
|
supplyKeeper types.SupplyKeeper
|
2019-06-04 15:06:58 -07:00
|
|
|
hooks types.StakingHooks
|
2018-12-10 14:14:52 -08:00
|
|
|
paramstore params.Subspace
|
|
|
|
validatorCache map[string]cachedValidator
|
|
|
|
validatorCacheList *list.List
|
2018-06-26 19:00:12 -07:00
|
|
|
|
2018-09-18 10:16:51 -07:00
|
|
|
// codespace
|
|
|
|
codespace sdk.CodespaceType
|
2018-06-26 19:00:12 -07:00
|
|
|
}
|
|
|
|
|
2019-06-28 13:11:27 -07:00
|
|
|
// NewKeeper creates a new staking Keeper instance
|
|
|
|
func NewKeeper(cdc *codec.Codec, key, tkey sdk.StoreKey, supplyKeeper types.SupplyKeeper,
|
2019-02-08 15:57:33 -08:00
|
|
|
paramstore params.Subspace, codespace sdk.CodespaceType) Keeper {
|
|
|
|
|
2019-06-28 13:11:27 -07:00
|
|
|
// ensure bonded and not bonded module accounts are set
|
|
|
|
if addr := supplyKeeper.GetModuleAddress(types.BondedPoolName); addr == nil {
|
|
|
|
panic(fmt.Sprintf("%s module account has not been set", types.BondedPoolName))
|
|
|
|
}
|
|
|
|
|
|
|
|
if addr := supplyKeeper.GetModuleAddress(types.NotBondedPoolName); addr == nil {
|
|
|
|
panic(fmt.Sprintf("%s module account has not been set", types.NotBondedPoolName))
|
|
|
|
}
|
|
|
|
|
|
|
|
return Keeper{
|
2018-12-10 14:14:52 -08:00
|
|
|
storeKey: key,
|
|
|
|
storeTKey: tkey,
|
|
|
|
cdc: cdc,
|
2019-06-28 13:11:27 -07:00
|
|
|
supplyKeeper: supplyKeeper,
|
2019-02-04 18:13:04 -08:00
|
|
|
paramstore: paramstore.WithKeyTable(ParamKeyTable()),
|
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(),
|
|
|
|
codespace: codespace,
|
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-09-18 10:16:51 -07:00
|
|
|
// return the codespace
|
2018-06-26 19:00:12 -07:00
|
|
|
func (k Keeper) Codespace() sdk.CodespaceType {
|
2018-09-18 10:16:51 -07:00
|
|
|
return k.codespace
|
2018-06-26 19:00:12 -07:00
|
|
|
}
|
|
|
|
|
2018-10-22 01:18:10 -07:00
|
|
|
// Load the last total validator power.
|
2018-10-23 11:48:50 -07:00
|
|
|
func (k Keeper) GetLastTotalPower(ctx sdk.Context) (power sdk.Int) {
|
2018-06-26 19:00:12 -07:00
|
|
|
store := ctx.KVStore(k.storeKey)
|
2019-05-28 01:44:04 -07:00
|
|
|
b := store.Get(types.LastTotalPowerKey)
|
2018-06-26 19:00:12 -07:00
|
|
|
if b == nil {
|
2018-10-23 11:48:50 -07:00
|
|
|
return sdk.ZeroInt()
|
2018-06-26 19:00:12 -07:00
|
|
|
}
|
2018-11-04 18:28:38 -08:00
|
|
|
k.cdc.MustUnmarshalBinaryLengthPrefixed(b, &power)
|
2018-10-22 01:18:10 -07:00
|
|
|
return
|
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)
|
2018-11-04 18:28:38 -08:00
|
|
|
b := k.cdc.MustMarshalBinaryLengthPrefixed(power)
|
2019-05-28 01:44:04 -07:00
|
|
|
store.Set(types.LastTotalPowerKey, b)
|
2018-10-22 01:18:10 -07:00
|
|
|
}
|