github.com/Finschia/finschia-sdk@v0.48.1/x/staking/keeper/keeper.go (about)

     1  package keeper
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/Finschia/ostracon/libs/log"
     7  
     8  	"github.com/Finschia/finschia-sdk/codec"
     9  	sdk "github.com/Finschia/finschia-sdk/types"
    10  	paramtypes "github.com/Finschia/finschia-sdk/x/params/types"
    11  	"github.com/Finschia/finschia-sdk/x/staking/types"
    12  )
    13  
    14  // Implements ValidatorSet interface
    15  var _ types.ValidatorSet = Keeper{}
    16  
    17  // Implements DelegationSet interface
    18  var _ types.DelegationSet = Keeper{}
    19  
    20  // keeper of the staking store
    21  type Keeper struct {
    22  	storeKey   sdk.StoreKey
    23  	cdc        codec.BinaryCodec
    24  	authKeeper types.AccountKeeper
    25  	bankKeeper types.BankKeeper
    26  	hooks      types.StakingHooks
    27  	paramstore paramtypes.Subspace
    28  }
    29  
    30  // NewKeeper creates a new staking Keeper instance
    31  func NewKeeper(
    32  	cdc codec.BinaryCodec, key sdk.StoreKey, ak types.AccountKeeper, bk types.BankKeeper,
    33  	ps paramtypes.Subspace,
    34  ) Keeper {
    35  	// set KeyTable if it has not already been set
    36  	if !ps.HasKeyTable() {
    37  		ps = ps.WithKeyTable(types.ParamKeyTable())
    38  	}
    39  
    40  	// ensure bonded and not bonded module accounts are set
    41  	if addr := ak.GetModuleAddress(types.BondedPoolName); addr.Empty() {
    42  		panic(fmt.Sprintf("%s module account has not been set", types.BondedPoolName))
    43  	}
    44  
    45  	if addr := ak.GetModuleAddress(types.NotBondedPoolName); addr.Empty() {
    46  		panic(fmt.Sprintf("%s module account has not been set", types.NotBondedPoolName))
    47  	}
    48  
    49  	return Keeper{
    50  		storeKey:   key,
    51  		cdc:        cdc,
    52  		authKeeper: ak,
    53  		bankKeeper: bk,
    54  		paramstore: ps,
    55  		hooks:      nil,
    56  	}
    57  }
    58  
    59  // Logger returns a module-specific logger.
    60  func (k Keeper) Logger(ctx sdk.Context) log.Logger {
    61  	return ctx.Logger().With("module", "x/"+types.ModuleName)
    62  }
    63  
    64  // Set the validator hooks
    65  func (k *Keeper) SetHooks(sh types.StakingHooks) *Keeper {
    66  	if k.hooks != nil {
    67  		panic("cannot set validator hooks twice")
    68  	}
    69  
    70  	k.hooks = sh
    71  
    72  	return k
    73  }
    74  
    75  // Load the last total validator power.
    76  func (k Keeper) GetLastTotalPower(ctx sdk.Context) sdk.Int {
    77  	store := ctx.KVStore(k.storeKey)
    78  	bz := store.Get(types.LastTotalPowerKey)
    79  
    80  	if bz == nil {
    81  		return sdk.ZeroInt()
    82  	}
    83  
    84  	ip := sdk.IntProto{}
    85  	k.cdc.MustUnmarshal(bz, &ip)
    86  
    87  	return ip.Int
    88  }
    89  
    90  // Set the last total validator power.
    91  func (k Keeper) SetLastTotalPower(ctx sdk.Context, power sdk.Int) {
    92  	store := ctx.KVStore(k.storeKey)
    93  	bz := k.cdc.MustMarshal(&sdk.IntProto{Int: power})
    94  	store.Set(types.LastTotalPowerKey, bz)
    95  }