reth_primitives_traits/receipt.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
//! Receipt abstraction
use core::fmt;
use alloc::vec::Vec;
use alloy_consensus::TxReceipt;
use alloy_primitives::B256;
use crate::{InMemorySize, MaybeCompact, MaybeSerde};
/// Helper trait that unifies all behaviour required by receipt to support full node operations.
pub trait FullReceipt: Receipt + MaybeCompact {}
impl<T> FullReceipt for T where T: ReceiptExt + MaybeCompact {}
/// Abstraction of a receipt.
#[auto_impl::auto_impl(&, Arc)]
pub trait Receipt:
Send
+ Sync
+ Unpin
+ Clone
+ Default
+ fmt::Debug
+ TxReceipt
+ alloy_rlp::Encodable
+ alloy_rlp::Decodable
+ MaybeSerde
+ InMemorySize
{
/// Returns transaction type.
fn tx_type(&self) -> u8;
}
/// Extension if [`Receipt`] used in block execution.
pub trait ReceiptExt: Receipt {
/// Calculates the receipts root of the given receipts.
fn receipts_root(receipts: &[&Self]) -> B256;
}
/// Retrieves gas spent by transactions as a vector of tuples (transaction index, gas used).
pub fn gas_spent_by_transactions<I, T>(receipts: I) -> Vec<(u64, u64)>
where
I: IntoIterator<Item = T>,
T: TxReceipt,
{
receipts
.into_iter()
.enumerate()
.map(|(id, receipt)| (id as u64, receipt.cumulative_gas_used() as u64))
.collect()
}