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: remove setting of finalizeBlockState in FinalizeBlock + fix initialHeight + add ProcessProposal in tests/sims #16794

Merged
merged 11 commits into from
Jul 6, 2023

Conversation

facundomedica
Copy link
Member

@facundomedica facundomedica commented Jun 30, 2023

Description

This is problem because if for some reason FinalizeBlock gets called without first calling ProcessProposal/InitChain a faulty block might be created (using old state probably) or some other undefined behavior.
So with this change if FinalizeBlock gets called in an unexpected way, it should panic given that there is no initialized state to read/write on.

Fixes some of the issues outlined here: #16796
A follow up PR is needed to fix integration tests (working on it now)


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
  • added ! to 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
  • followed the guidelines for building modules
  • included the necessary unit and integration tests
  • added a changelog entry to CHANGELOG.md
  • included comments for documenting Go code
  • updated the relevant documentation or specification
  • reviewed "Files changed" and left comments if necessary
  • 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 ! in the type prefix if API or client breaking change
  • confirmed all author checklist items have been addressed
  • reviewed state machine logic
  • reviewed API design and naming
  • reviewed documentation is accurate
  • reviewed tests and test coverage
  • manually tested (if applicable)

@facundomedica
Copy link
Member Author

There's a test failing because finalizeBlockState is nil. So that means there is a test that calls FinalizeBlock without initializing finalizeBlockState first?
There are 2 options:

  • we initialize in InitChain (height == initialHeight)
  • we initialize in ProcessProposal (height > initialHeight)

So we are calling FinalizeBlock without executing either of those 🤔

@alexanderbez
Copy link
Contributor

ProcessProposal sets the finalize block state and then its cleared on Commit

@alexanderbez
Copy link
Contributor

Seems that ProcessProposal does not set finalizeBlock state..which I swear it did 🤔

@facundomedica facundomedica changed the title refactor: simplify check/init state in FinalizeBlock refactor: remove setting of finalizeBlockState in FinalizeBlock7 Jul 3, 2023
@facundomedica facundomedica changed the title refactor: remove setting of finalizeBlockState in FinalizeBlock7 refactor: remove setting of finalizeBlockState in FinalizeBlock Jul 3, 2023
@facundomedica facundomedica changed the title refactor: remove setting of finalizeBlockState in FinalizeBlock refactor: remove setting of finalizeBlockState in FinalizeBlock + fix initialHeight + add ProcessProposal in tests/sims Jul 3, 2023
@facundomedica facundomedica changed the title refactor: remove setting of finalizeBlockState in FinalizeBlock + fix initialHeight + add ProcessProposal in tests/sims fix: remove setting of finalizeBlockState in FinalizeBlock + fix initialHeight + add ProcessProposal in tests/sims Jul 3, 2023
@facundomedica facundomedica marked this pull request as ready for review July 4, 2023 17:15
@facundomedica facundomedica requested a review from a team as a code owner July 4, 2023 17:15
Copy link
Contributor

@alexanderbez alexanderbez left a comment

Choose a reason for hiding this comment

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

utACK. Changes make sense to me. The setting of finalizeBlockState is set on ProcessProposal and thus should not be in FinalizeBlock. Good work

Comment on lines +54 to +56
if app.initialHeight == 0 { // If initial height is 0, set it to 1
app.initialHeight = 1
}
Copy link
Contributor

Choose a reason for hiding this comment

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

I would put this in the BaseApp constructor, not here.

Also, can you describe what issue this was causing?

Copy link
Member Author

Choose a reason for hiding this comment

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

I don't think we can do it in the constructor, we have to get it in InitChain which contains InitialHeight (the line above app.initialHeight = req.InitialHeight).

The issue here is the following:

  1. InitChain is called with InitialHeight of 0 (according to CometBFT it 0 == 1 in this context) and app.initialHeight is set to 0.
  2. After that ProcessProposal gets called with Height 1 (calling it with height 0 is invalid; we have that check in place)
  3. getContextForProposal is called with height 1. Then height != app.initialHeight (1 != 0), so it doesn't return the right context (the one that contains all the state written during InitChain).

Writing this makes me doubt if PrepareProposal/ProcessProposal can actually get called with height 0, AFAIK no, but I'm checking Comet's code. Do you know this @alexanderbez ?

See getContextForProposal

Copy link
Contributor

Choose a reason for hiding this comment

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

PrepareProposal/ProcessProposal can actually get called with height 0

This should never be the case I'd imagine, but either way, yeah, let's keep it here.

@julienrbrt julienrbrt added the backport/v0.50.x PR scheduled for inclusion in the v0.50's next stable release label Jul 4, 2023
@facundomedica
Copy link
Member Author

merging this to continue working on it in a separate PR

@facundomedica facundomedica added this pull request to the merge queue Jul 6, 2023
Merged via the queue into main with commit 0fd6227 Jul 6, 2023
@facundomedica facundomedica deleted the facu/refactor-finalizeblock branch July 6, 2023 13:35
mergify bot pushed a commit that referenced this pull request Jul 6, 2023
…ialHeight + add ProcessProposal in tests/sims (#16794)

Co-authored-by: Aleksandr Bezobchuk <alexanderbez@users.noreply.github.com>
(cherry picked from commit 0fd6227)
alexanderbez pushed a commit that referenced this pull request Jul 6, 2023
…ialHeight + add ProcessProposal in tests/sims (backport #16794) (#16857)

Co-authored-by: Facundo Medica <14063057+facundomedica@users.noreply.github.com>
facundomedica added a commit that referenced this pull request Jul 10, 2023
…fix initialHeight + add ProcessProposal in tests/sims (#16794)"

This reverts commit 0fd6227.
@facundomedica facundomedica mentioned this pull request Jul 10, 2023
19 tasks
@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:ABCI C:Simulations C:x/slashing C:x/staking
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants