2018-06-26 19:00:12 -07:00
|
|
|
package keeper
|
|
|
|
|
|
|
|
import (
|
2018-12-10 14:14:52 -08:00
|
|
|
"container/list"
|
|
|
|
|
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"
|
|
|
|
|
|
|
|
"github.com/cosmos/cosmos-sdk/x/bank"
|
2018-08-31 02:03:43 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/x/params"
|
2018-06-26 19:00:12 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/x/stake/types"
|
|
|
|
)
|
|
|
|
|
|
|
|
// keeper of the stake store
|
|
|
|
type Keeper struct {
|
2018-12-10 14:14:52 -08:00
|
|
|
storeKey sdk.StoreKey
|
|
|
|
storeTKey sdk.StoreKey
|
|
|
|
cdc *codec.Codec
|
|
|
|
bankKeeper bank.Keeper
|
|
|
|
hooks sdk.StakingHooks
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2018-10-10 13:01:30 -07:00
|
|
|
func NewKeeper(cdc *codec.Codec, key, tkey sdk.StoreKey, ck bank.Keeper, paramstore params.Subspace, codespace sdk.CodespaceType) Keeper {
|
2018-06-26 19:00:12 -07:00
|
|
|
keeper := Keeper{
|
2018-12-10 14:14:52 -08:00
|
|
|
storeKey: key,
|
|
|
|
storeTKey: tkey,
|
|
|
|
cdc: cdc,
|
|
|
|
bankKeeper: ck,
|
|
|
|
paramstore: paramstore.WithTypeTable(ParamTypeTable()),
|
|
|
|
hooks: nil,
|
|
|
|
validatorCache: make(map[string]cachedValidator, 500),
|
|
|
|
validatorCacheList: list.New(),
|
|
|
|
codespace: codespace,
|
2018-06-26 19:00:12 -07:00
|
|
|
}
|
|
|
|
return keeper
|
|
|
|
}
|
|
|
|
|
2018-08-31 17:01:23 -07:00
|
|
|
// Set the validator hooks
|
2018-11-04 22:11:03 -08:00
|
|
|
func (k *Keeper) SetHooks(sh sdk.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-06-26 19:00:12 -07:00
|
|
|
//_________________________________________________________________________
|
|
|
|
|
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 pool
|
2018-06-26 19:00:12 -07:00
|
|
|
func (k Keeper) GetPool(ctx sdk.Context) (pool types.Pool) {
|
|
|
|
store := ctx.KVStore(k.storeKey)
|
|
|
|
b := store.Get(PoolKey)
|
|
|
|
if b == nil {
|
2018-10-22 01:18:10 -07:00
|
|
|
panic("stored pool should not have been nil")
|
2018-06-26 19:00:12 -07:00
|
|
|
}
|
2018-11-04 18:28:38 -08:00
|
|
|
k.cdc.MustUnmarshalBinaryLengthPrefixed(b, &pool)
|
2018-06-26 19:00:12 -07:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// set the pool
|
|
|
|
func (k Keeper) SetPool(ctx sdk.Context, pool types.Pool) {
|
|
|
|
store := ctx.KVStore(k.storeKey)
|
2018-11-04 18:28:38 -08:00
|
|
|
b := k.cdc.MustMarshalBinaryLengthPrefixed(pool)
|
2018-06-26 19:00:12 -07:00
|
|
|
store.Set(PoolKey, b)
|
|
|
|
}
|
|
|
|
|
2018-10-22 01:18:10 -07:00
|
|
|
//_______________________________________________________________________
|
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)
|
2018-10-22 01:18:10 -07:00
|
|
|
b := store.Get(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)
|
2018-10-22 01:18:10 -07:00
|
|
|
store.Set(LastTotalPowerKey, b)
|
|
|
|
}
|
|
|
|
|
|
|
|
//_______________________________________________________________________
|
|
|
|
|
|
|
|
// Load the last validator power.
|
|
|
|
// Returns zero if the operator was not a validator last block.
|
2018-10-24 00:04:06 -07:00
|
|
|
func (k Keeper) GetLastValidatorPower(ctx sdk.Context, operator sdk.ValAddress) (power sdk.Int) {
|
2018-10-22 01:18:10 -07:00
|
|
|
store := ctx.KVStore(k.storeKey)
|
|
|
|
bz := store.Get(GetLastValidatorPowerKey(operator))
|
|
|
|
if bz == nil {
|
2018-10-24 00:04:06 -07:00
|
|
|
return sdk.ZeroInt()
|
2018-10-22 01:18:10 -07:00
|
|
|
}
|
2018-11-04 18:28:38 -08:00
|
|
|
k.cdc.MustUnmarshalBinaryLengthPrefixed(bz, &power)
|
2018-10-22 01:18:10 -07:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set the last validator power.
|
2018-10-24 00:04:06 -07:00
|
|
|
func (k Keeper) SetLastValidatorPower(ctx sdk.Context, operator sdk.ValAddress, power sdk.Int) {
|
2018-10-22 01:18:10 -07:00
|
|
|
store := ctx.KVStore(k.storeKey)
|
2018-11-04 18:28:38 -08:00
|
|
|
bz := k.cdc.MustMarshalBinaryLengthPrefixed(power)
|
2018-10-22 01:18:10 -07:00
|
|
|
store.Set(GetLastValidatorPowerKey(operator), bz)
|
|
|
|
}
|
|
|
|
|
2018-11-13 06:08:14 -08:00
|
|
|
// Iterate over last validator powers.
|
|
|
|
func (k Keeper) IterateLastValidatorPowers(ctx sdk.Context, handler func(operator sdk.ValAddress, power sdk.Int) (stop bool)) {
|
|
|
|
store := ctx.KVStore(k.storeKey)
|
|
|
|
iter := sdk.KVStorePrefixIterator(store, LastValidatorPowerKey)
|
|
|
|
defer iter.Close()
|
|
|
|
for ; iter.Valid(); iter.Next() {
|
|
|
|
addr := sdk.ValAddress(iter.Key()[len(LastValidatorPowerKey):])
|
|
|
|
var power sdk.Int
|
|
|
|
k.cdc.MustUnmarshalBinaryLengthPrefixed(iter.Value(), &power)
|
|
|
|
if handler(addr, power) {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-22 01:18:10 -07:00
|
|
|
// Delete the last validator power.
|
|
|
|
func (k Keeper) DeleteLastValidatorPower(ctx sdk.Context, operator sdk.ValAddress) {
|
|
|
|
store := ctx.KVStore(k.storeKey)
|
|
|
|
store.Delete(GetLastValidatorPowerKey(operator))
|
|
|
|
}
|