Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: Don't deserialize params for every validator in slashing #18959

Merged
merged 5 commits into from
Jan 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor nit, changelog of modules should be under their own changelog.
Here x/slashing/changelog.md

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah thank you will move that


### 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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit go doc naming.

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()
}