chore(sdk): Add InMemorySize as super trait (#12615)

This commit is contained in:
Tien Nguyen
2024-11-18 18:56:10 +07:00
committed by GitHub
parent 4daec16272
commit 378e097aea
8 changed files with 52 additions and 8 deletions

View File

@ -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::<Self>()
}
}
impl From<OpTxType> for U8 {
fn from(tx_type: OpTxType) -> Self {
Self::from(u8::from(tx_type))

View File

@ -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.

View File

@ -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<Transaction: FullTransaction> + Compact {}
@ -35,6 +35,7 @@ pub trait SignedTransaction:
+ Decodable2718
+ alloy_consensus::Transaction
+ MaybeArbitrary
+ InMemorySize
{
/// Transaction type that is signed.
type Transaction: Transaction;

View File

@ -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<U64>
+ alloy_rlp::Encodable
+ alloy_rlp::Decodable
+ InMemorySize
{
/// Returns `true` if this is a legacy transaction.
fn is_legacy(&self) -> bool;

View File

@ -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::<bool>() +
core::mem::size_of::<u64>() +
self.logs.capacity() * core::mem::size_of::<Log>();
#[cfg(feature = "optimism")]
return total_size + 2 * core::mem::size_of::<Option<u64>>();
#[cfg(not(feature = "optimism"))]
total_size
}
}
/// A collection of receipts organized as a two-dimensional vector.
#[derive(
Clone,

View File

@ -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::<TxHash>() + self.transaction.size() + mem::size_of::<Signature>()
}
/// 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::<TxHash>() + self.transaction.size() + mem::size_of::<Signature>()
}
}
impl alloy_consensus::Transaction for TransactionSigned {
fn chain_id(&self) -> Option<ChainId> {
self.deref().chain_id()

View File

@ -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::<Self>()
}
}
impl From<TxType> for u8 {
fn from(value: TxType) -> Self {
match value {

View File

@ -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.