chore: replace TrieAccount with alloy's (#13397)

This commit is contained in:
Matthias Seitz
2024-12-16 03:57:02 +01:00
committed by GitHub
parent 4e6ed39b6d
commit 091c5499ba
17 changed files with 93 additions and 173 deletions

2
Cargo.lock generated
View File

@ -8792,6 +8792,7 @@ dependencies = [
"alloy-genesis", "alloy-genesis",
"alloy-primitives", "alloy-primitives",
"alloy-rlp", "alloy-rlp",
"alloy-trie",
"arbitrary", "arbitrary",
"auto_impl", "auto_impl",
"bincode", "bincode",
@ -9593,7 +9594,6 @@ dependencies = [
"proptest-arbitrary-interop", "proptest-arbitrary-interop",
"reth-codecs", "reth-codecs",
"reth-primitives-traits", "reth-primitives-traits",
"revm-primitives",
"serde", "serde",
"serde_json", "serde_json",
"serde_with", "serde_with",

View File

@ -1399,7 +1399,6 @@ mod tests {
}, },
ProviderFactory, StorageLocation, ProviderFactory, StorageLocation,
}; };
use reth_revm::primitives::AccountInfo;
use reth_stages_api::StageCheckpoint; use reth_stages_api::StageCheckpoint;
use reth_trie::{root::state_root_unhashed, StateRoot}; use reth_trie::{root::state_root_unhashed, StateRoot};
use std::collections::HashMap; use std::collections::HashMap;
@ -1624,15 +1623,13 @@ mod tests {
receipts_root, receipts_root,
state_root: state_root_unhashed(HashMap::from([( state_root: state_root_unhashed(HashMap::from([(
signer, signer,
( Account {
AccountInfo { balance: initial_signer_balance -
balance: initial_signer_balance - (single_tx_cost * U256::from(num_of_signer_txs)),
(single_tx_cost * U256::from(num_of_signer_txs)), nonce: num_of_signer_txs,
nonce: num_of_signer_txs, ..Default::default()
..Default::default() }
}, .into_trie_account(EMPTY_ROOT_HASH),
EMPTY_ROOT_HASH,
),
)])), )])),
..Default::default() ..Default::default()
}; };

View File

