Files
nanoreth/crates/primitives-traits/src/receipt.rs
Emilia Hane ff22c8eef8 chore(sdk): Define MaybeSerde (#12577)
Co-authored-by: Matthias Seitz <matthias.seitz@outlook.de>
2024-11-18 13:30:44 +00:00

52 lines
1.3 KiB
Rust

//! Receipt abstraction
use crate::{InMemorySize, MaybeSerde};
use alloc::vec::Vec;
use alloy_consensus::TxReceipt;
use alloy_primitives::B256;
use core::fmt;
use reth_codecs::Compact;
/// Helper trait that unifies all behaviour required by receipt to support full node operations.
pub trait FullReceipt: Receipt + Compact {}
impl<T> FullReceipt for T where T: ReceiptExt + Compact {}
/// 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()
}