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: getFilterLog issue #1337

Merged
merged 6 commits into from
Aug 12, 2024
Merged

Conversation

roninjin10
Copy link
Collaborator

@roninjin10 roninjin10 commented Jul 25, 2024

Description

Concise description of proposed changes

Testing

Explain the quality checks that have been done on the code changes

Additional Information

Your ENS/address:

Summary by CodeRabbit

  • New Features

    • Introduced a new filter handler for Ethereum logs, allowing users to create custom log filters based on various parameters.
    • Added a utility function to generate unique hexadecimal identifiers.
    • Implemented a function to parse block tags, enhancing usability in blockchain applications.
  • Bug Fixes

    • Improved error handling in the filter logging procedure to ensure structured responses during exceptions.
  • Tests

    • Added unit tests for the new identifier generation and block tag parsing functions to ensure reliability and expected behavior.

Copy link

vercel bot commented Jul 25, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

1 Skipped Deployment
Name Status Preview Comments Updated (UTC)
tevm-monorepo-tevm ⬜️ Ignored (Inspect) Aug 12, 2024 1:50am

Copy link

changeset-bot bot commented Jul 25, 2024

⚠️ No Changeset found

Latest commit: dcc3f73

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

Copy link
Contributor

coderabbitai bot commented Jul 25, 2024

Walkthrough

This update introduces significant enhancements to the Ethereum log filtering mechanism. A new ethNewFilterHandler function simplifies the creation of log filters, improving usability. Additional utility functions like generateRandomId and parseBlockTag enhance the robustness of the filtering process. Unit tests have been added to ensure reliability, while existing procedures have been refactored for clarity and efficiency. Overall, these changes streamline filter management and error handling within the Ethereum client.

Changes

Files Change Summary
packages/actions/src/eth/ethNewFilterHandler.js Introduced ethNewFilterHandler for creating Ethereum log filters with improved criteria handling.
packages/actions/src/eth/index.ts Exported the new ethNewFilterHandler for broader accessibility.
packages/actions/src/eth/utils/generateRandomId.js Added generateRandomId function for generating unique hex IDs using the Web Crypto API.
packages/actions/src/eth/utils/generateRandomId.spec.ts Implemented unit tests for generateRandomId, verifying format and uniqueness.
packages/actions/src/eth/utils/parseBlockTag.js Created parseBlockTag function to handle various block tag formats efficiently.
packages/actions/src/eth/utils/parseBlockTag.spec.ts Added tests for parseBlockTag, covering multiple input scenarios.
packages/procedures/src/eth/ethGetFilterLogsProcedure.js Added error logging to the ethGetFilterLogsProcedure.
packages/procedures/src/eth/ethNewFilterProcedure.js Refactored to use ethNewFilterHandler, simplifying log filter setup and improving error handling.

Sequence Diagram(s)

sequenceDiagram
    participant Client
    participant FilterHandler
    participant LogManager
    participant BlockManager

    Client->>FilterHandler: createFilter(topics, address, fromBlock, toBlock)
    FilterHandler->>BlockManager: resolveBlocks(fromBlock, toBlock)
    BlockManager-->>FilterHandler: return resolved blocks
    FilterHandler->>LogManager: setupListener()
    LogManager-->>FilterHandler: return filter ID
    FilterHandler-->>Client: return filter ID
Loading

🐇 In the meadow, logs do dance,
With new handlers, take a chance.
Random IDs hop and play,
In Ethereum's bright ballet.
So let us cheer, both near and far,
For code is magic, like a shooting star! ✨


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.
    • @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 as 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.

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

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
Collaborator Author

This stack of pull requests is managed by Graphite. Learn more about stacking.

Join @roninjin10 and the rest of your teammates on Graphite Graphite

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

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 528204b and 0b15e07.

Files selected for processing (9)
  • packages/actions/src/eth/ethNewFilterHandler.js (1 hunks)
  • packages/actions/src/eth/index.ts (1 hunks)
  • packages/actions/src/eth/utils/generateRandomId.js (1 hunks)
  • packages/actions/src/eth/utils/generateRandomId.spec.ts (1 hunks)
  • packages/actions/src/eth/utils/parseBlockTag.js (1 hunks)
  • packages/actions/src/eth/utils/parseBlockTag.spec.ts (1 hunks)
  • packages/procedures/src/eth/ethGetFilterLogsProcedure.js (1 hunks)
  • packages/procedures/src/eth/ethGetFilterLogsProcedure.spec.ts (1 hunks)
  • packages/procedures/src/eth/ethNewFilterProcedure.js (2 hunks)
Additional comments not posted (22)
packages/actions/src/eth/utils/generateRandomId.js (1)

1-8: LGTM! But verify the usage of crypto.getRandomValues.

The code changes are approved.

However, ensure that crypto.getRandomValues is supported in all target environments.

packages/actions/src/eth/utils/generateRandomId.spec.ts (1)

1-15: LGTM! But verify the test coverage.

The code changes are approved.

However, ensure that the tests cover all edge cases and potential issues.

packages/actions/src/eth/utils/parseBlockTag.js (1)

1-14: LGTM! But verify the usage of hexToBigInt.

The code changes are approved.

However, ensure that hexToBigInt is correctly used and tested.

Verification successful

