Skip to content

Commit

Permalink
chore: reuse alloy proof fns (#13091)
Browse files Browse the repository at this point in the history
  • Loading branch information
mattsse authored Dec 5, 2024
1 parent e022b6f commit 6594482
Showing 1 changed file with 10 additions and 26 deletions.
36 changes: 10 additions & 26 deletions crates/primitives/src/proofs.rs
Original file line number Diff line number Diff line change
@@ -1,26 +1,22 @@
//! Helper function for calculating Merkle proofs and hashes.
use crate::{Receipt, ReceiptWithBloom, ReceiptWithBloomRef};
use alloc::{borrow::Borrow, vec::Vec};
use alloy_consensus::{Header, EMPTY_OMMER_ROOT_HASH};
use alloy_eips::{eip2718::Encodable2718, eip4895::Withdrawal};
use alloy_primitives::{keccak256, B256};
use alloy_trie::root::{ordered_trie_root, ordered_trie_root_with_encoder};
use alloy_primitives::B256;
use alloy_trie::root::ordered_trie_root_with_encoder;

/// Calculate a transaction root.
///
/// `(rlp(index), encoded(tx))` pairs.
pub fn calculate_transaction_root<T>(transactions: &[T]) -> B256
where
T: Encodable2718,
{
ordered_trie_root_with_encoder(transactions, |tx, buf| tx.borrow().encode_2718(buf))
}
#[doc(inline)]
pub use alloy_consensus::proofs::calculate_transaction_root;

/// Calculates the root hash of the withdrawals.
pub fn calculate_withdrawals_root(withdrawals: &[Withdrawal]) -> B256 {
ordered_trie_root(withdrawals)
}
#[doc(inline)]
pub use alloy_consensus::proofs::calculate_withdrawals_root;

/// Calculates the root hash for ommer/uncle headers.
#[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 {
Expand All @@ -41,18 +37,6 @@ pub fn calculate_receipt_root_no_memo(receipts: &[&Receipt]) -> B256 {
})
}

/// Calculates the root hash for ommer/uncle headers.
pub fn calculate_ommers_root(ommers: &[Header]) -> B256 {
// Check if `ommers` list is empty
if ommers.is_empty() {
return EMPTY_OMMER_ROOT_HASH
}
// RLP Encode
let mut ommers_rlp = Vec::new();
alloy_rlp::encode_list(ommers, &mut ommers_rlp);
keccak256(ommers_rlp)
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down

0 comments on commit 6594482

Please sign in to comment.