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: Introduce trait for OpTransaction #11745

Merged
merged 18 commits into from
Oct 29, 2024
78 changes: 60 additions & 18 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 9 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -463,10 +463,15 @@ alloy-transport-ipc = { version = "0.4.2", default-features = false }
alloy-transport-ws = { version = "0.4.2", default-features = false }

# op
op-alloy-rpc-types = "0.4"
op-alloy-rpc-types-engine = "0.4"
op-alloy-network = "0.4"
op-alloy-consensus = "0.4"
op-alloy-rpc-types = { git = "https://github.com/hoank101/op-alloy.git", tag = "v0.4.0-1"}
op-alloy-rpc-types-engine = { git = "https://github.com/hoank101/op-alloy.git", tag = "v0.4.0-1"}
op-alloy-network = { git = "https://github.com/hoank101/op-alloy.git", tag = "v0.4.0-1"}
op-alloy-consensus = { git = "https://github.com/hoank101/op-alloy.git", tag = "v0.4.0-1"}
#op-alloy-rpc-types = "0.4.0"
#op-alloy-rpc-types-engine = "0.4.0"
#op-alloy-network = "0.4.0"
#op-alloy-consensus = "0.4.0"


# misc
aquamarine = "0.5"
Expand Down
2 changes: 2 additions & 0 deletions crates/optimism/evm/src/execute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ use crate::{
l1::ensure_create2_deployer, OpChainSpec, OptimismBlockExecutionError, OptimismEvmConfig,
};
use alloy_primitives::{BlockNumber, U256};
#[cfg(feature = "optimism")]
use op_alloy_consensus::DepositTransaction;
use reth_chainspec::{ChainSpec, EthereumHardforks};
use reth_evm::{
execute::{
Expand Down
3 changes: 3 additions & 0 deletions crates/optimism/payload/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ alloy-eips.workspace = true
alloy-primitives.workspace = true
alloy-rlp.workspace = true
op-alloy-rpc-types-engine.workspace = true
op-alloy-consensus.workspace = true
revm-primitives.workspace = true
alloy-rpc-types-engine.workspace = true

Expand All @@ -54,3 +55,5 @@ optimism = [
"reth-optimism-evm/optimism",
"reth-revm/optimism",
]


2 changes: 2 additions & 0 deletions crates/optimism/payload/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ use crate::{
error::OptimismPayloadBuilderError,
payload::{OptimismBuiltPayload, OptimismPayloadBuilderAttributes},
};
#[cfg(feature = "optimism")]
use op_alloy_consensus::DepositTransaction;

/// Optimism's payload builder
#[derive(Debug, Clone, PartialEq, Eq)]
Expand Down
4 changes: 3 additions & 1 deletion crates/optimism/rpc/src/eth/receipt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

use alloy_eips::eip2718::Encodable2718;
use alloy_rpc_types::{AnyReceiptEnvelope, Log, TransactionReceipt};
use op_alloy_consensus::{OpDepositReceipt, OpDepositReceiptWithBloom, OpReceiptEnvelope};
use op_alloy_consensus::{
DepositTransaction, OpDepositReceipt, OpDepositReceiptWithBloom, OpReceiptEnvelope,
};
use op_alloy_rpc_types::{receipt::L1BlockInfo, OpTransactionReceipt, OpTransactionReceiptFields};
use reth_node_api::{FullNodeComponents, NodeTypes};
use reth_optimism_chainspec::OpChainSpec;
Expand Down
1 change: 1 addition & 0 deletions crates/optimism/rpc/src/eth/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

use alloy_primitives::{Bytes, B256};
use alloy_rpc_types::TransactionInfo;
use op_alloy_consensus::DepositTransaction;
use op_alloy_rpc_types::Transaction;
use reth_node_api::FullNodeComponents;
use reth_primitives::TransactionSignedEcRecovered;
Expand Down
2 changes: 2 additions & 0 deletions crates/primitives/src/transaction/compat.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use crate::{Transaction, TransactionSigned};
use alloy_primitives::{Address, TxKind, U256};
#[cfg(feature = "optimism")]
use op_alloy_consensus::DepositTransaction;
use revm_primitives::{AuthorizationList, TxEnv};

/// Implements behaviour to fill a [`TxEnv`] from another transaction.
Expand Down
72 changes: 32 additions & 40 deletions crates/primitives/src/transaction/mod.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
//! Transaction types.

use crate::BlockHashOrNumber;
use alloy_eips::eip7702::SignedAuthorization;
use alloy_primitives::{keccak256, Address, ChainId, TxKind, B256, U256};

use alloy_consensus::{SignableTransaction, TxEip1559, TxEip2930, TxEip4844, TxEip7702, TxLegacy};
use alloy_eips::{
eip2718::{Decodable2718, Eip2718Error, Eip2718Result, Encodable2718},
eip2930::AccessList,
eip7702::SignedAuthorization,
};
use alloy_primitives::{Bytes, TxHash};
use alloy_primitives::{keccak256, Address, Bytes, ChainId, TxHash, TxKind, B256, U256};
use alloy_rlp::{Decodable, Encodable, Error as RlpError, Header};
use core::mem;
use derive_more::{AsRef, Deref};
Expand Down Expand Up @@ -54,6 +52,8 @@ mod tx_type;
pub(crate) mod util;
mod variant;

#[cfg(feature = "optimism")]
use op_alloy_consensus::DepositTransaction;
#[cfg(feature = "optimism")]
use op_alloy_consensus::TxDeposit;
#[cfg(feature = "optimism")]
Expand Down Expand Up @@ -139,6 +139,34 @@ pub enum Transaction {
Deposit(TxDeposit),
}

#[cfg(feature = "optimism")]
impl DepositTransaction for Transaction {
fn source_hash(&self) -> Option<B256> {
match self {
Self::Deposit(tx) => tx.source_hash(),
_ => None,
}
}

fn mint(&self) -> Option<u128> {
match self {
Self::Deposit(tx) => tx.mint(),
_ => None,
}
}

fn is_system_transaction(&self) -> bool {
match self {
Self::Deposit(tx) => tx.is_system_transaction(),
_ => false,
}
}

fn is_deposit(&self) -> bool {
matches!(self, Self::Deposit(_))
}
}

#[cfg(any(test, feature = "arbitrary"))]
impl<'a> arbitrary::Arbitrary<'a> for Transaction {
fn arbitrary(u: &mut arbitrary::Unstructured<'a>) -> arbitrary::Result<Self> {
Expand Down Expand Up @@ -482,42 +510,6 @@ impl Transaction {
}
}

/// Returns the source hash of the transaction, which uniquely identifies its source.
/// If not a deposit transaction, this will always return `None`.
#[cfg(feature = "optimism")]
pub const fn source_hash(&self) -> Option<B256> {
match self {
Self::Deposit(TxDeposit { source_hash, .. }) => Some(*source_hash),
_ => None,
}
}

/// Returns the amount of ETH locked up on L1 that will be minted on L2. If the transaction
/// is not a deposit transaction, this will always return `None`.
#[cfg(feature = "optimism")]
pub const fn mint(&self) -> Option<u128> {
match self {
Self::Deposit(TxDeposit { mint, .. }) => *mint,
_ => None,
}
}

/// Returns whether or not the transaction is a system transaction. If the transaction
/// is not a deposit transaction, this will always return `false`.
#[cfg(feature = "optimism")]
pub const fn is_system_transaction(&self) -> bool {
match self {
Self::Deposit(TxDeposit { is_system_transaction, .. }) => *is_system_transaction,
_ => false,
}
}

/// Returns whether or not the transaction is an Optimism Deposited transaction.
#[cfg(feature = "optimism")]
pub const fn is_deposit(&self) -> bool {
matches!(self, Self::Deposit(_))
}

/// This encodes the transaction _without_ the signature, and is only suitable for creating a
/// hash intended for signing.
pub fn encode_without_signature(&self, out: &mut dyn bytes::BufMut) {
Expand Down