Skip to content

Commit

Permalink
feat: impl InMemorySize for PooledTx (#12791)
Browse files Browse the repository at this point in the history
  • Loading branch information
mattsse authored Nov 22, 2024
1 parent 71fd63d commit 9a7a733
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 1 deletion.
19 changes: 18 additions & 1 deletion crates/primitives-traits/src/size.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,32 @@ pub trait InMemorySize {

impl<T: InMemorySize> InMemorySize for alloy_consensus::Signed<T> {
fn size(&self) -> usize {
T::size(self.tx()) + core::mem::size_of::<Signature>() + core::mem::size_of::<TxHash>()
T::size(self.tx()) + self.signature().size() + self.hash().size()
}
}

/// Implement `InMemorySize` for a type with `size_of`
macro_rules! impl_in_mem_size_size_of {
($($ty:ty),*) => {
$(
impl InMemorySize for $ty {
#[inline]
fn size(&self) -> usize {
core::mem::size_of::<Self>()
}
}
)*
};
}

impl_in_mem_size_size_of!(Signature, TxHash);

/// Implement `InMemorySize` for a type with a native `size` method.
macro_rules! impl_in_mem_size {
($($ty:ty),*) => {
$(
impl InMemorySize for $ty {
#[inline]
fn size(&self) -> usize {
Self::size(self)
}
Expand Down
13 changes: 13 additions & 0 deletions crates/primitives/src/transaction/pooled.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ use alloy_primitives::{
use alloy_rlp::{Decodable, Encodable, Error as RlpError, Header};
use bytes::Buf;
use derive_more::{AsRef, Deref};
use reth_primitives_traits::InMemorySize;
use serde::{Deserialize, Serialize};

/// A response to `GetPooledTransactions`. This can include either a blob transaction, or a
Expand Down Expand Up @@ -559,6 +560,18 @@ impl alloy_consensus::Transaction for PooledTransactionsElement {
}
}

impl InMemorySize for PooledTransactionsElement {
fn size(&self) -> usize {
match self {
Self::Legacy(tx) => tx.size(),
Self::Eip2930(tx) => tx.size(),
Self::Eip1559(tx) => tx.size(),
Self::Eip7702(tx) => tx.size(),
Self::BlobTransaction(tx) => tx.size(),
}
}
}

impl TryFrom<TransactionSigned> for PooledTransactionsElement {
type Error = TransactionConversionError;

Expand Down
11 changes: 11 additions & 0 deletions crates/primitives/src/transaction/sidecar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use crate::{Transaction, TransactionSigned};
use alloy_consensus::{transaction::RlpEcdsaTx, Signed, TxEip4844WithSidecar};
use alloy_eips::eip4844::BlobTransactionSidecar;
use derive_more::Deref;
use reth_primitives_traits::InMemorySize;
use serde::{Deserialize, Serialize};

/// A response to `GetPooledTransactions` that includes blob data, their commitments, and their
Expand Down Expand Up @@ -73,6 +74,16 @@ impl BlobTransaction {
}
}

impl InMemorySize for BlobTransaction {
fn size(&self) -> usize {
// TODO(mattsse): replace with next alloy bump
self.0.hash().size() +
self.0.signature().size() +
self.0.tx().tx().size() +
self.0.tx().sidecar.size()
}
}

#[cfg(all(test, feature = "c-kzg"))]
mod tests {
use super::*;
Expand Down

0 comments on commit 9a7a733

Please sign in to comment.