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

feat!: limit the max tx size to 2 MiB #3909

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

Conversation

ninabarbakadze
Copy link
Member

@ninabarbakadze ninabarbakadze commented Sep 25, 2024

Overview

Fixes #3686

Copy link

github-actions bot commented Sep 25, 2024

PR Preview Action v1.4.8
🚀 Deployed preview to https://celestiaorg.github.io/celestia-app/pr-preview/pr-3909/
on branch gh-pages at 2024-10-04 07:19 UTC

app/ante/max_tx_size.go Outdated Show resolved Hide resolved
app/ante/max_tx_size.go Outdated Show resolved Hide resolved
pkg/appconsts/v3/app_consts.go Outdated Show resolved Hide resolved
pkg/appconsts/versioned_consts.go Outdated Show resolved Hide resolved
@@ -3,6 +3,7 @@
The AnteHandler chains together several decorators to ensure the following criteria are met for app version 3:

- The tx does not contain any messages that are unsupported by the current app version. See `MsgVersioningGateKeeper`.
- The tx size is not larger than the application's configured versioned constant MaxTxSize.
Copy link
Member Author

Choose a reason for hiding this comment

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

I'll add the link to MaxTxSize in appconsts once this pr is merged

@ninabarbakadze ninabarbakadze marked this pull request as ready for review October 4, 2024 08:05
@ninabarbakadze ninabarbakadze requested review from liamsi and a team as code owners October 4, 2024 08:05
@ninabarbakadze ninabarbakadze requested review from rootulp and evan-forbes and removed request for a team October 4, 2024 08:05
Copy link
Contributor

coderabbitai bot commented Oct 4, 2024

📝 Walkthrough
📝 Walkthrough

Walkthrough

The changes introduce a new decorator, MaxTxSizeDecorator, to the transaction handling process, ensuring transactions do not exceed a specified size limit. This decorator is added to the NewAnteHandler function, which is responsible for processing transactions. Additionally, a new constant MaxTxBytes is defined to set the maximum transaction size, and corresponding tests have been implemented to validate the decorator's functionality across various scenarios. Documentation has also been updated to reflect these changes.

Changes

File Change Summary
app/ante/ante.go Added NewMaxTxSizeDecorator() to NewAnteHandler for transaction size validation.
app/ante/max_tx_size.go Introduced MaxTxSizeDecorator type with AnteHandle method to enforce transaction size limits.
app/ante/max_tx_size_test.go Created tests for MaxTxSizeDecorator to validate behavior with various transaction sizes and versions.
pkg/appconsts/v3/app_consts.go Added constant MaxTxBytes set to 2097152 (2 MiB).
pkg/appconsts/versioned_consts.go Added function MaxTxBytes to return the value of v3.MaxTxBytes.
pkg/appconsts/versioned_consts_test.go Added test case for MaxTxBytes in TestVersionedConsts.
specs/src/ante_handler_v3.md Updated documentation to include transaction size validation criteria.

Assessment against linked issues

