Skip to content

Commit

Permalink
chore(consensus): Migrate deposit tx behaviour to maili (#383)
Browse files Browse the repository at this point in the history
Implements `maili-common` traits `DepsoitTransaction` and
`DepositTxEnvelope` for `DepositTx` and `OpTxEnvelope` respectively
  • Loading branch information
emhane authored Jan 13, 2025
1 parent 19635a8 commit 399b368
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 51 deletions.
5 changes: 3 additions & 2 deletions crates/consensus/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
9 changes: 4 additions & 5 deletions crates/consensus/src/transaction/deposit.rs
Original file line number Diff line number Diff line change
@@ -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::{
Expand All @@ -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)]
Expand Down Expand Up @@ -55,21 +55,20 @@ pub struct TxDeposit {
}

impl DepositTransaction for TxDeposit {
#[inline]
fn source_hash(&self) -> Option<B256> {
Some(self.source_hash)
}

#[inline]
fn mint(&self) -> Option<u128> {
self.mint
}

#[inline]
fn is_system_transaction(&self) -> bool {
self.is_system_transaction
}

fn is_deposit(&self) -> bool {
true
}
}

impl TxDeposit {
Expand Down
33 changes: 19 additions & 14 deletions crates/consensus/src/transaction/envelope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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<TxDeposit>> {
match self {
Self::Deposit(tx) => Some(tx),
_ => None,
}
}

/// Return the [`OpTxType`] of the inner txn.
pub const fn tx_type(&self) -> OpTxType {
match self {
Expand Down Expand Up @@ -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<TxDeposit>> {
match self {
Self::Deposit(tx) => Some(tx),
_ => None,
}
}
}

#[cfg(feature = "serde")]
mod serde_from {
//! NB: Why do we need this?
Expand Down
29 changes: 0 additions & 29 deletions crates/consensus/src/transaction/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<B256>` containing the source hash if available.
fn source_hash(&self) -> Option<B256>;

/// Returns the optional mint value of the deposit transaction.
///
/// # Returns
/// An `Option<u128>` representing the ETH value to mint on L2, if any.
fn mint(&self) -> Option<u128>;

/// 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;
}
2 changes: 1 addition & 1 deletion crates/rpc-types/src/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 399b368

Please sign in to comment.