@ -20,6 +20,7 @@ use reth_primitives::{
BlockBody, EthPrimitives, NodePrimitives, Receipt, Receipts, RecoveredTx, SealedBlock, BlockBody, EthPrimitives, NodePrimitives, Receipt, Receipts, RecoveredTx, SealedBlock,
SealedBlockWithSenders, SealedHeader, Transaction, TransactionSigned, SealedBlockWithSenders, SealedHeader, Transaction, TransactionSigned,
}; };
use reth_primitives_traits::Account;
use reth_storage_api::NodePrimitivesProvider; use reth_storage_api::NodePrimitivesProvider;
use reth_trie::{root::state_root_unhashed, updates::TrieUpdates, HashedPostState}; use reth_trie::{root::state_root_unhashed, updates::TrieUpdates, HashedPostState};
use revm::{db::BundleState, primitives::AccountInfo}; use revm::{db::BundleState, primitives::AccountInfo};
@ -150,14 +151,12 @@ impl TestBlockBuilder {
beneficiary: Address::random(), beneficiary: Address::random(),
state_root: state_root_unhashed(HashMap::from([( state_root: state_root_unhashed(HashMap::from([(
self.signer, self.signer,
( Account {
AccountInfo { balance: initial_signer_balance - signer_balance_decrease,
balance: initial_signer_balance - signer_balance_decrease, nonce: num_txs,
nonce: num_txs, ..Default::default()
..Default::default() }
}, .into_trie_account(EMPTY_ROOT_HASH),
EMPTY_ROOT_HASH,
),
)])), )])),
// use the number as the timestamp so it is monotonically increasing // use the number as the timestamp so it is monotonically increasing
timestamp: number + timestamp: number +

View File

@ -21,6 +21,7 @@ alloy-eips.workspace = true
alloy-genesis.workspace = true alloy-genesis.workspace = true
alloy-primitives.workspace = true alloy-primitives.workspace = true
alloy-rlp.workspace = true alloy-rlp.workspace = true
alloy-trie.workspace = true
revm-primitives.workspace = true revm-primitives.workspace = true
# op # op
@ -80,7 +81,8 @@ std = [
"bytes/std", "bytes/std",
"derive_more/std", "derive_more/std",
"k256/std", "k256/std",
"secp256k1?/std" "secp256k1?/std",
"alloy-trie/std"
] ]
secp256k1 = ["dep:secp256k1"] secp256k1 = ["dep:secp256k1"]
test-utils = [ test-utils = [
@ -99,7 +101,8 @@ arbitrary = [
"reth-codecs?/arbitrary", "reth-codecs?/arbitrary",
"secp256k1?/global-context", "secp256k1?/global-context",
"secp256k1?/rand", "secp256k1?/rand",
"op-alloy-consensus?/arbitrary" "op-alloy-consensus?/arbitrary",
"alloy-trie/arbitrary"
] ]
serde-bincode-compat = [ serde-bincode-compat = [
"serde", "serde",
@ -120,7 +123,8 @@ serde = [
"revm-primitives/serde", "revm-primitives/serde",
"op-alloy-consensus?/serde", "op-alloy-consensus?/serde",
"k256/serde", "k256/serde",
"secp256k1?/serde" "secp256k1?/serde",
"alloy-trie/serde"
] ]
reth-codec = [ reth-codec = [
"dep:reth-codecs", "dep:reth-codecs",

View File

@ -1,6 +1,7 @@
use alloy_consensus::constants::KECCAK_EMPTY; use alloy_consensus::constants::KECCAK_EMPTY;
use alloy_genesis::GenesisAccount; use alloy_genesis::GenesisAccount;
use alloy_primitives::{keccak256, Bytes, B256, U256}; use alloy_primitives::{keccak256, Bytes, B256, U256};
use alloy_trie::TrieAccount;
use derive_more::Deref; use derive_more::Deref;
use revm_primitives::{AccountInfo, Bytecode as RevmBytecode, BytecodeDecodeError}; use revm_primitives::{AccountInfo, Bytecode as RevmBytecode, BytecodeDecodeError};
@ -57,6 +58,17 @@ impl Account {
pub fn get_bytecode_hash(&self) -> B256 { pub fn get_bytecode_hash(&self) -> B256 {
self.bytecode_hash.unwrap_or(KECCAK_EMPTY) self.bytecode_hash.unwrap_or(KECCAK_EMPTY)
} }
/// Converts the account into a trie account with the given storage root.
pub fn into_trie_account(self, storage_root: B256) -> TrieAccount {
let Self { nonce, balance, bytecode_hash } = self;
TrieAccount {
nonce,
balance,
storage_root,
code_hash: bytecode_hash.unwrap_or(KECCAK_EMPTY),
}
}
} }
/// Bytecode for an account. /// Bytecode for an account.

View File

@ -170,16 +170,13 @@ fn bundle_state_root(execution_outcome: &ExecutionOutcome) -> B256 {
account.info.as_ref().map(|info| { account.info.as_ref().map(|info| {
( (
address, address,
( Account::from(info).into_trie_account(storage_root_unhashed(
Account::from(info), account
storage_root_unhashed( .storage
account .iter()
.storage .filter(|(_, value)| !value.present_value.is_zero())
.iter() .map(|(slot, value)| ((*slot).into(), value.present_value)),
.filter(|(_, value)| !value.present_value.is_zero()) )),
.map(|(slot, value)| ((*slot).into(), value.present_value)),
),
),
) )
}) })
}, },

View File

@ -19,9 +19,7 @@ alloy-trie.workspace = true
alloy-consensus.workspace = true alloy-consensus.workspace = true
reth-primitives-traits.workspace = true reth-primitives-traits.workspace = true
reth-codecs = { workspace = true, optional = true } reth-codecs = { workspace = true, optional = true }
revm-primitives.workspace = true
alloy-genesis.workspace = true
alloy-rpc-types-eth = { workspace = true, optional = true } alloy-rpc-types-eth = { workspace = true, optional = true }
alloy-serde = { workspace = true, optional = true } alloy-serde = { workspace = true, optional = true }
@ -43,6 +41,7 @@ arbitrary = { workspace = true, features = ["derive"], optional = true }
[dev-dependencies] [dev-dependencies]
reth-primitives-traits = { workspace = true, features = ["serde"] } reth-primitives-traits = { workspace = true, features = ["serde"] }
reth-codecs.workspace = true reth-codecs.workspace = true
alloy-genesis.workspace = true
alloy-primitives = { workspace = true, features = ["getrandom"] } alloy-primitives = { workspace = true, features = ["getrandom"] }
alloy-trie = { workspace = true, features = ["arbitrary", "serde"] } alloy-trie = { workspace = true, features = ["arbitrary", "serde"] }
@ -71,7 +70,6 @@ serde = [
"alloy-consensus/serde", "alloy-consensus/serde",
"alloy-trie/serde", "alloy-trie/serde",
"alloy-rpc-types-eth?/serde", "alloy-rpc-types-eth?/serde",
"revm-primitives/serde",
"reth-primitives-traits/serde", "reth-primitives-traits/serde",
"reth-codecs?/serde" "reth-codecs?/serde"
] ]
@ -101,7 +99,6 @@ arbitrary = [
"alloy-consensus/arbitrary", "alloy-consensus/arbitrary",
"alloy-primitives/arbitrary", "alloy-primitives/arbitrary",
"nybbles/arbitrary", "nybbles/arbitrary",
"revm-primitives/arbitrary",
"reth-codecs/arbitrary", "reth-codecs/arbitrary",
"alloy-rpc-types-eth?/arbitrary" "alloy-rpc-types-eth?/arbitrary"
] ]

