Skip to content

Commit

Permalink
Use upstreamed Signed + SignableTransaction
Browse files Browse the repository at this point in the history
  • Loading branch information
clabby committed Apr 13, 2024
1 parent 4ae4b22 commit af3099b
Show file tree
Hide file tree
Showing 13 changed files with 37 additions and 196 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ exclude = ["benches/", "tests/"]
[workspace.dependencies]
alloy-rlp = { version = "0.3", default-features = false }
alloy-primitives = { version = "0.7.0", default-features = false }
alloy-consensus = { git = "https://github.com/alloy-rs/alloy", branch = "main", default-features = false }
alloy-eips = { git = "https://github.com/alloy-rs/alloy", branch = "main", default-features = false }
alloy-signer = { git = "https://github.com/alloy-rs/alloy", branch = "main", default-features = false }
alloy-serde = { git = "https://github.com/alloy-rs/alloy", branch = "main", default-features = false }
Expand Down
11 changes: 6 additions & 5 deletions crates/op-consensus/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ exclude.workspace = true

[dependencies]
alloy-primitives = { workspace = true, features = ["rlp"] }
alloy-consensus.workspace = true
alloy-rlp.workspace = true
alloy-eips.workspace = true
alloy-serde = { workspace = true, optional = true }
Expand All @@ -38,8 +39,8 @@ serde_json.workspace = true

[features]
default = ["std"]
std = ["alloy-eips/std", "sha2/std", "c-kzg?/std"]
k256 = ["alloy-primitives/k256"]
kzg = ["dep:c-kzg", "dep:thiserror", "alloy-eips/kzg", "std"]
arbitrary = ["std", "dep:arbitrary", "alloy-eips/arbitrary", "alloy-primitives/rand"]
serde = ["dep:serde", "alloy-primitives/serde", "dep:alloy-serde", "alloy-eips/serde"]
std = ["alloy-eips/std", "alloy-consensus/std", "sha2/std", "c-kzg?/std"]
k256 = ["alloy-primitives/k256", "alloy-consensus/k256"]
kzg = ["dep:c-kzg", "dep:thiserror", "alloy-eips/kzg", "alloy-consensus/kzg", "std"]
arbitrary = ["std", "dep:arbitrary", "alloy-consensus/arbitrary", "alloy-eips/arbitrary", "alloy-primitives/rand"]
serde = ["dep:serde", "alloy-primitives/serde", "alloy-consensus/serde", "dep:alloy-serde", "alloy-eips/serde"]
9 changes: 3 additions & 6 deletions crates/op-consensus/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,13 @@ pub use receipt::{OpReceipt, OpReceiptEnvelope, OpReceiptWithBloom, OpTxReceipt}

mod transaction;
pub use transaction::{
eip4844_utils, Blob, BlobTransactionSidecar, Bytes48, OpTransaction, OpTxEnvelope, OpTxType,
OpTypedTransaction, SidecarBuilder, SidecarCoder, SignableTransaction, SimpleCoder, TxDeposit,
TxEip1559, TxEip2930, TxEip4844, TxEip4844Variant, TxEip4844WithSidecar, TxLegacy,
eip4844_utils, Blob, BlobTransactionSidecar, Bytes48, OpTxEnvelope, OpTxType,
OpTypedTransaction, SidecarBuilder, SidecarCoder, SimpleCoder, TxDeposit, TxEip1559, TxEip2930,
TxEip4844, TxEip4844Variant, TxEip4844WithSidecar, TxLegacy,
};

#[cfg(feature = "kzg")]
pub use transaction::BlobTransactionValidationError;

#[cfg(feature = "kzg")]
pub use alloy_eips::eip4844::env_settings::EnvKzgSettings;

