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 previous header in Prepare/Process Proposal + provide chain id in baseapp + fix context for verifying txs #15303

Merged
merged 34 commits into from
Mar 13, 2023

Conversation

facundomedica
Copy link
Member

@facundomedica facundomedica commented Mar 8, 2023

Description

Issue

Some values (like chain ID) were being leaked from the previous block/initialization into PrepareProposal and ProcessProposal, these values are only available if:

  1. The node has never been stopped since the genesis block (as these values are set on InitChain)
  2. The node has already commited a block (as the previous header was being used for the new state of prepare and process proposal).

So if a node is restarted, during the first prepare and process proposal these values won't be populated, and that will cause issues if they are being used.

Solution

Remove any previous header information from a previous block in the prepare and process proposal contexts, making things consistent at every height.

  • Added ChainID to baseapp
  • Use an empty header in Commit() with only the chain id set
  • Fix context for prepare and process proposal

Closes: #15269


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 facundomedica marked this pull request as ready for review March 8, 2023 14:31
@facundomedica facundomedica requested a review from a team as a code owner March 8, 2023 14:31
@github-prbot github-prbot requested review from a team, kocubinski and tac0turtle and removed request for a team March 8, 2023 14:32
@facundomedica facundomedica requested review from JeancarloBarrios and removed request for tac0turtle March 8, 2023 14:32
@facundomedica facundomedica added C:ABCI backport/v0.47.x PR scheduled for inclusion in the v0.47's next stable release labels Mar 8, 2023
baseapp/abci.go Outdated Show resolved Hide resolved
@github-actions github-actions bot added the C:CLI label Mar 8, 2023
@facundomedica facundomedica changed the title fix: remove previous header to avoid inconsistencies in Prepare/Process Proposal fix: remove previous header in Prepare/Process Proposal + provide chain id in baseapp Mar 8, 2023
@08d2
Copy link
Contributor

08d2 commented Mar 9, 2023

Solution: Remove any previous header information from a previous block in the prepare and process proposal contexts, making things consistent at every height.

Does this mean that PrepareProposal and ProcessProposal would lose access to metadata for the previous/committed height? That information is important! It shouldn't be removed.

@facundomedica
Copy link
Member Author

@08d2

Does this mean that PrepareProposal and ProcessProposal would lose access to metadata for the previous/committed height? That information is important! It shouldn't be removed.

That data is being leaked from previous blocks, they are not something we can get in prepareproposal and processproposal's request; this could have some nasty side effects. You might be able to query stuff from a previous block, but I'm not entirely sure tbh. We are trying to always provide the chain id tho.

@alexanderbez
Copy link
Contributor

alexanderbez commented Mar 9, 2023

So the things that are vital for PrepareProposal and ProcessProposal:

  • Be based on the previous block's state (we have this today)
  • Have access to the request types (we have this today)
  • Have a context that is populated with metadata that could be useful for various use-cases such as Chain-ID (WIP in this PR)

Everything else, e.g. previous header, is useless.

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.

Approach looks reasonable to me. What do others think?

@tac0turtle @aaronc

baseapp/abci.go Outdated Show resolved Hide resolved
@@ -154,6 +164,10 @@

