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

Simplify usage of FixedHash field on Block type #520

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

m-Peter
Copy link
Collaborator

@m-Peter m-Peter commented Sep 5, 2024

Description

It is safer/simpler to use the gethCommon.Hash type for FixedHash, instead of a *string, all we have to do is check that FixedHash is not the empty hash.
The problem is that this change will require a re-index, so I am not sure if it is worth it.
DO NOT MERGE UNLESS REINDEX


For contributor use:

  • Targeted PR against master branch
  • Linked to Github issue with discussion and accepted design OR link to spec that describes this work.
  • Code follows the standards mentioned here.
  • Updated relevant documentation
  • Re-reviewed Files changed in the Github PR explorer
  • Added appropriate labels

Summary by CodeRabbit

  • New Features

    • Improved handling of block hashes, enhancing performance and clarity.
    • Updated block size assertions in tests to reflect changes in blockchain data.
  • Bug Fixes

    • Enhanced validation of block hashes in test functions to ensure accuracy and reliability.

@m-Peter m-Peter added this to the Flow-EVM-M2 milestone Sep 5, 2024
@m-Peter m-Peter self-assigned this Sep 5, 2024
Copy link
Contributor

coderabbitai bot commented Sep 5, 2024

Walkthrough

The changes involve updates to the Block structure in models/block.go, specifically altering the FixedHash field from a pointer to a string to a value of type gethCommon.Hash. This simplifies hash handling and improves method logic. Corresponding test files were updated to ensure proper validation of block hashes, and an adjustment was made to the expected block size in a JavaScript test file.

Changes

Files Change Summary
models/block.go, models/block_test.go Modified Block structure to change FixedHash from *string to gethCommon.Hash; updated related methods and tests for hash validation.
tests/web3js/eth_non_interactive_test.js Updated expected value for block.size from 3995n to 4028n in a test case.

Sequence Diagram(s)

sequenceDiagram
    participant Block as Block
    participant Hash as Hash
    participant Test as Test

    Block->>Hash: Set FixedHash to gethCommon.Hash
    Hash-->>Block: Return hash value
    Test->>Block: Validate block hash
    Test-->>Test: Assert hash matches expected value
Loading

🐰 In the meadow where bunnies play,
A change was made, hip-hip-hooray!
No more pointers, just a hash,
Simplified logic in a flash!
With tests updated, all is bright,
Hopping forward, what a delight! 🌼✨


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

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

Outside diff range, codebase verification and nitpick comments (2)
models/block.go (2)

49-50: Clarification on FixedHash field documentation.

The comment above the FixedHash field explains the purpose of this field well, ensuring that the hash calculated with the previous format is preserved. However, it might be beneficial to clarify further why this specific implementation choice (using gethCommon.Hash instead of a pointer) improves safety and simplicity, as mentioned in the PR summary.


121-121: Ensure consistency in hash assignment during legacy block decoding.

The assignment of FixedHash directly from the block.Hash in the decodeLegacyBlockEvent function is a good use of the new FixedHash type. This ensures that the hash is preserved accurately during the decoding of legacy events. It would be beneficial to add a comment here explaining that this change is part of the transition to the new hash handling method.

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 808db37 and d4282c7.

Files selected for processing (3)
  • models/block.go (9 hunks)
  • models/block_test.go (4 hunks)
  • tests/web3js/eth_non_interactive_test.js (1 hunks)
Additional comments not posted (5)
models/block.go (2)

59-60: Approval of the updated hash check logic.

The updated logic in the Hash() method to check if FixedHash is not the empty hash (gethCommon.HexToHash("0x0")) is a clear improvement over the previous need to check for nil and an empty string. This change simplifies the code and reduces potential errors related to nil dereferencing.


175-175: Validate the new FixedHash assignment in decodeBlockBreakingChanges.

The assignment of FixedHash to blockHash in the decodeBlockBreakingChanges function is a crucial part of ensuring that blocks decoded from older formats retain their original hash. This change aligns with the PR's goal of simplifying hash handling. Ensure that this change is thoroughly tested, especially since it involves historical data and could impact data integrity during re-indexing.

Verification successful

The FixedHash assignment is properly tested in decodeBlockBreakingChanges. The Test_FixedHashBlock function in models/block_test.go ensures that the FixedHash is retained correctly during encoding and decoding, addressing the review comment's concern about data integrity.

  • models/block_test.go: Test_FixedHashBlock verifies the FixedHash functionality.
Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify that the `FixedHash` assignment in `decodeBlockBreakingChanges` retains the original hash.

# Test: Search for the function usage. Expect: Only occurrences of the new signature.
rg --type go -A 5 $'decodeBlockBreakingChanges'

Length of output: 763


Script:

#!/bin/bash
# Search for test files or functions that involve `decodeBlockBreakingChanges` or `FixedHash`.
rg --type go 'decodeBlockBreakingChanges|FixedHash' --glob '*_test.go'

Length of output: 175


Script:

#!/bin/bash
# Extract and review the `Test_FixedHashBlock` function from `models/block_test.go`.
rg --type go -A 20 'func Test_FixedHashBlock' models/block_test.go

Length of output: 513

models/block_test.go (2)

35-42: Improved hash comparison in tests.

The update to use gethCommon.HexToHash for direct hash comparison instead of converting to strings enhances type safety and clarity in the test assertions.


77-77: Enhanced handling and testing of FixedHash.

The changes to directly use gethCommon.Hash for FixedHash and update the assertions accordingly improve the clarity and efficiency of the tests. This aligns well with the PR's objectives to simplify and enhance safety in hash handling.

Also applies to: 82-82, 91-91, 102-102

tests/web3js/eth_non_interactive_test.js (1)

28-28: Verify the updated block size expectation.

The change in the expected block size from 3995n to 4028n needs verification to ensure it aligns with the updated blockchain logic or data changes. It's crucial to confirm that this new expected size is accurate based on the modifications made to the blockchain's behavior.

Run the following script to verify the block size calculation:

models/block.go Show resolved Hide resolved
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

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between d4282c7 and 65ec134.

Files selected for processing (3)
  • models/block.go (9 hunks)
  • models/block_test.go (4 hunks)
  • tests/web3js/eth_non_interactive_test.js (1 hunks)
Files skipped from review as they are similar to previous changes (3)
  • models/block.go
  • models/block_test.go
  • tests/web3js/eth_non_interactive_test.js

@sideninja
Copy link
Member

Yeah this is nicer, I did have this approach but with a pointer, I should remove the pointer. I think this is good but I wouldn't do it unless we plan a reindex anyway. So we can keep this in draft until then and add a comment in the description
DO NOT MERGE UNLESS REINDEX

@m-Peter m-Peter marked this pull request as draft September 5, 2024 11:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: 👀 In Review
Development

Successfully merging this pull request may close these issues.

2 participants