github.com/cosmos/cosmos-sdk@v0.50.10/x/staking/keeper/keeper.go (about)

     1  package keeper
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  
     7  	abci "github.com/cometbft/cometbft/abci/types"
     8  
     9  	addresscodec "cosmossdk.io/core/address"
    10  	storetypes "cosmossdk.io/core/store"
    11  	"cosmossdk.io/log"
    12  	"cosmossdk.io/math"
    13  
    14  	"github.com/cosmos/cosmos-sdk/codec"
    15  	sdk "github.com/cosmos/cosmos-sdk/types"
    16  	"github.com/cosmos/cosmos-sdk/x/staking/types"
    17  )
    18  
    19  // Implements ValidatorSet interface
    20  var _ types.ValidatorSet = Keeper{}
    21  
    22  // Implements DelegationSet interface
    23  var _ types.DelegationSet = Keeper{}
    24  
    25  // Keeper of the x/staking store
    26  type Keeper struct {
    27  	storeService          storetypes.KVStoreService
    28  	cdc                   codec.BinaryCodec
    29  	authKeeper            types.AccountKeeper
    30  	bankKeeper            types.BankKeeper
    31  	hooks                 types.StakingHooks
    32  	authority             string
    33  	validatorAddressCodec addresscodec.Codec
    34  	consensusAddressCodec addresscodec.Codec
    35  }
    36  
    37  // NewKeeper creates a new staking Keeper instance
    38  func NewKeeper(
    39  	cdc codec.BinaryCodec,
    40  	storeService storetypes.KVStoreService,
    41  	ak types.AccountKeeper,
    42  	bk types.BankKeeper,
    43  	authority string,
    44  	validatorAddressCodec addresscodec.Codec,
    45  	consensusAddressCodec addresscodec.Codec,
    46  ) *Keeper {
    47  	// ensure bonded and not bonded module accounts are set
    48  	if addr := ak.GetModuleAddress(types.BondedPoolName); addr == nil {
    49  		panic(fmt.Sprintf("%s module account has not been set", types.BondedPoolName))
    50  	}
    51  
    52  	if addr := ak.GetModuleAddress(types.NotBondedPoolName); addr == nil {
    53  		panic(fmt.Sprintf("%s module account has not been set", types.NotBondedPoolName))
    54  	}
    55  
    56  	// ensure that authority is a valid AccAddress
    57  	if _, err := ak.AddressCodec().StringToBytes(authority); err != nil {
    58  		panic("authority is not a valid acc address")
    59  	}
    60  
    61  	if validatorAddressCodec == nil || consensusAddressCodec == nil {
    62  		panic("validator and/or consensus address codec are nil")
    63  	}
    64  
    65  	return &Keeper{
    66  		storeService:          storeService,
    67  		cdc:                   cdc,
    68  		authKeeper:            ak,
    69  		bankKeeper:            bk,
    70  		hooks:                 nil,
    71  		authority:             authority,
    72  		validatorAddressCodec: validatorAddressCodec,
    73  		consensusAddressCodec: consensusAddressCodec,
    74  	}
    75  }
    76  
    77  // Logger returns a module-specific logger.
    78  func (k Keeper) Logger(ctx context.Context) log.Logger {
    79  	sdkCtx := sdk.UnwrapSDKContext(ctx)
    80  	return sdkCtx.Logger().With("module", "x/"+types.ModuleName)
    81  }
    82  
    83  // Hooks gets the hooks for staking *Keeper {
    84  func (k *Keeper) Hooks() types.StakingHooks {
    85  	if k.hooks == nil {
    86  		// return a no-op implementation if no hooks are set
    87  		return types.MultiStakingHooks{}
    88  	}
    89  
    90  	return k.hooks
    91  }
    92  
    93  // SetHooks sets the validator hooks.  In contrast to other receivers, this method must take a pointer due to nature
    94  // of the hooks interface and SDK start up sequence.
    95  func (k *Keeper) SetHooks(sh types.StakingHooks) {
    96  	if k.hooks != nil {
    97  		panic("cannot set validator hooks twice")
    98  	}
    99  
   100  	k.hooks = sh
   101  }
   102  
   103  // GetLastTotalPower loads the last total validator power.
   104  func (k Keeper) GetLastTotalPower(ctx context.Context) (math.Int, error) {
   105  	store := k.storeService.OpenKVStore(ctx)
   106  	bz, err := store.Get(types.LastTotalPowerKey)
   107  	if err != nil {
   108  		return math.ZeroInt(), err
   109  	}
   110  
   111  	if bz == nil {
   112  		return math.ZeroInt(), nil
   113  	}
   114  
   115  	ip := sdk.IntProto{}
   116  	err = k.cdc.Unmarshal(bz, &ip)
   117  	if err != nil {
   118  		return math.ZeroInt(), err
   119  	}
   120  
   121  	return ip.Int, nil
   122  }
   123  
   124  // SetLastTotalPower sets the last total validator power.
   125  func (k Keeper) SetLastTotalPower(ctx context.Context, power math.Int) error {
   126  	store := k.storeService.OpenKVStore(ctx)
   127  	bz, err := k.cdc.Marshal(&sdk.IntProto{Int: power})
   128  	if err != nil {
   129  		return err
   130  	}
   131  	return store.Set(types.LastTotalPowerKey, bz)
   132  }
   133  
   134  // GetAuthority returns the x/staking module's authority.
   135  func (k Keeper) GetAuthority() string {
   136  	return k.authority
   137  }
   138  
   139  // ValidatorAddressCodec returns the app validator address codec.
   140  func (k Keeper) ValidatorAddressCodec() addresscodec.Codec {
   141  	return k.validatorAddressCodec
   142  }
   143  
   144  // ConsensusAddressCodec returns the app consensus address codec.
   145  func (k Keeper) ConsensusAddressCodec() addresscodec.Codec {
   146  	return k.consensusAddressCodec
   147  }
   148  
   149  // SetValidatorUpdates sets the ABCI validator power updates for the current block.
   150  func (k Keeper) SetValidatorUpdates(ctx context.Context, valUpdates []abci.ValidatorUpdate) error {
   151  	store := k.storeService.OpenKVStore(ctx)
   152  	bz, err := k.cdc.Marshal(&types.ValidatorUpdates{Updates: valUpdates})
   153  	if err != nil {
   154  		return err
   155  	}
   156  	return store.Set(types.ValidatorUpdatesKey, bz)
   157  }
   158  
   159  // GetValidatorUpdates returns the ABCI validator power updates within the current block.
   160  func (k Keeper) GetValidatorUpdates(ctx context.Context) ([]abci.ValidatorUpdate, error) {
   161  	store := k.storeService.OpenKVStore(ctx)
   162  	bz, err := store.Get(types.ValidatorUpdatesKey)
   163  	if err != nil {
   164  		return nil, err
   165  	}
   166  
   167  	var valUpdates types.ValidatorUpdates
   168  	err = k.cdc.Unmarshal(bz, &valUpdates)
   169  	if err != nil {
   170  		return nil, err
   171  	}
   172  
   173  	return valUpdates.Updates, nil
   174  }