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: should revert tx when block gas limit exceeded #10770

Merged
merged 4 commits into from
Jan 11, 2022

Conversation

yihuang
Copy link
Collaborator

@yihuang yihuang commented Dec 14, 2021

Closes: #10769

Description

Solution:

  • create a WithBranchedStore to handle state snapshot and revert
  • extract ConsumeBlockGasMiddleware out from RecoveryTxMiddleware.
  • order the middlewares properly.

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)

@tomtau
Copy link
Contributor

tomtau commented Dec 14, 2021

good to back port to 0.45 @robert-zaremba @aaronc @alexanderbez

x/auth/middleware/recovery.go Outdated Show resolved Hide resolved
x/auth/middleware/recovery.go Outdated Show resolved Hide resolved
x/auth/middleware/recovery.go Outdated Show resolved Hide resolved
x/auth/middleware/recovery.go Outdated Show resolved Hide resolved
@alexanderbez
Copy link
Contributor

Also, let's add a few tests for this. Thanks @ethanfrey 🙏

@yihuang yihuang force-pushed the block-gas-meter-panic branch from 3760624 to 93ac439 Compare December 15, 2021 03:06
@yihuang yihuang changed the title Don't panic when block gas limit exceeded Don't return error after tx is committed Dec 15, 2021
@yihuang yihuang force-pushed the block-gas-meter-panic branch from 93ac439 to 0f263de Compare December 15, 2021 03:23
@yihuang
Copy link
Collaborator Author

yihuang commented Dec 15, 2021

I've redone the PR in a cleaner way, by creating a SnapshotTxMiddlware outside of RecoveryTxMiddleware.

@yihuang yihuang changed the title Don't return error after tx is committed fix: Don't return error after tx is committed Dec 15, 2021
@yihuang yihuang changed the title fix: Don't return error after tx is committed fix: should revert tx when block gas limit exceeded Dec 15, 2021
Copy link
Contributor

@amaury1093 amaury1093 left a comment

Choose a reason for hiding this comment

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

I like this SnapshotMiddleware solution

x/auth/middleware/snapshot.go Outdated Show resolved Hide resolved
x/auth/middleware/middleware.go Outdated Show resolved Hide resolved
@yihuang yihuang force-pushed the block-gas-meter-panic branch from a834885 to 4c54a5e Compare December 15, 2021 13:17
@yihuang
Copy link
Collaborator Author

yihuang commented Dec 15, 2021

I'm not sure how to write a unit test for this though, it seems we need to execute some msg that does storage operations, then we can check the side-effects are reverted when the block gas limit is exceeded.

@yihuang
Copy link
Collaborator Author

yihuang commented Dec 15, 2021

I like this SnapshotMiddleware solution

Do you think there's a similar issue with this tip logic: https://github.com/cosmos/cosmos-sdk/blob/master/x/auth/middleware/tips.go#L38 . It's also executed after tx msgs, and it seems to be possible to return error. Maybe we should put it inside the SnapshotTxMiddleware?

@alexanderbez
Copy link
Contributor

I'm not sure how to write a unit test for this though, it seems we need to execute some msg that does storage operations, then we can check the side-effects are reverted when the block gas limit is exceeded.

  1. Yes, this exactly how we should test it. In fact, there might already be some BaseApp tests that do something similar.
  2. Can we rename the new middleware? Snapshot is not indicative of what is really going on. If anything, you may call it PersistTxMiddleware

@amaury1093
Copy link
Contributor

Do you think there's a similar issue with this tip logic: https://github.com/cosmos/cosmos-sdk/blob/master/x/auth/middleware/tips.go#L38 . It's also executed after tx msgs, and it seems to be possible to return error. Maybe we should put it inside the SnapshotTxMiddleware?

Yes you're right. But with the current code, the TipsMiddleware is already inside SnapshotTxMiddleware (see #10770 (comment)), so all good

@yihuang yihuang force-pushed the block-gas-meter-panic branch 2 times, most recently from 38ff9aa to b3bb21d Compare December 16, 2021 13:24
Copy link
Contributor

@tomtau tomtau left a comment

Choose a reason for hiding this comment

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

looks good, just a small nit: given it's now renamed PersistTxMiddleware, perhaps the file could be persist_tx.go or persist.go instead of snapshot.go for consistency?

@yihuang
Copy link
Collaborator Author

yihuang commented Dec 17, 2021

looks good, just a small nit: given it's now renamed PersistTxMiddleware, perhaps the file could be persist_tx.go or persist.go instead of snapshot.go for consistency?