View File

@ -1,83 +1,18 @@
use crate::root::storage_root_unhashed; /// Re-export for convenience.
use alloy_consensus::constants::KECCAK_EMPTY; pub use alloy_trie::TrieAccount;
use alloy_genesis::GenesisAccount;
use alloy_primitives::{keccak256, B256, U256};
use alloy_rlp::{RlpDecodable, RlpEncodable};
use alloy_trie::EMPTY_ROOT_HASH;
use reth_primitives_traits::Account;
use revm_primitives::AccountInfo;
/// An Ethereum account as represented in the trie.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Default, RlpEncodable, RlpDecodable)]
pub struct TrieAccount {
/// Account nonce.
pub nonce: u64,
/// Account balance.
pub balance: U256,
/// Account's storage root.
pub storage_root: B256,
/// Hash of the account's bytecode.
pub code_hash: B256,
}
impl TrieAccount {
/// Get account's storage root.
pub const fn storage_root(&self) -> B256 {
self.storage_root
}
}
impl From<GenesisAccount> for TrieAccount {
fn from(account: GenesisAccount) -> Self {
let storage_root = account
.storage
.map(|storage| {
storage_root_unhashed(
storage
.into_iter()
.filter(|(_, value)| !value.is_zero())
.map(|(slot, value)| (slot, U256::from_be_bytes(*value))),
)
})
.unwrap_or(EMPTY_ROOT_HASH);
Self {
nonce: account.nonce.unwrap_or_default(),
balance: account.balance,
storage_root,
code_hash: account.code.map_or(KECCAK_EMPTY, keccak256),
}
}
}
impl From<(Account, B256)> for TrieAccount {
fn from((account, storage_root): (Account, B256)) -> Self {
Self {
nonce: account.nonce,
balance: account.balance,
storage_root,
code_hash: account.bytecode_hash.unwrap_or(KECCAK_EMPTY),
}
}
}
impl From<(AccountInfo, B256)> for TrieAccount {
fn from((account, storage_root): (AccountInfo, B256)) -> Self {
Self {
nonce: account.nonce,
balance: account.balance,
storage_root,
code_hash: account.code_hash,
}
}
}
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;
use alloy_primitives::Bytes; use crate::root::storage_root_unhashed;
use alloy_consensus::constants::KECCAK_EMPTY;
use alloy_genesis::GenesisAccount;
use alloy_primitives::{keccak256, Bytes, B256, U256};
use std::collections::BTreeMap; use std::collections::BTreeMap;
use alloy_trie::EMPTY_ROOT_HASH;
use reth_primitives_traits::Account;
#[test] #[test]
fn test_from_genesis_account_with_default_values() { fn test_from_genesis_account_with_default_values() {
let genesis_account = GenesisAccount::default(); let genesis_account = GenesisAccount::default();
@ -88,14 +23,11 @@ mod tests {
// Check the fields are properly set. // Check the fields are properly set.
assert_eq!(trie_account.nonce, 0); assert_eq!(trie_account.nonce, 0);
assert_eq!(trie_account.balance, U256::default()); assert_eq!(trie_account.balance, U256::default());
assert_eq!(trie_account.storage_root(), EMPTY_ROOT_HASH); assert_eq!(trie_account.storage_root, EMPTY_ROOT_HASH);
assert_eq!(trie_account.code_hash, KECCAK_EMPTY); assert_eq!(trie_account.code_hash, KECCAK_EMPTY);
// Check that the default Account converts to the same TrieAccount // Check that the default Account converts to the same TrieAccount
assert_eq!(TrieAccount::from((Account::default(), EMPTY_ROOT_HASH)), trie_account); assert_eq!(Account::default().into_trie_account(EMPTY_ROOT_HASH), trie_account);
// Check that the default AccountInfo converts to the same TrieAccount
assert_eq!(TrieAccount::from((AccountInfo::default(), EMPTY_ROOT_HASH)), trie_account);
} }
#[test] #[test]
@ -123,33 +55,17 @@ mod tests {
// Check that the fields are properly set. // Check that the fields are properly set.
assert_eq!(trie_account.nonce, 10); assert_eq!(trie_account.nonce, 10);
assert_eq!(trie_account.balance, U256::from(1000)); assert_eq!(trie_account.balance, U256::from(1000));
assert_eq!(trie_account.storage_root(), expected_storage_root); assert_eq!(trie_account.storage_root, expected_storage_root);
assert_eq!(trie_account.code_hash, keccak256([0x60, 0x61])); assert_eq!(trie_account.code_hash, keccak256([0x60, 0x61]));
// Check that the Account converts to the same TrieAccount // Check that the Account converts to the same TrieAccount
assert_eq!( assert_eq!(
TrieAccount::from(( Account {
Account { nonce: 10,
nonce: 10, balance: U256::from(1000),
balance: U256::from(1000), bytecode_hash: Some(keccak256([0x60, 0x61]))
bytecode_hash: Some(keccak256([0x60, 0x61])) }
}, .into_trie_account(expected_storage_root),
expected_storage_root
)),
trie_account
);
// Check that the AccountInfo converts to the same TrieAccount
assert_eq!(
TrieAccount::from((
AccountInfo {
nonce: 10,
balance: U256::from(1000),
code_hash: keccak256([0x60, 0x61]),
..Default::default()
},
expected_storage_root
)),
trie_account trie_account
); );
} }
@ -174,7 +90,7 @@ mod tests {
assert_eq!(trie_account.nonce, 3); assert_eq!(trie_account.nonce, 3);
assert_eq!(trie_account.balance, U256::from(300)); assert_eq!(trie_account.balance, U256::from(300));
// Zero values in storage should result in EMPTY_ROOT_HASH // Zero values in storage should result in EMPTY_ROOT_HASH
assert_eq!(trie_account.storage_root(), EMPTY_ROOT_HASH); assert_eq!(trie_account.storage_root, EMPTY_ROOT_HASH);
// No code provided, so code hash should be KECCAK_EMPTY // No code provided, so code hash should be KECCAK_EMPTY
assert_eq!(trie_account.code_hash, KECCAK_EMPTY); assert_eq!(trie_account.code_hash, KECCAK_EMPTY);
} }

