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

chore: remove ethers keccak #2003

Merged
merged 5 commits into from
Feb 4, 2025
Merged

Conversation

gabriel-aranha-cw
Copy link
Contributor

@gabriel-aranha-cw gabriel-aranha-cw commented Feb 4, 2025

PR Type

Enhancement


Description

  • Replace ethers_core keccak256 with alloy_primitives keccak256

  • Update hash calculation in BlockNumber struct

  • Add FixedBytes<32> conversion for SlotIndex

  • Improve code consistency and dependencies


Changes walkthrough 📝

Relevant files
Enhancement
block_number.rs
Update keccak256 implementation for BlockNumber                   

src/eth/primitives/block_number.rs

  • Replace ethers_core keccak256 with alloy_primitives keccak256
  • Update hash calculation method in BlockNumber struct
  • Remove ethers_core import
  • +2/-2     
    slot_index.rs
    Enhance SlotIndex with alloy_primitives and new conversion

    src/eth/primitives/slot_index.rs

  • Add alloy_primitives keccak256 and FixedBytes imports
  • Remove ethers_core keccak256 import
  • Implement From> for SlotIndex
  • +8/-1     

    Need help?
  • Type /help how to ... in the comments thread for any questions about PR-Agent usage.
  • Check out the documentation for more information.
  • Copy link

    github-actions bot commented Feb 4, 2025

    PR Reviewer Guide 🔍

    (Review updated until commit 808d5a2)

    Here are some key observations to aid the review process:

    ⏱️ Estimated effort to review: 2 🔵🔵⚪⚪⚪
    🧪 No relevant tests
    🔒 No security concerns identified
    ⚡ Recommended focus areas for review

    Potential Performance Impact

    The change from ethers_core::utils::keccak256 to alloy_primitives::keccak256 might have performance implications. Verify that the new implementation maintains or improves performance.

    Hash::new(*keccak256(<[u8; 8]>::from(*self)))

    Copy link

    github-actions bot commented Feb 4, 2025

    PR Code Suggestions ✨

    Latest suggestions up to 808d5a2
    Explore these optional code suggestions:

    CategorySuggestion                                                                                                                                    Score
    Possible issue
    Use fallible conversion for safety

    The new From<FixedBytes<32>> implementation for SlotIndex might lead to unexpected behavior if the
    FixedBytes<32> value doesn't fit into the SlotIndex type. Consider adding a check or
    using a fallible conversion method.

    src/eth/primitives/slot_index.rs [71-75]

    -impl From<FixedBytes<32>> for SlotIndex {
    -    fn from(value: FixedBytes<32>) -> Self {
    -        Self::from(value.0)
    +impl TryFrom<FixedBytes<32>> for SlotIndex {
    +    type Error = ConversionError;
    +    
    +    fn try_from(value: FixedBytes<32>) -> Result<Self, Self::Error> {
    +        // Add appropriate checks here
    +        Ok(Self::from(value.0))
         }
     }
    Suggestion importance[1-10]: 7

    Why: The suggestion to use TryFrom instead of From for FixedBytes<32> to SlotIndex conversion is valuable. It adds a layer of safety by allowing for potential conversion failures, which is important when dealing with fixed-size types.

    7
    Handle potential conversion errors

    Consider handling potential errors when converting the block number to a byte array.
    The current implementation assumes the conversion will always succeed, which may not
    be the case for all possible block numbers.

    src/eth/primitives/block_number.rs [26-28]

    -pub fn hash(&self) -> Hash {
    -    Hash::new(*keccak256(<[u8; 8]>::from(*self)))
    +pub fn hash(&self) -> Result<Hash, ConversionError> {
    +    let bytes = <[u8; 8]>::try_from(*self)?;
    +    Ok(Hash::new(*keccak256(bytes)))
     }
    Suggestion importance[1-10]: 5

    Why: The suggestion proposes error handling for the conversion, which could improve robustness. However, the current implementation using From trait suggests that the conversion is infallible, reducing the urgency of this change.

    5

    Previous suggestions

    Suggestions up to commit af91a35
    CategorySuggestion                                                                                                                                    Score
    Possible issue
    Verify keccak256 return type compatibility

    Ensure that the new keccak256 function from alloy_primitives returns a compatible
    type with the Self::from() method. The change from hashed_bytes to hashed_bytes.0
    suggests a potential type mismatch.

    src/eth/primitives/slot_index.rs [35-36]

     let hashed_bytes = keccak256(mapping_index_bytes);
    -Self::from(hashed_bytes.0)
    +Self::from(hashed_bytes)
    Suggestion importance[1-10]: 8

    Why: The suggestion correctly identifies a potential type mismatch issue after switching to the new keccak256 function. This is a critical change that could lead to compilation errors or runtime issues if not addressed properly.

    8

    @gabriel-aranha-cw
    Copy link
    Contributor Author

    /benchmark

    @gabriel-aranha-cw gabriel-aranha-cw marked this pull request as ready for review February 4, 2025 20:41
    Copy link

    github-actions bot commented Feb 4, 2025

    Persistent review updated to latest commit 808d5a2

    @gabriel-aranha-cw
    Copy link
    Contributor Author

    Benchmark:
    Run ID: bench-1915205002

    Git Info:

    Configuration:

    • Target Account Strategy: Default

    RPS Stats: Max: 2062.00, Min: 1279.00, Avg: 1850.24, StdDev: 79.42
    TPS Stats: Max: 2049.00, Min: 1620.00, Avg: 1796.15, StdDev: 91.12

    Plot: View Plot

    @gabriel-aranha-cw gabriel-aranha-cw merged commit 29f6841 into main Feb 4, 2025
    40 checks passed
    @gabriel-aranha-cw gabriel-aranha-cw deleted the chore-remove-ethers-keccak branch February 4, 2025 20:54
    @gabriel-aranha-cw
    Copy link
    Contributor Author

    Final benchmark:
    Run ID: bench-828692176

    Git Info:

    Configuration:

    • Target Account Strategy: Default

    RPS Stats: Max: 1992.00, Min: 1035.00, Avg: 1787.10, StdDev: 96.12
    TPS Stats: Max: 2001.00, Min: 1548.00, Avg: 1735.61, StdDev: 100.79

    Plot: View Plot

    @gabriel-aranha-cw
    Copy link
    Contributor Author

    #1963

    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