Skip to content

Commit

Permalink
perf: Don't deserialize params for every validator in slashing (#18959)
Browse files Browse the repository at this point in the history
Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
  • Loading branch information
ValarDragon and coderabbitai[bot] authored Jan 6, 2024
1 parent 631f160 commit 0b01347
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 16 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
* (types) [#18372](https://github.com/cosmos/cosmos-sdk/pull/18372) Removed global configuration for coin type and purpose. Setters and getters should be removed and access directly to defined types.
* (types) [#18695](https://github.com/cosmos/cosmos-sdk/pull/18695) Removed global configuration for txEncoder.
* (server) [#18909](https://github.com/cosmos/cosmos-sdk/pull/18909) Remove configuration endpoint on grpc reflection endpoint in favour of auth module bech32prefix endpoint already exposed.
* (slashing) [#18959](https://github.com/cosmos/cosmos-sdk/pull/18959) Slight speedup to Slashing Beginblock logic

### CLI Breaking Changes

Expand Down
7 changes: 5 additions & 2 deletions x/slashing/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,12 @@ func BeginBlocker(ctx context.Context, k keeper.Keeper) error {
// store whether or not they have actually signed it and slash/unbond any
// which have missed too many blocks in a row (downtime slashing)
sdkCtx := sdk.UnwrapSDKContext(ctx)
params, err := k.Params.Get(ctx)
if err != nil {
return err
}
for _, vote := range sdkCtx.CometInfo().LastCommit.Votes {

err := k.HandleValidatorSignature(ctx, vote.Validator.Address, vote.Validator.Power, vote.BlockIDFlag)
err := k.HandleValidatorSignatureWithParams(ctx, params, vote.Validator.Address, vote.Validator.Power, vote.BlockIDFlag)
if err != nil {
return err
}
Expand Down
18 changes: 10 additions & 8 deletions x/slashing/keeper/infractions.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@ import (

// HandleValidatorSignature handles a validator signature, must be called once per validator per 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 {
return err
}
return k.HandleValidatorSignatureWithParams(ctx, params, addr, power, signed)
}

func (k Keeper) HandleValidatorSignatureWithParams(ctx context.Context, params types.Params, addr cryptotypes.Address, power int64, signed comet.BlockIDFlag) error {
sdkCtx := sdk.UnwrapSDKContext(ctx)
logger := k.Logger(ctx)
height := sdkCtx.BlockHeight()
Expand All @@ -39,10 +47,7 @@ func (k Keeper) HandleValidatorSignature(ctx context.Context, addr cryptotypes.A
return err
}

signedBlocksWindow, err := k.SignedBlocksWindow(ctx)
if err != nil {
return err
}
signedBlocksWindow := params.SignedBlocksWindow

// Compute the relative index, so we count the blocks the validator *should*
// have signed. We will use the 0-value default signing info if not present,
Expand Down Expand Up @@ -82,10 +87,7 @@ func (k Keeper) HandleValidatorSignature(ctx context.Context, addr cryptotypes.A
// bitmap value at this index has not changed, no need to update counter
}

minSignedPerWindow, err := k.MinSignedPerWindow(ctx)
if err != nil {
return err
}
minSignedPerWindow := params.MinSignedPerWindowInt()

consStr, err := k.sk.ConsensusAddressCodec().BytesToString(consAddr)
if err != nil {
Expand Down
7 changes: 1 addition & 6 deletions x/slashing/keeper/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,7 @@ func (k Keeper) MinSignedPerWindow(ctx context.Context) (int64, error) {
return 0, err
}

signedBlocksWindow := params.SignedBlocksWindow
minSignedPerWindow := params.MinSignedPerWindow

// NOTE: RoundInt64 will never panic as minSignedPerWindow is
// less than 1.
return minSignedPerWindow.MulInt64(signedBlocksWindow).RoundInt64(), nil
return params.MinSignedPerWindowInt(), nil
}

// DowntimeJailDuration - Downtime unbond duration
Expand Down
10 changes: 10 additions & 0 deletions x/slashing/types/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,3 +146,13 @@ func validateSlashFractionDowntime(i interface{}) error {

return nil
}

// return min signed per window as an integer (vs the decimal in the param)
func (p *Params) MinSignedPerWindowInt() int64 {
signedBlocksWindow := p.SignedBlocksWindow
minSignedPerWindow := p.MinSignedPerWindow

// NOTE: RoundInt64 will never panic as minSignedPerWindow is
// less than 1.
return minSignedPerWindow.MulInt64(signedBlocksWindow).RoundInt64()
}

0 comments on commit 0b01347

Please sign in to comment.