Skip to content

Commit

Permalink
Revert "feat: impl TryFrom<Transaction> for TxEnvelope (#343)"
Browse files Browse the repository at this point in the history
This reverts commit 5515915.
  • Loading branch information
prestwich authored Mar 22, 2024
1 parent 5515915 commit ee4a8d1
Show file tree
Hide file tree
Showing 6 changed files with 4 additions and 171 deletions.
3 changes: 2 additions & 1 deletion crates/consensus/src/transaction/envelope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,9 @@ impl TryFrom<u8> for TxType {

fn try_from(value: u8) -> Result<Self, Self::Error> {
match value {
0 => Err(Eip2718Error::UnexpectedType(value)),
// SAFETY: repr(u8) with explicit discriminant
0..=3 => Ok(unsafe { std::mem::transmute(value) }),
1..=3 => Ok(unsafe { std::mem::transmute(value) }),
_ => Err(Eip2718Error::UnexpectedType(value)),
}
}
Expand Down
2 changes: 0 additions & 2 deletions crates/rpc-types/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ exclude.workspace = true
alloy-rlp = { workspace = true, features = ["arrayvec", "derive"] }
alloy-primitives = { workspace = true, features = ["rlp", "serde", "std"] }
alloy-serde.workspace = true
alloy-consensus.workspace = true
alloy-eips = { workspace = true, features = ["std"] }

ethereum_ssz_derive = { workspace = true, optional = true }
ethereum_ssz = { workspace = true, optional = true }
Expand Down
7 changes: 0 additions & 7 deletions crates/rpc-types/src/eth/transaction/access_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,6 @@ impl AccessList {
}
}

impl From<AccessList> for alloy_eips::eip2930::AccessList {
fn from(value: AccessList) -> Self {
// SAFETY: Same repr and size
unsafe { std::mem::transmute(value) }
}
}

/// Access list with gas used appended.
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, Default)]
#[serde(rename_all = "camelCase")]
Expand Down
24 changes: 0 additions & 24 deletions crates/rpc-types/src/eth/transaction/error.rs

This file was deleted.

126 changes: 2 additions & 124 deletions crates/rpc-types/src/eth/transaction/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,25 @@
use crate::eth::other::OtherFields;
pub use access_list::{AccessList, AccessListItem, AccessListWithGasUsed};
use alloy_consensus::{
SignableTransaction, Signed, TxEip1559, TxEip2930, TxEip4844, TxEip4844Variant, TxEnvelope,
TxLegacy, TxType,
};
use alloy_primitives::{Address, Bytes, B256, U256, U8};
pub use blob::BlobTransactionSidecar;
pub use common::TransactionInfo;
pub use error::ConversionError;
pub use optimism::OptimismTransactionReceiptFields;
pub use receipt::TransactionReceipt;
pub use request::{TransactionInput, TransactionRequest};
use serde::{Deserialize, Serialize};
pub use signature::{Parity, Signature};

mod access_list;
mod blob;
mod common;
mod error;
pub mod kzg;
pub mod optimism;
mod receipt;
pub mod request;
mod signature;

mod blob;

/// Transaction object used in RPC
#[derive(Debug, Default, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
Expand Down Expand Up @@ -120,123 +115,6 @@ impl Transaction {
}
}

impl TryFrom<Transaction> for Signed<TxLegacy> {
type Error = ConversionError;

fn try_from(tx: Transaction) -> Result<Self, Self::Error> {
let signature = tx.signature.ok_or(ConversionError::MissingSignature)?.try_into()?;

let tx = TxLegacy {
chain_id: tx.chain_id,
nonce: tx.nonce,
gas_price: tx.gas_price.ok_or(ConversionError::MissingGasPrice)?.to(),
gas_limit: tx.gas.to(),
to: tx.to.into(),
value: tx.value,
input: tx.input,
};
Ok(tx.into_signed(signature))
}
}

