Skip to content

Commit

Permalink
feat(consensus): add is_deposit to OpTxEnvelope (#396)
Browse files Browse the repository at this point in the history
## Motivation

This is a useful helper method for when you have a concrete
`OpTxEnvelope` and want to know whether or not it's a deposit
transaction.

## Solution

Adds `fn is_deposit`

## PR Checklist

- [ ] Added Tests
- [ ] Added Documentation
- [ ] Breaking changes
  • Loading branch information
Rjected authored Jan 20, 2025
1 parent b506e16 commit fe383f6
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
23 changes: 20 additions & 3 deletions crates/consensus/src/transaction/envelope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,12 @@ impl OpTxEnvelope {
}
}

/// Returns true if the transaction is a deposit transaction.
#[inline]
pub const fn is_deposit(&self) -> bool {
matches!(self, Self::Deposit(_))
}

/// Returns the [`TxLegacy`] variant if the transaction is a legacy transaction.
pub const fn as_legacy(&self) -> Option<&Signed<TxLegacy>> {
match self {
Expand Down Expand Up @@ -539,7 +545,8 @@ mod serde_from {
mod tests {
use super::*;
use alloc::vec;
use alloy_primitives::{hex, Address, Bytes, TxKind, B256, U256};
use alloy_consensus::SignableTransaction;
use alloy_primitives::{hex, Address, Bytes, PrimitiveSignature, TxKind, B256, U256};

#[test]
fn test_tx_gas_limit() {
Expand All @@ -548,6 +555,18 @@ mod tests {
assert_eq!(tx_envelope.gas_limit(), 1);
}

#[test]
fn test_deposit() {
let tx = TxDeposit { is_system_transaction: true, ..Default::default() };
let tx_envelope = OpTxEnvelope::Deposit(tx.seal_slow());
assert!(tx_envelope.is_deposit());

let tx = TxEip1559::default();
let sig = PrimitiveSignature::test_signature();
let tx_envelope = OpTxEnvelope::Eip1559(tx.into_signed(sig));
assert!(!tx_envelope.is_system_transaction());
}

#[test]
fn test_system_transaction() {
let mut tx = TxDeposit { is_system_transaction: true, ..Default::default() };
Expand Down Expand Up @@ -611,8 +630,6 @@ mod tests {

#[test]
fn eip1559_decode() {
use alloy_consensus::SignableTransaction;
use alloy_primitives::PrimitiveSignature;
let tx = TxEip1559 {
chain_id: 1u64,
nonce: 2,
Expand Down
1 change: 0 additions & 1 deletion crates/rpc-types/src/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ 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 maili_consensus::DepositTxEnvelope;
use op_alloy_consensus::OpTxEnvelope;
use serde::{Deserialize, Serialize};

Expand Down

0 comments on commit fe383f6

Please sign in to comment.