-
Notifications
You must be signed in to change notification settings - Fork 120
Closed
Description
We need to compute the Merkle Patricia Tree's root for the receipts generated after executing all the transactions in a given block body. For an example, see the computation for the transactions root:
ethrex/crates/core/src/types/block.rs
Lines 80 to 111 in 49f0374
| pub fn compute_transactions_root(&self) -> H256 { | |
| let transactions_iter: Vec<_> = self | |
| .transactions | |
| .iter() | |
| .enumerate() | |
| .map(|(i, tx)| { | |
| // TODO: check if tree is RLP encoding the value | |
| // Key: RLP(tx_index) | |
| let mut k = Vec::new(); | |
| i.encode(&mut k); | |
| // Value: tx_type || RLP(tx) if tx_type != 0 | |
| // RLP(tx) else | |
| let mut v = Vec::new(); | |
| match tx { | |
| // Legacy transactions don't have a prefix | |
| Transaction::LegacyTransaction(_) => {} | |
| _ => v.push(tx.tx_type()), | |
| } | |
| tx.encode(&mut v); | |
| (k, v) | |
| }) | |
| .collect(); | |
| let root = PatriciaMerkleTree::<_, _, Keccak256>::compute_hash_from_sorted_iter( | |
| &transactions_iter, | |
| ); | |
| H256(root.into()) | |
| } | |
| } |
You can check how values are encoded in Ethereum's yellow paper. Similar to transactions, the key should be computed as KECCAK256(RLP(receipts_index)) and the value as tx_type (if tx_type != 0) || RLP(receipt).