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): Use CometBFT's ComputeProtoSizeForTxs in defaultTxSelector.SelectTxForProposal #18551

Merged
merged 2 commits into from
Nov 30, 2023

Conversation

lllleven
Copy link
Contributor

@lllleven lllleven commented Nov 23, 2023

Description

ref: #18547

Tx byte size calculations are consistent with cometbft

PrepareProposalHandler SelectTxForProposal Tx bytes the inconsistent calculation methods are causing block production failures.


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
  • run make lint and make test
  • 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)

Summary by CodeRabbit

  • Bug Fixes

    • Fixed an issue in the transaction selection process where the calculation of transaction byte size was inconsistent with CometBFT standards.
  • Documentation

    • Updated the changelog to reflect the fix in the transaction byte size calculation method.

@lllleven lllleven requested a review from a team as a code owner November 23, 2023 13:14
Copy link
Contributor

coderabbitai bot commented Nov 23, 2023

Walkthrough

Walkthrough

The changes involve a fix to the SelectTxForProposal function in the baseapp module, ensuring the method for calculating transaction byte size aligns with CometBFT's approach. This update is likely to enhance the accuracy and consistency of transaction size computation during the proposal selection process. The modifications include adjustments to the function itself and corresponding test cases, as well as documentation updates in the CHANGELOG.md.

Changes

File Change Summary
baseapp/abci_utils.go Updated SelectTxForProposal function to use tmtypes.ComputeProtoSizeForTxs for transaction size calculation. Added tmtypes package import.
baseapp/abci_utils_test.go Added new test case and updated existing ones to reflect changes in transaction size calculation. Introduced txDataSize variable and adjusted MaxTxBytes.
CHANGELOG.md Documented the fix for transaction byte size calculation in SelectTxForProposal, ensuring consistency with CometBFT.

Tips

Chat with CodeRabbit Bot (@coderabbitai)

  • If you reply to a review comment from CodeRabbit, the bot will automatically respond.
  • To engage with CodeRabbit bot directly around the specific lines of code in the PR, mention @coderabbitai in your review comment
  • Note: Review comments are made on code diffs or files, not on the PR overview.
  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

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.

Note: For conversation with the bot, please use the review comments on code diffs or files.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • The JSON schema for the configuration file is available here.
  • 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/coderabbit-overrides.v2.json

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

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 875a71c and eb5eb16.
Files selected for processing (1)
  • baseapp/abci_utils.go (2 hunks)
Additional comments: 1
baseapp/abci_utils.go (1)
  • 10-16: The import of the tmtypes package from github.com/cometbft/cometbft/types is correctly added to use the ComputeProtoSizeForTxs function. Ensure that there are no existing functions within the Cosmos SDK that already provide the same functionality to avoid redundancy.

Comment on lines 369 to 375
}