The usage of hexToBigInt is correct and well-tested.

  • The function hexToBigInt is correctly defined and exported in @tevm/utils.
  • It is used consistently across various files in the codebase.
  • There are sufficient test cases covering its functionality.
Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify the usage of `hexToBigInt` in the codebase.

# Test: Search for the usage of `hexToBigInt`. Expect: Only valid usages.
rg --type js --type ts $'hexToBigInt'

Length of output: 34564


Script:

#!/bin/bash
# 1. Verify the definition and export of `hexToBigInt` in `@tevm/utils`.
ast-grep --lang javascript --pattern 'export function hexToBigInt($_) { $$$ }' 

# 2. Check the usage of `hexToBigInt` in various files to ensure it is used correctly.
# We will search for the function calls to `hexToBigInt` across the codebase.
rg --type js --type ts 'hexToBigInt(' -A 3

# 3. Verify that there are sufficient test cases for `hexToBigInt`.
# We will look for test cases that involve `hexToBigInt`.
rg --type js --type ts 'hexToBigInt' | grep -i 'test'

Length of output: 5799

packages/actions/src/eth/index.ts (1)

4-4: LGTM!

The new export statement for ethNewFilterHandler.js is correctly added and enhances the module's functionality.

packages/procedures/src/eth/ethNewFilterProcedure.js (3)

1-1: Import statement is correct.

The import statement correctly brings in ethNewFilterHandler from @tevm/actions.


11-16: Function implementation is streamlined and efficient.

The function implementation is streamlined, utilizing ethNewFilterHandler to handle the request. This improves readability and maintainability.


18-28: Error handling is robust.

The error handling is robust, logging the error and returning a structured error response in JSON-RPC format.

packages/actions/src/eth/utils/parseBlockTag.spec.ts (7)

1-3: Import statements are correct.

The import statements correctly bring in required modules and functions.


5-10: Test case for parsing hex block numbers to bigint is correct.

The test case correctly verifies that hex block numbers are parsed to bigint.


12-16: Test case for returning block hash as is correct.

The test case correctly verifies that block hashes are returned as is.


18-24: Test case for returning special block tags as is correct.

The test case correctly verifies that special block tags are returned as is.


26-30: Test case for returning block number as bigint for valid hex strings is correct.

The test case correctly verifies that valid hex strings are parsed to bigint.


32-36: Test case for handling block tag as a number string correctly is correct.

The test case correctly verifies that block tags as number strings are parsed correctly.


38-42: Test case for returning blockTag unchanged if it is a non-hex string is correct.

The test case correctly verifies that non-hex strings are returned unchanged.

packages/procedures/src/eth/ethGetFilterLogsProcedure.spec.ts (3)

1-9: Good use of imports and setup.

The imports cover necessary utilities and components for the test. The setup is clear and prepares the environment for testing.


11-46: Well-structured test setup.

The beforeEach block ensures that the client is created and the initial conditions are set up before each test. This promotes test isolation and reliability.


48-83: Comprehensive test case for log retrieval.

The test case verifies the creation of a filter, execution of a transaction, mining of a block, and retrieval of logs. The use of inline snapshots for result verification is a good practice.

packages/actions/src/eth/ethNewFilterHandler.js (5)

1-5: Good use of imports and utility functions.

The imports cover necessary utilities and error classes. The use of utility functions like generateRandomId and parseBlockTag promotes code reuse and readability.


7-9: Clear type definition for errors.

The type definition for EthNewFilterError is clear and helps in understanding the possible errors that can be thrown by the handler.


11-15: Well-defined handler function signature.

The handler function signature is clear and includes the necessary type annotations, which improve code readability and maintainability.


17-49: Robust block retrieval logic.

The logic for retrieving blocks based on the tag is robust and handles various block tags, including special tags like 'latest' and 'earliest'. The error handling for invalid and unknown block tags is appropriate.


51-104: Comprehensive filter creation and log handling.

The handler creates a filter, registers a listener for new logs, and populates the filter with past logs. This comprehensive approach ensures that the filter is correctly set up and ready to use.

@@ -50,6 +50,7 @@ export const ethGetFilterLogsProcedure = (client) => {
result: jsonRpcResult,
}
} catch (e) {
console.log(e)
Copy link
Contributor

Choose a reason for hiding this comment

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

Avoid using console.log for error logging in production code.

Using console.log for error logging is not recommended in production environments as it can expose sensitive information and is not a robust logging solution. Consider using a proper logging library that supports different log levels and better error tracking.

- console.log(e)
+ // Consider using a proper logging library, e.g., winston or pino.
+ logger.error(e)
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. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
console.log(e)
// Consider using a proper logging library, e.g., winston or pino.
logger.error(e)

@roninjin10 roninjin10 force-pushed the 07-24-_bug_fix_getfilterlog_issue branch from 0b15e07 to 31ece76 Compare August 11, 2024 21:42
@roninjin10 roninjin10 force-pushed the 07-24-_bug_fix_getfilterlog_issue branch from 3205da2 to 044e7a1 Compare August 12, 2024 01:14
@roninjin10 roninjin10 merged commit 44f32db into main Aug 12, 2024
9 checks passed
@roninjin10 roninjin10 deleted the 07-24-_bug_fix_getfilterlog_issue branch August 12, 2024 01:55
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.

1 participant