// BeginBlock implements the ABCI application interface.
func (app *BaseApp) BeginBlock(req abci.RequestBeginBlock) (res abci.ResponseBeginBlock) {
if req.Header.ChainID != app.chainID {
panic(fmt.Sprintf("invalid chain-id on BeginBlock; expected: %s, got: %s", app.chainID, req.Header.ChainID))

Check warning

Code scanning / CodeQL

Panic in BeginBock or EndBlock consensus methods

Possible panics in BeginBock- or EndBlock-related consensus methods could cause a chain halt
@@ -154,6 +164,10 @@

// BeginBlock implements the ABCI application interface.
func (app *BaseApp) BeginBlock(req abci.RequestBeginBlock) (res abci.ResponseBeginBlock) {
if req.Header.ChainID != app.chainID {
panic(fmt.Sprintf("invalid chain-id on BeginBlock; expected: %s, got: %s", app.chainID, req.Header.ChainID))

Check warning

Code scanning / CodeQL

Panic in BeginBock or EndBlock consensus methods

path flow from Begin/EndBlock to a panic call
julienrbrt
julienrbrt previously approved these changes Mar 10, 2023
@julienrbrt julienrbrt dismissed their stale review March 10, 2023 22:31

wrong button on mobile.. I just wanted to read it again

@github-actions github-actions bot removed the C:CLI label Mar 13, 2023
Copy link
Member

@kocubinski kocubinski left a comment

Choose a reason for hiding this comment

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

LGTM, nice fix.

Copy link
Contributor

@samricotta samricotta left a comment

Choose a reason for hiding this comment

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

lgtm but maybe someone a bit more seasoned on this one

@julienrbrt julienrbrt added the A:automerge Automatically merge PR once all prerequisites pass. label Mar 13, 2023
@mergify mergify bot merged commit 6a03586 into main Mar 13, 2023
@mergify mergify bot deleted the facu/empty-header-prepproc-prop branch March 13, 2023 17:15
mergify bot pushed a commit that referenced this pull request Mar 13, 2023
…in id in baseapp + fix context for verifying txs (#15303)

## Description

### Issue
Some values (like chain ID) were being leaked from the previous block/initialization into PrepareProposal and ProcessProposal, these values are only available if:
1. The node has never been stopped since the genesis block (as these values are set on `InitChain`)
2. The node has already commited a block (as the previous header was being used for the new state of prepare and process proposal).

So if a node is restarted, during the first prepare and process proposal these values won't be populated, and that will cause issues if they are being used.

### Solution

Remove any previous header information from a previous block in the prepare and process proposal contexts, making things consistent at every height.

- Added ChainID to baseapp
- Use an empty header in Commit() with only the chain id set
- Fix context for prepare and process proposal

Closes: #15269

---

### 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](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title
- [ ] added `!` to the type prefix if API or client breaking change
- [ ] targeted the correct branch (see [PR Targeting](https://github.com/cosmos/cosmos-sdk/blob/main/CONTRIBUTING.md#pr-targeting))
- [ ] provided a link to the relevant issue or specification
- [ ] followed the guidelines for [building modules](https://github.com/cosmos/cosmos-sdk/blob/main/docs/docs/building-modules)
- [ ] included the necessary unit and integration [tests](https://github.com/cosmos/cosmos-sdk/blob/main/CONTRIBUTING.md#testing)
- [ ] added a changelog entry to `CHANGELOG.md`
- [ ] included comments for [documenting Go code](https://blog.golang.org/godoc)
- [ ] 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](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) 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)

(cherry picked from commit 6a03586)

# Conflicts:
#	baseapp/abci_test.go
#	baseapp/baseapp.go
#	server/rollback.go
#	testutil/network/network.go
facundomedica added a commit that referenced this pull request Mar 13, 2023
facundomedica added a commit that referenced this pull request Mar 13, 2023
…in id in baseapp + fix context for verifying txs (backport #15303) (#15377)

Co-authored-by: Facundo Medica <[email protected]>
Co-authored-by: Facundo Medica <[email protected]>
facundomedica added a commit that referenced this pull request Mar 13, 2023
larry0x pushed a commit to larry0x/cosmos-sdk that referenced this pull request May 22, 2023
…in id in baseapp + fix context for verifying txs (cosmos#15303)

## Description

### Issue
Some values (like chain ID) were being leaked from the previous block/initialization into PrepareProposal and ProcessProposal, these values are only available if:
1. The node has never been stopped since the genesis block (as these values are set on `InitChain`)
2. The node has already commited a block (as the previous header was being used for the new state of prepare and process proposal).

So if a node is restarted, during the first prepare and process proposal these values won't be populated, and that will cause issues if they are being used.

### Solution

Remove any previous header information from a previous block in the prepare and process proposal contexts, making things consistent at every height.

- Added ChainID to baseapp
- Use an empty header in Commit() with only the chain id set
- Fix context for prepare and process proposal

Closes: cosmos#15269



---

### 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](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title
- [ ] added `!` to the type prefix if API or client breaking change
- [ ] targeted the correct branch (see [PR Targeting](https://github.com/cosmos/cosmos-sdk/blob/main/CONTRIBUTING.md#pr-targeting))
- [ ] provided a link to the relevant issue or specification
- [ ] followed the guidelines for [building modules](https://github.com/cosmos/cosmos-sdk/blob/main/docs/docs/building-modules)
- [ ] included the necessary unit and integration [tests](https://github.com/cosmos/cosmos-sdk/blob/main/CONTRIBUTING.md#testing)
- [ ] added a changelog entry to `CHANGELOG.md`
- [ ] included comments for [documenting Go code](https://blog.golang.org/godoc)
- [ ] 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](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) 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)
larry0x pushed a commit to larry0x/cosmos-sdk that referenced this pull request May 22, 2023
@@ -455,7 +456,19 @@ func DefaultBaseappOptions(appOpts types.AppOptions) []func(*baseapp.BaseApp) {
panic(err)
}

snapshotDir := filepath.Join(cast.ToString(appOpts.Get(flags.FlagHome)), "data", "snapshots")
homeDir := cast.ToString(appOpts.Get(flags.FlagHome))
chainID := cast.ToString(appOpts.Get(flags.FlagChainID))
Copy link
Collaborator

Choose a reason for hiding this comment

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

  1. I think this should not be a part of app config / flags. ChainID should be always read from the genesis , and panic if it's not there.
  2. Info about this is missing in UPGRADING.md (0.47)

Copy link
Member

Choose a reason for hiding this comment

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

genesis does not always have the correct genesis, in most cases the file is incorrect

@danwt
Copy link
Contributor

danwt commented Sep 12, 2024

In light of this, is there any way to change chain ID on the fly without requiring a full genesis import? Supposing a single trustworthy validator.

Thanks a lot

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A:automerge Automatically merge PR once all prerequisites pass. backport/v0.47.x PR scheduled for inclusion in the v0.47's next stable release C:ABCI
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Context chain ID can be empty
9 participants