impl TryFrom<Transaction> for Signed<TxEip1559> {
type Error = ConversionError;

fn try_from(tx: Transaction) -> Result<Self, Self::Error> {
let signature = tx.signature.ok_or(ConversionError::MissingSignature)?.try_into()?;

let tx = TxEip1559 {
chain_id: tx.chain_id.ok_or(ConversionError::MissingChainId)?,
nonce: tx.nonce,
max_fee_per_gas: tx.max_fee_per_gas.ok_or(ConversionError::MissingMaxFeePerGas)?.to(),
max_priority_fee_per_gas: tx
.max_priority_fee_per_gas
.ok_or(ConversionError::MissingMaxPriorityFeePerGas)?
.to(),
gas_limit: tx.gas.to(),
to: tx.to.into(),
value: tx.value,
input: tx.input,
access_list: tx.access_list.unwrap_or_default().into(),
};
Ok(tx.into_signed(signature))
}
}

impl TryFrom<Transaction> for Signed<TxEip2930> {
type Error = ConversionError;

fn try_from(tx: Transaction) -> Result<Self, Self::Error> {
let signature = tx.signature.ok_or(ConversionError::MissingSignature)?.try_into()?;

let tx = TxEip2930 {
chain_id: tx.chain_id.ok_or(ConversionError::MissingChainId)?,
nonce: tx.nonce,
gas_price: tx.gas_price.ok_or(ConversionError::MissingGasPrice)?.to(),
gas_limit: tx.gas.to(),
to: tx.to.into(),
value: tx.value,
input: tx.input,
access_list: tx.access_list.ok_or(ConversionError::MissingAccessList)?.into(),
};
Ok(tx.into_signed(signature))
}
}

impl TryFrom<Transaction> for Signed<TxEip4844> {
type Error = ConversionError;

fn try_from(tx: Transaction) -> Result<Self, Self::Error> {
let signature = tx.signature.ok_or(ConversionError::MissingSignature)?.try_into()?;

let tx = TxEip4844 {
chain_id: tx.chain_id.ok_or(ConversionError::MissingChainId)?,
nonce: tx.nonce,
max_fee_per_gas: tx.max_fee_per_gas.ok_or(ConversionError::MissingMaxFeePerGas)?.to(),
max_priority_fee_per_gas: tx
.max_priority_fee_per_gas
.ok_or(ConversionError::MissingMaxPriorityFeePerGas)?
.to(),
gas_limit: tx.gas.to(),
to: tx.to.into(),
value: tx.value,
input: tx.input,
access_list: tx.access_list.unwrap_or_default().into(),
blob_versioned_hashes: tx.blob_versioned_hashes,
max_fee_per_blob_gas: tx
.max_fee_per_blob_gas
.ok_or(ConversionError::MissingMaxFeePerBlobGas)?
.to(),
};
Ok(tx.into_signed(signature))
}
}

impl TryFrom<Transaction> for Signed<TxEip4844Variant> {
type Error = ConversionError;

fn try_from(tx: Transaction) -> Result<Self, Self::Error> {
let tx: Signed<TxEip4844> = tx.try_into()?;
let (inner, signature, _) = tx.into_parts();
let tx = TxEip4844Variant::TxEip4844(inner);

Ok(tx.into_signed(signature))
}
}

impl TryFrom<Transaction> for TxEnvelope {
type Error = ConversionError;

fn try_from(tx: Transaction) -> Result<Self, Self::Error> {
match tx.transaction_type.unwrap_or_default().to::<u8>().try_into()? {
TxType::Legacy => Ok(Self::Legacy(tx.try_into()?)),
TxType::Eip1559 => Ok(Self::Eip1559(tx.try_into()?)),
TxType::Eip2930 => Ok(Self::Eip2930(tx.try_into()?)),
TxType::Eip4844 => Ok(Self::Eip4844(tx.try_into()?)),
}
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
13 changes: 0 additions & 13 deletions crates/rpc-types/src/eth/transaction/signature.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,19 +60,6 @@ where
}
}

impl TryFrom<Signature> for alloy_primitives::Signature {
type Error = alloy_primitives::SignatureError;

fn try_from(value: Signature) -> Result<Self, Self::Error> {
let parity = if let Some(y_parity) = value.y_parity {
alloy_primitives::Parity::Parity(y_parity.0)
} else {
value.v.to::<u64>().try_into()?
};
alloy_primitives::Signature::from_rs_and_parity(value.r, value.s, parity)
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down

0 comments on commit ee4a8d1

Please sign in to comment.