View File

@ -258,10 +258,9 @@ impl AccountProof {
let expected = if self.info.is_none() && self.storage_root == EMPTY_ROOT_HASH { let expected = if self.info.is_none() && self.storage_root == EMPTY_ROOT_HASH {
None None
} else { } else {
Some(alloy_rlp::encode(TrieAccount::from(( Some(alloy_rlp::encode(
self.info.unwrap_or_default(), self.info.unwrap_or_default().into_trie_account(self.storage_root),
self.storage_root, ))
))))
}; };
let nibbles = Nibbles::unpack(keccak256(self.address)); let nibbles = Nibbles::unpack(keccak256(self.address));
verify_proof(root, nibbles, expected, &self.proof) verify_proof(root, nibbles, expected, &self.proof)

View File

@ -21,7 +21,7 @@ use reth_trie::{
triehash::KeccakHasher, triehash::KeccakHasher,
updates::StorageTrieUpdates, updates::StorageTrieUpdates,
BranchNodeCompact, HashBuilder, IntermediateStateRootState, Nibbles, StateRoot, BranchNodeCompact, HashBuilder, IntermediateStateRootState, Nibbles, StateRoot,
StateRootProgress, StorageRoot, TrieAccount, TrieMask, StateRootProgress, StorageRoot, TrieMask,
}; };
use reth_trie_db::{DatabaseStateRoot, DatabaseStorageRoot}; use reth_trie_db::{DatabaseStateRoot, DatabaseStorageRoot};
use std::{collections::BTreeMap, ops::Mul, str::FromStr, sync::Arc}; use std::{collections::BTreeMap, ops::Mul, str::FromStr, sync::Arc};
@ -284,7 +284,7 @@ fn test_state_root_with_state(state: State) {
} }
fn encode_account(account: Account, storage_root: Option<B256>) -> Vec<u8> { fn encode_account(account: Account, storage_root: Option<B256>) -> Vec<u8> {
let account = TrieAccount::from((account, storage_root.unwrap_or(EMPTY_ROOT_HASH))); let account = account.into_trie_account(storage_root.unwrap_or(EMPTY_ROOT_HASH));
let mut account_rlp = Vec::with_capacity(account.length()); let mut account_rlp = Vec::with_capacity(account.length());
account.encode(&mut account_rlp); account.encode(&mut account_rlp);
account_rlp account_rlp

View File

@ -20,7 +20,7 @@ use reth_trie::{
updates::TrieUpdatesSorted, updates::TrieUpdatesSorted,
walker::TrieWalker, walker::TrieWalker,
HashBuilder, HashedPostStateSorted, MultiProof, MultiProofTargets, Nibbles, StorageMultiProof, HashBuilder, HashedPostStateSorted, MultiProof, MultiProofTargets, Nibbles, StorageMultiProof,
TrieAccount, TRIE_ACCOUNT_RLP_MAX_SIZE, TRIE_ACCOUNT_RLP_MAX_SIZE,
}; };
use reth_trie_common::proof::ProofRetainer; use reth_trie_common::proof::ProofRetainer;
use reth_trie_db::{DatabaseHashedCursorFactory, DatabaseTrieCursorFactory}; use reth_trie_db::{DatabaseHashedCursorFactory, DatabaseTrieCursorFactory};
@ -229,7 +229,7 @@ where
// Encode account // Encode account
account_rlp.clear(); account_rlp.clear();
let account = TrieAccount::from((account, storage_multiproof.root)); let account = account.into_trie_account(storage_multiproof.root);
account.encode(&mut account_rlp as &mut dyn BufMut); account.encode(&mut account_rlp as &mut dyn BufMut);
hash_builder.add_leaf(Nibbles::unpack(hashed_address), &account_rlp); hash_builder.add_leaf(Nibbles::unpack(hashed_address), &account_rlp);

View File

@ -16,7 +16,7 @@ use reth_trie::{
trie_cursor::{InMemoryTrieCursorFactory, TrieCursorFactory}, trie_cursor::{InMemoryTrieCursorFactory, TrieCursorFactory},
updates::TrieUpdates, updates::TrieUpdates,
walker::TrieWalker, walker::TrieWalker,
HashBuilder, Nibbles, StorageRoot, TrieAccount, TrieInput, TRIE_ACCOUNT_RLP_MAX_SIZE, HashBuilder, Nibbles, StorageRoot, TrieInput, TRIE_ACCOUNT_RLP_MAX_SIZE,
}; };
use reth_trie_db::{DatabaseHashedCursorFactory, DatabaseTrieCursorFactory}; use reth_trie_db::{DatabaseHashedCursorFactory, DatabaseTrieCursorFactory};
use std::{collections::HashMap, sync::Arc}; use std::{collections::HashMap, sync::Arc};
@ -192,7 +192,7 @@ where
} }
account_rlp.clear(); account_rlp.clear();
let account = TrieAccount::from((account, storage_root)); let account = account.into_trie_account(storage_root);
account.encode(&mut account_rlp as &mut dyn BufMut); account.encode(&mut account_rlp as &mut dyn BufMut);
hash_builder.add_leaf(Nibbles::unpack(hashed_address), &account_rlp); hash_builder.add_leaf(Nibbles::unpack(hashed_address), &account_rlp);
} }

