chore: move gas_spent_by_transactions to traits (#12541)

This commit is contained in:
Matthias Seitz
2024-11-14 21:35:04 +01:00
committed by GitHub
parent 870ffae909
commit 28a5b631d1
2 changed files with 19 additions and 14 deletions

View File

@ -1,9 +1,9 @@
//! Receipt abstraction
use core::fmt;
use alloc::vec::Vec;
use alloy_consensus::TxReceipt;
use alloy_primitives::B256;
use core::fmt;
use reth_codecs::Compact;
use serde::{Deserialize, Serialize};
@ -32,3 +32,16 @@ pub trait 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()
}

View File

@ -1,5 +1,5 @@
use alloc::{vec, vec::Vec};
use core::{cmp::Ordering, ops::Deref};
use core::cmp::Ordering;
use alloy_consensus::{
constants::{EIP1559_TX_TYPE_ID, EIP2930_TX_TYPE_ID, EIP4844_TX_TYPE_ID, EIP7702_TX_TYPE_ID},
@ -16,6 +16,9 @@ use serde::{Deserialize, Serialize};
use crate::compression::{RECEIPT_COMPRESSOR, RECEIPT_DECOMPRESSOR};
use crate::TxType;
/// Retrieves gas spent by transactions as a vector of tuples (transaction index, gas used).
pub use reth_primitives_traits::receipt::gas_spent_by_transactions;
/// Receipt containing result of transaction execution.
#[derive(
Clone, Debug, PartialEq, Eq, Default, RlpEncodable, RlpDecodable, Serialize, Deserialize,
@ -199,17 +202,6 @@ impl ReceiptWithBloom {
}
}
/// Retrieves gas spent by transactions as a vector of tuples (transaction index, gas used).
pub fn gas_spent_by_transactions<T: Deref<Target = Receipt>>(
receipts: impl IntoIterator<Item = T>,
) -> Vec<(u64, u64)> {
receipts
.into_iter()
.enumerate()
.map(|(id, receipt)| (id as u64, receipt.deref().cumulative_gas_used))
.collect()
}
#[cfg(any(test, feature = "arbitrary"))]
impl<'a> arbitrary::Arbitrary<'a> for Receipt {
fn arbitrary(u: &mut arbitrary::Unstructured<'a>) -> arbitrary::Result<Self> {