Skip to content

Commit

Permalink
Make slashing not write sign info every block
Browse files Browse the repository at this point in the history
  • Loading branch information
ValarDragon committed Feb 17, 2024
1 parent a79f932 commit 8d6162a
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 8 deletions.
6 changes: 2 additions & 4 deletions proto/cosmos/slashing/v1beta1/slashing.proto
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,8 @@ message ValidatorSigningInfo {
string address = 1 [(cosmos_proto.scalar) = "cosmos.ConsensusAddressString"];
// Height at which validator was first a candidate OR was un-jailed
int64 start_height = 2;
// Index which is incremented every time a validator is bonded in a block and
// _may_ have signed a pre-commit or not. This in conjunction with the
// signed_blocks_window param determines the index in the missed block bitmap.
int64 index_offset = 3;
// No longer used.
reserved index_offset = 3;

Check failure on line 22 in proto/cosmos/slashing/v1beta1/slashing.proto

View workflow job for this annotation

GitHub Actions / lint

syntax error: expecting ';'

Check failure on line 22 in proto/cosmos/slashing/v1beta1/slashing.proto

View workflow job for this annotation

GitHub Actions / lint

syntax error: unexpected '='
// Timestamp until which the validator is jailed due to liveness downtime.
google.protobuf.Timestamp jailed_until = 4
[(gogoproto.stdtime) = true, (gogoproto.nullable) = false, (amino.dont_omitempty) = true];
Expand Down
17 changes: 13 additions & 4 deletions x/slashing/keeper/infractions.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,19 +58,22 @@ func (k Keeper) HandleValidatorSignatureWithParams(ctx context.Context, params t
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,
// have signed.
// We will also 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++
// The validator start height should get mapped to index 0, so we computed index as:
// (height - startHeight) % signedBlocksWindow
index := (height - signInfo.StartHeight) % signedBlocksWindow

// determine if the validator signed the previous block
previous, err := k.GetMissedBlockBitmapValue(ctx, consAddr, index)
if err != nil {
return errors.Wrap(err, "failed to get the validator's bitmap value")
}

editedSignInfo := false
missed := signed == comet.BlockIDFlagAbsent
switch {
case !previous && missed:
Expand All @@ -81,6 +84,7 @@ func (k Keeper) HandleValidatorSignatureWithParams(ctx context.Context, params t
}

signInfo.MissedBlocksCounter++
editedSignInfo = true

case previous && !missed:
// Bitmap value has changed from missed to not missed, so we flip the bit
Expand All @@ -90,6 +94,7 @@ func (k Keeper) HandleValidatorSignatureWithParams(ctx context.Context, params t
}

signInfo.MissedBlocksCounter--
editedSignInfo = true

default:
// bitmap value at this index has not changed, no need to update counter
Expand Down Expand Up @@ -126,6 +131,7 @@ func (k Keeper) HandleValidatorSignatureWithParams(ctx context.Context, params t

// if we are past the minimum height and the validator has missed too many blocks, punish them
if height > minHeight && signInfo.MissedBlocksCounter > maxMissed {
editedSignInfo = true
validator, err := k.sk.ValidatorByConsAddr(ctx, consAddr)
if err != nil {
return err
Expand Down Expand Up @@ -198,5 +204,8 @@ func (k Keeper) HandleValidatorSignatureWithParams(ctx context.Context, params t
}

// Set the updated signing info
return k.ValidatorSigningInfo.Set(ctx, consAddr, signInfo)
if editedSignInfo {
return k.ValidatorSigningInfo.Set(ctx, consAddr, signInfo)
}
return nil
}

0 comments on commit 8d6162a

Please sign in to comment.