mod signed;
pub use signed::Signed;
5 changes: 2 additions & 3 deletions crates/op-consensus/src/receipt/envelope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,13 +205,12 @@ where
fn arbitrary(u: &mut arbitrary::Unstructured<'a>) -> arbitrary::Result<Self> {
let receipt = OpReceiptWithBloom::<T>::arbitrary(u)?;

match u.int_in_range(0..=3)? {
match u.int_in_range(0..=4)? {
0 => Ok(Self::Legacy(receipt)),
1 => Ok(Self::Eip2930(receipt)),
2 => Ok(Self::Eip1559(receipt)),
3 => Ok(Self::Eip4844(receipt)),
0x7E => Ok(Self::Deposit(receipt)),
_ => unreachable!(),
_ => Ok(Self::Deposit(receipt)),
}
}
}
63 changes: 0 additions & 63 deletions crates/op-consensus/src/signed.rs

This file was deleted.

5 changes: 3 additions & 2 deletions crates/op-consensus/src/transaction/eip1559.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::{OpTransaction, OpTxType, SignableTransaction, Signed};
use crate::OpTxType;
use alloy_consensus::{SignableTransaction, Signed, Transaction};
use alloy_eips::eip2930::AccessList;
use alloy_primitives::{keccak256, Bytes, ChainId, Signature, TxKind, U256};
use alloy_rlp::{BufMut, Decodable, Encodable, Header};
Expand Down Expand Up @@ -255,7 +256,7 @@ impl TxEip1559 {
}
}

