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

Problem: mempool nonce logic is not tested #1734

Merged
merged 4 commits into from
Jan 17, 2025

Conversation

yihuang
Copy link
Collaborator

@yihuang yihuang commented Jan 17, 2025

👮🏻👮🏻👮🏻 !!!! REFERENCE THE PROBLEM YOUR ARE SOLVING IN THE PR TITLE AND DESCRIBE YOUR SOLUTION HERE !!!! DO NOT FORGET !!!! 👮🏻👮🏻👮🏻

PR Checklist:

  • Have you read the CONTRIBUTING.md?
  • Does your PR follow the C4 patch requirements?
  • Have you rebased your work on top of the latest master?
  • Have you checked your code compiles? (make)
  • Have you included tests for any non-trivial functionality?
  • Have you checked your code passes the unit tests? (make test)
  • Have you checked your code formatting is correct? (go fmt)
  • Have you checked your basic code style is fine? (golangci-lint run)
  • If you added any dependencies, have you checked they do not contain any known vulnerabilities? (go list -json -m all | nancy sleuth)
  • If your changes affect the client infrastructure, have you run the integration test?
  • If your changes affect public APIs, does your PR follow the C4 evolution of public contracts?
  • If your code changes public APIs, have you incremented the crate version numbers and documented your changes in the CHANGELOG.md?
  • If you are contributing for the first time, please read the agreement in CONTRIBUTING.md now and add a comment to this pull request stating that your PR is in accordance with the Developer's Certificate of Origin.

Thank you for your code, it's appreciated! :)

Summary by CodeRabbit

  • Tests
    • Added a new integration test for mempool nonce validation.
    • Verifies transaction processing and nonce handling across multiple block creations.

@yihuang yihuang requested a review from a team as a code owner January 17, 2025 07:20
@yihuang yihuang requested review from devashishdxt and thomas-nguy and removed request for a team January 17, 2025 07:20
Copy link
Contributor

coderabbitai bot commented Jan 17, 2025

Walkthrough

This pull request introduces a new integration test function test_mempool_nonce in the integration_tests/test_mempool.py file. The test is designed to validate the nonce logic within the transaction checking process after a new block is created. It systematically sends multiple transactions with incrementing nonces, waits for block confirmations, and verifies the transaction count to ensure proper mempool behavior and nonce management.

Changes

File Change Summary
integration_tests/test_mempool.py Added test_mempool_nonce function to test transaction nonce handling and mempool behavior

Sequence Diagram

sequenceDiagram
    participant User
    participant Mempool
    participant Blockchain
    
    User->>Mempool: Send Transaction (Nonce 1)
    User->>Mempool: Send Transaction (Nonce 2)
    User->>Mempool: Send Transaction (Nonce 3)
    
    Blockchain->>Mempool: Create New Block
    Mempool-->>Blockchain: Validate Transactions
    
    User->>Mempool: Send Additional Transactions
    Blockchain->>Mempool: Check Nonce Sequence
Loading

Possibly related PRs

Suggested reviewers

  • thomas-nguy

Poem

🐰 Nonce dance in the mempool's light,
Transactions hop with all their might,
Blocks confirm, numbers align,
Integration test, a rabbit's design!
Mempool logic, crisp and bright 🧪

Finishing Touches

  • 📝 Generate Docstrings (Beta)

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>, 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. (Beta)
  • @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.

CodeRabbit Configuration 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.

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 (6)
integration_tests/test_mempool.py (6)

12-12: Remove unused import.

The send_transaction import is not used in the code.

-    send_transaction,
🧰 Tools
🪛 Ruff (0.8.2)

12-12: .utils.send_transaction imported but unused

Remove unused import: .utils.send_transaction

(F401)


104-114: Consider adding transaction success verification.

The helper function should verify that the transaction was successfully included in a block.

 def send_with_nonce(nonce):
     tx = {
         "to": ADDRS["community"],
         "value": 1,
         "gas": 4121000,
         "data": "0x" + "00" * tx_bytes,
         "nonce": nonce,
     }
     signed = sign_transaction(w3, tx, KEYS["validator"])
     txhash = w3.eth.send_raw_transaction(signed.rawTransaction)
