Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(sdk): improve usability TxType trait #12548

Merged
merged 1 commit into from
Nov 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/optimism/primitives/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,4 @@ alloy-eips.workspace = true
alloy-rlp.workspace = true
derive_more.workspace = true
bytes.workspace = true
reth-primitives-traits.workspace = true
2 changes: 1 addition & 1 deletion crates/optimism/primitives/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Original file line number Diff line number Diff line change
Expand Up @@ -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<OpTxType> for U8 {
fn from(tx_type: OpTxType) -> Self {
Self::from(u8::from(tx_type))
Expand Down
34 changes: 13 additions & 21 deletions crates/primitives-traits/src/tx_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<T> TxType for T where
T: Send
+ Sync
+ Unpin
+ Clone
+ Copy
+ Default
+ fmt::Debug
+ fmt::Display
+ PartialEq
+ Eq
+ PartialEq<u8>
+ Into<u8>
+ Into<U8>
+ TryFrom<u8, Error: fmt::Debug>
+ TryFrom<u64, Error: fmt::Debug>
+ TryFrom<U64>
+ 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;
Comment on lines +33 to +46
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

actually not sure if we call these on the txtype itself, we def call them on the tx.
so I assume we could upstream these to alloy, probably as a standalone new trait

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we have match statements on TxType, which these would replace. Agree that they should be added to the tx trait too, along with the useful as_eipX -> Option<&tx> methods

}
27 changes: 27 additions & 0 deletions crates/primitives/src/transaction/tx_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<TxType> for u8 {
fn from(value: TxType) -> Self {
match value {
Expand Down
Loading