From 974b197d3037795abfe0eab5cdc9f0a9c85201ea Mon Sep 17 00:00:00 2001 From: Matthias Seitz Date: Tue, 11 Feb 2025 20:15:34 +0100 Subject: [PATCH] feat: add alloy-compat for op prims (#14406) --- Cargo.lock | 3 ++ crates/optimism/primitives/Cargo.toml | 9 ++++ .../optimism/primitives/src/alloy_compat.rs | 53 +++++++++++++++++++ crates/optimism/primitives/src/lib.rs | 3 ++ 4 files changed, 68 insertions(+) create mode 100644 crates/optimism/primitives/src/alloy_compat.rs diff --git a/Cargo.lock b/Cargo.lock index 53695d913..f875c6092 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -8581,8 +8581,11 @@ version = "1.1.5" dependencies = [ "alloy-consensus", "alloy-eips", + "alloy-network", "alloy-primitives", "alloy-rlp", + "alloy-rpc-types-eth", + "alloy-serde", "arbitrary", "bytes", "derive_more", diff --git a/crates/optimism/primitives/Cargo.toml b/crates/optimism/primitives/Cargo.toml index 4e8fc588c..c2de3d6e4 100644 --- a/crates/optimism/primitives/Cargo.toml +++ b/crates/optimism/primitives/Cargo.toml @@ -27,6 +27,9 @@ secp256k1 = { workspace = true, optional = true } # op op-alloy-consensus.workspace = true +alloy-rpc-types-eth = { workspace = true, optional = true } +alloy-network = { workspace = true, optional = true } +alloy-serde = { workspace = true, optional = true } # codec bytes = { workspace = true, optional = true } @@ -66,7 +69,10 @@ std = [ "alloy-rlp/std", "reth-zstd-compressors?/std", "op-alloy-consensus/std", + "alloy-rpc-types-eth?/std", + "alloy-serde?/std", ] +alloy-compat = ["dep:alloy-network", "dep:alloy-serde", "dep:alloy-rpc-types-eth"] reth-codec = [ "dep:reth-codecs", "std", @@ -89,6 +95,7 @@ serde = [ "rand?/serde", "revm-primitives?/serde", "secp256k1?/serde", + "alloy-rpc-types-eth?/serde", ] serde-bincode-compat = [ "alloy-consensus/serde-bincode-compat", @@ -108,6 +115,8 @@ arbitrary = [ "alloy-primitives/arbitrary", "revm-primitives?/arbitrary", "rand", + "alloy-rpc-types-eth?/arbitrary", + "alloy-serde?/arbitrary", ] optimism = [ "dep:revm-primitives", diff --git a/crates/optimism/primitives/src/alloy_compat.rs b/crates/optimism/primitives/src/alloy_compat.rs new file mode 100644 index 000000000..cd96479f8 --- /dev/null +++ b/crates/optimism/primitives/src/alloy_compat.rs @@ -0,0 +1,53 @@ +//! Common conversions from alloy types. + +use crate::OpTransactionSigned; +use alloc::string::ToString; +use alloy_consensus::TxEnvelope; +use alloy_network::{AnyRpcTransaction, AnyTxEnvelope}; +use alloy_rpc_types_eth::Transaction as AlloyRpcTransaction; +use alloy_serde::WithOtherFields; +use op_alloy_consensus::OpTypedTransaction; + +impl TryFrom for OpTransactionSigned { + type Error = alloy_rpc_types_eth::ConversionError; + + fn try_from(tx: AnyRpcTransaction) -> Result { + use alloy_rpc_types_eth::ConversionError; + + let WithOtherFields { inner: tx, other: _ } = tx; + + let (transaction, signature, hash) = match tx.inner { + AnyTxEnvelope::Ethereum(TxEnvelope::Legacy(tx)) => { + let (tx, signature, hash) = tx.into_parts(); + (OpTypedTransaction::Legacy(tx), signature, hash) + } + AnyTxEnvelope::Ethereum(TxEnvelope::Eip2930(tx)) => { + let (tx, signature, hash) = tx.into_parts(); + (OpTypedTransaction::Eip2930(tx), signature, hash) + } + AnyTxEnvelope::Ethereum(TxEnvelope::Eip1559(tx)) => { + let (tx, signature, hash) = tx.into_parts(); + (OpTypedTransaction::Eip1559(tx), signature, hash) + } + AnyTxEnvelope::Ethereum(TxEnvelope::Eip7702(tx)) => { + let (tx, signature, hash) = tx.into_parts(); + (OpTypedTransaction::Eip7702(tx), signature, hash) + } + _ => { + // TODO: support tx deposit: + return Err(ConversionError::Custom("unknown transaction type".to_string())) + } + }; + + Ok(Self::new(transaction, signature, hash)) + } +} + +impl From> for OpTransactionSigned +where + Self: From, +{ + fn from(value: AlloyRpcTransaction) -> Self { + value.inner.into() + } +} diff --git a/crates/optimism/primitives/src/lib.rs b/crates/optimism/primitives/src/lib.rs index 77056420a..d8f5c6bef 100644 --- a/crates/optimism/primitives/src/lib.rs +++ b/crates/optimism/primitives/src/lib.rs @@ -11,6 +11,9 @@ extern crate alloc; +#[cfg(feature = "alloy-compat")] +mod alloy_compat; + pub mod bedrock; pub mod predeploys;