133 lines
3.4 KiB
Go
133 lines
3.4 KiB
Go
package keeper
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
"cosmossdk.io/math"
|
|
|
|
storetypes "github.com/cosmos/cosmos-sdk/store/types"
|
|
abci "github.com/tendermint/tendermint/abci/types"
|
|
"github.com/tendermint/tendermint/libs/log"
|
|
|
|
"github.com/cosmos/cosmos-sdk/codec"
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
"github.com/cosmos/cosmos-sdk/x/staking/types"
|
|
)
|
|
|
|
// Implements ValidatorSet interface
|
|
var _ types.ValidatorSet = Keeper{}
|
|
|
|
// Implements DelegationSet interface
|
|
var _ types.DelegationSet = Keeper{}
|
|
|
|
// Keeper of the x/staking store
|
|
type Keeper struct {
|
|
storeKey storetypes.StoreKey
|
|
cdc codec.BinaryCodec
|
|
authKeeper types.AccountKeeper
|
|
bankKeeper types.BankKeeper
|
|
hooks types.StakingHooks
|
|
authority string
|
|
}
|
|
|
|
// NewKeeper creates a new staking Keeper instance
|
|
func NewKeeper(
|
|
cdc codec.BinaryCodec,
|
|
key storetypes.StoreKey,
|
|
ak types.AccountKeeper,
|
|
bk types.BankKeeper,
|
|
authority string,
|
|
) *Keeper {
|
|
// ensure bonded and not bonded module accounts are set
|
|
if addr := ak.GetModuleAddress(types.BondedPoolName); addr == nil {
|
|
panic(fmt.Sprintf("%s module account has not been set", types.BondedPoolName))
|
|
}
|
|
|
|
if addr := ak.GetModuleAddress(types.NotBondedPoolName); addr == nil {
|
|
panic(fmt.Sprintf("%s module account has not been set", types.NotBondedPoolName))
|
|
}
|
|
|
|
// ensure that authority is a valid AccAddress
|
|
if _, err := sdk.AccAddressFromBech32(authority); err != nil {
|
|
panic(("authority is not a valid acc address"))
|
|
}
|
|
|
|
return &Keeper{
|
|
storeKey: key,
|
|
cdc: cdc,
|
|
authKeeper: ak,
|
|
bankKeeper: bk,
|
|
hooks: nil,
|
|
authority: authority,
|
|
}
|
|
}
|
|
|
|
// Logger returns a module-specific logger.
|
|
func (k Keeper) Logger(ctx sdk.Context) log.Logger {
|
|
return ctx.Logger().With("module", "x/"+types.ModuleName)
|
|
}
|
|
|
|
// Hooks gets the hooks for staking *Keeper {
|
|
func (keeper *Keeper) Hooks() types.StakingHooks {
|
|
if keeper.hooks == nil {
|
|
// return a no-op implementation if no hooks are set
|
|
return types.MultiStakingHooks{}
|
|
}
|
|
|
|
return keeper.hooks
|
|
}
|
|
|
|
// SetHooks Set the validator hooks
|
|
func (k *Keeper) SetHooks(sh types.StakingHooks) {
|
|
if k.hooks != nil {
|
|
panic("cannot set validator hooks twice")
|
|
}
|
|
|
|
k.hooks = sh
|
|
}
|
|
|
|
// GetLastTotalPower Load the last total validator power.
|
|
func (k Keeper) GetLastTotalPower(ctx sdk.Context) math.Int {
|
|
store := ctx.KVStore(k.storeKey)
|
|
bz := store.Get(types.LastTotalPowerKey)
|
|
|
|
if bz == nil {
|
|
return math.ZeroInt()
|
|
}
|
|
|
|
ip := sdk.IntProto{}
|
|
k.cdc.MustUnmarshal(bz, &ip)
|
|
|
|
return ip.Int
|
|
}
|
|
|
|
// SetLastTotalPower Set the last total validator power.
|
|
func (k Keeper) SetLastTotalPower(ctx sdk.Context, power math.Int) {
|
|
store := ctx.KVStore(k.storeKey)
|
|
bz := k.cdc.MustMarshal(&sdk.IntProto{Int: power})
|
|
store.Set(types.LastTotalPowerKey, bz)
|
|
}
|
|
|
|
// GetAuthority returns the x/staking module's authority.
|
|
func (k Keeper) GetAuthority() string {
|
|
return k.authority
|
|
}
|
|
|
|
// SetValidatorUpdates sets the ABCI validator power updates for the current block.
|
|
func (k Keeper) SetValidatorUpdates(ctx sdk.Context, valUpdates []abci.ValidatorUpdate) {
|
|
store := ctx.KVStore(k.storeKey)
|
|
bz := k.cdc.MustMarshal(&types.ValidatorUpdates{Updates: valUpdates})
|
|
store.Set(types.ValidatorUpdatesKey, bz)
|
|
}
|
|
|
|
// GetValidatorUpdates returns the ABCI validator power updates within the current block.
|
|
func (k Keeper) GetValidatorUpdates(ctx sdk.Context) []abci.ValidatorUpdate {
|
|
store := ctx.KVStore(k.storeKey)
|
|
bz := store.Get(types.ValidatorUpdatesKey)
|
|
|
|
var valUpdates types.ValidatorUpdates
|
|
k.cdc.MustUnmarshal(bz, &valUpdates)
|
|
|
|
return valUpdates.Updates
|
|
}
|