View File

@ -402,7 +402,7 @@ where
} else { } else {
trace!(target: "trie::sparse", ?address, "Updating account"); trace!(target: "trie::sparse", ?address, "Updating account");
self.account_rlp_buf.clear(); self.account_rlp_buf.clear();
TrieAccount::from((account, storage_root)).encode(&mut self.account_rlp_buf); account.into_trie_account(storage_root).encode(&mut self.account_rlp_buf);
self.update_account_leaf(nibbles, self.account_rlp_buf.clone()) self.update_account_leaf(nibbles, self.account_rlp_buf.clone())
} }
} }
@ -438,7 +438,7 @@ mod tests {
use assert_matches::assert_matches; use assert_matches::assert_matches;
use rand::{rngs::StdRng, Rng, SeedableRng}; use rand::{rngs::StdRng, Rng, SeedableRng};
use reth_primitives_traits::Account; use reth_primitives_traits::Account;
use reth_trie::{updates::StorageTrieUpdates, HashBuilder, TrieAccount, EMPTY_ROOT_HASH}; use reth_trie::{updates::StorageTrieUpdates, HashBuilder, EMPTY_ROOT_HASH};
use reth_trie_common::{proof::ProofRetainer, StorageMultiProof, TrieMask}; use reth_trie_common::{proof::ProofRetainer, StorageMultiProof, TrieMask};
#[test] #[test]
@ -537,11 +537,11 @@ mod tests {
let address_1 = b256!("1000000000000000000000000000000000000000000000000000000000000000"); let address_1 = b256!("1000000000000000000000000000000000000000000000000000000000000000");
let address_path_1 = Nibbles::unpack(address_1); let address_path_1 = Nibbles::unpack(address_1);
let account_1 = Account::arbitrary(&mut arbitrary::Unstructured::new(&bytes)).unwrap(); let account_1 = Account::arbitrary(&mut arbitrary::Unstructured::new(&bytes)).unwrap();
let mut trie_account_1 = TrieAccount::from((account_1, storage_root)); let mut trie_account_1 = account_1.into_trie_account(storage_root);
let address_2 = b256!("1100000000000000000000000000000000000000000000000000000000000000"); let address_2 = b256!("1100000000000000000000000000000000000000000000000000000000000000");
let address_path_2 = Nibbles::unpack(address_2); let address_path_2 = Nibbles::unpack(address_2);
let account_2 = Account::arbitrary(&mut arbitrary::Unstructured::new(&bytes)).unwrap(); let account_2 = Account::arbitrary(&mut arbitrary::Unstructured::new(&bytes)).unwrap();
let mut trie_account_2 = TrieAccount::from((account_2, EMPTY_ROOT_HASH)); let mut trie_account_2 = account_2.into_trie_account(EMPTY_ROOT_HASH);
let mut hash_builder = let mut hash_builder =
HashBuilder::default().with_proof_retainer(ProofRetainer::from_iter([ HashBuilder::default().with_proof_retainer(ProofRetainer::from_iter([
@ -594,7 +594,7 @@ mod tests {
let address_3 = b256!("2000000000000000000000000000000000000000000000000000000000000000"); let address_3 = b256!("2000000000000000000000000000000000000000000000000000000000000000");
let address_path_3 = Nibbles::unpack(address_3); let address_path_3 = Nibbles::unpack(address_3);
let account_3 = Account { nonce: account_1.nonce + 1, ..account_1 }; let account_3 = Account { nonce: account_1.nonce + 1, ..account_1 };
let trie_account_3 = TrieAccount::from((account_3, EMPTY_ROOT_HASH)); let trie_account_3 = account_3.into_trie_account(EMPTY_ROOT_HASH);
sparse.update_account_leaf(address_path_3, alloy_rlp::encode(trie_account_3)).unwrap(); sparse.update_account_leaf(address_path_3, alloy_rlp::encode(trie_account_3)).unwrap();

View File

@ -1297,7 +1297,7 @@ mod tests {
trie_cursor::noop::NoopAccountTrieCursor, trie_cursor::noop::NoopAccountTrieCursor,
updates::TrieUpdates, updates::TrieUpdates,
walker::TrieWalker, walker::TrieWalker,
BranchNode, ExtensionNode, HashedPostState, LeafNode, TrieAccount, BranchNode, ExtensionNode, HashedPostState, LeafNode,
}; };
use reth_trie_common::{ use reth_trie_common::{
proof::{ProofNodes, ProofRetainer}, proof::{ProofNodes, ProofRetainer},
@ -1357,7 +1357,7 @@ mod tests {
hash_builder.add_branch(branch.key, branch.value, branch.children_are_in_trie); hash_builder.add_branch(branch.key, branch.value, branch.children_are_in_trie);
} }
TrieElement::Leaf(key, account) => { TrieElement::Leaf(key, account) => {
let account = TrieAccount::from((account, EMPTY_ROOT_HASH)); let account = account.into_trie_account(EMPTY_ROOT_HASH);
account.encode(&mut account_rlp); account.encode(&mut account_rlp);
hash_builder.add_leaf(Nibbles::unpack(key), &account_rlp); hash_builder.add_leaf(Nibbles::unpack(key), &account_rlp);
@ -1437,7 +1437,7 @@ mod tests {
let value = || Account::default(); let value = || Account::default();
let value_encoded = || { let value_encoded = || {
let mut account_rlp = Vec::new(); let mut account_rlp = Vec::new();
TrieAccount::from((value(), EMPTY_ROOT_HASH)).encode(&mut account_rlp); value().into_trie_account(EMPTY_ROOT_HASH).encode(&mut account_rlp);
account_rlp account_rlp
}; };
@ -1462,7 +1462,7 @@ mod tests {
let value = || Account::default(); let value = || Account::default();
let value_encoded = || { let value_encoded = || {
let mut account_rlp = Vec::new(); let mut account_rlp = Vec::new();
TrieAccount::from((value(), EMPTY_ROOT_HASH)).encode(&mut account_rlp); value().into_trie_account(EMPTY_ROOT_HASH).encode(&mut account_rlp);
account_rlp account_rlp
}; };
@ -1491,7 +1491,7 @@ mod tests {
let value = || Account::default(); let value = || Account::default();
let value_encoded = || { let value_encoded = || {
let mut account_rlp = Vec::new(); let mut account_rlp = Vec::new();
TrieAccount::from((value(), EMPTY_ROOT_HASH)).encode(&mut account_rlp); value().into_trie_account(EMPTY_ROOT_HASH).encode(&mut account_rlp);
account_rlp account_rlp
}; };
@ -1528,7 +1528,7 @@ mod tests {
let value = || Account::default(); let value = || Account::default();
let value_encoded = || { let value_encoded = || {
let mut account_rlp = Vec::new(); let mut account_rlp = Vec::new();
TrieAccount::from((value(), EMPTY_ROOT_HASH)).encode(&mut account_rlp); value().into_trie_account(EMPTY_ROOT_HASH).encode(&mut account_rlp);
account_rlp account_rlp
}; };
@ -1560,13 +1560,13 @@ mod tests {
let old_value = Account { nonce: 1, ..Default::default() }; let old_value = Account { nonce: 1, ..Default::default() };
let old_value_encoded = { let old_value_encoded = {
let mut account_rlp = Vec::new(); let mut account_rlp = Vec::new();
TrieAccount::from((old_value, EMPTY_ROOT_HASH)).encode(&mut account_rlp); old_value.into_trie_account(EMPTY_ROOT_HASH).encode(&mut account_rlp);
account_rlp account_rlp
}; };
let new_value = Account { nonce: 2, ..Default::default() }; let new_value = Account { nonce: 2, ..Default::default() };
let new_value_encoded = { let new_value_encoded = {
let mut account_rlp = Vec::new(); let mut account_rlp = Vec::new();
TrieAccount::from((new_value, EMPTY_ROOT_HASH)).encode(&mut account_rlp); new_value.into_trie_account(EMPTY_ROOT_HASH).encode(&mut account_rlp);
account_rlp account_rlp
}; };
@ -1928,7 +1928,7 @@ mod tests {
for (update, keys_to_delete) in updates { for (update, keys_to_delete) in updates {
// Insert state updates into the sparse trie and calculate the root // Insert state updates into the sparse trie and calculate the root
for (key, account) in update.clone() { for (key, account) in update.clone() {
let account = TrieAccount::from((account, EMPTY_ROOT_HASH)); let account = account.into_trie_account(EMPTY_ROOT_HASH);
let mut account_rlp = Vec::new(); let mut account_rlp = Vec::new();
account.encode(&mut account_rlp); account.encode(&mut account_rlp);
sparse.update_leaf(key, account_rlp).unwrap(); sparse.update_leaf(key, account_rlp).unwrap();
@ -2049,7 +2049,7 @@ mod tests {
let value = || Account::default(); let value = || Account::default();
let value_encoded = || { let value_encoded = || {
let mut account_rlp = Vec::new(); let mut account_rlp = Vec::new();
TrieAccount::from((value(), EMPTY_ROOT_HASH)).encode(&mut account_rlp); value().into_trie_account(EMPTY_ROOT_HASH).encode(&mut account_rlp);
account_rlp account_rlp
}; };
@ -2205,7 +2205,7 @@ mod tests {
let value = || Account::default(); let value = || Account::default();
let value_encoded = || { let value_encoded = || {
let mut account_rlp = Vec::new(); let mut account_rlp = Vec::new();
TrieAccount::from((value(), EMPTY_ROOT_HASH)).encode(&mut account_rlp); value().into_trie_account(EMPTY_ROOT_HASH).encode(&mut account_rlp);
account_rlp account_rlp
}; };
@ -2332,7 +2332,7 @@ mod tests {
let value = || Account { bytecode_hash: Some(B256::repeat_byte(1)), ..Default::default() }; let value = || Account { bytecode_hash: Some(B256::repeat_byte(1)), ..Default::default() };
let value_encoded = || { let value_encoded = || {
let mut account_rlp = Vec::new(); let mut account_rlp = Vec::new();
TrieAccount::from((value(), EMPTY_ROOT_HASH)).encode(&mut account_rlp); value().into_trie_account(EMPTY_ROOT_HASH).encode(&mut account_rlp);
account_rlp account_rlp
}; };

View File

@ -15,7 +15,6 @@ use alloy_rlp::{BufMut, Encodable};
use reth_execution_errors::trie::StateProofError; use reth_execution_errors::trie::StateProofError;
use reth_trie_common::{ use reth_trie_common::{
proof::ProofRetainer, AccountProof, MultiProof, MultiProofTargets, StorageMultiProof, proof::ProofRetainer, AccountProof, MultiProof, MultiProofTargets, StorageMultiProof,
TrieAccount,
}; };
mod blinded; mod blinded;
@ -150,7 +149,7 @@ where
// Encode account // Encode account
account_rlp.clear(); account_rlp.clear();
let account = TrieAccount::from((account, storage_multiproof.root)); let account = account.into_trie_account(storage_multiproof.root);
account.encode(&mut account_rlp as &mut dyn BufMut); account.encode(&mut account_rlp as &mut dyn BufMut);
hash_builder.add_leaf(Nibbles::unpack(hashed_address), &account_rlp); hash_builder.add_leaf(Nibbles::unpack(hashed_address), &account_rlp);

View File

@ -1,7 +1,7 @@
use alloy_primitives::{Address, B256, U256}; use alloy_primitives::{Address, B256, U256};
use alloy_rlp::encode_fixed_size; use alloy_rlp::encode_fixed_size;
use reth_primitives::Account; use reth_primitives::Account;
use reth_trie_common::{triehash::KeccakHasher, TrieAccount}; use reth_trie_common::triehash::KeccakHasher;
/// Re-export of [triehash]. /// Re-export of [triehash].
pub use triehash; pub use triehash;
@ -14,7 +14,7 @@ where
{ {
let encoded_accounts = accounts.into_iter().map(|(address, (account, storage))| { let encoded_accounts = accounts.into_iter().map(|(address, (account, storage))| {
let storage_root = storage_root(storage); let storage_root = storage_root(storage);
let account = TrieAccount::from((account, storage_root)); let account = account.into_trie_account(storage_root);
(address, alloy_rlp::encode(account)) (address, alloy_rlp::encode(account))
}); });
triehash::sec_trie_root::<KeccakHasher, _, _, _>(encoded_accounts) triehash::sec_trie_root::<KeccakHasher, _, _, _>(encoded_accounts)
@ -35,7 +35,7 @@ where
{ {
let encoded_accounts = accounts.into_iter().map(|(address, (account, storage))| { let encoded_accounts = accounts.into_iter().map(|(address, (account, storage))| {
let storage_root = storage_root_prehashed(storage); let storage_root = storage_root_prehashed(storage);
let account = TrieAccount::from((account, storage_root)); let account = account.into_trie_account(storage_root);
(address, alloy_rlp::encode(account)) (address, alloy_rlp::encode(account))
}); });

View File

@ -7,7 +7,7 @@ use crate::{
trie_cursor::TrieCursorFactory, trie_cursor::TrieCursorFactory,
updates::{StorageTrieUpdates, TrieUpdates}, updates::{StorageTrieUpdates, TrieUpdates},
walker::TrieWalker, walker::TrieWalker,
HashBuilder, Nibbles, TrieAccount, TRIE_ACCOUNT_RLP_MAX_SIZE, HashBuilder, Nibbles, TRIE_ACCOUNT_RLP_MAX_SIZE,
}; };
use alloy_consensus::EMPTY_ROOT_HASH; use alloy_consensus::EMPTY_ROOT_HASH;
use alloy_primitives::{keccak256, Address, B256}; use alloy_primitives::{keccak256, Address, B256};
@ -224,7 +224,7 @@ where
}; };
account_rlp.clear(); account_rlp.clear();
let account = TrieAccount::from((account, storage_root)); let account = account.into_trie_account(storage_root);
account.encode(&mut account_rlp as &mut dyn BufMut); account.encode(&mut account_rlp as &mut dyn BufMut);
hash_builder.add_leaf(Nibbles::unpack(hashed_address), &account_rlp); hash_builder.add_leaf(Nibbles::unpack(hashed_address), &account_rlp);