refactor(x/slashing): audit QA (#21477)

This commit is contained in:
Akhil Kumar P
2024-09-02 11:13:52 +00:00
committed by GitHub
parent a51b432b76
commit 496cd0de9b
10 changed files with 22 additions and 24 deletions
+1 -1
View File
@@ -98,7 +98,7 @@ func (h Hooks) AfterUnbondingInitiated(_ context.Context, _ uint64) error {
return nil
}
// AfterConsensusPubKeyUpdate triggers the functions to rotate the signing-infos also sets address pubkey relation.
// AfterConsensusPubKeyUpdate handles the rotation of signing info and updates the address-pubkey relation after a consensus key update.
func (h Hooks) AfterConsensusPubKeyUpdate(ctx context.Context, oldPubKey, newPubKey cryptotypes.PubKey, _ sdk.Coin) error {
if err := h.k.performConsensusPubKeyUpdate(ctx, oldPubKey, newPubKey); err != nil {
return err
+3 -2
View File
@@ -13,7 +13,7 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"
)
// HandleValidatorSignature handles a validator signature, must be called once per validator per block.
// HandleValidatorSignature handles a validator signature, must be called once per validator for each block.
func (k Keeper) HandleValidatorSignature(ctx context.Context, addr cryptotypes.Address, power int64, signed comet.BlockIDFlag) error {
params, err := k.Params.Get(ctx)
if err != nil {
@@ -22,6 +22,7 @@ func (k Keeper) HandleValidatorSignature(ctx context.Context, addr cryptotypes.A
return k.HandleValidatorSignatureWithParams(ctx, params, addr, power, signed)
}
// HandleValidatorSignature handles a validator signature with the provided slashing module params.
func (k Keeper) HandleValidatorSignatureWithParams(ctx context.Context, params types.Params, addr cryptotypes.Address, power int64, signed comet.BlockIDFlag) error {
height := k.HeaderService.HeaderInfo(ctx).Height
@@ -38,7 +39,7 @@ func (k Keeper) HandleValidatorSignatureWithParams(ctx context.Context, params t
return nil
}
// read the cons address again because validator may've rotated it's key
// read the cons address again because validator may've rotated its key
valConsAddr, err := val.GetConsAddr()
if err != nil {
return err
+7 -10
View File
@@ -83,8 +83,8 @@ func (k Keeper) SetMissedBlockBitmapChunk(ctx context.Context, addr sdk.ConsAddr
return k.ValidatorMissedBlockBitmap.Set(ctx, collections.Join(addr.Bytes(), uint64(chunkIndex)), chunk)
}
// getPreviousConsKey checks if the key rotated, returns the old consKey to get the missed blocks
// because missed blocks are still pointing to the old key
// getPreviousConsKey returns the old consensus key if it has rotated,
// allowing retrieval of missed blocks associated with the old key.
func (k Keeper) getPreviousConsKey(ctx context.Context, addr sdk.ConsAddress) (sdk.ConsAddress, error) {
oldPk, err := k.sk.ValidatorIdentifier(ctx, addr)
if err != nil {
@@ -105,8 +105,7 @@ func (k Keeper) getPreviousConsKey(ctx context.Context, addr sdk.ConsAddress) (s
// IndexOffset modulo SignedBlocksWindow. This index is used to fetch the chunk
// in the bitmap and the relative bit in that chunk.
func (k Keeper) GetMissedBlockBitmapValue(ctx context.Context, addr sdk.ConsAddress, index int64) (bool, error) {
// check the key rotated, if rotated use the returned consKey to get the missed blocks
// because missed blocks are still pointing to the old key
// get the old consensus key if it has rotated, allowing retrieval of missed blocks associated with the old key
addr, err := k.getPreviousConsKey(ctx, addr)
if err != nil {
return false, err
@@ -141,8 +140,7 @@ func (k Keeper) GetMissedBlockBitmapValue(ctx context.Context, addr sdk.ConsAddr
// index is used to fetch the chunk in the bitmap and the relative bit in that
// chunk.
func (k Keeper) SetMissedBlockBitmapValue(ctx context.Context, addr sdk.ConsAddress, index int64, missed bool) error {
// check the key rotated, if rotated use the returned consKey to get the missed blocks
// because missed blocks are still pointing to the old key
// get the old consensus key if it has rotated, allowing retrieval of missed blocks associated with the old key
addr, err := k.getPreviousConsKey(ctx, addr)
if err != nil {
return err
@@ -181,8 +179,7 @@ func (k Keeper) SetMissedBlockBitmapValue(ctx context.Context, addr sdk.ConsAddr
// DeleteMissedBlockBitmap removes a validator's missed block bitmap from state.
func (k Keeper) DeleteMissedBlockBitmap(ctx context.Context, addr sdk.ConsAddress) error {
// check the key rotated, if rotated use the returned consKey to delete the missed blocks
// because missed blocks are still pointing to the old key
// get the old consensus key if it has rotated, allowing retrieval of missed blocks associated with the old key
addr, err := k.getPreviousConsKey(ctx, addr)
if err != nil {
return err
@@ -239,8 +236,8 @@ func (k Keeper) GetValidatorMissedBlocks(ctx context.Context, addr sdk.ConsAddre
return missedBlocks, err
}
// performConsensusPubKeyUpdate updates cons address to its pub key relation
// Updates signing info, missed blocks (removes old one, and sets new one)
// performConsensusPubKeyUpdate updates the consensus address-pubkey relation
// and refreshes the signing info by replacing the old key with the new one.
func (k Keeper) performConsensusPubKeyUpdate(ctx context.Context, oldPubKey, newPubKey cryptotypes.PubKey) error {
// Connect new consensus address with PubKey
if err := k.AddrPubkeyRelation.Set(ctx, newPubKey.Address(), newPubKey); err != nil {
+3 -3
View File
@@ -101,7 +101,7 @@ func (s *KeeperTestSuite) TestValidatorMissedBlockBitmap_SmallWindow() {
require.NoError(err)
require.Len(missedBlocks, int(params.SignedBlocksWindow)-1)
// if the validator rotated it's key there will be different consKeys and a mapping will be added in the state.
// if the validator rotated its key, there will be different consKeys and a mapping will be added in the state
consAddr1 := sdk.ConsAddress("addr1_______________")
s.stakingKeeper.EXPECT().ValidatorIdentifier(gomock.Any(), consAddr1).Return(consAddr, nil).AnyTimes()
@@ -147,12 +147,12 @@ func (s *KeeperTestSuite) TestPerformConsensusPubKeyUpdate() {
require.NoError(err)
require.Equal(savedPubKey, pks[1])
// check validator SigningInfo is set properly to new consensus pubkey
// check validator's SigningInfo is set properly with new consensus pubkey
signingInfo, err := slashingKeeper.ValidatorSigningInfo.Get(ctx, newConsAddr)
require.NoError(err)
require.Equal(signingInfo, newInfo)
// missed blocks maps to old cons key only since there is a identifier added to get the missed blocks using the new cons key.
// missed blocks map corresponds only to the old cons key, as there is an identifier added to get the missed blocks using the new cons key
missedBlocks, err := slashingKeeper.GetValidatorMissedBlocks(ctx, oldConsAddr)
require.NoError(err)