Objective Addressed Explanation
Cap the size of the transaction to reduce strain on network (#3686)

Possibly related PRs

Suggested labels

enhancement, WS: Big Blonks 🔭

Suggested reviewers

  • liamsi
  • staheri14
  • evan-forbes
  • rootulp
  • cmwaters

📜 Recent review details

Configuration used: .coderabbit.yaml
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between 941f55b and 567d832.

📒 Files selected for processing (2)
  • app/ante/max_tx_size.go (1 hunks)
  • app/ante/max_tx_size_test.go (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (2)
  • app/ante/max_tx_size.go
  • app/ante/max_tx_size_test.go

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:

‼️ IMPORTANT
Auto-reply has been disabled for this repository in the CodeRabbit settings. The CodeRabbit bot will not respond to your replies unless it is explicitly tagged.

  • 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 resolve resolve all the CodeRabbit review comments.
  • @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.

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

🧹 Outside diff range and nitpick comments (7)
pkg/appconsts/versioned_consts.go (2)

45-47: Consider implementing the previous review suggestion

As suggested in a previous review, consider removing the parameter name for consistency:

-func MaxTxBytes(_ uint64) int {
+func MaxTxBytes(uint64) int {
 	return v3.MaxTxBytes
}

This change improves code clarity by explicitly showing that the parameter is unused.


45-47: Add a comment explaining the unused parameter

Consider adding a comment to explain the purpose of the unused uint64 parameter. If it's for future versioning, make this explicit:

+// MaxTxBytes returns the maximum transaction size in bytes.
+// The uint64 parameter is reserved for future versioning and is currently unused.
func MaxTxBytes(_ uint64) int {
	return v3.MaxTxBytes
}

This addition would improve code documentation and clarify the intent behind the function signature.

app/ante/max_tx_size_test.go (3)

20-50: LGTM: Test cases cover important scenarios.

The test cases are well-defined and cover crucial scenarios including valid transaction size, oversized transactions, edge cases, and version compatibility. The use of v3.MaxTxBytes constant ensures the test adapts to changes in the maximum transaction size.

Consider adding an additional test case for a transaction size slightly below the maximum threshold (e.g., v3.MaxTxBytes - 1000) to ensure there's no off-by-one error in the implementation.


52-72: LGTM: Test execution logic is robust and well-implemented.

The test execution logic is sound, iterating over each test case, setting up the appropriate context, and verifying the results. The use of the require package ensures clear test failure messages.

Consider adding a comment explaining the purpose of ctx.WithTxBytes(txBytes) to improve code readability. For example:

// Set the transaction bytes in the context to simulate a transaction of the specified size
ctx = ctx.WithTxBytes(txBytes)

1-72: Great job on implementing comprehensive tests for MaxTxSizeDecorator!

This test file effectively validates the functionality of the MaxTxSizeDecorator, aligning well with the PR objectives of implementing a maximum transaction size. The tests cover various scenarios, including valid and invalid transaction sizes, edge cases, and version compatibility.

The code is well-structured, follows Go best practices, and uses appropriate testing utilities. This will help ensure the robustness of the transaction size limitation feature.

As the project evolves, consider expanding these tests to cover more edge cases and potential real-world scenarios. This could include testing with different types of transactions or simulating network conditions that might affect transaction processing.

specs/src/ante_handler_v3.md (1)

6-6: Approved addition with a suggestion for improvement

The new criterion for transaction size validation aligns well with the PR objectives of capping transaction size to reduce network strain. This is a valuable addition to the AnteHandler specifications.

To enhance clarity and maintainability, consider adding a reference or link to the definition of MaxTxBytes. This would help developers quickly locate the specific value and any related configuration details.

Consider adding a reference like this:

- The tx size is not larger than the application's configured versioned constant MaxTxBytes (defined in [appconsts](link-to-appconsts-file)).
app/ante/max_tx_size.go (1)

10-11: Improve comment clarity for MaxTxSizeDecorator.

Consider rephrasing the comment to form a complete sentence and enhance readability. For example:

-// MaxTxSizeDecorator ensures that a tx can not be larger than
-// application's configured versioned constant.
+// MaxTxSizeDecorator ensures that a transaction cannot exceed
+// the application's configured maximum size.
📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between c713401 and 941f55b.

📒 Files selected for processing (7)
  • app/ante/ante.go (1 hunks)
  • app/ante/max_tx_size.go (1 hunks)
  • app/ante/max_tx_size_test.go (1 hunks)
  • pkg/appconsts/v3/app_consts.go (1 hunks)
  • pkg/appconsts/versioned_consts.go (1 hunks)
  • pkg/appconsts/versioned_consts_test.go (1 hunks)
  • specs/src/ante_handler_v3.md (1 hunks)
✅ Files skipped from review due to trivial changes (1)
  • pkg/appconsts/v3/app_consts.go
🔇 Additional comments (6)
pkg/appconsts/versioned_consts.go (2)

45-47: LGTM: Function aligns with PR objectives

The MaxTxBytes function correctly provides access to the maximum transaction size, which is crucial for implementing the transaction size cap as per the PR objectives.


45-47: Verify the usage of MaxTxBytes in the codebase

To ensure that this function is being used correctly to implement the transaction size cap, we should verify its usage across the codebase.

✅ Verification successful

MaxTxBytes usage verified across the codebase

The MaxTxBytes function is consistently utilized in all relevant areas to enforce transaction size limits, ensuring correct implementation of the transaction size cap.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Search for usages of MaxTxBytes function to verify its implementation in capping transaction sizes.

# Test: Search for MaxTxBytes usage
echo "Searching for MaxTxBytes usage:"
rg --type go "MaxTxBytes\(" -A 5

# Test: Search for potential transaction size checks
echo "Searching for potential transaction size checks:"
rg --type go "tx.*size|size.*tx" -i

Length of output: 15509

app/ante/max_tx_size_test.go (2)

1-14: LGTM: Package declaration and imports are well-structured.

The package name and imports are appropriate for the test file. The use of aliases for different versions of app constants (v2, v3) enhances readability.


16-19: LGTM: Test function setup is correct.

The test function is properly named and the setup correctly initializes the MaxTxSizeDecorator and chains it with sdk.ChainAnteDecorators.

pkg/appconsts/versioned_consts_test.go (1)

69-74: LGTM! The new test case aligns well with the PR objectives.

The addition of this test case for MaxTxBytes in version 3 is a good implementation. It directly supports the PR's goal of capping transaction sizes by ensuring that the new constant is correctly set and can be retrieved. This test will help maintain the integrity of the transaction size limit as the codebase evolves.

app/ante/ante.go (1)

35-36: Approve addition of NewMaxTxSizeDecorator with suggestions

The addition of NewMaxTxSizeDecorator() aligns well with the PR objectives of capping transaction size to alleviate network strain. Its placement in the decorator chain is logical, allowing for early rejection of oversized transactions.

However, I have a few suggestions to enhance clarity and ensure proper implementation:

  1. Please provide information about the implementation of NewMaxTxSizeDecorator(). What specific size limit is being enforced?
  2. Consider adding a comment to explain how this new decorator interacts with the existing NewConsumeGasForTxSizeDecorator. Are they complementary or potentially redundant?
  3. It would be helpful to see the implementation of NewMaxTxSizeDecorator() to ensure it correctly enforces the desired transaction size limit.

To verify the implementation and usage of NewMaxTxSizeDecorator(), please run the following script:

✅ Verification successful

Approve addition of NewMaxTxSizeDecorator with implementation verified

The NewMaxTxSizeDecorator() correctly enforces a transaction size limit of 2 MiB, aligning with the PR objectives of capping transaction size to alleviate network strain. Its placement in the decorator chain allows for early rejection of oversized transactions, and the implementation is supported by relevant test cases (TestMaxTxSizeDecorator).

Suggestions:

  1. Add comments to clearly state the enforced transaction size limit.
  2. Document the interaction between NewMaxTxSizeDecorator and existing decorators to ensure clarity.
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify the implementation of NewMaxTxSizeDecorator

# Test: Search for the implementation of NewMaxTxSizeDecorator
echo "Searching for NewMaxTxSizeDecorator implementation:"
rg --type go "func NewMaxTxSizeDecorator\(" -A 10

# Test: Check for any constants or variables related to max transaction size
echo "\nSearching for max transaction size constants or variables:"
rg --type go "(MaxTxSize|MAX_TX_SIZE)"

# Test: Look for tests related to max transaction size
echo "\nSearching for related test cases:"
rg --type go "TestMaxTxSize"

Length of output: 1955


Script:

#!/bin/bash
# Description: Locate the definition and value of MaxTxBytes

echo "Searching for the definition of MaxTxBytes:"
rg --type go "const\s+MaxTxBytes\s*="

echo "\nSearching for configurations or usages of MaxTxBytes:"
rg --type go "MaxTxBytes"

Length of output: 1785

Comment on lines 22 to 23
if len(ctx.TxBytes()) >= appconsts.MaxTxBytes(ctx.BlockHeader().Version.App) {
return ctx, sdkerrors.ErrTxTooLarge
Copy link
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue

Correct the transaction size comparison operator.

The current condition rejects transactions that are exactly the maximum allowed size:

if len(ctx.TxBytes()) >= appconsts.MaxTxBytes(ctx.BlockHeader().Version.App) {

If the intention is to allow transactions up to and including the maximum size, consider changing the comparison operator to >:

-if len(ctx.TxBytes()) >= appconsts.MaxTxBytes(ctx.BlockHeader().Version.App) {
+if len(ctx.TxBytes()) > appconsts.MaxTxBytes(ctx.BlockHeader().Version.App) {

This change ensures that transactions equal to the maximum size are accepted, and only those exceeding it are rejected.

Copy link
Member

Choose a reason for hiding this comment

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

this is good to note, whichever is used, we simply need to document it

using strictly greater than makes sense to me, as we can just use that constant everywhere instead of subtracting 1 everywhere as well

Copy link
Contributor

Choose a reason for hiding this comment

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

I second this that it would be better if it were strictly greater than 2MB

rootulp
rootulp previously approved these changes Oct 4, 2024
Copy link
Collaborator

@rootulp rootulp left a comment

Choose a reason for hiding this comment

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

Implementation LGTM, great work! I think we need a FLUP issue to write a CIP for this.

nit: PR title should include ! because I think this is consensus breaking.

@rootulp rootulp changed the title feat: cap tx size AnteHandler feat!: cap tx size AnteHandler Oct 4, 2024
@rootulp
Copy link
Collaborator

rootulp commented Oct 4, 2024

Another nit: PR title could be more readable to end-users b/c it will show up in release notes. For example: "feat!: limit the max tx size to 2 MiB".

Can also add this to release notes.

@ninabarbakadze ninabarbakadze changed the title feat!: cap tx size AnteHandler feat!: limit the max tx size to 2 MiB Oct 7, 2024
@ninabarbakadze
Copy link
Member Author

Another nit: PR title could be more readable to end-users b/c it will show up in release notes. For example: "feat!: limit the max tx size to 2 MiB".

Can also add this to release notes.

Thanks @rootulp! Updated :)

// AnteHandle implements the AnteHandler interface. It ensures that tx size is under application's configured threshold.
func (d MaxTxSizeDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate bool, next sdk.AnteHandler) (sdk.Context, error) {
// Tx size rule applies to app versions v3 and onwards.
if ctx.BlockHeader().Version.App >= v3.Version {
Copy link
Member

Choose a reason for hiding this comment

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

[very optional]

imo, if we can exit with a negative, then we should. this follows golang's norms

so

if ctx.BlockHeader().Version.App < v3.Version {
    next() // or whatever
   }

Comment on lines 22 to 23
if len(ctx.TxBytes()) >= appconsts.MaxTxBytes(ctx.BlockHeader().Version.App) {
return ctx, sdkerrors.ErrTxTooLarge
Copy link
Member

Choose a reason for hiding this comment

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

this is good to note, whichever is used, we simply need to document it

using strictly greater than makes sense to me, as we can just use that constant everywhere instead of subtracting 1 everywhere as well

Copy link
Contributor

@cmwaters cmwaters left a comment

Choose a reason for hiding this comment

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

A few nits otherwise looks good

@celestia-bot celestia-bot requested a review from a team October 8, 2024 14:02
@celestia-bot celestia-bot requested review from cmwaters and rach-id and removed request for a team October 8, 2024 14:02
Copy link
Collaborator

@rootulp rootulp left a comment

Choose a reason for hiding this comment

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

LGTM with one nit and one proposal

}

if len(ctx.TxBytes()) > appconsts.MaxTxBytes(ctx.BlockHeader().Version.App) {
return ctx, sdkerrors.ErrTxTooLarge
Copy link
Collaborator

Choose a reason for hiding this comment

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

[nit] The current error lacks context for a user to understand how large their transaction is and how large of a tx the system can support.

Proposal to include two more pieces of information in this error: the size of the current tx and the max tx bytes.

Copy link
Collaborator

@rootulp rootulp Oct 8, 2024

Choose a reason for hiding this comment

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

Also for posterity it looks like mempool.ErrTxTooLarge is thrown by Tendermint if the tx exceeds the mempool's max tx bytes parameter.

In BroadcastTx the Cosmos SDK translates the mempool.ErrTxTooLarge into a sdkerrors.ErrTxTooLarge which is the same error used here.

[proposal] I wonder if we should use a different error to differentiate that this error is not because the tx exceeds the mempool's configured max tx bytes but instead is the application's max tx bytes. Since the mempool can be configured locally, user's may or may not observe a mempool.ErrTxTooLarge (and therefore sdkerrors.ErrTxTooLarge) depending on which node they submit transactions to.

Copy link
Member Author

Choose a reason for hiding this comment

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

I can address these in a follow-up since I need to link the new versioned constant in ante handler docs anyway

evan-forbes
evan-forbes previously approved these changes Oct 8, 2024
Copy link
Member

@evan-forbes evan-forbes left a comment

Choose a reason for hiding this comment

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

nice, thanks for applying feedback ☺️

👉 👈 in the future, do you mind responding on the threads after the feedback is applied (ideally with a commit 😇 ), so then its even fewer brain cycles to hit the green button. thanks!! 🙏

return next(ctx, tx, simulate)
}

if len(ctx.TxBytes()) > appconsts.MaxTxBytes(ctx.BlockHeader().Version.App) {
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 also delete the MaxTotalBlobSizeDecorator?

are there any scenarios where this context is not set properly? like in prepare proposal?

Copy link
Contributor

Choose a reason for hiding this comment

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

I think we should still keep it because one limits by number of shares and the other number of bytes. I think you pointed out that you could have a PFB that is full of 1 byte blobs which would exceed the share limit but no the byte limit

Copy link
Collaborator

Choose a reason for hiding this comment

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

MaxTotalBlobSizeDecorator only applies to app version 1

		// Ensure that the tx's total blob size is <= the max blob size.
		// Only applies to app version == 1.
		blobante.NewMaxTotalBlobSizeDecorator(blobKeeper),

and since the ante handler added in this PR only applies to app version >= 3, I think we need to retain MaxTotalBlobSizeDecorator.

The ante handler that limits based on number of shares is NewBlobShareDecorator and that one is applied to app version >= 2.

		// Ensure that the blob shares occupied by the tx <= the max shares
		// available to blob data in a data square. Only applies to app version
		// >= 2.
		blobante.NewBlobShareDecorator(blobKeeper),

@celestia-bot celestia-bot requested a review from a team October 9, 2024 16:33
@evan-forbes evan-forbes dismissed their stale review October 9, 2024 17:08

I think we need tests for prepare and process

@ninabarbakadze ninabarbakadze marked this pull request as draft October 9, 2024 18:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Cap the size of the transaction to reduce strain on network
4 participants