impl OpTransaction for TxEip1559 {
impl Transaction for TxEip1559 {
fn input(&self) -> &[u8] {
&self.input
}
Expand Down
8 changes: 5 additions & 3 deletions crates/op-consensus/src/transaction/eip2930.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::{OpTransaction, OpTxType, SignableTransaction, Signed};
use crate::OpTxType;
use alloy_consensus::{SignableTransaction, Signed, Transaction};
use alloy_eips::eip2930::AccessList;
use alloy_primitives::{keccak256, Bytes, ChainId, Signature, TxKind, U256};
use alloy_rlp::{length_of_length, BufMut, Decodable, Encodable, Header};
Expand Down Expand Up @@ -219,7 +220,7 @@ impl TxEip2930 {
}
}

impl OpTransaction for TxEip2930 {
impl Transaction for TxEip2930 {
fn input(&self) -> &[u8] {
&self.input
}
Expand Down Expand Up @@ -306,7 +307,8 @@ impl Decodable for TxEip2930 {
#[cfg(test)]
mod tests {
use super::TxEip2930;
use crate::{OpTxEnvelope, SignableTransaction};
use crate::OpTxEnvelope;
use alloy_consensus::SignableTransaction;
use alloy_primitives::{Address, Bytes, Signature, TxKind, U256};
use alloy_rlp::{Decodable, Encodable};

Expand Down
13 changes: 7 additions & 6 deletions crates/op-consensus/src/transaction/eip4844.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ pub use builder::{SidecarBuilder, SidecarCoder, SimpleCoder};

pub mod utils;

use crate::{OpTransaction, OpTxType, SignableTransaction, Signed};

use crate::OpTxType;
use alloy_consensus::{SignableTransaction, Signed, Transaction};
use alloy_eips::{
eip2930::AccessList,
eip4844::{BYTES_PER_BLOB, BYTES_PER_COMMITMENT, BYTES_PER_PROOF, DATA_GAS_PER_BLOB},
Expand Down Expand Up @@ -227,7 +227,7 @@ impl TxEip4844Variant {
}
}

impl OpTransaction for TxEip4844Variant {
impl Transaction for TxEip4844Variant {
fn chain_id(&self) -> Option<ChainId> {
match self {
TxEip4844Variant::TxEip4844(tx) => Some(tx.chain_id),
Expand Down Expand Up @@ -698,7 +698,7 @@ impl SignableTransaction<Signature> for TxEip4844 {
}
}

impl OpTransaction for TxEip4844 {
impl Transaction for TxEip4844 {
fn input(&self) -> &[u8] {
&self.input
}
Expand Down Expand Up @@ -929,7 +929,7 @@ impl SignableTransaction<Signature> for TxEip4844WithSidecar {
}
}

impl OpTransaction for TxEip4844WithSidecar {
impl Transaction for TxEip4844WithSidecar {
fn chain_id(&self) -> Option<ChainId> {
self.tx.chain_id()
}
Expand Down Expand Up @@ -1097,7 +1097,8 @@ pub(crate) fn kzg_to_versioned_hash(commitment: &[u8]) -> B256 {
#[cfg(test)]
mod tests {
use super::{BlobTransactionSidecar, TxEip4844, TxEip4844WithSidecar};
use crate::{OpTxEnvelope, SignableTransaction};
use crate::OpTxEnvelope;
use alloy_consensus::SignableTransaction;
use alloy_primitives::{Signature, U256};
use alloy_rlp::{Decodable, Encodable};

Expand Down
6 changes: 3 additions & 3 deletions crates/op-consensus/src/transaction/envelope.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::{
Signed, TxDeposit, TxEip1559, TxEip2930, TxEip4844, TxEip4844Variant, TxEip4844WithSidecar,
TxLegacy,
TxDeposit, TxEip1559, TxEip2930, TxEip4844, TxEip4844Variant, TxEip4844WithSidecar, TxLegacy,
};
use alloy_consensus::Signed;
use alloy_eips::eip2718::{Decodable2718, Eip2718Error, Encodable2718};
use alloy_rlp::{Decodable, Encodable, Header};
use core::mem;
Expand Down Expand Up @@ -280,7 +280,7 @@ impl Encodable2718 for OpTxEnvelope {
#[cfg(test)]
mod tests {
use super::*;
use crate::transaction::SignableTransaction;
use alloy_consensus::SignableTransaction;
use alloy_eips::eip2930::{AccessList, AccessListItem};
use alloy_primitives::{hex, Address, Bytes, Signature, TxKind, B256, U256};
use std::{fs, path::PathBuf, vec};
Expand Down
4 changes: 2 additions & 2 deletions crates/op-consensus/src/transaction/legacy.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{OpTransaction, SignableTransaction, Signed};
use alloy_consensus::{SignableTransaction, Signed, Transaction};
use alloy_primitives::{keccak256, Bytes, ChainId, Signature, TxKind, U256};
use alloy_rlp::{length_of_length, BufMut, Decodable, Encodable, Header, Result};
use core::mem;
Expand Down Expand Up @@ -195,7 +195,7 @@ impl TxLegacy {
}
}

impl OpTransaction for TxLegacy {
impl Transaction for TxLegacy {
fn input(&self) -> &[u8] {
&self.input
}
Expand Down
96 changes: 0 additions & 96 deletions crates/op-consensus/src/transaction/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
use crate::Signed;
use alloy_primitives::{keccak256, ChainId, TxKind, B256, U256};
use core::any;

#[cfg(not(feature = "std"))]
use alloc::vec::Vec;

Expand Down Expand Up @@ -30,95 +26,3 @@ pub use legacy::TxLegacy;

mod typed;
pub use typed::OpTypedTransaction;

/// Represents a minimal EVM transaction.
pub trait OpTransaction: any::Any + Send + Sync + 'static {
/// Get `data`.
fn input(&self) -> &[u8];

/// Get `to`.
fn to(&self) -> TxKind;

/// Get `value`.
fn value(&self) -> U256;

/// Get `chain_id`.
fn chain_id(&self) -> Option<ChainId>;

/// Get `nonce`.
fn nonce(&self) -> u64;

/// Get `gas_limit`.
fn gas_limit(&self) -> u128;

/// Get `gas_price`.
fn gas_price(&self) -> Option<u128>;
}

/// A signable transaction.
///
/// A transaction can have multiple signature types. This is usually
/// [`alloy_primitives::Signature`], however, it may be different for future EIP-2718 transaction
/// types, or in other networks. For example, in Optimism, the deposit transaction signature is the
/// unit type `()`.
pub trait SignableTransaction<Signature>: OpTransaction {
/// Sets `chain_id`.
///
/// Prefer [`set_chain_id_checked`](Self::set_chain_id_checked).
fn set_chain_id(&mut self, chain_id: ChainId);

/// Set `chain_id` if it is not already set. Checks that the provided `chain_id` matches the
/// existing `chain_id` if it is already set, returning `false` if they do not match.
fn set_chain_id_checked(&mut self, chain_id: ChainId) -> bool {
match self.chain_id() {
Some(tx_chain_id) => {
if tx_chain_id != chain_id {
return false;
}
self.set_chain_id(chain_id);
}
None => {
self.set_chain_id(chain_id);
}
}
true
}

/// RLP-encodes the transaction for signing.
fn encode_for_signing(&self, out: &mut dyn alloy_rlp::BufMut);

/// Outputs the length of the signature RLP encoding for the transaction.
fn payload_len_for_signature(&self) -> usize;

/// RLP-encodes the transaction for signing it. Used to calculate `signature_hash`.
///
/// See [`SignableTransaction::encode_for_signing`].
fn encoded_for_signing(&self) -> Vec<u8> {
let mut buf = Vec::with_capacity(self.payload_len_for_signature());
self.encode_for_signing(&mut buf);
buf
}

/// Calculate the signing hash for the transaction.
fn signature_hash(&self) -> B256 {
keccak256(self.encoded_for_signing())
}

/// Convert to a signed transaction by adding a signature and computing the
/// hash.
fn into_signed(self, signature: Signature) -> Signed<Self, Signature>
where
Self: Sized;
}

// TODO: Remove in favor of dyn trait upcasting (TBD, see https://github.com/rust-lang/rust/issues/65991#issuecomment-1903120162)
#[doc(hidden)]
impl<S: 'static> dyn SignableTransaction<S> {
pub fn __downcast_ref<T: any::Any>(&self) -> Option<&T> {
if any::Any::type_id(self) == any::TypeId::of::<T>() {
unsafe { Some(&*(self as *const _ as *const T)) }
} else {
None
}
}
}
4 changes: 2 additions & 2 deletions crates/op-consensus/src/transaction/optimism.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::OpTransaction;
use alloy_consensus::Transaction;
use alloy_primitives::{Address, Bytes, ChainId, TxKind, B256, U256};
use alloy_rlp::{
Buf, BufMut, Decodable, Encodable, Error as DecodeError, Header, EMPTY_STRING_CODE,
Expand Down Expand Up @@ -119,7 +119,7 @@ impl TxDeposit {
}
}

impl OpTransaction for TxDeposit {
impl Transaction for TxDeposit {
fn input(&self) -> &[u8] {
&self.input
}
Expand Down
8 changes: 3 additions & 5 deletions crates/op-consensus/src/transaction/typed.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
use crate::{
OpTransaction, OpTxEnvelope, OpTxType, TxDeposit, TxEip1559, TxEip2930, TxEip4844Variant,
TxLegacy,
};
use crate::{OpTxEnvelope, OpTxType, TxDeposit, TxEip1559, TxEip2930, TxEip4844Variant, TxLegacy};
use alloy_consensus::Transaction;
use alloy_primitives::TxKind;

/// The TypedTransaction enum represents all Ethereum transaction request types, modified for the OP
Expand Down Expand Up @@ -113,7 +111,7 @@ impl OpTypedTransaction {
}
}

impl OpTransaction for OpTypedTransaction {
impl Transaction for OpTypedTransaction {
fn chain_id(&self) -> Option<alloy_primitives::ChainId> {
match self {
Self::Legacy(tx) => tx.chain_id(),
Expand Down

0 comments on commit af3099b

Please sign in to comment.