Skip to content

Commit 6896ae7

Browse files
committed
replace by alloy's one
1 parent 804dc99 commit 6896ae7

File tree

10 files changed

+126
-285
lines changed

10 files changed

+126
-285
lines changed

Cargo.lock

-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/trie/common/Cargo.toml

+17-22
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,14 @@ workspace = true
1515
# alloy
1616
alloy-primitives.workspace = true
1717
alloy-rlp = { workspace = true, features = ["arrayvec"] }
18+
alloy-rpc-types-eth = { workspace = true, optional = true }
19+
alloy-serde = { workspace = true, optional = true }
1820
alloy-trie.workspace = true
1921
alloy-consensus.workspace = true
2022
reth-primitives-traits.workspace = true
2123
reth-codecs.workspace = true
2224
revm-primitives.workspace = true
2325

24-
alloy-genesis.workspace = true
25-
alloy-rpc-types-eth = { workspace = true, optional = true }
26-
alloy-serde = { workspace = true, optional = true }
27-
2826
bytes.workspace = true
2927
derive_more.workspace = true
3028
itertools.workspace = true
@@ -56,10 +54,7 @@ serde_json.workspace = true
5654
serde_with.workspace = true
5755

5856
[features]
59-
eip1186 = [
60-
"dep:alloy-rpc-types-eth",
61-
"dep:alloy-serde",
62-
]
57+
eip1186 = ["dep:alloy-rpc-types-eth", "dep:alloy-serde"]
6358
serde = [
6459
"dep:serde",
6560
"bytes/serde",
@@ -70,13 +65,13 @@ serde = [
7065
"alloy-rpc-types-eth?/serde",
7166
"revm-primitives/serde",
7267
"reth-primitives-traits/serde",
73-
"reth-codecs/serde"
68+
"reth-codecs/serde",
7469
]
7570
serde-bincode-compat = [
7671
"serde",
77-
"reth-primitives-traits/serde-bincode-compat",
78-
"alloy-consensus/serde-bincode-compat",
79-
"dep:serde_with"
72+
"reth-primitives-traits/serde-bincode-compat",
73+
"alloy-consensus/serde-bincode-compat",
74+
"dep:serde_with",
8075
]
8176
test-utils = [
8277
"dep:plain_hasher",
@@ -86,16 +81,16 @@ test-utils = [
8681
"reth-codecs/test-utils",
8782
]
8883
arbitrary = [
89-
"alloy-trie/arbitrary",
90-
"dep:arbitrary",
91-
"alloy-serde/arbitrary",
92-
"reth-primitives-traits/arbitrary",
93-
"alloy-consensus/arbitrary",
94-
"alloy-primitives/arbitrary",
95-
"nybbles/arbitrary",
96-
"revm-primitives/arbitrary",
97-
"reth-codecs/arbitrary",
98-
"alloy-rpc-types-eth?/arbitrary"
84+
"alloy-trie/arbitrary",
85+
"dep:arbitrary",
86+
"alloy-serde/arbitrary",
87+
"reth-primitives-traits/arbitrary",
88+
"alloy-consensus/arbitrary",
89+
"alloy-primitives/arbitrary",
90+
"nybbles/arbitrary",
91+
"revm-primitives/arbitrary",
92+
"reth-codecs/arbitrary",
93+
"alloy-rpc-types-eth?/arbitrary",
9994
]
10095

10196
[[bench]]

crates/trie/common/src/account.rs

+16-173
Original file line numberDiff line numberDiff line change
@@ -1,184 +1,27 @@
1-
use crate::root::storage_root_unhashed;
21
use alloy_consensus::constants::KECCAK_EMPTY;
3-
use alloy_genesis::GenesisAccount;
4-
use alloy_primitives::{keccak256, B256, U256};
5-
use alloy_rlp::{RlpDecodable, RlpEncodable};
6-
use alloy_trie::EMPTY_ROOT_HASH;
2+
use alloy_primitives::B256;
3+
use alloy_trie::TrieAccount;
74
use reth_primitives_traits::Account;
85
use revm_primitives::AccountInfo;
96

10-
/// An Ethereum account as represented in the trie.
11-
#[derive(Clone, Copy, Debug, PartialEq, Eq, Default, RlpEncodable, RlpDecodable)]
12-
pub struct TrieAccount {
13-
/// Account nonce.
14-
pub nonce: u64,
15-
/// Account balance.
16-
pub balance: U256,
17-
/// Account's storage root.
18-
pub storage_root: B256,
19-
/// Hash of the account's bytecode.
20-
pub code_hash: B256,
21-
}
22-
23-
impl TrieAccount {
24-
/// Get account's storage root.
25-
pub const fn storage_root(&self) -> B256 {
26-
self.storage_root
27-
}
28-
}
29-
30-
impl From<GenesisAccount> for TrieAccount {
31-
fn from(account: GenesisAccount) -> Self {
32-
let storage_root = account
33-
.storage
34-
.map(|storage| {
35-
storage_root_unhashed(
36-
storage
37-
.into_iter()
38-
.filter(|(_, value)| !value.is_zero())
39-
.map(|(slot, value)| (slot, U256::from_be_bytes(*value))),
40-
)
41-
})
42-
.unwrap_or(EMPTY_ROOT_HASH);
43-
44-
Self {
45-
nonce: account.nonce.unwrap_or_default(),
46-
balance: account.balance,
47-
storage_root,
48-
code_hash: account.code.map_or(KECCAK_EMPTY, keccak256),
49-
}
50-
}
51-
}
52-
53-
impl From<(Account, B256)> for TrieAccount {
54-
fn from((account, storage_root): (Account, B256)) -> Self {
55-
Self {
56-
nonce: account.nonce,
57-
balance: account.balance,
58-
storage_root,
59-
code_hash: account.bytecode_hash.unwrap_or(KECCAK_EMPTY),
60-
}
7+
/// Convert an Account to a TrieAccount of alloy
8+
pub fn from_account_to_trie_account(account: Account, storage_root: B256) -> TrieAccount {
9+
TrieAccount {
10+
nonce: account.nonce,
11+
balance: account.balance,
12+
storage_root,
13+
code_hash: account.bytecode_hash.unwrap_or(KECCAK_EMPTY),
6114
}
6215
}
6316

64-
impl From<(AccountInfo, B256)> for TrieAccount {
65-
fn from((account, storage_root): (AccountInfo, B256)) -> Self {
66-
Self {
67-
nonce: account.nonce,
68-
balance: account.balance,
69-
storage_root,
70-
code_hash: account.code_hash,
71-
}
17+
/// Convert an AccountInfo to a TrieAccount of alloy
18+
pub fn from_account_info_to_trie_account(account: AccountInfo, storage_root: B256) -> TrieAccount {
19+
TrieAccount {
20+
nonce: account.nonce,
21+
balance: account.balance,
22+
storage_root,
23+
code_hash: account.code_hash,
7224
}
7325
}
7426

75-
#[cfg(test)]
76-
mod tests {
77-
use super::*;
78-
use alloy_primitives::Bytes;
79-
use std::collections::BTreeMap;
80-
81-
#[test]
82-
fn test_from_genesis_account_with_default_values() {
83-
let genesis_account = GenesisAccount::default();
84-
85-
// Convert the GenesisAccount to a TrieAccount
86-
let trie_account: TrieAccount = genesis_account.into();
87-
88-
// Check the fields are properly set.
89-
assert_eq!(trie_account.nonce, 0);
90-
assert_eq!(trie_account.balance, U256::default());
91-
assert_eq!(trie_account.storage_root(), EMPTY_ROOT_HASH);
92-
assert_eq!(trie_account.code_hash, KECCAK_EMPTY);
93-
94-
// Check that the default Account converts to the same TrieAccount
95-
assert_eq!(Into::<TrieAccount>::into((Account::default(), EMPTY_ROOT_HASH)), trie_account);
96-
97-
// Check that the default AccountInfo converts to the same TrieAccount
98-
assert_eq!(
99-
Into::<TrieAccount>::into((AccountInfo::default(), EMPTY_ROOT_HASH)),
100-
trie_account
101-
);
102-
}
103-
104-
#[test]
105-
fn test_from_genesis_account_with_values() {
106-
// Create a GenesisAccount with specific values
107-
let mut storage = BTreeMap::new();
108-
storage.insert(B256::from([0x01; 32]), B256::from([0x02; 32]));
109-
110-
let genesis_account = GenesisAccount {
111-
nonce: Some(10),
112-
balance: U256::from(1000),
113-
code: Some(Bytes::from(vec![0x60, 0x61])),
114-
storage: Some(storage),
115-
private_key: None,
116-
};
117-
118-
// Convert the GenesisAccount to a TrieAccount
119-
let trie_account: TrieAccount = genesis_account.into();
120-
121-
let expected_storage_root = storage_root_unhashed(BTreeMap::from([(
122-
B256::from([0x01; 32]),
123-
U256::from_be_bytes(*B256::from([0x02; 32])),
124-
)]));
125-
126-
// Check that the fields are properly set.
127-
assert_eq!(trie_account.nonce, 10);
128-
assert_eq!(trie_account.balance, U256::from(1000));
129-
assert_eq!(trie_account.storage_root(), expected_storage_root);
130-
assert_eq!(trie_account.code_hash, keccak256([0x60, 0x61]));
131-
132-
// Check that the Account converts to the same TrieAccount
133-
assert_eq!(
134-
Into::<TrieAccount>::into((
135-
Account {
136-
nonce: 10,
137-
balance: U256::from(1000),
138-
bytecode_hash: Some(keccak256([0x60, 0x61]))
139-
},
140-
expected_storage_root
141-
)),
142-
trie_account
143-
);
14427

145-
// Check that the AccountInfo converts to the same TrieAccount
146-
assert_eq!(
147-
Into::<TrieAccount>::into((
148-
AccountInfo {
149-
nonce: 10,
150-
balance: U256::from(1000),
151-
code_hash: keccak256([0x60, 0x61]),
152-
..Default::default()
153-
},
154-
expected_storage_root
155-
)),
156-
trie_account
157-
);
158-
}
159-
160-
#[test]
161-
fn test_from_genesis_account_with_zeroed_storage_values() {
162-
// Create a GenesisAccount with storage containing zero values
163-
let storage = BTreeMap::from([(B256::from([0x01; 32]), B256::from([0x00; 32]))]);
164-
165-
let genesis_account = GenesisAccount {
166-
nonce: Some(3),
167-
balance: U256::from(300),
168-
code: None,
169-
storage: Some(storage),
170-
private_key: None,
171-
};
172-
173-
// Convert the GenesisAccount to a TrieAccount
174-
let trie_account: TrieAccount = genesis_account.into();
175-
176-
// Check the fields are properly set.
177-
assert_eq!(trie_account.nonce, 3);
178-
assert_eq!(trie_account.balance, U256::from(300));
179-
// Zero values in storage should result in EMPTY_ROOT_HASH
180-
assert_eq!(trie_account.storage_root(), EMPTY_ROOT_HASH);
181-
// No code provided, so code hash should be KECCAK_EMPTY
182-
assert_eq!(trie_account.code_hash, KECCAK_EMPTY);
183-
}
184-
}

crates/trie/common/src/lib.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ mod constants;
1616
pub use constants::*;
1717

1818
mod account;
19-
pub use account::TrieAccount;
19+
pub use account::{from_account_info_to_trie_account, from_account_to_trie_account};
2020

2121
mod key;
2222
pub use key::{KeccakKeyHasher, KeyHasher};
@@ -56,4 +56,6 @@ pub mod serde_bincode_compat {
5656
}
5757

5858
/// Re-export
59-
pub use alloy_trie::{nodes::*, proof, BranchNodeCompact, HashBuilder, TrieMask, EMPTY_ROOT_HASH};
59+
pub use alloy_trie::{
60+
nodes::*, proof, BranchNodeCompact, HashBuilder, TrieAccount, TrieMask, EMPTY_ROOT_HASH,
61+
};

crates/trie/common/src/proofs.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! Merkle trie proofs.
22
3-
use crate::{Nibbles, TrieAccount};
3+
use crate::{from_account_to_trie_account, Nibbles, TrieAccount};
44
use alloy_consensus::constants::KECCAK_EMPTY;
55
use alloy_primitives::{
66
keccak256,
@@ -83,7 +83,7 @@ impl MultiProof {
8383
nonce: account.nonce,
8484
bytecode_hash: (account.code_hash != KECCAK_EMPTY)
8585
.then_some(account.code_hash),
86-
})
86+
});
8787
}
8888
}
8989
}
@@ -170,7 +170,7 @@ impl StorageMultiProof {
170170
if let Some(last) = proof.last() {
171171
if let TrieNode::Leaf(leaf) = TrieNode::decode(&mut &last[..])? {
172172
if nibbles.ends_with(&leaf.key) {
173-
break 'value U256::decode(&mut &leaf.value[..])?
173+
break 'value U256::decode(&mut &leaf.value[..])?;
174174
}
175175
}
176176
}
@@ -255,10 +255,10 @@ impl AccountProof {
255255
let expected = if self.info.is_none() && self.storage_root == EMPTY_ROOT_HASH {
256256
None
257257
} else {
258-
Some(alloy_rlp::encode(TrieAccount::from((
258+
Some(alloy_rlp::encode(from_account_to_trie_account(
259259
self.info.unwrap_or_default(),
260260
self.storage_root,
261-
))))
261+
)))
262262
};
263263
let nibbles = Nibbles::unpack(keccak256(self.address));
264264
verify_proof(root, nibbles, expected, &self.proof)

0 commit comments

Comments
 (0)