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

feat: Add Account helpers #1717

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open

Conversation

moricho
Copy link
Contributor

@moricho moricho commented Nov 30, 2024

Motivation

TrieAccount in Reth will be replaced with Account from alloy, so it would be better for TrieAccount related helpers to be exported to alloy.
paradigmxyz/reth#12318

https://github.com/paradigmxyz/reth/blob/c8e5b233ef242c4c5eaebe6dc5ae66b98184d8a8/crates/trie/common/src/root.rs#L10-L36

/// Hashes and sorts account keys, then proceeds to calculating the root hash of the state
/// represented as MPT.
/// See [`state_root_unsorted`] for more info.
pub fn state_root_ref_unhashed<'a, A: Into<TrieAccount> + Clone + 'a>(
    state: impl IntoIterator<Item = (&'a Address, &'a A)>,
) -> B256 {
    state_root_unsorted(
        state.into_iter().map(|(address, account)| (keccak256(address), account.clone())),
    )
}

/// Hashes and sorts account keys, then proceeds to calculating the root hash of the state
/// represented as MPT.
/// See [`state_root_unsorted`] for more info.
pub fn state_root_unhashed<A: Into<TrieAccount>>(
    state: impl IntoIterator<Item = (Address, A)>,
) -> B256 {
    state_root_unsorted(state.into_iter().map(|(address, account)| (keccak256(address), account)))
}

/// Sorts the hashed account keys and calculates the root hash of the state represented as MPT.
/// See [`state_root`] for more info.
pub fn state_root_unsorted<A: Into<TrieAccount>>(
    state: impl IntoIterator<Item = (B256, A)>,
) -> B256 {
    state_root(state.into_iter().sorted_unstable_by_key(|(key, _)| *key))
}

Also implements From<GenesisAccount> for Account since Reth needs it in fn state_root_ref_unhashed<'a, A: Into<Account> + Clone + 'a>

Solution

PR Checklist

  • Added Tests
  • Added Documentation
  • Breaking changes

Copy link
Member

@mattsse mattsse left a comment

Choose a reason for hiding this comment

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

@moricho
Copy link
Contributor Author

moricho commented Nov 30, 2024

Got it, I'll add them 👍

@moricho moricho requested a review from mattsse November 30, 2024 11:59
Copy link
Member

@mattsse mattsse left a comment

Choose a reason for hiding this comment

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

actually, now that I looked at this,
it would be easier if we upstream this to alloy-trie entirely

the trieaccount type + state root functions

could you please open these changes as a pr to alloy-trie
then we import alloy-trie in alloy-genesis and to the from impl there

@moricho
Copy link
Contributor Author

moricho commented Dec 2, 2024

Ah, that makes sense. I'll work on it!

@moricho
Copy link
Contributor Author

moricho commented Dec 3, 2024

@mattsse
Opened a PR to export this to alloy-trie. Could you take a look at it when you have time?: alloy-rs/trie#65

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