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

fix(x/staking)!: continue instead of panic on validator.Jailed #20059

Closed
wants to merge 2 commits into from

Conversation

Reecepbcups
Copy link
Member

@Reecepbcups Reecepbcups commented Apr 16, 2024

Description

If approved, requesting backport to release/v0.50.x as well to support current POA users

In building the x/POA module, Ethos recently discovered some misbehavior with respect to power and then we found cases for jailing. - strangelove-ventures/poa#165. In staking ApplyAndReturnValidatorSetUpdates the validator.Jailed panic stops us from building on top with our overrides. A continue rather than panic resolves the issue since we can't jail the validator until h+1 / h+2 anyways. As it will jail the next block after

It is very possible I am doing something wrong, but after a week of working on this i am going insane & is my last resort


Author Checklist

All items are required. Please add a note to the item if the item is not applicable and
please add links to any relevant follow up issues.

I have...

  • included the correct type prefix in the PR title
  • confirmed ! in the type prefix if API or client breaking change
  • targeted the correct branch (see PR Targeting)
  • provided a link to the relevant issue or specification
  • reviewed "Files changed" and left comments if necessary
  • included the necessary unit and integration tests
  • added a changelog entry to CHANGELOG.md
  • updated the relevant documentation or specification, including comments for documenting Go code
  • confirmed all CI checks have passed

Reviewers Checklist

All items are required. Please add a note if the item is not applicable and please add
your handle next to the items reviewed if you only reviewed selected items.

I have...

  • confirmed the correct type prefix in the PR title
  • confirmed all author checklist items have been addressed
  • reviewed state machine logic, API design and naming, documentation is accurate, tests and test coverage

Summary by CodeRabbit

  • Refactor
    • Improved handling of jailed validators in the validator set updates process.

Copy link
Contributor

coderabbitai bot commented Apr 16, 2024

Walkthrough

Walkthrough

The update in the x/staking module of the cosmos-sdk improves the handling of jailed validators by modifying the behavior in ApplyAndReturnValidatorSetUpdates. Previously, encountering a jailed validator could cause a panic, but now the code continues iteration, enhancing the stability of the process.

Changes

File Change Summary
x/staking/keeper/.../val_state_change.go Added a continue statement for jailed validators to prevent error returns.

Recent Review Details

Configuration used: .coderabbit.yml

Commits Files that changed from the base of the PR and between a571289 and cf268e1.
Files selected for processing (1)
  • CHANGELOG.md (1 hunks)
Additional Context Used
Path-based Instructions (1)
CHANGELOG.md (1)

