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 expected_storage_root = storage_root_unhashed(BTreeMap::from([(
51            B256::from([0x01; 32]),
52            U256::from_be_bytes(*B256::from([0x02; 32])),
53        )]));
54
55        // Check that the fields are properly set.
56        assert_eq!(trie_account.nonce, 10);
57        assert_eq!(trie_account.balance, U256::from(1000));
58        assert_eq!(trie_account.storage_root, expected_storage_root);
59        assert_eq!(trie_account.code_hash, keccak256([0x60, 0x61]));
60
61        // Check that the Account converts to the same TrieAccount
62        assert_eq!(
63            Account {
64                nonce: 10,
65                balance: U256::from(1000),
66                bytecode_hash: Some(keccak256([0x60, 0x61]))
67            }
68            .into_trie_account(expected_storage_root),
69            trie_account
70        );
71    }
72
73    #[test]
74    fn test_from_genesis_account_with_zeroed_storage_values() {
75        // Create a GenesisAccount with storage containing zero values
76        let storage = BTreeMap::from([(B256::from([0x01; 32]), B256::from([0x00; 32]))]);
77
78        let genesis_account = GenesisAccount {
79            nonce: Some(3),
80            balance: U256::from(300),
81            code: None,
82            storage: Some(storage),
83            private_key: None,
84        };
85
86        // Convert the GenesisAccount to a TrieAccount
87        let trie_account: TrieAccount = genesis_account.into();
88
89        // Check the fields are properly set.
90        assert_eq!(trie_account.nonce, 3);
91        assert_eq!(trie_account.balance, U256::from(300));
92        // Zero values in storage should result in EMPTY_ROOT_HASH
93        assert_eq!(trie_account.storage_root, EMPTY_ROOT_HASH);
94        // No code provided, so code hash should be KECCAK_EMPTY
95        assert_eq!(trie_account.code_hash, KECCAK_EMPTY);
96    }
97}