func (ts *defaultTxSelector) SelectTxForProposal(_ context.Context, maxTxBytes, maxBlockGas uint64, memTx sdk.Tx, txBz []byte) bool {
txSize := uint64(len(txBz))
txSize := uint64(tmtypes.ComputeProtoSizeForTxs([]tmtypes.Tx{txBz}))

var txGasLimit uint64
if memTx != nil {
Copy link
Contributor

Choose a reason for hiding this comment

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

The SelectTxForProposal function now uses tmtypes.ComputeProtoSizeForTxs to compute the size of transactions, which aligns with the cometbft calculation methods. However, there is a potential issue with the check for memTx != nil after the txSize has been calculated. If memTx is nil, the txGasLimit will remain uninitialized, potentially leading to incorrect behavior when adding the transaction to the proposal. This should be handled before attempting to cast memTx to GasTx.

- txSize := uint64(tmtypes.ComputeProtoSizeForTxs([]tmtypes.Tx{txBz}))
- 
- var txGasLimit uint64
- if memTx != nil {
+ txSize := uint64(tmtypes.ComputeProtoSizeForTxs([]tmtypes.Tx{txBz}))
+ var txGasLimit uint64
+ if memTx == nil {
+     return true // or handle the error appropriately
+ }

Commitable 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
}
func (ts *defaultTxSelector) SelectTxForProposal(_ context.Context, maxTxBytes, maxBlockGas uint64, memTx sdk.Tx, txBz []byte) bool {
txSize := uint64(len(txBz))
txSize := uint64(tmtypes.ComputeProtoSizeForTxs([]tmtypes.Tx{txBz}))
var txGasLimit uint64
if memTx != nil {
}
func (ts *defaultTxSelector) SelectTxForProposal(_ context.Context, maxTxBytes, maxBlockGas uint64, memTx sdk.Tx, txBz []byte) bool {
txSize := uint64(tmtypes.ComputeProtoSizeForTxs([]tmtypes.Tx{txBz}))
var txGasLimit uint64
if memTx == nil {
return true // or handle the error appropriately
}

@julienrbrt julienrbrt changed the title fix: defaultTxSelector fix(baseapp): defaultTxSelector Nov 23, 2023
@facundomedica
Copy link
Member

This looks good to me but I would love to see if there's a way to do this without having to use more Comet methods. Or maybe we do this now but we think about having a configurable "TxSize" per consensus engine

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 UI

Commits Files that changed from the base of the PR and between eb5eb16 and b09ea24.
Files selected for processing (1)
  • CHANGELOG.md (1 hunks)
Additional comments: 1
CHANGELOG.md (1)
  • 77-77:
    The changelog entry is clear and accurately reflects the changes made in the pull request.

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 UI

Commits Files that changed from the base of the PR and between 875a71c and d43715d.
Files selected for processing (2)
  • CHANGELOG.md (1 hunks)
  • baseapp/abci_utils.go (2 hunks)
Additional comments: 3
CHANGELOG.md (1)
baseapp/abci_utils.go (2)
  • 10-16:
    The import of tmtypes from github.com/cometbft/cometbft/types is correctly added to use ComputeProtoSizeForTxs.

  • 369-375:
    The issue with the check for memTx != nil after the txSize has been calculated is still present. If memTx is nil, the txGasLimit will remain uninitialized, potentially leading to incorrect behavior when adding the transaction to the proposal. This should be handled before attempting to cast memTx to GasTx.

@@ -368,7 +369,7 @@ func (ts *defaultTxSelector) Clear() {
}

func (ts *defaultTxSelector) SelectTxForProposal(_ context.Context, maxTxBytes, maxBlockGas uint64, memTx sdk.Tx, txBz []byte) bool {
txSize := uint64(len(txBz))
txSize := uint64(tmtypes.ComputeProtoSizeForTxs([]tmtypes.Tx{txBz}))
Copy link
Member

Choose a reason for hiding this comment

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

Can you add a test?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Copy link
Member

Choose a reason for hiding this comment

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

Sure, adding a case there works!

@@ -10,6 +10,7 @@ import (
cryptoenc "github.com/cometbft/cometbft/crypto/encoding"
cmtprotocrypto "github.com/cometbft/cometbft/proto/tendermint/crypto"
cmtproto "github.com/cometbft/cometbft/proto/tendermint/types"
tmtypes "github.com/cometbft/cometbft/types"
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
tmtypes "github.com/cometbft/cometbft/types"
cmttypes "github.com/cometbft/cometbft/types"

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 UI

Commits Files that changed from the base of the PR and between 875a71c and 8f5efb1.
Files selected for processing (2)
  • CHANGELOG.md (1 hunks)
  • baseapp/abci_utils.go (2 hunks)
Additional comments: 3
CHANGELOG.md (1)
baseapp/abci_utils.go (2)
  • 10-16:
    The import of cmttypes is correctly added to support the new transaction size calculation method.

  • 369-375:
    The change to use cmttypes.ComputeProtoSizeForTxs for calculating txSize is correctly implemented and aligns with CometBFT standards.

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

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 875a71c and 029bb69.
Files selected for processing (3)
  • CHANGELOG.md (1 hunks)
  • baseapp/abci_utils.go (2 hunks)
  • baseapp/abci_utils_test.go (5 hunks)
Additional comments: 7
baseapp/abci_utils.go (2)
  • 10-16:
    The import of cmttypes from "github.com/cometbft/cometbft/types" is correctly added to facilitate the use of ComputeProtoSizeForTxs.

  • 369-375:
    The change to calculate txSize using cmttypes.ComputeProtoSizeForTxs aligns with the CometBFT standards as described in the summary.

baseapp/abci_utils_test.go (5)
  • 3-8:
    The import alias cmttypes for the package github.com/cometbft/cometbft/types is correctly added to facilitate the use of the ComputeProtoSizeForTxs function.

  • 311-315:
    The test case now correctly calculates the transaction size using ComputeProtoSizeForTxs from the cmttypes package, aligning with the CometBFT standards.

  • 335-360: > Note: This review was outside the patches, so it was mapped to the patch with the greatest overlap. Original lines [335-367]

The test cases "small max tx bytes" and "small max gas" have been updated with new MaxTxBytes values to accommodate the changes in transaction size calculation.

  • 335-360: > Note: This review was outside the patches, so it was mapped to the patch with the greatest overlap. Original lines [342-368]

New test cases "large max tx bytes" and "large max tx bytes len calculation" have been added to validate the transaction size calculation for different MaxTxBytes limits.

  • 372-378:
    The loop in the test case ensures that the transaction selector is cleared each time, which is a good practice to avoid state leakage between iterations.

Copy link
Contributor

@alexanderbez alexanderbez left a comment

Choose a reason for hiding this comment

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

We do not use the CometBFT ComputeProtoSizeForTxs API anywhere in the codebase. I'd like to better understand what and where the failures are for you. Note, we use len(txBz) as the tx in many places, e.g. gas computation.


EDIT: Apologies, I somehow missed the created issue for this. I understand now 👍

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

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 029bb69 and ff7f1fa.
Files selected for processing (1)
  • CHANGELOG.md (1 hunks)

@@ -78,6 +78,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
* (client/server) [#18345](https://github.com/cosmos/cosmos-sdk/pull/18345) Consistently set viper prefix in client and server. It defaults for the binary name for both client and server.
* (simulation) [#17911](https://github.com/cosmos/cosmos-sdk/pull/17911) Fix all problems with executing command `make test-sim-custom-genesis-fast` for simulation test.
* (simulation) [#18196](https://github.com/cosmos/cosmos-sdk/pull/18196) Fix the problem of `validator set is empty after InitGenesis` in simulation test.
* (baseapp) [#18551](https://github.com/cosmos/cosmos-sdk/pull/18551) Fix SelectTxForProposal the calculation method of tx bytes size is inconsistent with CometBFT
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 could be more descriptive by mentioning the specific method tmtypes.ComputeProtoSizeForTxs used for transaction size calculation to align with CometBFT standards.

@tac0turtle
Copy link
Member

@LX-Xiang could you run make lint-fix then we can merge the pr

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 UI

Commits Files that changed from the base of the PR and between ff7f1fa and 76b219b.
Files selected for processing (1)
  • CHANGELOG.md (1 hunks)
Additional comments: 1
CHANGELOG.md (1)
  • 83-83: The changelog entry could be more descriptive by mentioning the specific method tmtypes.ComputeProtoSizeForTxs used for transaction size calculation to align with CometBFT standards.

@lllleven
Copy link
Contributor Author

@tac0turtle i running make lint only one issues , but this issues not committed by me
image

@julienrbrt
Copy link
Member

@tac0turtle i running make lint only one issues , but this issues not committed by me image

Yes, you should run make lint-fix:
image

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 UI

Commits Files that changed from the base of the PR and between 7aa5635 and e75ae01.
Files selected for processing (3)
  • CHANGELOG.md (1 hunks)
  • baseapp/abci_utils.go (2 hunks)
  • baseapp/abci_utils_test.go (5 hunks)
Files skipped from review due to trivial changes (1)
  • CHANGELOG.md
Additional comments: 8
baseapp/abci_utils.go (1)
  • 369-375: The use of ComputeProtoSizeForTxs from the cmttypes package for calculating transaction size is a good change for consistency with CometBFT standards. However, ensure that the ComputeProtoSizeForTxs function is thoroughly tested, especially since transaction size calculation is critical for block production.
baseapp/abci_utils_test.go (7)
  • 8-14: The import alias cmttypes has been removed in the diff summary, but it is still present in the hunk. Please ensure that the code reflects the intended changes.

  • 314-315: The update to use types.ComputeProtoSizeForTxs for calculating txDataSize is consistent with the summary and the hunk. Ensure that the types package provides the same functionality as the cmttypes package to avoid any unexpected behavior.

  • 350-356: The summary mentions the addition of a new test case "large max tx bytes len calculation," which seems to be a new scenario within the existing test cases. Ensure that this new scenario is adequately tested and that the expected behavior aligns with the changes made to the transaction byte size calculation.

  • 338-338: The MaxTxBytes value in the test case "small max gas" has been updated. Verify that this value is consistent with the expected transaction size after the changes to the transaction byte size calculation.

  • 346-346: The MaxTxBytes value in the test case "large max tx bytes" has been updated. Verify that this value is consistent with the expected transaction size after the changes to the transaction byte size calculation.

  • 354-354: The MaxTxBytes value in the test case "large max tx bytes len calculation" has been updated. Verify that this value is consistent with the expected transaction size after the changes to the transaction byte size calculation.

  • 366-366: The MaxTxBytes value in the test case "max gas and tx bytes" has been updated. Verify that this value is consistent with the expected transaction size after the changes to the transaction byte size calculation.

@lllleven
Copy link
Contributor Author

@julienrbrt sorry, I didn't pay attention to this modification,I recommitted

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 UI

Commits Files that changed from the base of the PR and between e75ae01 and 600590f.
Files selected for processing (1)
  • CHANGELOG.md (1 hunks)

@julienrbrt julienrbrt changed the title fix(baseapp): defaultTxSelector fix(baseapp): Use CometBFT's ComputeProtoSizeForTxs in defaultTxSelector.SelectTxForProposal Nov 30, 2023
@julienrbrt julienrbrt added this pull request to the merge queue Nov 30, 2023
Merged via the queue into cosmos:main with commit d52bd92 Nov 30, 2023
57 of 59 checks passed
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.

Block Proposal Failure(proposal block parts exceeds maximum block bytes)
5 participants