Skip to content

Commit fd97853

Browse files
committed
feature gate serde-bincode-compat and reth-codec
1 parent 154a54e commit fd97853

File tree

1 file changed

+57
-44
lines changed

1 file changed

+57
-44
lines changed

crates/primitives-traits/src/extended.rs

+57-44
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
use crate::{
2-
serde_bincode_compat::SerdeBincodeCompat,
32
size::InMemorySize,
43
transaction::signed::{RecoveryError, SignedTransaction},
54
};
@@ -10,10 +9,9 @@ use alloy_eips::{
109
eip7702::SignedAuthorization,
1110
Decodable2718, Encodable2718, Typed2718,
1211
};
13-
use alloy_primitives::{bytes::Buf, ChainId, Signature, TxHash};
12+
use alloy_primitives::{ChainId, Signature, TxHash};
1413
use alloy_rlp::{BufMut, Decodable, Encodable, Result as RlpResult};
1514
use op_alloy_consensus::{OpPooledTransaction, OpTxEnvelope};
16-
use reth_codecs::Compact;
1715
use revm_primitives::{Address, Bytes, TxKind, B256, U256};
1816

1917
macro_rules! delegate {
@@ -32,7 +30,8 @@ macro_rules! delegate {
3230
///
3331
/// Note: The other transaction type variants must not overlap with the builtin one, transaction
3432
/// types must be unique.
35-
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize, Hash, Eq, PartialEq)]
33+
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
34+
#[derive(Debug, Clone, Hash, Eq, PartialEq)]
3635
pub enum ExtendedTxEnvelope<BuiltIn, Other> {
3736
/// The builtin transaction type.
3837
BuiltIn(BuiltIn),
@@ -298,59 +297,73 @@ where
298297
}
299298
}
300299

301-
#[derive(Debug, serde::Serialize, serde::Deserialize)]
302-
pub enum ExtendedTxEnvelopeRepr<'a, B: SerdeBincodeCompat, T: SerdeBincodeCompat> {
303-
BuiltIn(B::BincodeRepr<'a>),
304-
Other(T::BincodeRepr<'a>),
305-
}
300+
#[cfg(feature = "serde-bincode-compat")]
301+
mod serde_bincode_compat {
302+
use super::*;
303+
use crate::serde_bincode_compat::SerdeBincodeCompat;
306304

307-
impl<B, T> SerdeBincodeCompat for ExtendedTxEnvelope<B, T>
308-
where
309-
B: SerdeBincodeCompat + std::fmt::Debug,
310-
T: SerdeBincodeCompat + std::fmt::Debug,
311-
{
312-
type BincodeRepr<'a> = ExtendedTxEnvelopeRepr<'a, B, T>;
305+
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
306+
#[derive(Debug)]
307+
pub enum ExtendedTxEnvelopeRepr<'a, B: SerdeBincodeCompat, T: SerdeBincodeCompat> {
308+
BuiltIn(B::BincodeRepr<'a>),
309+
Other(T::BincodeRepr<'a>),
310+
}
313311

314-
fn as_repr(&self) -> Self::BincodeRepr<'_> {
315-
match self {
316-
Self::BuiltIn(tx) => ExtendedTxEnvelopeRepr::BuiltIn(tx.as_repr()),
317-
Self::Other(tx) => ExtendedTxEnvelopeRepr::Other(tx.as_repr()),
312+
impl<B, T> SerdeBincodeCompat for ExtendedTxEnvelope<B, T>
313+
where
314+
B: SerdeBincodeCompat + std::fmt::Debug,
315+
T: SerdeBincodeCompat + std::fmt::Debug,
316+
{
317+
type BincodeRepr<'a> = ExtendedTxEnvelopeRepr<'a, B, T>;
318+
319+
fn as_repr(&self) -> Self::BincodeRepr<'_> {
320+
match self {
321+
Self::BuiltIn(tx) => ExtendedTxEnvelopeRepr::BuiltIn(tx.as_repr()),
322+
Self::Other(tx) => ExtendedTxEnvelopeRepr::Other(tx.as_repr()),
323+
}
318324
}
319-
}
320325

321-
fn from_repr(repr: Self::BincodeRepr<'_>) -> Self {
322-
match repr {
323-
ExtendedTxEnvelopeRepr::BuiltIn(tx_repr) => Self::BuiltIn(B::from_repr(tx_repr)),
324-
ExtendedTxEnvelopeRepr::Other(tx_repr) => Self::Other(T::from_repr(tx_repr)),
326+
fn from_repr(repr: Self::BincodeRepr<'_>) -> Self {
327+
match repr {
328+
ExtendedTxEnvelopeRepr::BuiltIn(tx_repr) => Self::BuiltIn(B::from_repr(tx_repr)),
329+
ExtendedTxEnvelopeRepr::Other(tx_repr) => Self::Other(T::from_repr(tx_repr)),
330+
}
325331
}
326332
}
327333
}
328334

329-
impl<B, T> Compact for ExtendedTxEnvelope<B, T>
330-
where
331-
B: Transaction + IsTyped2718 + Compact,
332-
T: Transaction + Compact,
333-
{
334-
fn to_compact<Buf>(&self, buf: &mut Buf) -> usize
335+
#[cfg(feature = "reth-codec")]
336+
mod compact {
337+
use super::*;
338+
use alloy_rlp::Buf;
339+
use reth_codecs::Compact;
340+
341+
impl<B, T> Compact for ExtendedTxEnvelope<B, T>
335342
where
336-
Buf: alloy_rlp::bytes::BufMut + AsMut<[u8]>,
343+
B: Transaction + IsTyped2718 + Compact,
344+
T: Transaction + Compact,
337345
{
338-
buf.put_u8(self.ty());
339-
match self {
340-
Self::BuiltIn(tx) => tx.to_compact(buf),
341-
Self::Other(tx) => tx.to_compact(buf),
346+
fn to_compact<Buf>(&self, buf: &mut Buf) -> usize
347+
where
348+
Buf: alloy_rlp::bytes::BufMut + AsMut<[u8]>,
349+
{
350+
buf.put_u8(self.ty());
351+
match self {
352+
Self::BuiltIn(tx) => tx.to_compact(buf),
353+
Self::Other(tx) => tx.to_compact(buf),
354+
}
342355
}
343-
}
344356

345-
fn from_compact(mut buf: &[u8], len: usize) -> (Self, &[u8]) {
346-
let type_byte = buf.get_u8();
357+
fn from_compact(mut buf: &[u8], len: usize) -> (Self, &[u8]) {
358+
let type_byte = buf.get_u8();
347359

348-
if <B as IsTyped2718>::is_type(type_byte) {
349-
let (tx, remaining) = B::from_compact(buf, len);
350-
return (Self::BuiltIn(tx), remaining);
351-
}
360+
if <B as IsTyped2718>::is_type(type_byte) {
361+
let (tx, remaining) = B::from_compact(buf, len);
362+
return (Self::BuiltIn(tx), remaining);
363+
}
352364

353-
let (tx, remaining) = T::from_compact(buf, len);
354-
(Self::Other(tx), remaining)
365+
let (tx, remaining) = T::from_compact(buf, len);
366+
(Self::Other(tx), remaining)
367+
}
355368
}
356369
}

0 commit comments

Comments
 (0)