+    receipt = w3.eth.wait_for_transaction_receipt(txhash)
+    assert receipt.status == 1, f"Transaction {txhash.hex()} failed"
     return txhash

108-108: Consider extracting magic number.

The gas limit 4121000 should be defined as a constant with a descriptive name.

+GAS_LIMIT_LARGE_TX = 4121000  # Gas limit for transactions with large data payload
+
 def send_with_nonce(nonce):
     tx = {
         "to": ADDRS["community"],
         "value": 1,
-        "gas": 4121000,
+        "gas": GAS_LIMIT_LARGE_TX,

116-120: Simplify loop using range object.

The loop control variable 'i' is not used in the loop body.

-    for i in range(3):
+    for _ in range(3):
🧰 Tools
🪛 Ruff (0.8.2)

116-116: Loop control variable i not used within loop body

(B007)


121-123: Format expression with consistent spacing.

The expression new_height-height needs consistent spacing around the operator.

-    assert orig_nonce + (new_height-height) == w3.eth.get_transaction_count(sender)
+    assert orig_nonce + (new_height - height) == w3.eth.get_transaction_count(sender)
🧰 Tools
🪛 GitHub Check: Lint python

[failure] 122-122:
./integration_tests/test_mempool.py:122:36: BLK100 Black would make changes.


92-92: Split long line for better readability.

The line exceeds the 88-character limit.

-    then we'll try to send a new tx with local nonce to see if it still get accepted even if
+    then we'll try to send a new tx with local nonce to see if it still
+    get accepted even if
🧰 Tools
🪛 GitHub Check: Lint python

[failure] 92-92:
./integration_tests/test_mempool.py:92:89: E501 line too long (92 > 88 characters)

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 253338e and 63dd7aa.

📒 Files selected for processing (1)
  • integration_tests/test_mempool.py (2 hunks)
🧰 Additional context used
🪛 Ruff (0.8.2)
integration_tests/test_mempool.py

12-12: .utils.send_transaction imported but unused

Remove unused import: .utils.send_transaction

(F401)


116-116: Loop control variable i not used within loop body

(B007)

🪛 GitHub Check: Lint python
integration_tests/test_mempool.py

[failure] 92-92:
./integration_tests/test_mempool.py:92:89: E501 line too long (92 > 88 characters)


[failure] 122-122:
./integration_tests/test_mempool.py:122:36: BLK100 Black would make changes.

⏰ Context from checks skipped due to timeout of 90000ms (11)
  • GitHub Check: integration_tests (gas)
  • GitHub Check: integration_tests (slow)
  • GitHub Check: integration_tests (upgrade)
  • GitHub Check: integration_tests (gov)
  • GitHub Check: integration_tests (ica)
  • GitHub Check: integration_tests (ibc_update_client)
  • GitHub Check: integration_tests (ibc_timeout)
  • GitHub Check: integration_tests (ibc_rly_gas)
  • GitHub Check: integration_tests (ibc_rly_evm)
  • GitHub Check: integration_tests (ibc)
  • GitHub Check: integration_tests (unmarked)
🔇 Additional comments (2)
integration_tests/test_mempool.py (2)

86-94: Well-documented test scenario.

The docstring clearly explains the test's purpose and methodology.

🧰 Tools
🪛 GitHub Check: Lint python

[failure] 92-92:
./integration_tests/test_mempool.py:92:89: E501 line too long (92 > 88 characters)


86-133: Comprehensive test coverage for mempool nonce handling.

The test effectively verifies:

  1. Mempool acceptance of transactions with future nonces
  2. Proper nonce tracking after block creation
  3. Transaction ordering based on nonces
🧰 Tools
🪛 Ruff (0.8.2)

116-116: Loop control variable i not used within loop body

(B007)

🪛 GitHub Check: Lint python

[failure] 92-92:
./integration_tests/test_mempool.py:92:89: E501 line too long (92 > 88 characters)


[failure] 122-122:
./integration_tests/test_mempool.py:122:36: BLK100 Black would make changes.

@yihuang yihuang requested a review from mmsqe January 17, 2025 07:24
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 (6)
integration_tests/test_mempool.py (6)

91-94: Format long lines in docstring.

The docstring lines exceed the 88-character limit. Consider wrapping them for better readability.

-    then we'll try to send a new tx with local nonce to see if it still get accepted even if check-tx state get reset.
+    then we'll try to send a new tx with local nonce to see if it still get accepted
+    even if check-tx state get reset.

-    the expected behavior is when mempool.recheck=true, this test should pass, because although check-tx state get reset when new blocks generated, but recheck logic will bring it back in sync with pending txs, so the client can keep sending new transactions with local nonce.
+    the expected behavior is when mempool.recheck=true, this test should pass, because
+    although check-tx state get reset when new blocks generated, but recheck logic will
+    bring it back in sync with pending txs, so the client can keep sending new
+    transactions with local nonce.
🧰 Tools
🪛 GitHub Check: Lint python

[failure] 91-91:
./integration_tests/test_mempool.py:91:89: E501 line too long (92 > 88 characters)


[failure] 94-94:
./integration_tests/test_mempool.py:94:89: E501 line too long (276 > 88 characters)


103-103: Enhance comment clarity for tx_bytes.

The comment could be more explicit about why this specific size ensures one transaction per block.

-    tx_bytes = 1000000  # can only include one tx at a time
+    tx_bytes = 1000000  # Size large enough to fill block gas limit, ensuring only one tx per block

105-116: Consider using dynamic gas estimation.

The function uses a hardcoded gas value which might be insufficient if the transaction data size or network conditions change.

     def send_with_nonce(nonce):
         tx = {
             "to": ADDRS["community"],
             "value": 1,
-            "gas": 4121000,
             "data": "0x" + "00" * tx_bytes,
             "nonce": nonce,
         }
+        tx["gas"] = w3.eth.estimate_gas(tx)
         signed = sign_transaction(w3, tx, KEYS["validator"])
         txhash = w3.eth.send_raw_transaction(signed.rawTransaction)
         return txhash

117-125: Consider using range(3) more idiomatically.

The loop control variable i is not used within the loop body. Consider using _ to indicate an unused variable.

-    for i in range(3):
+    for _ in range(3):
🧰 Tools
🪛 Ruff (0.8.2)

117-117: Loop control variable i not used within loop body

(B007)

🪛 GitHub Check: Lint python

[failure] 123-123:
./integration_tests/test_mempool.py:123:36: BLK100 Black would make changes.


123-123: Format assertions consistently.

Add spaces around the minus operator in the assertions for better readability.

-    assert orig_nonce + (new_height-height) == w3.eth.get_transaction_count(sender)
+    assert orig_nonce + (new_height - height) == w3.eth.get_transaction_count(sender)

Also applies to: 133-133

🧰 Tools
🪛 GitHub Check: Lint python

[failure] 123-123:
./integration_tests/test_mempool.py:123:36: BLK100 Black would make changes.


85-134: Consider adding error case tests.

The test effectively validates the happy path, but could benefit from additional test cases:

  1. Test with out-of-order nonces
  2. Test with duplicate nonces
  3. Test with gaps in nonce sequence

Would you like me to help implement these additional test cases?

🧰 Tools
🪛 Ruff (0.8.2)

117-117: Loop control variable i not used within loop body

(B007)

🪛 GitHub Check: Lint python

[failure] 91-91:
./integration_tests/test_mempool.py:91:89: E501 line too long (92 > 88 characters)


[failure] 94-94:
./integration_tests/test_mempool.py:94:89: E501 line too long (276 > 88 characters)


[failure] 123-123:
./integration_tests/test_mempool.py:123:36: BLK100 Black would make changes.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 63dd7aa and 21be818.

📒 Files selected for processing (1)
  • integration_tests/test_mempool.py (1 hunks)
🧰 Additional context used
🪛 Ruff (0.8.2)
integration_tests/test_mempool.py

117-117: Loop control variable i not used within loop body

(B007)

🪛 GitHub Check: Lint python
integration_tests/test_mempool.py

[failure] 91-91:
./integration_tests/test_mempool.py:91:89: E501 line too long (92 > 88 characters)


[failure] 94-94:
./integration_tests/test_mempool.py:94:89: E501 line too long (276 > 88 characters)


[failure] 123-123:
./integration_tests/test_mempool.py:123:36: BLK100 Black would make changes.

⏰ Context from checks skipped due to timeout of 90000ms (11)
  • GitHub Check: integration_tests (gas)
  • GitHub Check: integration_tests (slow)
  • GitHub Check: integration_tests (upgrade)
  • GitHub Check: integration_tests (gov)
  • GitHub Check: integration_tests (ica)
  • GitHub Check: integration_tests (ibc_update_client)
  • GitHub Check: integration_tests (ibc_timeout)
  • GitHub Check: integration_tests (ibc_rly_gas)
  • GitHub Check: integration_tests (ibc_rly_evm)
  • GitHub Check: integration_tests (ibc)
  • GitHub Check: integration_tests (unmarked)

Copy link

codecov bot commented Jan 17, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 17.10%. Comparing base (a3c2f70) to head (8a94497).
Report is 28 commits behind head on main.

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main    #1734      +/-   ##
==========================================
+ Coverage   16.87%   17.10%   +0.23%     
==========================================
  Files          72       74       +2     
  Lines        6163     6184      +21     
==========================================
+ Hits         1040     1058      +18     
- Misses       5000     5002       +2     
- Partials      123      124       +1     

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)
integration_tests/test_mempool.py (3)

