Skip to content

Commit

Permalink
feat: add depositTransaction trait (#171)
Browse files Browse the repository at this point in the history
This PR will help this issue
paradigmxyz/reth#11736
 converted into a trait from reth to alloy-rs
<!--
Thank you for your Pull Request. Please provide a description above and
review
the requirements below.

Bug fixes and new features should include tests.

Contributors guide:
https://github.com/alloy-rs/core/blob/main/CONTRIBUTING.md

The contributors guide includes instructions for running rustfmt and
building the
documentation.
-->

<!-- ** Please select "Allow edits from maintainers" in the PR Options
** -->

## Motivation

<!--
Explain the context and why you're making that change. What is the
problem
you're trying to solve? In some cases there is not a problem and this
can be
thought of as being the motivation for your change.
-->

## Solution

<!--
Summarize the solution and provide any necessary context needed to
understand
the code change.
-->

## PR Checklist

- [x] Added Tests
- [x] Added Documentation
- [ ] Breaking changes

---------

Co-authored-by: refcell <abigger87@gmail.com>
  • Loading branch information
hoank101 and refcell authored Oct 15, 2024
1 parent 83efdcd commit 20eb162
Show file tree
Hide file tree
Showing 2 changed files with 117 additions and 0 deletions.
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
113 changes: 113 additions & 0 deletions crates/consensus/src/traits.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
use crate::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;
}

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 crate::{traits::DepositTransaction, TxDeposit};
use alloy_consensus::Transaction;
use alloy_primitives::{Address, Bytes, TxKind, B256, U256};

#[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));
}
}

0 comments on commit 20eb162

Please sign in to comment.