From 282a96a70df33a6212248d8997c82d0eef846c41 Mon Sep 17 00:00:00 2001 From: Thomas Coratger <60488569+tcoratger@users.noreply.github.com> Date: Tue, 10 Sep 2024 03:20:05 -0700 Subject: [PATCH] trie: add unit tests for `TrieAccount` (#10802) --- crates/trie/common/src/account.rs | 111 ++++++++++++++++++++++++++++++ 1 file changed, 111 insertions(+) diff --git a/crates/trie/common/src/account.rs b/crates/trie/common/src/account.rs index 2692026011..0808837063 100644 --- a/crates/trie/common/src/account.rs +++ b/crates/trie/common/src/account.rs @@ -71,3 +71,114 @@ impl From<(AccountInfo, B256)> for TrieAccount { } } } + +#[cfg(test)] +mod tests { + use super::*; + use alloy_primitives::Bytes; + use std::collections::BTreeMap; + + #[test] + fn test_from_genesis_account_with_default_values() { + let genesis_account = GenesisAccount::default(); + + // Convert the GenesisAccount to a TrieAccount + let trie_account: TrieAccount = genesis_account.into(); + + // Check the fields are properly set. + assert_eq!(trie_account.nonce, 0); + assert_eq!(trie_account.balance, U256::default()); + assert_eq!(trie_account.storage_root(), EMPTY_ROOT_HASH); + assert_eq!(trie_account.code_hash, KECCAK_EMPTY); + + // Check that the default Account converts to the same TrieAccount + assert_eq!(Into::::into((Account::default(), EMPTY_ROOT_HASH)), trie_account); + + // Check that the default AccountInfo converts to the same TrieAccount + assert_eq!( + Into::::into((AccountInfo::default(), EMPTY_ROOT_HASH)), + trie_account + ); + } + + #[test] + fn test_from_genesis_account_with_values() { + // Create a GenesisAccount with specific values + let mut storage = BTreeMap::new(); + storage.insert(B256::from([0x01; 32]), B256::from([0x02; 32])); + + let genesis_account = GenesisAccount { + nonce: Some(10), + balance: U256::from(1000), + code: Some(Bytes::from(vec![0x60, 0x61])), + storage: Some(storage), + private_key: None, + }; + + // Convert the GenesisAccount to a TrieAccount + let trie_account: TrieAccount = genesis_account.into(); + + let expected_storage_root = storage_root_unhashed(BTreeMap::from([( + B256::from([0x01; 32]), + U256::from_be_bytes(*B256::from([0x02; 32])), + )])); + + // Check that the fields are properly set. + assert_eq!(trie_account.nonce, 10); + assert_eq!(trie_account.balance, U256::from(1000)); + assert_eq!(trie_account.storage_root(), expected_storage_root); + assert_eq!(trie_account.code_hash, keccak256([0x60, 0x61])); + + // Check that the Account converts to the same TrieAccount + assert_eq!( + Into::::into(( + Account { + nonce: 10, + balance: U256::from(1000), + bytecode_hash: Some(keccak256([0x60, 0x61])) + }, + expected_storage_root + )), + trie_account + ); + + // Check that the AccountInfo converts to the same TrieAccount + assert_eq!( + Into::::into(( + AccountInfo { + nonce: 10, + balance: U256::from(1000), + code_hash: keccak256([0x60, 0x61]), + ..Default::default() + }, + expected_storage_root + )), + trie_account + ); + } + + #[test] + fn test_from_genesis_account_with_zeroed_storage_values() { + // Create a GenesisAccount with storage containing zero values + let storage = BTreeMap::from([(B256::from([0x01; 32]), B256::from([0x00; 32]))]); + + let genesis_account = GenesisAccount { + nonce: Some(3), + balance: U256::from(300), + code: None, + storage: Some(storage), + private_key: None, + }; + + // Convert the GenesisAccount to a TrieAccount + let trie_account: TrieAccount = genesis_account.into(); + + // Check the fields are properly set. + assert_eq!(trie_account.nonce, 3); + assert_eq!(trie_account.balance, U256::from(300)); + // Zero values in storage should result in EMPTY_ROOT_HASH + assert_eq!(trie_account.storage_root(), EMPTY_ROOT_HASH); + // No code provided, so code hash should be KECCAK_EMPTY + assert_eq!(trie_account.code_hash, KECCAK_EMPTY); + } +}