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(baseapp): don't share global gas meter in tx execution #19616

Merged
merged 7 commits into from
Apr 15, 2024

Conversation

yihuang
Copy link
Collaborator

@yihuang yihuang commented Mar 1, 2024

ref: #19613

Before the ante handler set the gas meter using tx gas-wanted, the gas meter in ctx remains a globally shared one, although it don't have bad effect right now, but it's a race condition when executing txs in parallel.

Description

Closes: #XXXX


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
  • confirmed ! in 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
  • reviewed "Files changed" and left comments if necessary
  • included the necessary unit and integration tests
  • added a changelog entry to CHANGELOG.md
  • updated the relevant documentation or specification, including comments for documenting Go code
  • 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 all author checklist items have been addressed
  • reviewed state machine logic, API design and naming, documentation is accurate, tests and test coverage

Summary by CodeRabbit

  • Bug Fixes

    • Enhanced the transaction execution process by introducing individual gas meters for each transaction, preventing shared meter issues.
  • Documentation

    • Updated the changelog to reflect recent improvements in transaction context handling.

ref: cosmos#19613

Before the ante handler set the gas meter using tx gas-wanted, the gas meter in
ctx remains a globally shared one, although it don't have bad effect right now,
but it's a race condition when executing in parallel.
@yihuang yihuang requested a review from a team as a code owner March 1, 2024 13:45
Copy link
Contributor

coderabbitai bot commented Mar 1, 2024

Walkthrough

Walkthrough

The recent update focuses on enhancing the transaction context setup in the BaseApp by introducing a unique gas meter for each transaction, addressing the issue of a shared gas meter. This change ensures more robust and isolated handling of gas calculations during transaction execution. Additionally, adjustments were made to how the server context is modified, promoting more direct and efficient updates.

Changes

File Change Summary
baseapp/baseapp.go Introduced WithGasMeter in getContextForTx to add a unique gas meter using storetypes.NewInfiniteGasMeter().
CHANGELOG.md Updated to reflect the changes in gas meter handling and context modification in the baseapp module.

Recent Review Details

Configuration used: .coderabbit.yml

Commits Files that changed from the base of the PR and between 3ae64e7 and 9de6ff3.
Files selected for processing (1)
  • CHANGELOG.md (1 hunks)
Additional Context Used
Path-based Instructions (1)
CHANGELOG.md (1)

Pattern **/*.md: "Assess the documentation for misspellings, grammatical errors, missing documentation and correctness"


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share
Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

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.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • 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/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@yihuang yihuang requested a review from odeke-em March 1, 2024 13:45
@yihuang yihuang added the backport/v0.50.x PR scheduled for inclusion in the v0.50's next stable release label Mar 1, 2024
Comment on lines 668 to 675
panic(fmt.Sprintf("state is nil for mode %v", mode))
}
ctx := modeState.Context().
WithTxBytes(txBytes)
WithTxBytes(txBytes).
WithGasMeter(storetypes.NewInfiniteGasMeter())
// WithVoteInfos(app.voteInfos) // TODO: identify if this is needed

ctx = ctx.WithIsSigverifyTx(app.sigverifyTx)
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).getContextForTx (baseapp/baseapp.go:662)
(*github.com/cosmos/cosmos-sdk/baseapp.BaseApp).runTx (baseapp/baseapp.go:824)
(*github.com/cosmos/cosmos-sdk/baseapp.BaseApp).deliverTx (baseapp/baseapp.go:754)
(*github.com/cosmos/cosmos-sdk/baseapp.BaseApp).internalFinalizeBlock (baseapp/baseapp.go:713)
(*github.com/cosmos/cosmos-sdk/baseapp.BaseApp).FinalizeBlock (baseapp/baseapp.go:874)

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.yml

Commits Files that changed from the base of the PR and between 3d59013 and 6a5158f.
Files selected for processing (1)
  • baseapp/baseapp.go (1 hunks)
Additional comments: 1
baseapp/baseapp.go (1)
  • 671-672: The addition of a dedicated gas meter for each transaction context is a crucial improvement for concurrency and race condition prevention. This change ensures that each transaction is processed with its own gas meter, eliminating the risk of shared state issues. The implementation appears correct and follows the described objectives. However, it's important to ensure that all other parts of the system that interact with the gas meter are aware of this change and handle the gas meter appropriately. Additionally, thorough testing should be conducted to verify that this change does not introduce any new issues, especially in concurrent transaction processing scenarios.

@yihuang yihuang changed the title fix: tx context gas meter is shared at the beginning fix: don't share global gas meter in tx execution Mar 1, 2024
@julienrbrt
Copy link
Member

julienrbrt commented Mar 2, 2024

Can we get a changelog please?

@yihuang
Copy link
Collaborator Author

yihuang commented Mar 3, 2024

Can we get a changelog please?

sure, done

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.yml

Commits Files that changed from the base of the PR and between 6a5158f and 52e3e9b.
Files selected for processing (1)
  • CHANGELOG.md (1 hunks)
Files not reviewed due to errors (1)
  • CHANGELOG.md (Error: unable to parse review)

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.yml

Commits Files that changed from the base of the PR and between 52e3e9b and 692d646.
Files selected for processing (2)
  • CHANGELOG.md (1 hunks)
  • baseapp/baseapp.go (1 hunks)
Check Runs (25)
tests (03) completed (1)
tests (02) completed (1)
tests (01) completed (1)
test-x-upgrade completed (1)
test-x-tx completed (1)
test-x-staking completed (1)
test-x-slashing completed (1)
test-x-params completed (1)
test-x-protocolpool completed (1)
test-x-nft completed (1)
test-x-mint completed (1)
test-x-group completed (1)
test-x-gov completed (1)
test-x-feegrant completed (1)
test-x-evidence completed (1)
test-x-distribution completed (1)
test-x-circuit completed (1)
test-x-bank completed (1)
test-x-authz completed (1)
test-x-auth completed (1)
test-x-accounts completed (1)
test-store completed (1)
test-sim-nondeterminism completed (1)
test-simapp completed (1)
test-orm completed (1)
Additional comments: 2
baseapp/baseapp.go (1)
  • 669-670: The introduction of storetypes.NewInfiniteGasMeter() in the getContextForTx function ensures that each transaction context has its own dedicated gas meter. This change is crucial for preventing race conditions in parallel transaction execution by isolating the gas meter to the transaction level rather than sharing it globally. This approach aligns well with the PR's objective to improve transaction processing reliability in the Cosmos SDK.
CHANGELOG.md (1)
  • 97-97: The changelog entry for PR fix(baseapp): don't share global gas meter in tx execution #19616 is clear and concise. However, it might be beneficial for readers to have a bit more context about the issue. Consider expanding the description to briefly explain the problem of the shared gas meter and how this change resolves it, enhancing the understanding of the change's impact.

@@ -666,7 +666,8 @@ func (app *BaseApp) getContextForTx(mode execMode, txBytes []byte) sdk.Context {
panic(fmt.Sprintf("state is nil for mode %v", mode))
}
ctx := modeState.Context().
WithTxBytes(txBytes)
WithTxBytes(txBytes).
WithGasMeter(storetypes.NewInfiniteGasMeter())
Copy link
Member

Choose a reason for hiding this comment

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

I see you've added the backport tag, but is this really backportable?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I guess so, the gas meter will be replaced by ante handler, so whatever recorded in the initial gas meter is discarded there.

Copy link
Contributor

Choose a reason for hiding this comment

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

The root gas meter is an infinite gas meter. This change solves the race as the root gas meter is not shared anymore with the new context. Looks good for me

@julienrbrt julienrbrt changed the title fix: don't share global gas meter in tx execution fix(baseapp): don't share global gas meter in tx execution Mar 19, 2024
CHANGELOG.md Outdated Show resolved Hide resolved
CHANGELOG.md Outdated Show resolved Hide resolved
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.yml

Commits Files that changed from the base of the PR and between 692d646 and 45dfe41.
Files selected for processing (1)
  • CHANGELOG.md (1 hunks)
Files skipped from review as they are similar to previous changes (1)
  • CHANGELOG.md

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.yml

Commits Files that changed from the base of the PR and between 45dfe41 and 3ae64e7.
Files selected for processing (1)
  • CHANGELOG.md (1 hunks)
Files skipped from review as they are similar to previous changes (1)
  • CHANGELOG.md

@tac0turtle tac0turtle assigned alpe and sontrinh16 and unassigned alexanderbez Apr 15, 2024
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.

Actionable comments posted: 1

@@ -116,6 +116,7 @@ Every module contains its own CHANGELOG.md. Please refer to the module you are i
* (crypto) [#20027](https://github.com/cosmos/cosmos-sdk/pull/20027) secp256r1 keys now implement gogoproto's customtype interface.
* (x/bank) [#20028](https://github.com/cosmos/cosmos-sdk/pull/20028) Align query with multi denoms for send-enabled.
* (cli) [#20020](https://github.com/cosmos/cosmos-sdk/pull/20020) Make bootstrap-state command support both new and legacy genesis format.
* (baseapp) [#19616](https://github.com/cosmos/cosmos-sdk/pull/19616) Don't share gas meter in tx execution.
Copy link
Contributor

Choose a reason for hiding this comment

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

The changelog entry clearly documents the change made in PR #19616. Consider rephrasing for enhanced clarity:

- * (baseapp) [#19616](https://github.com/cosmos/cosmos-sdk/pull/19616) Don't share gas meter in tx execution.
+ * (baseapp) [#19616](https://github.com/cosmos/cosmos-sdk/pull/19616) Ensure each transaction has its own gas meter to prevent race conditions.

Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
* (baseapp) [#19616](https://github.com/cosmos/cosmos-sdk/pull/19616) Don't share gas meter in tx execution.
* (baseapp) [#19616](https://github.com/cosmos/cosmos-sdk/pull/19616) Ensure each transaction has its own gas meter to prevent race conditions.

Copy link
Contributor

@alpe alpe left a comment

Choose a reason for hiding this comment

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

Nice! 👍

@@ -666,7 +666,8 @@ func (app *BaseApp) getContextForTx(mode execMode, txBytes []byte) sdk.Context {
panic(fmt.Sprintf("state is nil for mode %v", mode))
}
ctx := modeState.Context().
WithTxBytes(txBytes)
WithTxBytes(txBytes).
WithGasMeter(storetypes.NewInfiniteGasMeter())
Copy link
Contributor

Choose a reason for hiding this comment

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

The root gas meter is an infinite gas meter. This change solves the race as the root gas meter is not shared anymore with the new context. Looks good for me

@julienrbrt julienrbrt added this pull request to the merge queue Apr 15, 2024
Merged via the queue into cosmos:main with commit 4ed8c84 Apr 15, 2024
59 of 60 checks passed
mergify bot pushed a commit that referenced this pull request Apr 15, 2024
(cherry picked from commit 4ed8c84)

# Conflicts:
#	CHANGELOG.md
@yihuang yihuang deleted the cleanup-tx-ctx branch April 16, 2024 00:00
alpe added a commit to alpe/cosmos-sdk that referenced this pull request Apr 16, 2024
* main:
  refactor(x/simulation)!: remove accounts string (cosmos#20056)
  fix(baseapp): don't share global gas meter in tx execution (cosmos#19616)
  feat(x/accounts): use router service from env (cosmos#20003)
  refactor(x): remove Address.String() (cosmos#20048)
  build(deps): Bump github.com/jhump/protoreflect from 1.15.6 to 1.16.0 in /tests (cosmos#20040)
  feat(collections): add `NewJSONValueCodec` (cosmos#19861)
alpe added a commit that referenced this pull request Apr 17, 2024
* main:
  build(deps): Bump github.com/pelletier/go-toml/v2 from 2.2.0 to 2.2.1 in /tools/confix (#20052)
  build(deps): Bump cosmossdk.io/api from 0.7.3 to 0.7.4 (#20063)
  fix: secp256r1 json missing quotes (#20060)
  fix(x/accounts): remove double execute (#20065)
  refactor(x/accounts): Skip Importing Unregistered Genesis Account Types (#20053)
  refactor(x/simulation)!: remove accounts string (#20056)
  fix(baseapp): don't share global gas meter in tx execution (#19616)
  feat(x/accounts): use router service from env (#20003)
  refactor(x): remove Address.String() (#20048)
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
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants