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

Backport of https://github.com/cosmos/cosmos-sdk/pull/18959 (backport #538) #540

Merged
merged 1 commit into from
Feb 23, 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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ Ref: https://keepachangelog.com/en/1.0.0/

## [Unreleased]

* (slashing) [#18959](https://github.com/cosmos/cosmos-sdk/pull/18959) Slight speedup to Slashing Beginblock logic

## [v0.47.5](https://github.com/cosmos/cosmos-sdk/releases/tag/v0.47.5) - 2023-09-01

### Features
Expand Down
3 changes: 2 additions & 1 deletion x/slashing/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
// Iterate over all the validators which *should* have signed this block
// store whether or not they have actually signed it and slash/unbond any
// which have missed too many blocks in a row (downtime slashing)
params := k.GetParams(ctx)

Check warning

Code scanning / CodeQL

Panic in BeginBock or EndBlock consensus methods Warning

path flow from Begin/EndBlock to a panic call
path flow from Begin/EndBlock to a panic call
path flow from Begin/EndBlock to a panic call
path flow from Begin/EndBlock to a panic call
path flow from Begin/EndBlock to a panic call
for _, voteInfo := range req.LastCommitInfo.GetVotes() {
k.HandleValidatorSignature(ctx, voteInfo.Validator.Address, voteInfo.Validator.Power, voteInfo.SignedLastBlock)
k.HandleValidatorSignatureWithParams(ctx, params, voteInfo.Validator.Address, voteInfo.Validator.Power, voteInfo.SignedLastBlock)

Check warning

Code scanning / CodeQL

Panic in BeginBock or EndBlock consensus methods Warning

path flow from Begin/EndBlock to a panic call
path flow from Begin/EndBlock to a panic call
path flow from Begin/EndBlock to a panic call
path flow from Begin/EndBlock to a panic call
path flow from Begin/EndBlock to a panic call
}
}
18 changes: 14 additions & 4 deletions x/slashing/keeper/infractions.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ import (

// HandleValidatorSignature handles a validator signature, must be called once per validator per block.
func (k Keeper) HandleValidatorSignature(ctx sdk.Context, addr cryptotypes.Address, power int64, signed bool) {
params := k.GetParams(ctx)
k.HandleValidatorSignatureWithParams(ctx, params, addr, power, signed)
}

func (k Keeper) HandleValidatorSignatureWithParams(ctx sdk.Context, params types.Params, addr cryptotypes.Address, power int64, signed bool) {
logger := k.Logger(ctx)
height := ctx.BlockHeight()

Expand All @@ -28,9 +33,14 @@ func (k Keeper) HandleValidatorSignature(ctx sdk.Context, addr cryptotypes.Addre
panic(fmt.Sprintf("Expected signing info for validator %s but not found", consAddr))
}

// this is a relative index, so it counts blocks the validator *should* have signed
// will use the 0-value default signing info if not present, except for start height
index := signInfo.IndexOffset % k.SignedBlocksWindow(ctx)
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,
// except for start height. The index is in the range [0, SignedBlocksWindow)
// and is used to see if a validator signed a block at the given height, which
// is represented by a bit in the bitmap.
index := signInfo.IndexOffset % signedBlocksWindow
signInfo.IndexOffset++

// Update signed block bit array & counter
Expand All @@ -51,7 +61,7 @@ func (k Keeper) HandleValidatorSignature(ctx sdk.Context, addr cryptotypes.Addre
// Array value at this index has not changed, no need to update counter
}

minSignedPerWindow := k.MinSignedPerWindow(ctx)
minSignedPerWindow := params.MinSignedPerWindowInt()

if missed {
ctx.EventManager().EmitEvent(
Expand Down
5 changes: 1 addition & 4 deletions x/slashing/keeper/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,8 @@ func (k Keeper) SignedBlocksWindow(ctx sdk.Context) (res int64) {
// MinSignedPerWindow - minimum blocks signed per window
func (k Keeper) MinSignedPerWindow(ctx sdk.Context) int64 {
params := k.GetParams(ctx)
signedBlocksWindow := params.SignedBlocksWindow

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

// 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 @@ -148,3 +148,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()
}
Loading