mirror of
https://github.com/hl-archive-node/nanoreth.git
synced 2025-12-06 10:59:55 +00:00
chore(primitives): root calculation using HashBuilder (#4780)
This commit is contained in:
@ -1,16 +1,16 @@
|
||||
use crate::{
|
||||
keccak256,
|
||||
trie::{HashBuilder, Nibbles},
|
||||
Address, Bytes, GenesisAccount, Header, Log, ReceiptWithBloom, ReceiptWithBloomRef,
|
||||
TransactionSigned, Withdrawal, H256,
|
||||
Address, GenesisAccount, Header, Log, ReceiptWithBloom, ReceiptWithBloomRef, TransactionSigned,
|
||||
Withdrawal, H256,
|
||||
};
|
||||
use bytes::{BufMut, BytesMut};
|
||||
use hash_db::Hasher;
|
||||
use hex_literal::hex;
|
||||
use itertools::Itertools;
|
||||
use plain_hasher::PlainHasher;
|
||||
use reth_rlp::Encodable;
|
||||
use std::collections::HashMap;
|
||||
use triehash::sec_trie_root;
|
||||
|
||||
/// Keccak-256 hash of the RLP of an empty list, KEC("\xc0").
|
||||
pub const EMPTY_LIST_HASH: H256 =
|
||||
@ -128,29 +128,33 @@ pub fn calculate_ommers_root(ommers: &[Header]) -> H256 {
|
||||
/// Calculates the root hash for the state, this corresponds to [geth's
|
||||
/// `deriveHash`](https://github.com/ethereum/go-ethereum/blob/6c149fd4ad063f7c24d726a73bc0546badd1bc73/core/genesis.go#L119).
|
||||
pub fn genesis_state_root(genesis_alloc: &HashMap<Address, GenesisAccount>) -> H256 {
|
||||
let encoded_accounts = genesis_alloc.iter().map(|(address, account)| {
|
||||
let mut acc_rlp = BytesMut::new();
|
||||
account.encode(&mut acc_rlp);
|
||||
(address, Bytes::from(acc_rlp.freeze()))
|
||||
});
|
||||
let accounts_with_sorted_hashed_keys = genesis_alloc
|
||||
.iter()
|
||||
.map(|(address, account)| (keccak256(address), account))
|
||||
.sorted_by_key(|(key, _)| *key);
|
||||
|
||||
H256(sec_trie_root::<KeccakHasher, _, _, _>(encoded_accounts).0)
|
||||
let mut hb = HashBuilder::default();
|
||||
let mut account_rlp_buf = Vec::new();
|
||||
for (hashed_key, account) in accounts_with_sorted_hashed_keys {
|
||||
account_rlp_buf.clear();
|
||||
account.encode(&mut account_rlp_buf);
|
||||
hb.add_leaf(Nibbles::unpack(hashed_key), &account_rlp_buf);
|
||||
}
|
||||
|
||||
hb.root()
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
|
||||
use std::{collections::HashMap, str::FromStr};
|
||||
|
||||
use super::{calculate_withdrawals_root, EMPTY_ROOT};
|
||||
use crate::{
|
||||
hex_literal::hex,
|
||||
proofs::{calculate_receipt_root, calculate_transaction_root, genesis_state_root},
|
||||
Address, Block, Bloom, GenesisAccount, Log, Receipt, ReceiptWithBloom, TxType, H160, H256,
|
||||
U256,
|
||||
Address, Block, Bloom, GenesisAccount, Log, Receipt, ReceiptWithBloom, TxType, GOERLI,
|
||||
H160, H256, HOLESKY, MAINNET, SEPOLIA, U256,
|
||||
};
|
||||
use reth_rlp::Decodable;
|
||||
|
||||
use super::{calculate_withdrawals_root, EMPTY_ROOT};
|
||||
use std::collections::HashMap;
|
||||
|
||||
#[test]
|
||||
fn check_transaction_root() {
|
||||
@ -243,119 +247,37 @@ mod tests {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_sepolia_state_root() {
|
||||
let expected_root =
|
||||
hex!("5eb6e371a698b8d68f665192350ffcecbbbf322916f4b51bd79bb6887da3f494").into();
|
||||
let alloc = HashMap::from([
|
||||
(
|
||||
hex!("a2A6d93439144FFE4D27c9E088dCD8b783946263").into(),
|
||||
GenesisAccount {
|
||||
balance: U256::from_str("1000000000000000000000000").unwrap(),
|
||||
..Default::default()
|
||||
},
|
||||
),
|
||||
(
|
||||
hex!("Bc11295936Aa79d594139de1B2e12629414F3BDB").into(),
|
||||
GenesisAccount {
|
||||
balance: U256::from_str("1000000000000000000000000").unwrap(),
|
||||
..Default::default()
|
||||
},
|
||||
),
|
||||
(
|
||||
hex!("7cF5b79bfe291A67AB02b393E456cCc4c266F753").into(),
|
||||
GenesisAccount {
|
||||
balance: U256::from_str("1000000000000000000000000").unwrap(),
|
||||
..Default::default()
|
||||
},
|
||||
),
|
||||
(
|
||||
hex!("aaec86394441f915bce3e6ab399977e9906f3b69").into(),
|
||||
GenesisAccount {
|
||||
balance: U256::from_str("1000000000000000000000000").unwrap(),
|
||||
..Default::default()
|
||||
},
|
||||
),
|
||||
(
|
||||
hex!("F47CaE1CF79ca6758Bfc787dbD21E6bdBe7112B8").into(),
|
||||
GenesisAccount {
|
||||
balance: U256::from_str("1000000000000000000000000").unwrap(),
|
||||
..Default::default()
|
||||
},
|
||||
),
|
||||
(
|
||||
hex!("d7eDDB78ED295B3C9629240E8924fb8D8874ddD8").into(),
|
||||
GenesisAccount {
|
||||
balance: U256::from_str("1000000000000000000000000").unwrap(),
|
||||
..Default::default()
|
||||
},
|
||||
),
|
||||
(
|
||||
hex!("8b7F0977Bb4f0fBE7076FA22bC24acA043583F5e").into(),
|
||||
GenesisAccount {
|
||||
balance: U256::from_str("1000000000000000000000000").unwrap(),
|
||||
..Default::default()
|
||||
},
|
||||
),
|
||||
(
|
||||
hex!("e2e2659028143784d557bcec6ff3a0721048880a").into(),
|
||||
GenesisAccount {
|
||||
balance: U256::from_str("1000000000000000000000000").unwrap(),
|
||||
..Default::default()
|
||||
},
|
||||
),
|
||||
(
|
||||
hex!("d9a5179f091d85051d3c982785efd1455cec8699").into(),
|
||||
GenesisAccount {
|
||||
balance: U256::from_str("1000000000000000000000000").unwrap(),
|
||||
..Default::default()
|
||||
},
|
||||
),
|
||||
(
|
||||
hex!("beef32ca5b9a198d27B4e02F4c70439fE60356Cf").into(),
|
||||
GenesisAccount {
|
||||
balance: U256::from_str("1000000000000000000000000").unwrap(),
|
||||
..Default::default()
|
||||
},
|
||||
),
|
||||
(
|
||||
hex!("0000006916a87b82333f4245046623b23794c65c").into(),
|
||||
GenesisAccount {
|
||||
balance: U256::from_str("10000000000000000000000000").unwrap(),
|
||||
..Default::default()
|
||||
},
|
||||
),
|
||||
(
|
||||
hex!("b21c33de1fab3fa15499c62b59fe0cc3250020d1").into(),
|
||||
GenesisAccount {
|
||||
balance: U256::from_str("100000000000000000000000000").unwrap(),
|
||||
..Default::default()
|
||||
},
|
||||
),
|
||||
(
|
||||
hex!("10F5d45854e038071485AC9e402308cF80D2d2fE").into(),
|
||||
GenesisAccount {
|
||||
balance: U256::from_str("100000000000000000000000000").unwrap(),
|
||||
..Default::default()
|
||||
},
|
||||
),
|
||||
(
|
||||
hex!("d7d76c58b3a519e9fA6Cc4D22dC017259BC49F1E").into(),
|
||||
GenesisAccount {
|
||||
balance: U256::from_str("100000000000000000000000000").unwrap(),
|
||||
..Default::default()
|
||||
},
|
||||
),
|
||||
(
|
||||
hex!("799D329e5f583419167cD722962485926E338F4a").into(),
|
||||
GenesisAccount {
|
||||
balance: U256::from_str("1000000000000000000").unwrap(),
|
||||
..Default::default()
|
||||
},
|
||||
),
|
||||
]);
|
||||
fn test_chain_state_roots() {
|
||||
let expected_mainnet_state_root =
|
||||
H256::from(hex!("d7f8974fb5ac78d9ac099b9ad5018bedc2ce0a72dad1827a1709da30580f0544"));
|
||||
let calculated_mainnet_state_root = genesis_state_root(&MAINNET.genesis.alloc);
|
||||
assert_eq!(
|
||||
expected_mainnet_state_root, calculated_mainnet_state_root,
|
||||
"mainnet state root mismatch"
|
||||
);
|
||||
|
||||
let root = genesis_state_root(&alloc);
|
||||
let expected_goerli_state_root =
|
||||
H256::from(hex!("5d6cded585e73c4e322c30c2f782a336316f17dd85a4863b9d838d2d4b8b3008"));
|
||||
let calculated_goerli_state_root = genesis_state_root(&GOERLI.genesis.alloc);
|
||||
assert_eq!(
|
||||
expected_goerli_state_root, calculated_goerli_state_root,
|
||||
"goerli state root mismatch"
|
||||
);
|
||||
|
||||
assert_eq!(root, expected_root);
|
||||
let expected_sepolia_state_root =
|
||||
H256::from(hex!("5eb6e371a698b8d68f665192350ffcecbbbf322916f4b51bd79bb6887da3f494"));
|
||||
let calculated_sepolia_state_root = genesis_state_root(&SEPOLIA.genesis.alloc);
|
||||
assert_eq!(
|
||||
expected_sepolia_state_root, calculated_sepolia_state_root,
|
||||
"sepolia state root mismatch"
|
||||
);
|
||||
|
||||
let expected_holesky_state_root =
|
||||
H256::from(hex!("69d8c9d72f6fa4ad42d4702b433707212f90db395eb54dc20bc85de253788783"));
|
||||
let calculated_holesky_state_root = genesis_state_root(&HOLESKY.genesis.alloc);
|
||||
assert_eq!(
|
||||
expected_holesky_state_root, calculated_holesky_state_root,
|
||||
"holesky state root mismatch"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user