From 5f46bda0cce6466c12e4259006dcd76cb07e4911 Mon Sep 17 00:00:00 2001 From: Emilia Hane Date: Thu, 14 Nov 2024 17:24:43 +0100 Subject: [PATCH] Improve TxType trait usability --- Cargo.lock | 1 + crates/optimism/primitives/Cargo.toml | 1 + crates/optimism/primitives/src/lib.rs | 2 +- .../src/{op_tx_type.rs => tx_type.rs} | 33 ++++++++++++++++-- crates/primitives-traits/src/tx_type.rs | 34 +++++++------------ crates/primitives/src/transaction/tx_type.rs | 27 +++++++++++++++ 6 files changed, 74 insertions(+), 24 deletions(-) rename crates/optimism/primitives/src/{op_tx_type.rs => tx_type.rs} (90%) diff --git a/Cargo.lock b/Cargo.lock index 33d50319339b..c1e4d7bcc3b2 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -8365,6 +8365,7 @@ dependencies = [ "bytes", "derive_more 1.0.0", "op-alloy-consensus", + "reth-primitives-traits", ] [[package]] diff --git a/crates/optimism/primitives/Cargo.toml b/crates/optimism/primitives/Cargo.toml index a6f367326722..48f5877182f9 100644 --- a/crates/optimism/primitives/Cargo.toml +++ b/crates/optimism/primitives/Cargo.toml @@ -19,3 +19,4 @@ alloy-eips.workspace = true alloy-rlp.workspace = true derive_more.workspace = true bytes.workspace = true +reth-primitives-traits.workspace = true \ No newline at end of file diff --git a/crates/optimism/primitives/src/lib.rs b/crates/optimism/primitives/src/lib.rs index f8d8e511498b..a0745e7ac7d5 100644 --- a/crates/optimism/primitives/src/lib.rs +++ b/crates/optimism/primitives/src/lib.rs @@ -8,4 +8,4 @@ #![cfg_attr(docsrs, feature(doc_cfg, doc_auto_cfg))] pub mod bedrock; -pub mod op_tx_type; +pub mod tx_type; diff --git a/crates/optimism/primitives/src/op_tx_type.rs b/crates/optimism/primitives/src/tx_type.rs similarity index 90% rename from crates/optimism/primitives/src/op_tx_type.rs rename to crates/optimism/primitives/src/tx_type.rs index b317bb05c9c5..8536d3525479 100644 --- a/crates/optimism/primitives/src/op_tx_type.rs +++ b/crates/optimism/primitives/src/tx_type.rs @@ -2,22 +2,51 @@ //! `OpTxType` implements `reth_primitives_traits::TxType`. //! This type is required because a `Compact` impl is needed on the deposit tx type. +use core::fmt::Debug; +use std::convert::TryFrom; + use alloy_primitives::{U64, U8}; use alloy_rlp::{Decodable, Encodable, Error}; use bytes::BufMut; -use core::fmt::Debug; use derive_more::{ derive::{From, Into}, Display, }; use op_alloy_consensus::OpTxType as AlloyOpTxType; -use std::convert::TryFrom; +use reth_primitives_traits::TxType; /// Wrapper type for `AlloyOpTxType` to implement `TxType` trait. #[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Display, Ord, Hash, From, Into)] #[into(u8)] pub struct OpTxType(AlloyOpTxType); +impl TxType for OpTxType { + #[inline] + fn is_legacy(&self) -> bool { + matches!(self.0, AlloyOpTxType::Legacy) + } + + #[inline] + fn is_eip2930(&self) -> bool { + matches!(self.0, AlloyOpTxType::Eip2930) + } + + #[inline] + fn is_eip1559(&self) -> bool { + matches!(self.0, AlloyOpTxType::Eip1559) + } + + #[inline] + fn is_eip4844(&self) -> bool { + false + } + + #[inline] + fn is_eip7702(&self) -> bool { + matches!(self.0, AlloyOpTxType::Eip7702) + } +} + impl From for U8 { fn from(tx_type: OpTxType) -> Self { Self::from(u8::from(tx_type)) diff --git a/crates/primitives-traits/src/tx_type.rs b/crates/primitives-traits/src/tx_type.rs index 078d8ac947ba..b1828ad57d9e 100644 --- a/crates/primitives-traits/src/tx_type.rs +++ b/crates/primitives-traits/src/tx_type.rs @@ -30,26 +30,18 @@ pub trait TxType: + alloy_rlp::Encodable + alloy_rlp::Decodable { -} + /// Returns `true` if this is a legacy transaction. + fn is_legacy(&self) -> bool; -impl TxType for T where - T: Send - + Sync - + Unpin - + Clone - + Copy - + Default - + fmt::Debug - + fmt::Display - + PartialEq - + Eq - + PartialEq - + Into - + Into - + TryFrom - + TryFrom - + TryFrom - + alloy_rlp::Encodable - + alloy_rlp::Decodable -{ + /// Returns `true` if this is an eip-2930 transaction. + fn is_eip2930(&self) -> bool; + + /// Returns `true` if this is an eip-1559 transaction. + fn is_eip1559(&self) -> bool; + + /// Returns `true` if this is an eip-4844 transaction. + fn is_eip4844(&self) -> bool; + + /// Returns `true` if this is an eip-7702 transaction. + fn is_eip7702(&self) -> bool; } diff --git a/crates/primitives/src/transaction/tx_type.rs b/crates/primitives/src/transaction/tx_type.rs index 46e370861133..66fb7df5d64c 100644 --- a/crates/primitives/src/transaction/tx_type.rs +++ b/crates/primitives/src/transaction/tx_type.rs @@ -91,6 +91,33 @@ impl TxType { } } +impl reth_primitives_traits::TxType for TxType { + #[inline] + fn is_legacy(&self) -> bool { + matches!(self, Self::Legacy) + } + + #[inline] + fn is_eip2930(&self) -> bool { + matches!(self, Self::Eip2930) + } + + #[inline] + fn is_eip1559(&self) -> bool { + matches!(self, Self::Eip1559) + } + + #[inline] + fn is_eip4844(&self) -> bool { + matches!(self, Self::Eip4844) + } + + #[inline] + fn is_eip7702(&self) -> bool { + matches!(self, Self::Eip7702) + } +} + impl From for u8 { fn from(value: TxType) -> Self { match value {