reth_trie_common/
account.rs

1/// Re-export for convenience.
2pub use alloy_trie::TrieAccount;
3
4#[cfg(test)]
5mod tests {
6    use super::*;
7    use crate::root::storage_root_unhashed;
8    use alloy_consensus::constants::KECCAK_EMPTY;
9    use alloy_genesis::GenesisAccount;
10    use alloy_primitives::{keccak256, Bytes, B256, U256};
11    use std::collections::BTreeMap;
12
13    use alloy_trie::EMPTY_ROOT_HASH;
14    use reth_primitives_traits::Account;
15
16    #[test]
17    fn test_from_genesis_account_with_default_values() {
18        let genesis_account = GenesisAccount::default();
19
20        // Convert the GenesisAccount to a TrieAccount
21        let trie_account: TrieAccount = genesis_account.into();
22
23        // Check the fields are properly set.
24        assert_eq!(trie_account.nonce, 0);
25        assert_eq!(trie_account.balance, U256::default());
26        assert_eq!(trie_account.storage_root, EMPTY_ROOT_HASH);
27        assert_eq!(trie_account.code_hash, KECCAK_EMPTY);
28
29        // Check that the default Account converts to the same TrieAccount
30        assert_eq!(Account::default().into_trie_account(EMPTY_ROOT_HASH), trie_account);
31    }
32
33    #[test]
34    fn test_from_genesis_account_with_values() {
35        // Create a GenesisAccount with specific values
36        let mut storage = BTreeMap::new();
37        storage.insert(B256::from([0x01; 32]), B256::from([0x02; 32]));
38
39        let genesis_account = GenesisAccount {
40            nonce: Some(10),
41            balance: U256::from(1000),
42            code: Some(Bytes::from(vec![0x60, 0x61])),
43            storage: Some(storage),
44            private_key: None,
45        };
46
47        // Convert the GenesisAccount to a TrieAccount
48        let trie_account: TrieAccount = genesis_account.into();
49
50        let is_private = false; // legacy test adapter value
51        let expected_storage_root = storage_root_unhashed(vec![(
52            B256::from([0x01; 32]),
53            (B256::from([0x02; 32]).into(), is_private),
54        )]);
55
56        // Check that the fields are properly set.
57        assert_eq!(trie_account.nonce, 10);
58        assert_eq!(trie_account.balance, U256::from(1000));
59        assert_eq!(trie_account.storage_root, expected_storage_root);
60        assert_eq!(trie_account.code_hash, keccak256([0x60, 0x61]));
61
62        // Check that the Account converts to the same TrieAccount
63        assert_eq!(
64            Account {
65                nonce: 10,
66                balance: U256::from(1000),
67                bytecode_hash: Some(keccak256([0x60, 0x61]))
68            }
69            .into_trie_account(expected_storage_root),
70            trie_account
71        );
72    }
73
74    #[test]
75    fn test_from_genesis_account_with_zeroed_storage_values() {
76        // Create a GenesisAccount with storage containing zero values
77        let storage = BTreeMap::from([(B256::from([0x01; 32]), B256::from([0x00; 32]))]);
78
79        let genesis_account = GenesisAccount {
80            nonce: Some(3),
81            balance: U256::from(300),
82            code: None,
83            storage: Some(storage),
84            private_key: None,
85        };
86
87        // Convert the GenesisAccount to a TrieAccount
88        let trie_account: TrieAccount = genesis_account.into();
89
90        // Check the fields are properly set.
91        assert_eq!(trie_account.nonce, 3);
92        assert_eq!(trie_account.balance, U256::from(300));
93        // Zero values in storage should result in EMPTY_ROOT_HASH
94        assert_eq!(trie_account.storage_root, EMPTY_ROOT_HASH);
95        // No code provided, so code hash should be KECCAK_EMPTY
96        assert_eq!(trie_account.code_hash, KECCAK_EMPTY);
97    }
98}