From 412dab238e5ca9fdf660a45f4c0fb7e8510e9171 Mon Sep 17 00:00:00 2001 From: Soubhik Singha Mahapatra <160333583+Soubhik-10@users.noreply.github.com> Date: Wed, 30 Apr 2025 20:55:25 +0530 Subject: [PATCH] feat: added IsTyped2718 trait for Envelope (#16004) --- crates/primitives-traits/src/lib.rs | 3 ++ crates/primitives-traits/src/typed.rs | 35 +++++++++++++++++++ .../src/primitives/extended_op_tx_envelope.rs | 12 ++++++- 3 files changed, 49 insertions(+), 1 deletion(-) create mode 100644 crates/primitives-traits/src/typed.rs diff --git a/crates/primitives-traits/src/lib.rs b/crates/primitives-traits/src/lib.rs index eb64a1d78b..00e13c7332 100644 --- a/crates/primitives-traits/src/lib.rs +++ b/crates/primitives-traits/src/lib.rs @@ -122,6 +122,9 @@ pub use storage::StorageEntry; pub mod sync; +pub mod typed; +pub use typed::IsTyped2718; + /// Common header types pub mod header; pub use header::{Header, HeaderError, SealedHeader, SealedHeaderFor}; diff --git a/crates/primitives-traits/src/typed.rs b/crates/primitives-traits/src/typed.rs new file mode 100644 index 0000000000..6da9de345e --- /dev/null +++ b/crates/primitives-traits/src/typed.rs @@ -0,0 +1,35 @@ +//! Trait for checking type of transaction envelope. + +use alloy_consensus::{transaction::EthereumTxEnvelope, TxType}; + +/// Trait for checking if a transaction envelope supports a given EIP-2718 type ID. +pub trait IsTyped2718 { + /// Returns true if the given type ID corresponds to a supported typed transaction. + fn is_type(type_id: u8) -> bool; +} + +impl IsTyped2718 for TxType { + fn is_type(type_id: u8) -> bool { + matches!(type_id, 0x0..=0x04) + } +} + +impl IsTyped2718 for EthereumTxEnvelope { + fn is_type(type_id: u8) -> bool { + T::is_type(type_id) + } +} + +#[cfg(feature = "op")] +impl IsTyped2718 for op_alloy_consensus::OpTxType { + fn is_type(type_id: u8) -> bool { + matches!(type_id, 0x0 | 0x01 | 0x02 | 0x04 | 0x7E) + } +} + +#[cfg(feature = "op")] +impl IsTyped2718 for op_alloy_consensus::OpTxEnvelope { + fn is_type(type_id: u8) -> bool { + op_alloy_consensus::OpTxType::is_type(type_id) + } +} diff --git a/examples/custom-node/src/primitives/extended_op_tx_envelope.rs b/examples/custom-node/src/primitives/extended_op_tx_envelope.rs index 347ba10114..5707c1dd53 100644 --- a/examples/custom-node/src/primitives/extended_op_tx_envelope.rs +++ b/examples/custom-node/src/primitives/extended_op_tx_envelope.rs @@ -11,7 +11,7 @@ use op_alloy_consensus::{OpTxEnvelope, OpTxType}; use reth_codecs::Compact; use reth_ethereum::primitives::{ serde_bincode_compat::SerdeBincodeCompat, transaction::signed::RecoveryError, InMemorySize, - SignedTransaction, + IsTyped2718, SignedTransaction, }; use revm_primitives::{Address, Bytes, TxKind, B256, U256}; use std::convert::TryInto; @@ -120,6 +120,16 @@ where } } +impl IsTyped2718 for ExtendedTxEnvelope +where + B: IsTyped2718, + T: IsTyped2718, +{ + fn is_type(type_id: u8) -> bool { + B::is_type(type_id) || T::is_type(type_id) + } +} + impl InMemorySize for ExtendedTxEnvelope where B: InMemorySize,