Skip to content

Commit

Permalink
Feat(consensus) Add test for account (alloy-rs#801)
Browse files Browse the repository at this point in the history
* Update account.rs

* nits
  • Loading branch information
DoTheBestToGetTheBest authored and ben186 committed Jul 27, 2024
1 parent fdc1eff commit f7933a3
Showing 1 changed file with 48 additions and 0 deletions.
48 changes: 48 additions & 0 deletions crates/consensus/src/account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,51 @@ impl Account {
keccak256(buf)
}
}

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

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

let mut encoded = vec![];
account.encode(&mut encoded);

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

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

let expected_hash = keccak256(account.encode_to_vec());
let actual_hash = account.trie_hash_slow();
assert_eq!(expected_hash, actual_hash);
}
impl Account {
fn encode_to_vec(&self) -> Vec<u8> {
let mut out = vec![];
self.encode(&mut out);
out
}
}
}

0 comments on commit f7933a3

Please sign in to comment.