Skip to content

Commit

Permalink
Rename to TrieAccount / Use simple u64 serialize/deserialize
Browse files Browse the repository at this point in the history
  • Loading branch information
moricho committed Dec 3, 2024
1 parent 64d8c36 commit 4f6f859
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 94 deletions.
33 changes: 26 additions & 7 deletions src/account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,12 @@ use crate::{EMPTY_ROOT_HASH, KECCAK_EMPTY};
use alloy_primitives::{keccak256, B256, U256};
use alloy_rlp::{RlpDecodable, RlpEncodable};

#[cfg(feature = "serde")]
use crate::serde::quantity;

/// Represents an Account in the account trie.
/// Represents an TrieAccount in the account trie.
#[derive(Copy, Clone, Debug, PartialEq, Eq, RlpDecodable, RlpEncodable)]
#[cfg_attr(any(test, feature = "arbitrary"), derive(arbitrary::Arbitrary))]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "serde", serde(rename_all = "camelCase"))]
pub struct Account {
pub struct TrieAccount {
/// The account's nonce.
#[cfg_attr(feature = "serde", serde(with = "quantity"))]
pub nonce: u64,
Expand All @@ -22,7 +19,7 @@ pub struct Account {
pub code_hash: B256,
}

impl Default for Account {
impl Default for TrieAccount {
fn default() -> Self {
Self {
nonce: 0,
Expand All @@ -33,9 +30,31 @@ impl Default for Account {
}
}

impl Account {
impl TrieAccount {
/// Compute hash as committed to in the MPT trie without memorizing.
pub fn trie_hash_slow(&self) -> B256 {
keccak256(alloy_rlp::encode(self))
}
}

#[cfg(feature = "serde")]
mod quantity {
use alloy_primitives::U64;
use serde::{Deserialize, Deserializer, Serialize, Serializer};

/// Serializes a primitive number as a "quantity" hex string.
pub(crate) fn serialize<S>(value: &u64, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
U64::from(*value).serialize(serializer)
}

/// Deserializes a primitive number from a "quantity" hex string.
pub(crate) fn deserialize<'de, D>(deserializer: D) -> Result<u64, D::Error>
where
D: Deserializer<'de>,
{
U64::deserialize(deserializer).map(|value| value.to())
}
}
5 changes: 1 addition & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,8 @@ pub use hash_builder::HashBuilder;

pub mod proof;

#[cfg(feature = "serde")]
pub mod serde;

mod account;
pub use account::Account;
pub use account::TrieAccount;

mod mask;
pub use mask::TrieMask;
Expand Down
12 changes: 7 additions & 5 deletions src/root.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{Account, HashBuilder, EMPTY_ROOT_HASH};
use crate::{HashBuilder, TrieAccount, EMPTY_ROOT_HASH};
use alloy_primitives::{keccak256, B256};
use alloy_rlp::Encodable;

Expand Down Expand Up @@ -55,7 +55,7 @@ where
/// represented as MPT.
/// See [`state_root_unsorted`] for more info.
#[cfg(feature = "std")]
pub fn state_root_ref_unhashed<'a, A: Into<Account> + Clone + 'a>(
pub fn state_root_ref_unhashed<'a, A: Into<TrieAccount> + Clone + 'a>(
state: impl IntoIterator<Item = (&'a Address, &'a A)>,
) -> B256 {
state_root_unsorted(
Expand All @@ -67,7 +67,7 @@ pub fn state_root_ref_unhashed<'a, A: Into<Account> + Clone + 'a>(
/// represented as MPT.
/// See [`state_root_unsorted`] for more info.
#[cfg(feature = "std")]
pub fn state_root_unhashed<A: Into<Account>>(
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)))
Expand All @@ -76,7 +76,9 @@ pub fn state_root_unhashed<A: Into<Account>>(
/// Sorts the hashed account keys and calculates the root hash of the state represented as MPT.
/// See [`state_root`] for more info.
#[cfg(feature = "std")]
pub fn state_root_unsorted<A: Into<Account>>(state: impl IntoIterator<Item = (B256, A)>) -> B256 {
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))
}

Expand All @@ -87,7 +89,7 @@ pub fn state_root_unsorted<A: Into<Account>>(state: impl IntoIterator<Item = (B2
/// # Panics
///
/// If the items are not in sorted order.
pub fn state_root<A: Into<Account>>(state: impl IntoIterator<Item = (B256, A)>) -> B256 {
pub fn state_root<A: Into<TrieAccount>>(state: impl IntoIterator<Item = (B256, A)>) -> B256 {
let mut hb = HashBuilder::default();
for (hashed_key, account) in state {
let account_rlp_buf = alloy_rlp::encode(account.into());
Expand Down
3 changes: 0 additions & 3 deletions src/serde/mod.rs

This file was deleted.

75 changes: 0 additions & 75 deletions src/serde/quantity.rs

This file was deleted.

0 comments on commit 4f6f859

Please sign in to comment.