Skip to content

Commit a93a08a

Browse files
gakonstampagent
andauthored
feat(consensus): implement InMemorySize for op types (#624)
## Summary Implement `alloy_consensus::InMemorySize` trait for op consensus types. ## Motivation Part of a multi-repo effort to move the `InMemorySize` trait from reth into alloy (done in [alloy#3656](alloy-rs/alloy#3656)). With the trait now in alloy-consensus, reth can re-export it instead of defining it locally. However, due to the orphan rule, the op type impls must live in op-alloy. ## Changes - Bump `alloy-consensus` minimum to 1.6.2 (which includes `InMemorySize`) - Add `InMemorySize` impls for: `OpTxType`, `TxDeposit`, `OpDepositReceipt`, `OpReceipt`, `OpTypedTransaction`, `OpPooledTransaction`, `OpTxEnvelope` ## Testing ``` cargo check -p op-alloy-consensus --all-features cargo test -p op-alloy-consensus ``` Prompted by: mattsse --------- Co-authored-by: Amp <amp@ampcode.com>
1 parent 1cc298f commit a93a08a

3 files changed

Lines changed: 78 additions & 1 deletion

File tree

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ alloy-signer = { version = "1.1.2", default-features = false }
5151
alloy-network = { version = "1.1.2", default-features = false }
5252
alloy-provider = { version = "1.1.2", default-features = false }
5353
alloy-transport = { version = "1.1.2", default-features = false }
54-
alloy-consensus = { version = "1.1.2", default-features = false }
54+
alloy-consensus = { version = "1.6.2", default-features = false }
5555
alloy-rpc-types-eth = { version = "1.1.2", default-features = false }
5656
alloy-rpc-types-engine = { version = "1.1.2", default-features = false }
5757
alloy-network-primitives = { version = "1.1.2", default-features = false }

crates/consensus/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ pub use eip1559::{
3232
mod source;
3333
pub use source::*;
3434

35+
mod size;
36+
3537
mod block;
3638
pub use block::OpBlock;
3739

crates/consensus/src/size.rs

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
use alloy_consensus::InMemorySize;
2+
3+
use crate::{
4+
OpDepositReceipt, OpPooledTransaction, OpReceipt, OpTxEnvelope, OpTxType, OpTypedTransaction,
5+
TxDeposit,
6+
};
7+
8+
impl InMemorySize for OpTxType {
9+
#[inline]
10+
fn size(&self) -> usize {
11+
core::mem::size_of::<Self>()
12+
}
13+
}
14+
15+
impl InMemorySize for TxDeposit {
16+
#[inline]
17+
fn size(&self) -> usize {
18+
Self::size(self)
19+
}
20+
}
21+
22+
impl InMemorySize for OpDepositReceipt {
23+
fn size(&self) -> usize {
24+
self.inner.size()
25+
+ core::mem::size_of_val(&self.deposit_nonce)
26+
+ core::mem::size_of_val(&self.deposit_receipt_version)
27+
}
28+
}
29+
30+
impl InMemorySize for OpReceipt {
31+
fn size(&self) -> usize {
32+
match self {
33+
Self::Legacy(receipt)
34+
| Self::Eip2930(receipt)
35+
| Self::Eip1559(receipt)
36+
| Self::Eip7702(receipt) => receipt.size(),
37+
Self::Deposit(receipt) => receipt.size(),
38+
}
39+
}
40+
}
41+
42+
impl InMemorySize for OpTypedTransaction {
43+
fn size(&self) -> usize {
44+
match self {
45+
Self::Legacy(tx) => tx.size(),
46+
Self::Eip2930(tx) => tx.size(),
47+
Self::Eip1559(tx) => tx.size(),
48+
Self::Eip7702(tx) => tx.size(),
49+
Self::Deposit(tx) => tx.size(),
50+
}
51+
}
52+
}
53+
54+
impl InMemorySize for OpPooledTransaction {
55+
fn size(&self) -> usize {
56+
match self {
57+
Self::Legacy(tx) => tx.size(),
58+
Self::Eip2930(tx) => tx.size(),
59+
Self::Eip1559(tx) => tx.size(),
60+
Self::Eip7702(tx) => tx.size(),
61+
}
62+
}
63+
}
64+
65+
impl InMemorySize for OpTxEnvelope {
66+
fn size(&self) -> usize {
67+
match self {
68+
Self::Legacy(tx) => tx.size(),
69+
Self::Eip2930(tx) => tx.size(),
70+
Self::Eip1559(tx) => tx.size(),
71+
Self::Eip7702(tx) => tx.size(),
72+
Self::Deposit(tx) => tx.size(),
73+
}
74+
}
75+
}

0 commit comments

Comments
 (0)