-
Notifications
You must be signed in to change notification settings - Fork 3.9k
[wip] slashing: remove validator missed bit array from HandleValidatorSignature #4641
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
Closed
defunctzombie
wants to merge
1
commit into
cosmos:master
from
Polychain:remove-validator-missed-block-bitarray
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -160,30 +160,17 @@ func (k Keeper) HandleValidatorSignature(ctx sdk.Context, addr crypto.Address, p | |
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) | ||
signInfo.IndexOffset++ | ||
|
||
// Update signed block bit array & counter | ||
// This counter just tracks the sum of the bit array | ||
// That way we avoid needing to read/write the whole array each time | ||
previous := k.getValidatorMissedBlockBitArray(ctx, consAddr, index) | ||
missed := !signed | ||
switch { | ||
case !previous && missed: | ||
// Array value has changed from not missed to missed, increment counter | ||
k.setValidatorMissedBlockBitArray(ctx, consAddr, index, true) | ||
signInfo.MissedBlocksCounter++ | ||
case previous && !missed: | ||
// Array value has changed from missed to not missed, decrement counter | ||
k.setValidatorMissedBlockBitArray(ctx, consAddr, index, false) | ||
signInfo.MissedBlocksCounter-- | ||
default: | ||
// Array value at this index has not changed, no need to update counter | ||
} | ||
signWindow := k.SignedBlocksWindow(ctx) | ||
minSignedPerWindow := k.MinSignedPerWindow(ctx) | ||
maxMissed := signWindow - minSignedPerWindow | ||
|
||
if !signed { | ||
// no need to increment the missed blocks counter beyond the sign window | ||
if signInfo.MissedBlocksCounter < signWindow { | ||
signInfo.MissedBlocksCounter++ | ||
} | ||
signInfo.LastMissHeight = height | ||
|
||
if missed { | ||
ctx.EventManager().EmitEvent( | ||
sdk.NewEvent( | ||
types.EventTypeLiveness, | ||
|
@@ -193,20 +180,23 @@ func (k Keeper) HandleValidatorSignature(ctx sdk.Context, addr crypto.Address, p | |
), | ||
) | ||
|
||
logger.Info(fmt.Sprintf("Absent validator %s (%s) at height %d, %d missed, threshold %d", addr, pubkey, height, signInfo.MissedBlocksCounter, k.MinSignedPerWindow(ctx))) | ||
logger.Info(fmt.Sprintf("Absent validator %s (%v) at height %d, %d missed, threshold %d", addr, pubkey, height, signInfo.MissedBlocksCounter, k.MinSignedPerWindow(ctx))) | ||
} | ||
|
||
minHeight := signInfo.StartHeight + k.SignedBlocksWindow(ctx) | ||
maxMissed := k.SignedBlocksWindow(ctx) - k.MinSignedPerWindow(ctx) | ||
// Only when the last miss height is past the sign window can we decrement the miss block counter | ||
// This ensures that a missed block is tracked throughout its lifetime in the sign window | ||
if signed && signInfo.MissedBlocksCounter > 0 && (signInfo.LastMissHeight+signWindow) < height { | ||
signInfo.MissedBlocksCounter-- | ||
} | ||
|
||
// if we are past the minimum height and the validator has missed too many blocks, punish them | ||
if height > minHeight && signInfo.MissedBlocksCounter > maxMissed { | ||
// if the validator has missed too many blocks, punish them | ||
if signInfo.MissedBlocksCounter > maxMissed { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Might be off by one here. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I kept the same logic as before which was also strictly greater-than. |
||
validator := k.sk.ValidatorByConsAddr(ctx, consAddr) | ||
if validator != nil && !validator.IsJailed() { | ||
|
||
// Downtime confirmed: slash and jail the validator | ||
logger.Info(fmt.Sprintf("Validator %s past min height of %d and below signed blocks threshold of %d", | ||
sdk.ConsAddress(pubkey.Address()), minHeight, k.MinSignedPerWindow(ctx))) | ||
logger.Info(fmt.Sprintf("Validator %s below signed blocks threshold of %d", | ||
pubkey.Address(), k.MinSignedPerWindow(ctx))) | ||
|
||
// We need to retrieve the stake distribution which signed the block, so we subtract ValidatorUpdateDelay from the evidence height, | ||
// and subtract an additional 1 since this is the LastCommit. | ||
|
@@ -231,7 +221,6 @@ func (k Keeper) HandleValidatorSignature(ctx sdk.Context, addr crypto.Address, p | |
|
||
// We need to reset the counter & array so that the validator won't be immediately slashed for downtime upon rebonding. | ||
signInfo.MissedBlocksCounter = 0 | ||
signInfo.IndexOffset = 0 | ||
k.clearValidatorMissedBlockBitArray(ctx, consAddr) | ||
} else { | ||
// Validator was (a) not found or (b) already jailed, don't slash | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This doesn't seem to exactly resemble the same logic as the rolling window? What if I miss a block within signed window?
(essentially what @cwgoes stated)