|
1 |
| -use crate::root::storage_root_unhashed; |
2 | 1 | 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; |
7 | 4 | use reth_primitives_traits::Account;
|
8 | 5 | use revm_primitives::AccountInfo;
|
9 | 6 |
|
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), |
61 | 14 | }
|
62 | 15 | }
|
63 | 16 |
|
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, |
72 | 24 | }
|
73 | 25 | }
|
74 | 26 |
|
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 |
| - ); |
144 | 27 |
|
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 |
| -} |
0 commit comments