107-107: Document the rationale for the transaction size.

Add a comment explaining why 1MB was chosen as the transaction size. This helps future maintainers understand the test's constraints.

-    tx_bytes = 1000000  # can only include one tx at a time
+    # Set transaction size to 1MB to ensure only one transaction can be included per block
+    # due to block gas limit constraints
+    tx_bytes = 1000000

113-113: Consider using dynamic gas estimation.

Instead of hardcoding the gas value, consider using w3.eth.estimate_gas(tx) to make the test more resilient to network parameter changes.


121-124: Address unused loop variable.

The loop control variable 'i' is unused. Consider using a clearer construct.

-    for i in range(3):
+    for _ in range(3):
🧰 Tools
🪛 Ruff (0.8.2)

121-121: Loop control variable i not used within loop body

(B007)

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 21be818 and 8a94497.

📒 Files selected for processing (1)
  • integration_tests/test_mempool.py (1 hunks)
🧰 Additional context used
🪛 Ruff (0.8.2)
integration_tests/test_mempool.py

121-121: Loop control variable i not used within loop body

(B007)

⏰ Context from checks skipped due to timeout of 90000ms (11)
  • GitHub Check: integration_tests (gas)
  • GitHub Check: integration_tests (slow)
  • GitHub Check: integration_tests (upgrade)
  • GitHub Check: integration_tests (gov)
  • GitHub Check: integration_tests (ica)
  • GitHub Check: integration_tests (ibc_update_client)
  • GitHub Check: integration_tests (ibc_timeout)
  • GitHub Check: integration_tests (ibc_rly_gas)
  • GitHub Check: integration_tests (ibc_rly_evm)
  • GitHub Check: integration_tests (ibc)
  • GitHub Check: integration_tests (unmarked)
🔇 Additional comments (2)
integration_tests/test_mempool.py (2)

85-99: LGTM! Well-documented test function.

The docstring clearly explains the test's purpose, strategy, and expected behavior. The flaky test decorator with max_runs=3 is appropriate given the nature of mempool tests.


136-139: Strengthen transaction inclusion verification.

The current check might be flaky as transactions could be included in subsequent blocks. Consider implementing a more robust verification:

  1. Wait for transaction receipt instead of just new blocks
  2. Verify transaction inclusion in blocks explicitly

Here's a script to analyze the transaction inclusion patterns in the codebase:

@yihuang yihuang added this pull request to the merge queue Jan 17, 2025
Merged via the queue into crypto-org-chain:main with commit c328720 Jan 17, 2025
35 checks passed
@yihuang yihuang deleted the test-mempool branch January 18, 2025 12:40
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.

2 participants