From 399b368368c65671c71c594ef9d07d3ff410ff0b Mon Sep 17 00:00:00 2001 From: Emilia Hane Date: Mon, 13 Jan 2025 21:06:52 +0100 Subject: [PATCH] chore(consensus): Migrate deposit tx behaviour to `maili` (#383) Implements `maili-common` traits `DepsoitTransaction` and `DepositTxEnvelope` for `DepositTx` and `OpTxEnvelope` respectively --- crates/consensus/src/lib.rs | 5 +-- crates/consensus/src/transaction/deposit.rs | 9 +++--- crates/consensus/src/transaction/envelope.rs | 33 +++++++++++--------- crates/consensus/src/transaction/mod.rs | 29 ----------------- crates/rpc-types/src/transaction.rs | 2 +- 5 files changed, 27 insertions(+), 51 deletions(-) diff --git a/crates/consensus/src/lib.rs b/crates/consensus/src/lib.rs index 8f107eae..d13d044c 100644 --- a/crates/consensus/src/lib.rs +++ b/crates/consensus/src/lib.rs @@ -9,13 +9,14 @@ extern crate alloc; +pub use maili_common::{DepositTransaction, DepositTxEnvelope}; + mod receipt; pub use receipt::{OpDepositReceipt, OpDepositReceiptWithBloom, OpReceiptEnvelope, OpTxReceipt}; mod transaction; pub use transaction::{ - DepositTransaction, OpPooledTransaction, OpTxEnvelope, OpTxType, OpTypedTransaction, TxDeposit, - DEPOSIT_TX_TYPE_ID, + OpPooledTransaction, OpTxEnvelope, OpTxType, OpTypedTransaction, TxDeposit, DEPOSIT_TX_TYPE_ID, }; pub mod eip1559; diff --git a/crates/consensus/src/transaction/deposit.rs b/crates/consensus/src/transaction/deposit.rs index 75149600..540a1483 100644 --- a/crates/consensus/src/transaction/deposit.rs +++ b/crates/consensus/src/transaction/deposit.rs @@ -1,7 +1,6 @@ //! Deposit Transaction type. use super::OpTxType; -use crate::DepositTransaction; use alloc::vec::Vec; use alloy_consensus::{Sealable, Transaction, Typed2718}; use alloy_eips::{ @@ -15,6 +14,7 @@ use alloy_rlp::{ Buf, BufMut, Decodable, Encodable, Error as DecodeError, Header, EMPTY_STRING_CODE, }; use core::mem; +use maili_common::DepositTransaction; /// Deposit transactions, also known as deposits are initiated on L1, and executed on L2. #[derive(Debug, Clone, PartialEq, Eq, Hash, Default)] @@ -55,21 +55,20 @@ pub struct TxDeposit { } impl DepositTransaction for TxDeposit { + #[inline] fn source_hash(&self) -> Option { Some(self.source_hash) } + #[inline] fn mint(&self) -> Option { self.mint } + #[inline] fn is_system_transaction(&self) -> bool { self.is_system_transaction } - - fn is_deposit(&self) -> bool { - true - } } impl TxDeposit { diff --git a/crates/consensus/src/transaction/envelope.rs b/crates/consensus/src/transaction/envelope.rs index 42c4e92d..1016d3c3 100644 --- a/crates/consensus/src/transaction/envelope.rs +++ b/crates/consensus/src/transaction/envelope.rs @@ -9,6 +9,7 @@ use alloy_eips::{ }; use alloy_primitives::{Address, Bytes, TxKind, B256, U256}; use alloy_rlp::{Decodable, Encodable}; +use maili_common::DepositTxEnvelope; use crate::{OpTxType, TxDeposit}; @@ -293,12 +294,6 @@ impl OpTxEnvelope { matches!(self, Self::Eip1559(_)) } - /// Returns true if the transaction is a deposit transaction. - #[inline] - pub const fn is_deposit(&self) -> bool { - matches!(self, Self::Deposit(_)) - } - /// Returns true if the transaction is a system transaction. #[inline] pub const fn is_system_transaction(&self) -> bool { @@ -332,14 +327,6 @@ impl OpTxEnvelope { } } - /// Returns the [`TxDeposit`] variant if the transaction is a deposit transaction. - pub const fn as_deposit(&self) -> Option<&Sealed> { - match self { - Self::Deposit(tx) => Some(tx), - _ => None, - } - } - /// Return the [`OpTxType`] of the inner txn. pub const fn tx_type(&self) -> OpTxType { match self { @@ -454,6 +441,24 @@ impl Encodable2718 for OpTxEnvelope { } } +impl DepositTxEnvelope for OpTxEnvelope { + type DepositTx = TxDeposit; + + /// Returns true if the transaction is a deposit transaction. + #[inline] + fn is_deposit(&self) -> bool { + matches!(self, Self::Deposit(_)) + } + + /// Returns the [`TxDeposit`] variant if the transaction is a deposit transaction. + fn as_deposit(&self) -> Option<&Sealed> { + match self { + Self::Deposit(tx) => Some(tx), + _ => None, + } + } +} + #[cfg(feature = "serde")] mod serde_from { //! NB: Why do we need this? diff --git a/crates/consensus/src/transaction/mod.rs b/crates/consensus/src/transaction/mod.rs index 5f379810..bed20127 100644 --- a/crates/consensus/src/transaction/mod.rs +++ b/crates/consensus/src/transaction/mod.rs @@ -23,32 +23,3 @@ pub use deposit::serde_deposit_tx_rpc; pub(super) mod serde_bincode_compat { pub use super::deposit::serde_bincode_compat::TxDeposit; } - -use alloy_primitives::B256; - -/// A trait representing a deposit transaction with specific attributes. -pub trait DepositTransaction { - /// Returns the hash that uniquely identifies the source of the deposit. - /// - /// # Returns - /// An `Option` containing the source hash if available. - fn source_hash(&self) -> Option; - - /// Returns the optional mint value of the deposit transaction. - /// - /// # Returns - /// An `Option` representing the ETH value to mint on L2, if any. - fn mint(&self) -> Option; - - /// Indicates whether the transaction is exempt from the L2 gas limit. - /// - /// # Returns - /// A `bool` indicating if the transaction is a system transaction. - fn is_system_transaction(&self) -> bool; - - /// Checks if the transaction is a deposit transaction. - /// - /// # Returns - /// A `bool` that is always `true` for deposit transactions. - fn is_deposit(&self) -> bool; -} diff --git a/crates/rpc-types/src/transaction.rs b/crates/rpc-types/src/transaction.rs index 81504d5b..65714251 100644 --- a/crates/rpc-types/src/transaction.rs +++ b/crates/rpc-types/src/transaction.rs @@ -4,7 +4,7 @@ use alloy_consensus::{Transaction as _, Typed2718}; use alloy_eips::{eip2930::AccessList, eip7702::SignedAuthorization}; use alloy_primitives::{Address, BlockHash, Bytes, ChainId, TxKind, B256, U256}; use alloy_serde::OtherFields; -use op_alloy_consensus::OpTxEnvelope; +use op_alloy_consensus::{DepositTxEnvelope, OpTxEnvelope}; use serde::{Deserialize, Serialize}; mod request;