Skip to content

Commit

Permalink
Backport of cosmos#18959 (#538)
Browse files Browse the repository at this point in the history
  • Loading branch information
ValarDragon authored and czarcas7ic committed May 9, 2024
1 parent 71a80e5 commit f0739cd
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 15 deletions.
7 changes: 6 additions & 1 deletion x/slashing/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,16 @@ 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.GetParams(ctx)
if err != nil {
return err
}
for _, voteInfo := range sdkCtx.VoteInfos() {
err := k.HandleValidatorSignature(ctx, voteInfo.Validator.Address, voteInfo.Validator.Power, comet.BlockIDFlag(voteInfo.BlockIdFlag))
err := k.HandleValidatorSignatureWithParams(ctx, params, voteInfo.Validator.Address, voteInfo.Validator.Power, comet.BlockIDFlag(voteInfo.BlockIdFlag))
if err != nil {
return err
}
}

return nil
}
19 changes: 11 additions & 8 deletions x/slashing/keeper/infractions.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,15 @@ 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.GetParams(ctx)
if err != nil {
return err
}
k.HandleValidatorSignatureWithParams(ctx, params, addr, power, signed)
return nil
}

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 +48,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 +88,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()

if missed {
sdkCtx.EventManager().EmitEvent(
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 @@ -22,12 +22,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 f0739cd

Please sign in to comment.