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: empty app hash on first block's FinalizeBlock #18524

Closed
wants to merge 1 commit into from

Conversation

vitsalis
Copy link
Contributor

@vitsalis vitsalis commented Nov 20, 2023

Description

The FinalizeBlock call for the first block after genesis contains a nil AppHash as app.LastCommitId().Hash is nil. This PR uses the same functionality as InitChain ref to handle an empty app hash for the first height.


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
  • run make lint and make test
  • 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)

Summary by CodeRabbit

  • Refactor
    • Improved the block finalization process to handle new chains or upgrades more effectively by adjusting how appHash is determined and utilized.

@vitsalis vitsalis requested a review from a team as a code owner November 20, 2023 14:00
Copy link
Contributor

coderabbitai bot commented Nov 20, 2023

Walkthrough

The update to the BaseApp package involves a modification to the internalFinalizeBlock function, ensuring that the application hash (appHash) used in the block header is now conditionally sourced. If a hash of the last committed block exists, it is used; otherwise, the hash of an empty string is assigned. This change improves the handling of new blockchain starts or upgrades by ensuring the appHash reflects the correct state.

Changes

File(s) Change Summary
baseapp/abci.go Updated internalFinalizeBlock to conditionally set appHash based on the last committed block's availability, affecting the header and BlockID's AppHash.

🐇 In the code's gentle flow, a change does come, a seed to sow,
As autumn leaves in breezes thrash, we hash the block, or string we hash. 🍂✨


Tips

Chat with CodeRabbit Bot (@coderabbitai)

  • If you reply to a review comment from CodeRabbit, the bot will automatically respond.
  • To engage with CodeRabbit bot directly around the specific lines of code in the PR, mention @coderabbitai in your review comment
  • Note: Review comments are made on code diffs or files, not on the PR overview.
  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

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.

Note: For conversation with the bot, please use the review comments on code diffs or files.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • The JSON schema for the configuration file is available here.
  • 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/coderabbit-overrides.v2.json

Comment on lines 726 to +739
))
}

// In the case of a new chain, AppHash will be the hash of an empty string.
// During an upgrade, it'll be the hash of the last committed block.
var appHash []byte
if !app.LastCommitID().IsZero() {
appHash = app.LastCommitID().Hash
} else {
// $ echo -n '' | sha256sum
// e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855
emptyHash := sha256.Sum256([]byte{})
appHash = emptyHash[:]
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Change potentially affects state.

Call sequence:

(*github.com/cosmos/cosmos-sdk/baseapp.BaseApp).internalFinalizeBlock (baseapp/abci.go:712)
(*github.com/cosmos/cosmos-sdk/baseapp.BaseApp).FinalizeBlock (baseapp/abci.go:869)

Copy link
Contributor

Choose a reason for hiding this comment

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

Is this really correct? In the case of the first block of a new chain, the app hash, IIRC, is the genesis hash which is supplied during BeginBlock.

Copy link
Contributor Author

@vitsalis vitsalis Nov 20, 2023

Choose a reason for hiding this comment

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

This is what I observed, i.e. a nil app hash. Also set up a BeginBlock under x/wasm of wasmd and observed the same behavior. Regarding the contents of the hash, I followed suit with what InitChain was doing, but not sure if this is the intended behavior.

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.

Review Status

Actionable comments generated: 0

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 8644e6b and 9d372f0.
Files selected for processing (1)
  • baseapp/abci.go (2 hunks)
Additional comments: 2
baseapp/abci.go (2)
  • 726-749: The code correctly handles the generation of appHash based on whether the last commit ID is zero or not. This is a crucial change that addresses the issue of an empty AppHash for the first block after genesis. The logic to use the hash of an empty string when there is no last commit is consistent with the behavior in InitChain. This change should ensure that the AppHash is not nil even when there is no last commit, which is the expected behavior for the first block.

  • 763-766: The coreheader.Info struct is being populated with the appHash which is correctly computed in the previous lines. This ensures that the AppHash is consistent and reflects the correct state of the application after the InitChain or the last commit. This is a critical fix for the issue described in the pull request summary.

@facundomedica
Copy link
Member

My understanding on why this happened:

Until v0.47.x the BeginBlockRequest included the appHash (which was returned before by InitChain), so that would get put into the context.

On v0.50.x we don't have the app hash in the FinalizeBlock request so we take it directly from the store, which can be nil and we don't do the "special treatment" we have in InitChain.

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.

Changes LGTM, but I'd like to see a unit test testing both flows.

Comment on lines +729 to +730
// In the case of a new chain, AppHash will be the hash of an empty string.
// During an upgrade, it'll be the hash of the last committed block.
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
// In the case of a new chain, AppHash will be the hash of an empty string.
// During an upgrade, it'll be the hash of the last committed block.
// In the case of a new chain, AppHash will be the hash of an empty string
// for the first block. During an upgrade, it'll be the hash of the last committed block.

header := cmtproto.Header{
ChainID: app.chainID,
Height: req.Height,
Time: req.Time,
ProposerAddress: req.ProposerAddress,
NextValidatorsHash: req.NextValidatorsHash,
AppHash: app.LastCommitID().Hash,
Copy link
Member

Choose a reason for hiding this comment

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

whynot set app.LastCommitID() to an empty value on start? this would help in avoiding the above code and potentially make it cleaner

Copy link
Member

@tac0turtle tac0turtle left a comment

Choose a reason for hiding this comment

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

if we assume a nil apphash should never happen we should fix it at the source instead of here. This would be a fix in store v1.0.0.

Is a nil apphash a bad thing?

@alexanderbez
Copy link
Contributor

Nil app hash is not a thing, we return the hash of an empty byte slice, which is different. I believe the problem is it's not present on the 1st block during FinalizeBlock.

@tac0turtle
Copy link
Member

That makes sense. Maybe we should disallow nil apphashes in general? would be a simple fix in store that would avoid having to be defensive every where we call .Hash()

@tac0turtle
Copy link
Member

did this pr to illustrate what i meant in my comment. #18563

Personally i like to root cause being fixed instead of at the source causing use to have to patch in many areas

@tac0turtle
Copy link
Member

closing this in favour of #18563. Thank you for the contribution @vitsalis

@tac0turtle tac0turtle closed this Nov 27, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants