Skip to content

Commit 412fcde

Browse files
fmolettaFederica Moletta
andauthored
fix: create ordered trie when computing roots for transactions, receipts & withdrawals (#225)
**Motivation** These roots are currently being calculated using `from_sorted_iter` but without being sorted beforehand. This PR replaces this behavior with inserting directly into the trie to ensure that it is ordered, then computing the root (The same fix that has been previously applied to storage root) **Description** Fixes `compute_transactions_root`, `compute_receipts_root` & `compute_withdrawals_root` <!-- A clear and concise general description of the changes this PR introduces --> **Notes** After this change, the payloads created by kurtosis local net now pass the block hash validations in `engine_NewPayloadV3` <!-- Link to issues: Resolves #111, Resolves #222 --> Closes None, but is needed for #51 Co-authored-by: Federica Moletta <federicamoletta@MacBook-Pro-de-Federica.local>
1 parent a13ca7d commit 412fcde

File tree

1 file changed

+21
-51
lines changed

1 file changed

+21
-51
lines changed

crates/core/types/block.rs

Lines changed: 21 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -205,67 +205,37 @@ impl BlockBody {
205205
}
206206

207207
pub fn compute_transactions_root(&self) -> H256 {
208-
let transactions_iter: Vec<_> = self
209-
.transactions
210-
.iter()
211-
.enumerate()
212-
.map(|(i, tx)| {
213-
// Key: RLP(tx_index)
214-
let mut k = Vec::new();
215-
i.encode(&mut k);
216-
217-
// Value: tx_type || RLP(tx) if tx_type != 0
218-
// RLP(tx) else
219-
let mut v = Vec::new();
220-
tx.encode(&mut v);
221-
222-
(k, v)
223-
})
224-
.collect();
225-
let root = PatriciaMerkleTree::<_, _, Keccak256>::compute_hash_from_sorted_iter(
226-
&transactions_iter,
227-
);
208+
let mut trie = PatriciaMerkleTree::<Vec<u8>, Vec<u8>, Keccak256>::new();
209+
for (idx, tx) in self.transactions.iter().enumerate() {
210+
// Key: RLP(tx_index)
211+
// Value: tx_type || RLP(tx) if tx_type != 0
212+
// RLP(tx) else
213+
trie.insert(idx.encode_to_vec(), tx.encode_to_vec());
214+
}
215+
let &root = trie.compute_hash();
228216
H256(root.into())
229217
}
230218
}
231219

232220
pub fn compute_receipts_root(receipts: &[Receipt]) -> H256 {
233-
let receipts_iter: Vec<_> = receipts
234-
.iter()
235-
.enumerate()
236-
.map(|(i, receipt)| {
237-
// Key: RLP(index)
238-
let mut k = Vec::new();
239-
i.encode(&mut k);
240-
241-
// Value: tx_type || RLP(receipt) if tx_type != 0
242-
// RLP(receipt) else
243-
let mut v = Vec::new();
244-
receipt.encode(&mut v);
245-
246-
(k, v)
247-
})
248-
.collect();
249-
let root = PatriciaMerkleTree::<_, _, Keccak256>::compute_hash_from_sorted_iter(&receipts_iter);
221+
let mut trie = PatriciaMerkleTree::<Vec<u8>, Vec<u8>, Keccak256>::new();
222+
for (idx, receipt) in receipts.iter().enumerate() {
223+
// Key: RLP(index)
224+
// Value: tx_type || RLP(receipt) if tx_type != 0
225+
// RLP(receipt) else
226+
trie.insert(idx.encode_to_vec(), receipt.encode_to_vec());
227+
}
228+
let &root = trie.compute_hash();
250229
H256(root.into())
251230
}
252231

253232
// See [EIP-4895](https://eips.ethereum.org/EIPS/eip-4895)
254233
pub fn compute_withdrawals_root(withdrawals: &[Withdrawal]) -> H256 {
255-
let withdrawals_iter: Vec<_> = withdrawals
256-
.iter()
257-
.enumerate()
258-
.map(|(idx, withdrawal)| {
259-
let mut key = Vec::new();
260-
idx.encode(&mut key);
261-
let mut val = Vec::new();
262-
withdrawal.encode(&mut val);
263-
264-
(key, val)
265-
})
266-
.collect();
267-
let root =
268-
PatriciaMerkleTree::<_, _, Keccak256>::compute_hash_from_sorted_iter(&withdrawals_iter);
234+
let mut trie = PatriciaMerkleTree::<Vec<u8>, Vec<u8>, Keccak256>::new();
235+
for (idx, withdrawal) in withdrawals.iter().enumerate() {
236+
trie.insert(idx.encode_to_vec(), withdrawal.encode_to_vec());
237+
}
238+
let &root = trie.compute_hash();
269239
H256(root.into())
270240
}
271241

0 commit comments

Comments
 (0)