yeah, good catch, done.

@robert-zaremba
Copy link
Collaborator

robert-zaremba commented Jan 7, 2022

Here is the followup issue: #10908 . It requires bigger refactor, so we won't be able to handle it in this PR unfortunately.

In this PR: let's add the missing tests and see what we can fix in the middleware we are changing.

@ethanfrey
Copy link
Contributor

I'm looking forward to 0.45 release and this seems like the only blocker left.

Are there concrete actions needed for this? "Missing tests and fix the middleware" are a bit vague.
I have not done a thorough review, as I trust there are plenty of eyes, but if my input could help, let me know. Just want to bump this up. Nice fix to have, but looking forward to 0.45 which brings in some very important other changes

yihuang and others added 3 commits January 8, 2022 23:58
Closes: cosmos#10769
Solution:
- create a `WithBranchedStore` to handle state snapshot and revert
- extract `ConsumeBlockGasMiddleware` out from `RecoveryTxMiddleware`.
- order the middlewares properly.

Co-authored-by: Aleksandr Bezobchuk <[email protected]>
Co-authored-by: Federico Kunze Küllmer <[email protected]>
Co-authored-by: Amaury <[email protected]>

Apply suggestions from code review

Co-authored-by: Robert Zaremba <[email protected]>
@yihuang yihuang force-pushed the block-gas-meter-panic branch from 1a47b58 to af0cc18 Compare January 8, 2022 16:06
@github-actions github-actions bot removed the C:Store label Jan 8, 2022
@amaury1093
Copy link
Contributor

@robert-zaremba are you okay to lift the block, and implementing more complex gas deductions/refunds in a later PR?

@fedekunze
Copy link
Collaborator

implementing more complex gas deductions/refunds in a later PR

I agree this is a nice addition but outside the scope of the PR. We'd be happy to collaborate on a design

Copy link
Collaborator

@robert-zaremba robert-zaremba left a comment

Choose a reason for hiding this comment

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

Thanks for adding tests. I've already created a new issue for other updates discussing here.

@amaury1093 amaury1093 added A:automerge Automatically merge PR once all prerequisites pass. and removed backport/0.45.x labels Jan 11, 2022
@amaury1093
Copy link
Contributor

amaury1093 commented Jan 11, 2022

Nice, adding automerge!

I removed the backport label, there's already #10814

@mergify mergify bot merged commit 17279fd into cosmos:master Jan 11, 2022
@yihuang yihuang deleted the block-gas-meter-panic branch January 11, 2022 10:21
yihuang added a commit to yihuang/cosmos-sdk that referenced this pull request Jan 11, 2022
amaury1093 pushed a commit that referenced this pull request Jan 12, 2022
* revert tx when block gas limit exceeded (backport: #10770)

- used a different solution.

changelog

* add unit test

* Update baseapp/baseapp.go

Co-authored-by: Robert Zaremba <[email protected]>

* simplfy the defer function

Co-authored-by: Robert Zaremba <[email protected]>
@faddat faddat mentioned this pull request Feb 28, 2022
8 tasks
zakir-code pushed a commit to PundiAI/cosmos-sdk that referenced this pull request Apr 8, 2022
…cosmos#10814)

* revert tx when block gas limit exceeded (backport: cosmos#10770)

- used a different solution.

changelog

* add unit test

* Update baseapp/baseapp.go

Co-authored-by: Robert Zaremba <[email protected]>

* simplfy the defer function

Co-authored-by: Robert Zaremba <[email protected]>
JimLarson pushed a commit to agoric-labs/cosmos-sdk that referenced this pull request Jul 7, 2022
…cosmos#10814)

* revert tx when block gas limit exceeded (backport: cosmos#10770)

- used a different solution.

changelog

* add unit test

* Update baseapp/baseapp.go

Co-authored-by: Robert Zaremba <[email protected]>

* simplfy the defer function

Co-authored-by: Robert Zaremba <[email protected]>
JeancarloBarrios pushed a commit to agoric-labs/cosmos-sdk that referenced this pull request Sep 28, 2024
…cosmos#10814)

* revert tx when block gas limit exceeded (backport: cosmos#10770)

- used a different solution.

changelog

* add unit test

* Update baseapp/baseapp.go

Co-authored-by: Robert Zaremba <[email protected]>

* simplfy the defer function

Co-authored-by: Robert Zaremba <[email protected]>
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. C:x/auth
Projects
None yet
Development

Successfully merging this pull request may close these issues.

tx committed successfully but return failure result
8 participants