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

feat: add depositTransaction trait #171

Merged
merged 6 commits into from
Oct 15, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
4 changes: 4 additions & 0 deletions crates/consensus/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ pub mod hardforks;
pub use hardforks::Hardforks;

mod block;
mod traits;

pub use traits::DepositTransaction;

pub use block::OpBlock;

/// Bincode-compatible serde implementations for consensus types.
Expand Down
114 changes: 114 additions & 0 deletions crates/consensus/src/traits.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
use alloy_primitives::B256;
use crate::TxDeposit;

/// 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;
}

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

fn mint(&self) -> Option<u128> {
self.mint
}

fn is_system_transaction(&self) -> bool {
self.is_system_transaction
}

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

#[cfg(test)]
mod tests {
use alloy_consensus::Transaction;
use alloy_primitives::{Address, Bytes, TxKind, B256, U256};
use crate::traits::DepositTransaction;
use crate::TxDeposit;

#[test]
fn test_deposit_transaction_trait() {
let tx = TxDeposit {
source_hash: B256::with_last_byte(42),
from: Address::default(),
to: TxKind::default(),
mint: Some(100),
value: U256::from(1000),
gas_limit: 50000,
is_system_transaction: true,
input: Bytes::default(),
};

assert_eq!(tx.source_hash(), Some(B256::with_last_byte(42)));
assert_eq!(tx.mint(), Some(100));
assert!(tx.is_system_transaction());
assert!(tx.is_deposit());
}

#[test]
fn test_deposit_transaction_without_mint() {
let tx = TxDeposit {
source_hash: B256::default(),
from: Address::default(),
to: TxKind::default(),
mint: None,
value: U256::default(),
gas_limit: 50000,
is_system_transaction: false,
input: Bytes::default(),
};

assert_eq!(tx.source_hash(), Some(B256::default()));
assert_eq!(tx.mint(), None);
assert!(!tx.is_system_transaction());
assert!(tx.is_deposit());
}

#[test]
fn test_deposit_transaction_to_contract() {
let contract_address = Address::with_last_byte(0xFF);
let tx = TxDeposit {
source_hash: B256::default(),
from: Address::default(),
to: TxKind::Call(contract_address),
mint: Some(200),
value: U256::from(500),
gas_limit: 100000,
is_system_transaction: false,
input: Bytes::from_static(&[1, 2, 3]),
};

assert_eq!(tx.source_hash(), Some(B256::default()));
assert_eq!(tx.mint(), Some(200));
assert!(!tx.is_system_transaction());
assert!(tx.is_deposit());
assert_eq!(tx.to(), TxKind::Call(contract_address));
}
}
Loading