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: ensure height is correct on first block #16259

Merged
merged 5 commits into from
May 24, 2023
Merged

Conversation

alexanderbez
Copy link
Contributor

Description

The block height after genesis and before the 2nd block is wrong. This is because we do not set the height in the default case on InitChain. I've added a fix and a test case to reflect this.

If you actually look at any major chain, you won't see any txs in the first block.

Bug was found by dydx team.


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)

@alexanderbez alexanderbez added backport/0.46.x PR scheduled for inclusion in the v0.46's next stable release backport/v0.47.x PR scheduled for inclusion in the v0.47's next stable release labels May 23, 2023
@alexanderbez alexanderbez marked this pull request as ready for review May 23, 2023 14:21
@alexanderbez alexanderbez requested a review from a team as a code owner May 23, 2023 14:21
@github-prbot github-prbot requested review from a team, JeancarloBarrios and likhita-809 and removed request for a team May 23, 2023 14:22
_ = app.Commit()

ctx := app.GetContextForCheckTx(nil)
require.Equal(t, int64(1), ctx.BlockHeight())
Copy link
Contributor Author

Choose a reason for hiding this comment

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

w/o this fix, this value is actually 0 which would cause CheckTx to fail for txs prior to the 1st block.

@alexanderbez alexanderbez requested a review from julienrbrt May 23, 2023 14:22
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.

lgtm!

// handler, the block height is zero by default. However, after Commit is called
// the height needs to reflect the true block height.
initHeader.Height = req.InitialHeight
app.checkState.ctx = app.checkState.ctx.WithBlockHeader(initHeader)
Copy link
Member

Choose a reason for hiding this comment

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

can we set this in header info as well?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Header info? What do you mean by that?

@@ -74,6 +74,16 @@ func (app *BaseApp) InitChain(req abci.RequestInitChain) (res abci.ResponseInitC
}
}

defer func() {
Copy link
Collaborator

@yihuang yihuang May 24, 2023

Choose a reason for hiding this comment

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

defer is usually for resource cleanup, I think it's clearer to simply put the code after the commit call, for example if panic happens before it, there's no point to still run this code.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Although the tutorial on defer states that, there's no idiomatic usage of it.

defer is often used where e.g. ensure and finally would be used in other languages.

Finally is exactly what we're trying to do here. I think if we do it in Commit, we'll have to explicitly check if height == 0.

Copy link
Collaborator

Choose a reason for hiding this comment

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

I mean just after the initChainer call, I think we don't want to run this if there's any errors or panics?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ahh yes, the reason why I added in the defer is if initChainer == nil, you'll need to add it before the return. That's why I added in the defer instead of having it twice.

Copy link
Contributor

@likhita-809 likhita-809 left a comment

Choose a reason for hiding this comment

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

lgtm

@tac0turtle tac0turtle added this pull request to the merge queue May 24, 2023
@tac0turtle tac0turtle removed backport/0.46.x PR scheduled for inclusion in the v0.46's next stable release backport/v0.47.x PR scheduled for inclusion in the v0.47's next stable release labels May 24, 2023
@tac0turtle
Copy link
Member

removed backport, dont think this should be backported. It is a bug but its not anything critical for users

Merged via the queue into main with commit 9d45ed4 May 24, 2023
@tac0turtle tac0turtle deleted the bez/fix-block-one-ctx branch May 24, 2023 11:19
@alexanderbez
Copy link
Contributor Author

alexanderbez commented May 24, 2023

That's fair. I think dydx is launching with v0.47 and I'd imagine they'd want this but I guess they can wait or (temp) fork.

roy-dydx pushed a commit to dydxprotocol/cosmos-sdk that referenced this pull request May 24, 2023
roy-dydx pushed a commit to dydxprotocol/cosmos-sdk that referenced this pull request May 24, 2023
roy-dydx pushed a commit to dydxprotocol/cosmos-sdk that referenced this pull request Jul 3, 2023
roy-dydx pushed a commit to dydxprotocol/cosmos-sdk that referenced this pull request Jul 3, 2023
lcwik pushed a commit to dydxprotocol/cosmos-sdk that referenced this pull request Aug 25, 2023
lcwik pushed a commit to dydxprotocol/cosmos-sdk that referenced this pull request Aug 25, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants