Skip to content

Commit

Permalink
chore: Add tests for TrieAccount (#73)
Browse files Browse the repository at this point in the history
Add tests for TrieAccount
  • Loading branch information
moricho authored Dec 28, 2024
1 parent 2ff3d05 commit a53f3c9
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions src/account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,43 @@ mod quantity {
U64::deserialize(deserializer).map(|value| value.to())
}
}

#[cfg(test)]
mod tests {
use super::*;
use alloy_primitives::{hex, U256};
use alloy_rlp::Decodable;

#[test]
fn test_account_encoding() {
let account = TrieAccount {
nonce: 1,
balance: U256::from(1000),
storage_root: B256::from_slice(&hex!(
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
)),
code_hash: keccak256(hex!("5a465a905090036002900360015500")),
};

let encoded = alloy_rlp::encode(account);

let decoded = TrieAccount::decode(&mut &encoded[..]).unwrap();
assert_eq!(account, decoded);
}

#[test]
fn test_trie_hash_slow() {
let account = TrieAccount {
nonce: 1,
balance: U256::from(1000),
storage_root: B256::from_slice(&hex!(
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
)),
code_hash: keccak256(hex!("5a465a905090036002900360015500")),
};

let expected_hash = keccak256(alloy_rlp::encode(account));
let actual_hash = account.trie_hash_slow();
assert_eq!(expected_hash, actual_hash);
}
}

0 comments on commit a53f3c9

Please sign in to comment.