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(x/staking/keeper): make Slash return early iff zero tokens to burn #18049

Merged

Conversation

odeke-em
Copy link
Collaborator

This change makes Keeper.Slash return early if there are no tokens to burn! This change also skips invoking the
.Hooks().BeforeValidatorSlashed hook because literally there is no slashing that can happen if there are no tokens to burn.

Given that the result of burning zero tokens SHOULD be a no-operation (noop) but we go through the whole routine, assuming no zero tokens would be burnt, one could argue on a protocol implementation/conformation that with zero tokens to burn, invoking Keeper.RemoveValidatorTokens which invokes:

Keeper.DeleteValidatorByPowerIndex
-> (Keeper.DeleteValidatorByPowerIndex)
-> validator.RemoveTokens
-> Keeper.SetValidator
-> Keeper.SetValidatorByPowerIndex

was causing a potential self inflicted Byzantine risk because that operation is non-atomic (it ran through a series of operations that could fail on their own yet could not be rolled back and not idempotent), will rely on network operations yet the actual result will have the operations back to the original: more investigation is needed to examine the risk/impact of previously letting zero tokens be burnt and run through that process.

Fixes #18034

@odeke-em odeke-em requested a review from a team as a code owner October 10, 2023 17:48
@github-prbot github-prbot requested review from a team, facundomedica and julienrbrt and removed request for a team October 10, 2023 17:49
@julienrbrt julienrbrt added the backport/v0.50.x PR scheduled for inclusion in the v0.50's next stable release label Oct 11, 2023
Copy link
Member

@julienrbrt julienrbrt left a comment

Choose a reason for hiding this comment

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

CHANGELOG.md Outdated Show resolved Hide resolved
This change makes Keeper.Slash return early if there are no tokens
to burn! This change also skips invoking the
.Hooks().BeforeValidatorSlashed hook because literally there is no
slashing that can happen if there are no tokens to burn.

Given that the result of burning zero tokens SHOULD be a no-operation
(noop) but we go through the whole routine, assuming no zero tokens
would be burnt, one could argue on a protocol implementation/conformation
that with zero tokens to burn, invoking Keeper.RemoveValidatorTokens which
invokes:

  Keeper.DeleteValidatorByPowerIndex
    -> (Keeper.DeleteValidatorByPowerIndex)
    -> validator.RemoveTokens
    -> Keeper.SetValidator
    -> Keeper.SetValidatorByPowerIndex

was causing a potential self inflicted Byzantine risk because that operation
is non-atomic (it ran through a series of operations that could fail on
their own yet could not be rolled back and not idempotent), will rely
on network operations yet the actual result will have the operations back
to the original: more investigation is needed to examine the risk/impact
of previously letting zero tokens be burnt and run through that process.

Also while here, employed a Go pattern to reuse condition variables just
as a code hygiene improvement and also given that as a Go reviewer, the
unnecessary allocation of variables however small must be avoided.

Fixes #18034
@odeke-em odeke-em force-pushed the x-staking-keeper-return-early-on-with-zero-tokens-to-burn branch from ecca2b9 to bb54a89 Compare October 16, 2023 11:08
@julienrbrt julienrbrt added this pull request to the merge queue Oct 16, 2023
Merged via the queue into main with commit 13afd93 Oct 16, 2023
49 of 50 checks passed
@julienrbrt julienrbrt deleted the x-staking-keeper-return-early-on-with-zero-tokens-to-burn branch October 16, 2023 14:52
mergify bot pushed a commit that referenced this pull request Oct 16, 2023
julienrbrt pushed a commit that referenced this pull request Oct 16, 2023
@faddat faddat mentioned this pull request Nov 8, 2024
12 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backport/v0.50.x PR scheduled for inclusion in the v0.50's next stable release C:x/staking
Projects
None yet
Development

Successfully merging this pull request may close these issues.

x/slashing/keeper: Keeper.Slash should return immediately if tokensToBurn.IsZero
5 participants