Pattern **/*.md: "Assess the documentation for misspellings, grammatical errors, missing documentation and correctness"

Additional comments not posted (1)
CHANGELOG.md (1)

185-185: Ensure consistency in changelog entries.

The changelog entry for PR #20059 is clear and directly relates to the changes made in the PR. However, consider adding a brief explanation or the impact of this change to provide more context to the users. This could enhance understanding and transparency regarding the update.


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share
Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger a review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@Reecepbcups Reecepbcups changed the title fix(x/staking): continue instead of panic on validator.Jailed fix(x/staking): continue instead of panic on validator.Jailed for poa chains Apr 16, 2024
@alexanderbez
Copy link
Contributor

LGTM!

@Reecepbcups Reecepbcups marked this pull request as ready for review April 16, 2024 15:41
@Reecepbcups Reecepbcups requested a review from a team as a code owner April 16, 2024 15:41
@julienrbrt
Copy link
Member

If approved, requesting backport to release/v0.50.x as well to support current POA users

This change is consensus breaking. Adding as a discussion point for our standup.
I was planning to tag v0.50.6 today, but let's postpone until we've addressed this on Thursday.
We may be able to do it.

@tac0turtle
Copy link
Member

how are you hitting this code in the poa module? can you add a way to reproduce this?

@Reecepbcups
Copy link
Member Author

Reecepbcups commented Apr 16, 2024

@tac0turtle The issue is that it continues to try and slash every single block once the validator is jailed. Running on POA main repeated:

12:04PM INF validator jailed module=x/staking validator=cosmosvalcons1teuqwhxdwz4c460ld0dy2wx6lgcqkmsc2z3p5h
12:04PM INF slashing and jailing validator due to liveness fault height=24 jailed_until=2024-04-16T17:14:34Z min_height=17 module=x/slashing slashed=0.000000000000000000 threshold=10 validator=cosmosvalcons1teuqwhxdwz4c460ld0dy2wx6lgcqkmsc2z3p5h

Seeing this, I attempted to have POA ABCI delete the validator index in the EndBlock (first), then have staking EndBlocker run. This would fail no matter how I deleted the power store, since the jailing was in the previous EndBlock and we were on the next. Returning CONSENSUS FAILURE!!! err="should never retrieve a jailed validator from the power store. And there is no way to frontrun the jailing even with staking hooks BeforeValidatorModified (also seen in that commit).

In removing this panic, the staking logic can handle as expected and move the validator from a power of 2 to 0 and properly jail. (POA patch)


You can run this in POA main & the patch branch. Seeing POA main continues the loop, and the patch handles the jailing as expected

# POA Run a single node with a new binary instance
cd simapp && BINARY="poad" CHAIN_ID="poa-1" HOME_DIR="$HOME/.poad" TIMEOUT_COMMIT="1200ms" CLEAN=true sh test_node.sh

# new terminal window
FLAGS="--keyring-backend=test --chain-id=poa-1 --home="$HOME/.poad" --yes"

MAIN_VAL=`poad q staking validators --output=json | jq .validators[0].operator_address -r` && echo $MAIN_VAL
poad tx poa set-power $MAIN_VAL 18356789 $FLAGS --from=acc1 --unsafe
sleep 3
poad q staking validators
poad q consensus comet validator-set

poad tx poa create-validator simapp/validator_file.json $FLAGS --from acc3
sleep 3
poad q poa pending-validators --output json # 1 pending

PENIDNG_OPPERATOR_ADDR=$(poad q poa pending-validators --output=json | jq .pending[0].operator_address -r) && echo $PENIDNG_OPPERATOR_ADDR
poad tx poa set-power $PENIDNG_OPPERATOR_ADDR 2000000 $FLAGS --from=acc1
sleep 3
poad q consensus comet validator-set

The patch returns the following output which is the expected behavior w/ jailing

@Reecepbcups
Copy link
Member Author

Reecepbcups commented Apr 16, 2024

@julienrbrt Curious how this is consensus breaking for my own knowledge. In the event the code was triggered it would already panic (which for PoS chains is bad under current circumstances). Meaning to flag this it would have to be done via a Software upgrade migration & setting a val to jailed.

So while technically it could be consensus breaking in such a rare case that no mainnet has done, I don't see a case where it is unless using PoA. Where am I wrong here? 🙏

@julienrbrt
Copy link
Member

julienrbrt commented Apr 16, 2024

@julienrbrt Curious how this is consensus breaking for my own knowledge. In the event the code was triggered it would already panic (which for PoS chains is bad under current circumstances). Meaning to flag this it would have to be done via a Software upgrade migration & setting a val to jailed.

So while technically it could be consensus breaking in such a rare case that no mainnet has done, I don't see a case where it is unless using PoA. Where am I wrong here? 🙏

Right, so this is why I said that we may be able to backport it. Indeed, a chain would halt if this ever gets triggered, and the other validators with the patch would just keep producing blocks (not optimal, but acceptable).
However, it is consensus breaking because ApplyAndReturnValidatorSetUpdates is public and could be called outside the ABCI calls (where panicking in this case is handy), so we should consider it as such.
I already double-checked on SourceGraph to verify if we had any user using this call directly (https://sourcegraph.com/search?q=context:global+.ApplyAndReturnValidatorSetUpdates%28+lang:Go+-repo:github.com/cosmos/cosmos-sdk+-file:export.go+-file:%5BA-z%5D%2B_test.go%24&patternType=keyword&sm=0)
and as no one uses it in a problematic way, I want to mention this during our Thursday standup (as we try to not backport potential consensus breaking things unless there is a good reason -- imho this is a good reason to backport).

@Reecepbcups Reecepbcups changed the title fix(x/staking): continue instead of panic on validator.Jailed for poa chains fix(x/staking)!: continue instead of panic on validator.Jailed for poa chains Apr 16, 2024
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

CHANGELOG.md Show resolved Hide resolved
@tac0turtle
Copy link
Member

Hey we dove into your code to figure out what is going on. We feel like you are approaching using the staking module in a way that it wasnt meant to be. We havent hit this error on any mainnet since the staking module does handle things correctly. We are worried your code could also leads to other unforseen issues. My personal recommendation would be to write a POA module without the staking module being used.

@alpe recognised your goals and mentioned he did something similar in mesh security via decorators. If you decide to continue with the current approach then the decorator approach may be safer.

We wont accept this pr

@Reecepbcups
Copy link
Member Author

Reecepbcups commented Jun 5, 2024

Just making future reference here: this is similar to what just happened to the hub with patch #20548. (another area of the codebase, may be due to PSS, but panic on vals off by 1 with respect to jailing)

@Reecepbcups Reecepbcups changed the title fix(x/staking)!: continue instead of panic on validator.Jailed for poa chains fix(x/staking)!: continue instead of panic on validator.Jailed Jun 5, 2024
@tac0turtle
Copy link
Member

Haha I knew this was coming when they explained the bug. The issue is in how the hub is using this function. This function works correctly. The issue is this function lacks documentation to how to use it and should be private. Like much of the staking module. Don't think this is a bug as it was built this way on purpose.

@fmorency
Copy link
Contributor

Hey we dove into your code to figure out what is going on. We feel like you are approaching using the staking module in a way that it wasnt meant to be. We havent hit this error on any mainnet since the staking module does handle things correctly. We are worried your code could also leads to other unforseen issues. My personal recommendation would be to write a POA module without the staking module being used.

@alpe recognised your goals and mentioned he did something similar in mesh security via decorators. If you decide to continue with the current approach then the decorator approach may be safer.

We wont accept this pr

Perhaps another "unforseen" issue? strangelove-ventures/poa#170 (comment)

Sim-ed POA chain crash appears to be caused by x/distribution.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants