Skip to content

feat(baseapp): avoid decoding tx in PrepareProposal when using NopMempool #24207

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

Open
wants to merge 11 commits into
base: main
Choose a base branch
from

Conversation

mmsqe
Copy link
Contributor

@mmsqe mmsqe commented Mar 31, 2025

  • Problem: tx is decoded in PrepareProposal when using NopMempool

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, you can find examples of the prefixes below:
  • 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.

Please see Pull Request Reviewer section in the contributing guide for more information on how to review a pull request.

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

  • New Features
    • Introduced an optimized proposal processing path that bypasses standard transaction decoding and filtering when conditions allow, leading to more efficient transaction selection and faster proposal preparation.
  • Tests
    • Added test coverage for the new fast proposal handler to ensure functionality and correctness without altering existing logic.

* Problem: tx is decoded in PrepareProposal when using NopMempool
Copy link
Contributor

coderabbitai bot commented Mar 31, 2025

📝 Walkthrough

Walkthrough

This pull request introduces a fast-path for transaction proposal preparation in the ABCI application. It adds a new flag and supporting methods to bypass the standard transaction selection when the mempool is a no-op or nil. This change allows the proposal handler to quickly return the incoming transactions without additional processing.

Changes

File Summary of Changes
CHANGELOG.md Updated to reflect the avoidance of unnecessary transaction decoding in PrepareProposal when using a no-op mempool.
baseapp/abci_utils.go - Added fastPrepareProposal boolean field to DefaultProposalHandler.
- Introduced NewDefaultProposalHandlerFast constructor to initialize the handler with fast mode.
- Updated PrepareProposalHandler to conditionally invoke SelectTxForProposalFast.
- Extended TxSelector with the new method and implemented it in defaultTxSelector to directly return the provided transactions.
baseapp/abci_utils_test.go Added tests for the new NewDefaultProposalHandlerFast to validate its functionality alongside the existing handler without altering existing logic.

Possibly related PRs

Suggested labels

C:server/v2, C:server/v2 cometbft, backport/v0.52.x

Suggested reviewers

  • aaronc
  • facundomedica
  • testinginprod
  • tac0turtle
  • julienrbrt
✨ Finishing Touches
  • 📝 Generate Docstrings

🪧 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>, please review it.
    • 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 gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @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.
    • @coderabbitai help me debug CodeRabbit configuration file.

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 using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai plan to trigger planning for file edits and PR creation.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

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.

@mmsqe mmsqe changed the title fix(baseapp): avoid decoding tx in PrepareProposal when using NopMempool feat(baseapp): avoid decoding tx in PrepareProposal when using NopMempool Mar 31, 2025
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: 0

🧹 Nitpick comments (3)
baseapp/abci_utils.go (1)

255-259: Missing function documentation

The new constructor NewDefaultProposalHandlerFast is missing a header comment to explain its purpose, unlike other exported functions in the file.

+// NewDefaultProposalHandlerFast creates a new DefaultProposalHandler with fastPrepareProposal set to true,
+// which optimizes PrepareProposal by avoiding tx decoding when using NoOpMempool.
 func NewDefaultProposalHandlerFast(mp mempool.Mempool, txVerifier ProposalTxVerifier) *DefaultProposalHandler {
 	h := NewDefaultProposalHandler(mp, txVerifier)
 	h.fastPrepareProposal = true
 	return h
 }
CHANGELOG.md (2)

51-53: Consider adding a blank line before list items [markdownlint MD032]

The markdown linter suggests adding a blank line before starting the list items in the "Improvements" section. Following markdown best practices, lists should be surrounded by blank lines to ensure proper rendering across different markdown processors.

### Improvements

+ 
* [#24207](https://github.com/cosmos/cosmos-sdk/pull/24207) Avoid decoding tx for in PrepareProposal if it's NopMempool.
🧰 Tools
🪛 markdownlint-cli2 (0.17.2)

52-52: Headings should be surrounded by blank lines
Expected: 1; Actual: 0; Below

(MD022, blanks-around-headings)


53-53: Lists should be surrounded by blank lines
null

(MD032, blanks-around-lists)


53-53: Fix minor typo in improvement description

There's a small grammatical error in the description of improvement #24207.

-* [#24207](https://github.com/cosmos/cosmos-sdk/pull/24207) Avoid decoding tx for in PrepareProposal if it's NopMempool.
+* [#24207](https://github.com/cosmos/cosmos-sdk/pull/24207) Avoid decoding tx in PrepareProposal if it's NopMempool.
🧰 Tools
🪛 markdownlint-cli2 (0.17.2)

53-53: Lists should be surrounded by blank lines
null

(MD032, blanks-around-lists)

📜 Review details

Configuration used: .coderabbit.yml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between a3e6264 and c2853a7.

📒 Files selected for processing (2)
  • CHANGELOG.md (1 hunks)
  • baseapp/abci_utils.go (6 hunks)
🧰 Additional context used
📓 Path-based instructions (2)
`**/*.go`: Review the Golang code for conformity with the Uber Golang style guide, highlighting any deviations.

**/*.go: Review the Golang code for conformity with the Uber Golang style guide, highlighting any deviations.

  • baseapp/abci_utils.go
`**/*.md`: "Assess the documentation for misspellings, grammatical errors, missing documentation and correctness"

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

  • CHANGELOG.md
🪛 markdownlint-cli2 (0.17.2)
CHANGELOG.md

53-53: Lists should be surrounded by blank lines
null

(MD032, blanks-around-lists)

⏰ Context from checks skipped due to timeout of 90000ms (4)
  • GitHub Check: tests (00)
  • GitHub Check: test-system-v2
  • GitHub Check: Analyze
  • GitHub Check: Summary
🔇 Additional comments (7)
baseapp/abci_utils.go (6)

241-242: Field addition looks good

The new field fastPrepareProposal is properly documented and clearly explains its purpose to bypass tx selector when using NoOpMempool.


312-315: Implementation of fast path looks good

The fastPrepareProposal condition is properly checked and applied only in the correct context (when mempool is nil or NoOp). This optimization avoids unnecessary transaction decoding.


520-524: Interface method is well documented

The addition of SelectTxForProposalFast to the TxSelector interface is properly documented with clear comments explaining its purpose and usage.


546-546: Good memory optimization

Using ts.selectedTxs[:0] instead of setting to nil preserves the allocated memory, which is more efficient for repeated operations.


579-581: Simple and effective implementation

The implementation of SelectTxForProposalFast correctly returns the input transactions without additional processing, which aligns with its purpose to provide a fast path.


312-324:

❓ Verification inconclusive

Verify all calling code is updated

While the fast path implementation itself looks good, it's important to ensure that any code creating DefaultProposalHandler instances is updated to use NewDefaultProposalHandlerFast where appropriate.


🏁 Script executed:

#!/bin/bash
# Description: Find all instances where DefaultProposalHandler is created
# and check if they might benefit from using NewDefaultProposalHandlerFast

# Find all calls to NewDefaultProposalHandler
echo "Checking calls to NewDefaultProposalHandler:"
rg --type go "NewDefaultProposalHandler" --no-heading

# Find places where NopMempool is used or checked
echo -e "\nChecking where NopMempool is used:"
rg --type go "NopMempool|NoOpMempool" --no-heading

Length of output: 5724


Ensure All Usage Sites Leverage the Fast Proposal Handler

The fast-path logic in baseapp/abci_utils.go is implemented correctly. However, our investigation shows that several instantiation points still use NewDefaultProposalHandler instead of the fast-construction helper. Please verify and update the following if applicable:

  • Production Code:

    • In baseapp/baseapp.go, the proposal handler is created using NewDefaultProposalHandler. Confirm that the fast path should be enabled here, and update to NewDefaultProposalHandlerFast if so.
  • Test Code:

    • In baseapp/abci_utils_test.go and server/v2/cometbft/abci_test.go, instances of DefaultProposalHandler are still constructed via the old constructor. Verify whether tests should reflect the fast path changes.
  • NoOpMempool Considerations:

    • Ensure that the use cases involving NoOpMempool are intentionally bypassing the fast path or need to be updated as well.

Please review each of these calling sites to ensure they correctly utilize the fast proposal handler where it benefits performance.

CHANGELOG.md (1)

1-3164: The CHANGELOG looks well-structured and comprehensive

The CHANGELOG adequately documents the changes for this PR within the "Improvements" section. The PR is properly linked and the description concisely explains the change - avoiding transaction decoding in PrepareProposal when using NopMempool. This aligns with the PR objectives mentioned earlier.

The overall structure of the CHANGELOG follows good practices with:

  • Versioned headers with release dates
  • Categorized changes (Features, Improvements, Bug Fixes, etc.)
  • PR references with links to the corresponding GitHub PRs
  • Brief but clear descriptions of the changes

This makes it easy for users to understand what's changed between versions.

🧰 Tools
🪛 markdownlint-cli2 (0.17.2)

52-52: Headings should be surrounded by blank lines
Expected: 1; Actual: 0; Below

(MD022, blanks-around-headings)


53-53: Lists should be surrounded by blank lines
null

(MD032, blanks-around-lists)


93-93: Unordered list indentation
Expected: 4; Actual: 2

(MD007, ul-indent)


94-94: Unordered list indentation
Expected: 4; Actual: 2

(MD007, ul-indent)


95-95: Unordered list indentation
Expected: 4; Actual: 2

(MD007, ul-indent)


366-366: Multiple headings with the same content
null

(MD024, no-duplicate-heading)


376-376: Multiple headings with the same content
null

(MD024, no-duplicate-heading)


418-418: Multiple headings with the same content
null

(MD024, no-duplicate-heading)


885-885: Multiple headings with the same content
null

(MD024, no-duplicate-heading)


1335-1335: Multiple headings with the same content
null

(MD024, no-duplicate-heading)


2125-2125: Multiple headings with the same content
null

(MD024, no-duplicate-heading)

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: 0

🧹 Nitpick comments (1)
baseapp/abci_utils_test.go (1)

488-490: Good implementation of the fast proposal handler test

The code correctly instantiates the new fast proposal handler which is designed to bypass tx decoding when using NoOpMempool.

Consider adding a brief comment explaining the purpose of the fast handler:

+ // Create a fast proposal handler that avoids decoding txs when using NoOpMempool
phf := baseapp.NewDefaultProposalHandlerFast(mempool.NoOpMempool{}, app)
handlerFast := phf.PrepareProposalHandler()
📜 Review details

Configuration used: .coderabbit.yml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between c2853a7 and b283aad.

📒 Files selected for processing (1)
  • baseapp/abci_utils_test.go (2 hunks)
🧰 Additional context used
📓 Path-based instructions (2)
`**/*.go`: Review the Golang code for conformity with the Uber Golang style guide, highlighting any deviations.

**/*.go: Review the Golang code for conformity with the Uber Golang style guide, highlighting any deviations.

  • baseapp/abci_utils_test.go
`**/*_test.go`: "Assess the unit test code assessing sufficient code coverage for the changes associated in the pull request"

**/*_test.go: "Assess the unit test code assessing sufficient code coverage for the changes associated in the pull request"

  • baseapp/abci_utils_test.go
⏰ Context from checks skipped due to timeout of 90000ms (4)
  • GitHub Check: tests (00)
  • GitHub Check: test-system-v2
  • GitHub Check: Analyze
  • GitHub Check: Summary
🔇 Additional comments (1)
baseapp/abci_utils_test.go (1)

574-576: Verification logic looks good

The test correctly verifies that the fast handler returns the original request transactions without modification, confirming it takes the expected fast path.

@aljo242
Copy link
Contributor

aljo242 commented Mar 31, 2025

What is the use case for this feature or what is the bug that we are actually fixing with this?

@mmsqe
Copy link
Contributor Author

mmsqe commented Mar 31, 2025

What is the use case for this feature or what is the bug that we are actually fixing with this?

This allows faster prepare proposal when using NoOpMempool which avoid unnecessary TxDecode
Screenshot 2025-03-31 at 10 30 58 PM

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: 0

🧹 Nitpick comments (1)
CHANGELOG.md (1)

53-53: Fix grammar in improvement description and ensure proper list formatting

The improvement description has a small grammatical issue - it includes an unnecessary "for" word. Also, according to markdown best practices (MD032), lists should be surrounded by blank lines.

- * [#24207](https://github.com/cosmos/cosmos-sdk/pull/24207) Avoid decoding tx for in PrepareProposal if it's NoOpMempool.
+ * [#24207](https://github.com/cosmos/cosmos-sdk/pull/24207) Avoid decoding tx in PrepareProposal if it's NoOpMempool.

Additionally, ensure there's a blank line before and after this list of improvements to follow proper markdown formatting.

🧰 Tools
🪛 markdownlint-cli2 (0.17.2)

53-53: Lists should be surrounded by blank lines
null

(MD032, blanks-around-lists)

📜 Review details

Configuration used: .coderabbit.yml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between b283aad and e9c9cc2.

📒 Files selected for processing (2)
  • CHANGELOG.md (1 hunks)
  • baseapp/abci_utils.go (7 hunks)
🧰 Additional context used
📓 Path-based instructions (2)
`**/*.go`: Review the Golang code for conformity with the Uber Golang style guide, highlighting any deviations.

**/*.go: Review the Golang code for conformity with the Uber Golang style guide, highlighting any deviations.

  • baseapp/abci_utils.go
`**/*.md`: "Assess the documentation for misspellings, grammatical errors, missing documentation and correctness"

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

  • CHANGELOG.md
🪛 markdownlint-cli2 (0.17.2)
CHANGELOG.md

53-53: Lists should be surrounded by blank lines
null

(MD032, blanks-around-lists)

⏰ Context from checks skipped due to timeout of 90000ms (4)
  • GitHub Check: tests (00)
  • GitHub Check: test-system-v2
  • GitHub Check: Analyze
  • GitHub Check: Summary
🔇 Additional comments (7)
baseapp/abci_utils.go (7)

241-242: Well-documented new feature flag.

The fastPrepareProposal field with its descriptive comment makes it clear that this is used to bypass the tx selector when using NoOpMempool.


255-259: Clean constructor implementation for the fast proposal handler.

Good approach to reuse the existing constructor and only modify the necessary field. This avoids code duplication and maintains a clear separation of concerns.


300-317: Good extraction of decoding logic.

Extracting the transaction decoding logic into a separate function with caching mechanism is a performance improvement. This allows for conditional execution based on whether decoding is needed.


319-322: Fast-path implementation for proposal preparation.

This is the core of the optimization - allowing direct processing of transactions without decoding when using NoOpMempool with the fast flag enabled. The implementation correctly leverages the new SelectTxForProposalFast method.


535-539: Clear interface extension with good documentation.

The SelectTxForProposalFast method is well-documented, explaining its purpose and noting that CometBFT has already performed necessary checks. The comment also clarifies that extra validations are still possible.


594-596: Simple and efficient implementation of fast proposal selection.

The implementation directly returns the provided transactions without additional processing, which aligns with the feature's purpose of skipping unnecessary decoding and validation when using NoOpMempool.


561-561: Good memory optimization.

Using ts.selectedTxs[:0] rather than creating a new slice preserves the allocated memory, which is more efficient than allocating a new slice, especially if the selector handles many transactions frequently.

@aljo242
Copy link
Contributor

aljo242 commented Mar 31, 2025

Is there a case where you are a no-op mempool and you wouldnt want this behavior? Why not make this the default behavior when using a no-op mempool?

@mmsqe
Copy link
Contributor Author

mmsqe commented Apr 1, 2025

Is there a case where you are a no-op mempool and you wouldnt want this behavior? Why not make this the default behavior when using a no-op mempool?

Not for all cases, our case is based on the fact that cometbft has already checked the block gas/size limit before select so we can skip the tx selection logic.

// SelectTxForProposalFast is called in the case of NoOpMempool,
// where cometbft already checked the block gas/size limit,
// so the tx selector should simply accept them all to the proposal.
// But extra validations on the tx are still possible though.
Copy link
Contributor

Choose a reason for hiding this comment

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

// But extra validations on the tx are still possible though.

is it still possible because i can implement my own TxSelector? i'm a bit confused on this

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@aljo242 aljo242 added the v54 label Apr 14, 2025
@mmsqe mmsqe requested a review from technicallyty April 23, 2025 15:01
CHANGELOG.md Outdated
@@ -100,6 +100,8 @@ Ref: https://keepachangelog.com/en/1.0.0/
* (server) [#24072](https://github.com/cosmos/cosmos-sdk/pull/24072) Return BlockHeader by shallow copy in server Context.
* (x/bank) [#24053](https://github.com/cosmos/cosmos-sdk/pull/24053) Resolve a foot-gun by swapping send restrictions check in `InputOutputCoins` before coin deduction.
* (codec/types) [#24336](https://github.com/cosmos/cosmos-sdk/pull/24336) Most types definitions were moved to `github.com/cosmos/gogoproto/types/any` with aliases to these left in `codec/types` so that there should be no breakage to existing code. This allows protobuf generated code to optionally reference the SDK's custom `Any` type without a direct dependency on the SDK. This can be done by changing the `protoc` `M` parameter for `any.proto` to `Mgoogle/protobuf/any.proto=github.com/cosmos/gogoproto/types/any`.
* [#24207](https://github.com/cosmos/cosmos-sdk/pull/24207) Avoid decoding tx for in PrepareProposal if it's NoOpMempool.
Copy link
Contributor

Choose a reason for hiding this comment

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

this needs to go in the Unreleased section

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.

4 participants