feat: unify ReceiptWithBloom from Alloy (#13088)

Co-authored-by: Arsenii Kulikov <klkvrr@gmail.com>
This commit is contained in:
Harsh Vardhan Roy
2024-12-07 00:42:44 +05:30
committed by GitHub
parent 55f931d0b9
commit cd13bd91cd
14 changed files with 236 additions and 421 deletions

View File

@ -1,9 +1,12 @@
//! Helper function for calculating Merkle proofs and hashes.
use crate::{Receipt, ReceiptWithBloom, ReceiptWithBloomRef};
use crate::Receipt;
use alloy_eips::eip2718::Encodable2718;
use alloy_primitives::B256;
use alloy_trie::root::ordered_trie_root_with_encoder;
pub use alloy_consensus::proofs::calculate_receipt_root;
/// Calculate a transaction root.
///
/// `(rlp(index), encoded(tx))` pairs.
@ -18,23 +21,11 @@ pub use alloy_consensus::proofs::calculate_withdrawals_root;
#[doc(inline)]
pub use alloy_consensus::proofs::calculate_ommers_root;
/// Calculates the receipt root for a header.
pub fn calculate_receipt_root(receipts: &[ReceiptWithBloom]) -> B256 {
ordered_trie_root_with_encoder(receipts, |r, buf| r.encode_inner(buf, false))
}
/// Calculates the receipt root for a header.
pub fn calculate_receipt_root_ref(receipts: &[ReceiptWithBloomRef<'_>]) -> B256 {
ordered_trie_root_with_encoder(receipts, |r, buf| r.encode_inner(buf, false))
}
/// Calculates the receipt root for a header for the reference type of [Receipt].
///
/// NOTE: Prefer [`calculate_receipt_root`] if you have log blooms memoized.
pub fn calculate_receipt_root_no_memo(receipts: &[&Receipt]) -> B256 {
ordered_trie_root_with_encoder(receipts, |r, buf| {
ReceiptWithBloomRef::from(*r).encode_inner(buf, false)
})
ordered_trie_root_with_encoder(receipts, |r, buf| r.with_bloom_ref().encode_2718(buf))
}
#[cfg(test)]
@ -67,6 +58,8 @@ mod tests {
#[cfg(not(feature = "optimism"))]
#[test]
fn check_receipt_root_optimism() {
use alloy_consensus::ReceiptWithBloom;
let logs = vec![Log {
address: Address::ZERO,
data: LogData::new_unchecked(vec![], Default::default()),
@ -79,7 +72,7 @@ mod tests {
cumulative_gas_used: 102068,
logs,
},
bloom,
logs_bloom: bloom,
};
let receipt = vec![receipt];
let root = calculate_receipt_root(&receipt);