feat(staking)!: add consensus and validator address codec in staking (#16959)
Co-authored-by: Facundo Medica <14063057+facundomedica@users.noreply.github.com>
This commit is contained in:
co-authored by
Facundo Medica
parent
b1ac5768f7
commit
e0be2b80fc
+33
-12
@@ -6,6 +6,7 @@ import (
|
||||
|
||||
abci "github.com/cometbft/cometbft/abci/types"
|
||||
|
||||
addresscodec "cosmossdk.io/core/address"
|
||||
storetypes "cosmossdk.io/core/store"
|
||||
"cosmossdk.io/log"
|
||||
"cosmossdk.io/math"
|
||||
@@ -23,12 +24,14 @@ var _ types.DelegationSet = Keeper{}
|
||||
|
||||
// Keeper of the x/staking store
|
||||
type Keeper struct {
|
||||
storeService storetypes.KVStoreService
|
||||
cdc codec.BinaryCodec
|
||||
authKeeper types.AccountKeeper
|
||||
bankKeeper types.BankKeeper
|
||||
hooks types.StakingHooks
|
||||
authority string
|
||||
storeService storetypes.KVStoreService
|
||||
cdc codec.BinaryCodec
|
||||
authKeeper types.AccountKeeper
|
||||
bankKeeper types.BankKeeper
|
||||
hooks types.StakingHooks
|
||||
authority string
|
||||
validatorAddressCodec addresscodec.Codec
|
||||
consensusAddressCodec addresscodec.Codec
|
||||
}
|
||||
|
||||
// NewKeeper creates a new staking Keeper instance
|
||||
@@ -38,6 +41,8 @@ func NewKeeper(
|
||||
ak types.AccountKeeper,
|
||||
bk types.BankKeeper,
|
||||
authority string,
|
||||
validatorAddressCodec addresscodec.Codec,
|
||||
consensusAddressCodec addresscodec.Codec,
|
||||
) *Keeper {
|
||||
// ensure bonded and not bonded module accounts are set
|
||||
if addr := ak.GetModuleAddress(types.BondedPoolName); addr == nil {
|
||||
@@ -53,13 +58,19 @@ func NewKeeper(
|
||||
panic("authority is not a valid acc address")
|
||||
}
|
||||
|
||||
if validatorAddressCodec == nil || consensusAddressCodec == nil {
|
||||
panic("validator and/or consensus address codec are nil")
|
||||
}
|
||||
|
||||
return &Keeper{
|
||||
storeService: storeService,
|
||||
cdc: cdc,
|
||||
authKeeper: ak,
|
||||
bankKeeper: bk,
|
||||
hooks: nil,
|
||||
authority: authority,
|
||||
storeService: storeService,
|
||||
cdc: cdc,
|
||||
authKeeper: ak,
|
||||
bankKeeper: bk,
|
||||
hooks: nil,
|
||||
authority: authority,
|
||||
validatorAddressCodec: validatorAddressCodec,
|
||||
consensusAddressCodec: consensusAddressCodec,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -124,6 +135,16 @@ func (k Keeper) GetAuthority() string {
|
||||
return k.authority
|
||||
}
|
||||
|
||||
// ValidatorAddressCodec returns the app validator address codec.
|
||||
func (k Keeper) ValidatorAddressCodec() addresscodec.Codec {
|
||||
return k.validatorAddressCodec
|
||||
}
|
||||
|
||||
// ConsensusAddressCodec returns the app consensus address codec.
|
||||
func (k Keeper) ConsensusAddressCodec() addresscodec.Codec {
|
||||
return k.consensusAddressCodec
|
||||
}
|
||||
|
||||
// SetValidatorUpdates sets the ABCI validator power updates for the current block.
|
||||
func (k Keeper) SetValidatorUpdates(ctx context.Context, valUpdates []abci.ValidatorUpdate) error {
|
||||
store := k.storeService.OpenKVStore(ctx)
|
||||
|
||||
@@ -54,7 +54,6 @@ func (s *KeeperTestSuite) SetupTest() {
|
||||
accountKeeper.EXPECT().GetModuleAddress(stakingtypes.BondedPoolName).Return(bondedAcc.GetAddress())
|
||||
accountKeeper.EXPECT().GetModuleAddress(stakingtypes.NotBondedPoolName).Return(notBondedAcc.GetAddress())
|
||||
accountKeeper.EXPECT().AddressCodec().Return(address.NewBech32Codec("cosmos")).AnyTimes()
|
||||
accountKeeper.EXPECT().ValidatorAddressCodec().Return(address.NewBech32Codec("cosmosvaloper")).AnyTimes()
|
||||
|
||||
bankKeeper := stakingtestutil.NewMockBankKeeper(ctrl)
|
||||
|
||||
@@ -64,6 +63,8 @@ func (s *KeeperTestSuite) SetupTest() {
|
||||
accountKeeper,
|
||||
bankKeeper,
|
||||
authtypes.NewModuleAddress(stakingtypes.GovModuleName).String(),
|
||||
address.NewBech32Codec("cosmosvaloper"),
|
||||
address.NewBech32Codec("cosmosvalcons"),
|
||||
)
|
||||
require.NoError(keeper.SetParams(ctx, stakingtypes.DefaultParams()))
|
||||
|
||||
|
||||
@@ -188,7 +188,7 @@ func (k Keeper) ApplyAndReturnValidatorSetUpdates(ctx context.Context) (updates
|
||||
}
|
||||
|
||||
// fetch the old power bytes
|
||||
valAddrStr, err := k.authKeeper.ValidatorAddressCodec().BytesToString(valAddr)
|
||||
valAddrStr, err := k.validatorAddressCodec.BytesToString(valAddr)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -456,7 +456,7 @@ func (k Keeper) getLastValidatorsByAddr(ctx context.Context) (validatorsByAddr,
|
||||
for ; iterator.Valid(); iterator.Next() {
|
||||
// extract the validator address from the key (prefix is 1-byte, addrLen is 1-byte)
|
||||
valAddr := types.AddressFromLastValidatorPowerKey(iterator.Key())
|
||||
valAddrStr, err := k.authKeeper.ValidatorAddressCodec().BytesToString(valAddr)
|
||||
valAddrStr, err := k.validatorAddressCodec.BytesToString(valAddr)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user