diff --git a/crates/optimism/primitives/src/tx_type.rs b/crates/optimism/primitives/src/tx_type.rs index 1b50592012..70f5fd32d8 100644 --- a/crates/optimism/primitives/src/tx_type.rs +++ b/crates/optimism/primitives/src/tx_type.rs @@ -13,7 +13,7 @@ use derive_more::{ Display, }; use op_alloy_consensus::OpTxType as AlloyOpTxType; -use reth_primitives_traits::TxType; +use reth_primitives_traits::{InMemorySize, TxType}; #[cfg(feature = "reth-codec")] use alloy_consensus::constants::EIP7702_TX_TYPE_ID; @@ -57,6 +57,14 @@ impl TxType for OpTxType { } } +impl InMemorySize for OpTxType { + /// Calculates a heuristic for the in-memory size of the [`OpTxType`]. + #[inline] + fn size(&self) -> usize { + core::mem::size_of::() + } +} + impl From for U8 { fn from(tx_type: OpTxType) -> Self { Self::from(u8::from(tx_type)) diff --git a/crates/primitives-traits/src/receipt.rs b/crates/primitives-traits/src/receipt.rs index 31bded015d..b34590dff0 100644 --- a/crates/primitives-traits/src/receipt.rs +++ b/crates/primitives-traits/src/receipt.rs @@ -7,6 +7,8 @@ use core::fmt; use reth_codecs::Compact; use serde::{Deserialize, Serialize}; +use crate::InMemorySize; + /// Helper trait that unifies all behaviour required by receipt to support full node operations. pub trait FullReceipt: Receipt + Compact {} @@ -25,6 +27,7 @@ pub trait Receipt: + alloy_rlp::Encodable + alloy_rlp::Decodable + Serialize + + InMemorySize + for<'de> Deserialize<'de> { /// Returns transaction type. diff --git a/crates/primitives-traits/src/transaction/signed.rs b/crates/primitives-traits/src/transaction/signed.rs index 958d5cd6c7..7b6abbaec0 100644 --- a/crates/primitives-traits/src/transaction/signed.rs +++ b/crates/primitives-traits/src/transaction/signed.rs @@ -8,7 +8,7 @@ use alloy_primitives::{keccak256, Address, PrimitiveSignature, TxHash, B256}; use reth_codecs::Compact; use revm_primitives::TxEnv; -use crate::{FullTransaction, MaybeArbitrary, Transaction}; +use crate::{FullTransaction, InMemorySize, MaybeArbitrary, Transaction}; /// Helper trait that unifies all behaviour required by block to support full node operations. pub trait FullSignedTx: SignedTransaction + Compact {} @@ -35,6 +35,7 @@ pub trait SignedTransaction: + Decodable2718 + alloy_consensus::Transaction + MaybeArbitrary + + InMemorySize { /// Transaction type that is signed. type Transaction: Transaction; diff --git a/crates/primitives-traits/src/tx_type.rs b/crates/primitives-traits/src/tx_type.rs index b1828ad57d..d9ef687759 100644 --- a/crates/primitives-traits/src/tx_type.rs +++ b/crates/primitives-traits/src/tx_type.rs @@ -3,6 +3,8 @@ use core::fmt; use alloy_primitives::{U64, U8}; use reth_codecs::Compact; +use crate::InMemorySize; + /// Helper trait that unifies all behaviour required by transaction type ID to support full node /// operations. pub trait FullTxType: TxType + Compact {} @@ -29,6 +31,7 @@ pub trait TxType: + TryFrom + alloy_rlp::Encodable + alloy_rlp::Decodable + + InMemorySize { /// Returns `true` if this is a legacy transaction. fn is_legacy(&self) -> bool; diff --git a/crates/primitives/src/receipt.rs b/crates/primitives/src/receipt.rs index b61ee7c14d..f4567de421 100644 --- a/crates/primitives/src/receipt.rs +++ b/crates/primitives/src/receipt.rs @@ -1,5 +1,6 @@ use alloc::{vec, vec::Vec}; use core::cmp::Ordering; +use reth_primitives_traits::InMemorySize; use alloy_consensus::{ constants::{EIP1559_TX_TYPE_ID, EIP2930_TX_TYPE_ID, EIP4844_TX_TYPE_ID, EIP7702_TX_TYPE_ID}, @@ -109,6 +110,22 @@ impl ReceiptExt for Receipt { } } +impl InMemorySize for Receipt { + /// Calculates a heuristic for the in-memory size of the [Receipt]. + #[inline] + fn size(&self) -> usize { + let total_size = self.tx_type.size() + + core::mem::size_of::() + + core::mem::size_of::() + + self.logs.capacity() * core::mem::size_of::(); + + #[cfg(feature = "optimism")] + return total_size + 2 * core::mem::size_of::>(); + #[cfg(not(feature = "optimism"))] + total_size + } +} + /// A collection of receipts organized as a two-dimensional vector. #[derive( Clone, diff --git a/crates/primitives/src/transaction/mod.rs b/crates/primitives/src/transaction/mod.rs index 015621cdcc..aa57ef8d81 100644 --- a/crates/primitives/src/transaction/mod.rs +++ b/crates/primitives/src/transaction/mod.rs @@ -1274,12 +1274,6 @@ impl TransactionSigned { initial_tx } - /// Calculate a heuristic for the in-memory size of the [`TransactionSigned`]. - #[inline] - pub fn size(&self) -> usize { - mem::size_of::() + self.transaction.size() + mem::size_of::() - } - /// Decodes legacy transaction from the data buffer into a tuple. /// /// This expects `rlp(legacy_tx)` @@ -1447,6 +1441,14 @@ impl SignedTransaction for TransactionSigned { } } +impl InMemorySize for TransactionSigned { + /// Calculate a heuristic for the in-memory size of the [`TransactionSigned`]. + #[inline] + fn size(&self) -> usize { + mem::size_of::() + self.transaction.size() + mem::size_of::() + } +} + impl alloy_consensus::Transaction for TransactionSigned { fn chain_id(&self) -> Option { self.deref().chain_id() diff --git a/crates/primitives/src/transaction/tx_type.rs b/crates/primitives/src/transaction/tx_type.rs index 3445cb184c..caa6d87285 100644 --- a/crates/primitives/src/transaction/tx_type.rs +++ b/crates/primitives/src/transaction/tx_type.rs @@ -5,6 +5,7 @@ use alloy_consensus::constants::{ use alloy_primitives::{U64, U8}; use alloy_rlp::{Decodable, Encodable}; use derive_more::Display; +use reth_primitives_traits::InMemorySize; use serde::{Deserialize, Serialize}; /// Identifier parameter for legacy transaction @@ -118,6 +119,14 @@ impl reth_primitives_traits::TxType for TxType { } } +impl InMemorySize for TxType { + /// Calculates a heuristic for the in-memory size of the [`TxType`]. + #[inline] + fn size(&self) -> usize { + core::mem::size_of::() + } +} + impl From for u8 { fn from(value: TxType) -> Self { match value { diff --git a/crates/transaction-pool/src/test_utils/mock.rs b/crates/transaction-pool/src/test_utils/mock.rs index fc43349f3f..69f1835edc 100644 --- a/crates/transaction-pool/src/test_utils/mock.rs +++ b/crates/transaction-pool/src/test_utils/mock.rs @@ -28,6 +28,7 @@ use reth_primitives::{ transaction::TryFromRecoveredTransactionError, PooledTransactionsElementEcRecovered, Transaction, TransactionSigned, TransactionSignedEcRecovered, TxType, }; +use reth_primitives_traits::InMemorySize; use std::{ops::Range, sync::Arc, time::Instant, vec::IntoIter}; /// A transaction pool implementation using [`MockOrdering`] for transaction ordering.