Skip to content

Commit

Permalink
separate error type
Browse files Browse the repository at this point in the history
  • Loading branch information
klkvr committed Mar 20, 2024
1 parent 1630a77 commit b1706e6
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 67 deletions.
4 changes: 2 additions & 2 deletions crates/consensus/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ alloy-primitives = { workspace = true, features = ["rlp"] }
alloy-rlp.workspace = true
alloy-eips.workspace = true
alloy-rpc-types.workspace = true
thiserror = { workspace = true, optional = true }
thiserror.workspace = true
c-kzg = { version = "1.0", features = ["serde"], optional = true }
sha2 = { version = "0.10", optional = true }

Expand All @@ -32,5 +32,5 @@ tokio = { workspace = true, features = ["macros"] }

[features]
k256 = ["alloy-primitives/k256"]
kzg = ["dep:c-kzg", "dep:sha2", "dep:thiserror"]
kzg = ["dep:c-kzg", "dep:sha2"]
arbitrary = ["dep:arbitrary", "alloy-eips/arbitrary"]
12 changes: 7 additions & 5 deletions crates/consensus/src/transaction/eip1559.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ use alloy_primitives::{keccak256, Bytes, ChainId, Signature, TxKind, U256};
use alloy_rlp::{BufMut, Decodable, Encodable, Header};
use std::mem;

use super::ConversionError;

/// A transaction with a priority fee ([EIP-1559](https://eips.ethereum.org/EIPS/eip-1559)).
#[derive(Debug, Clone, PartialEq, Eq, Hash, Default)]
pub struct TxEip1559 {
Expand Down Expand Up @@ -326,18 +328,18 @@ impl Decodable for TxEip1559 {
}

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

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

let tx = TxEip1559 {
chain_id: tx.chain_id.ok_or(Eip2718Error::MissingChainId)?.to(),
chain_id: tx.chain_id.ok_or(ConversionError::MissingChainId)?,
nonce: tx.nonce,
max_fee_per_gas: tx.max_fee_per_gas.ok_or(Eip2718Error::MissingMaxFeePerGas)?.to(),
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(Eip2718Error::MissingMaxPriorityFeePerGas)?
.ok_or(ConversionError::MissingMaxPriorityFeePerGas)?
.to(),
gas_limit: tx.gas.to(),
to: tx.to.into(),
Expand Down
12 changes: 7 additions & 5 deletions crates/consensus/src/transaction/eip2930.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ use alloy_primitives::{keccak256, Bytes, ChainId, Signature, TxKind, U256};
use alloy_rlp::{length_of_length, BufMut, Decodable, Encodable, Header};
use std::mem;

use super::ConversionError;

/// Transaction with an [`AccessList`] ([EIP-2930](https://eips.ethereum.org/EIPS/eip-2930)).
#[derive(Debug, Clone, PartialEq, Eq, Hash, Default)]
pub struct TxEip2930 {
Expand Down Expand Up @@ -294,20 +296,20 @@ impl Decodable for TxEip2930 {
}

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

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

let tx = TxEip2930 {
chain_id: tx.chain_id.ok_or(Eip2718Error::MissingChainId)?.to(),
chain_id: tx.chain_id.ok_or(ConversionError::MissingChainId)?,
nonce: tx.nonce,
gas_price: tx.gas_price.ok_or(Eip2718Error::MissingGasPrice)?.to(),
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(Eip2718Error::MissingAccessList)?.into(),
access_list: tx.access_list.ok_or(ConversionError::MissingAccessList)?.into(),
};
Ok(tx.into_signed(signature))
}
Expand Down
16 changes: 9 additions & 7 deletions crates/consensus/src/transaction/eip4844.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ use sha2::Digest;
#[cfg(feature = "kzg")]
use std::ops::Deref;

use super::ConversionError;

#[cfg(feature = "kzg")]
/// An error that can occur when validating a [TxEip4844Variant].
#[derive(Debug, thiserror::Error)]
Expand Down Expand Up @@ -282,7 +284,7 @@ impl SignableTransaction<Signature> for TxEip4844Variant {
}

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

fn try_from(tx: alloy_rpc_types::Transaction) -> Result<Self, Self::Error> {
let tx: Signed<TxEip4844> = tx.try_into()?;
Expand Down Expand Up @@ -722,18 +724,18 @@ impl Decodable for TxEip4844 {
}

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

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

let tx = TxEip4844 {
chain_id: tx.chain_id.ok_or(Eip2718Error::MissingChainId)?.to(),
chain_id: tx.chain_id.ok_or(ConversionError::MissingChainId)?,
nonce: tx.nonce,
max_fee_per_gas: tx.max_fee_per_gas.ok_or(Eip2718Error::MissingMaxFeePerGas)?.to(),
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(Eip2718Error::MissingMaxPriorityFeePerGas)?
.ok_or(ConversionError::MissingMaxPriorityFeePerGas)?
.to(),
gas_limit: tx.gas.to(),
to: tx.to.into(),
Expand All @@ -743,7 +745,7 @@ impl TryFrom<alloy_rpc_types::Transaction> for Signed<TxEip4844> {
blob_versioned_hashes: tx.blob_versioned_hashes,
max_fee_per_blob_gas: tx
.max_fee_per_blob_gas
.ok_or(Eip2718Error::MissingMaxFeePerBlobGas)?
.ok_or(ConversionError::MissingMaxFeePerBlobGas)?
.to(),
};
Ok(tx.into_signed(signature))
Expand Down
15 changes: 5 additions & 10 deletions crates/consensus/src/transaction/envelope.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
use crate::{
SignableTransaction, Signed, TxEip1559, TxEip2930, TxEip4844, TxEip4844Variant,
TxEip4844WithSidecar, TxLegacy,
Signed, TxEip1559, TxEip2930, TxEip4844, TxEip4844Variant, TxEip4844WithSidecar, TxLegacy,
};
use alloy_eips::eip2718::{Decodable2718, Eip2718Error, Encodable2718};
use alloy_rlp::{Decodable, Encodable, Header};
use alloy_rpc_types::Transaction;

use super::ConversionError;

/// Ethereum `TransactionType` flags as specified in EIPs [2718], [1559], and
/// [2930].
///
Expand Down Expand Up @@ -248,18 +249,12 @@ impl Encodable2718 for TxEnvelope {
}

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

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

match tx.transaction_type {
None => Ok(Self::Legacy(tx.try_into()?)),
Some(t) => match TxType::try_from(t.to())? {
Some(t) => match TxType::try_from(t.to::<u8>())? {
TxType::Eip1559 => Ok(Self::Eip1559(tx.try_into()?)),
TxType::Eip2930 => Ok(Self::Eip2930(tx.try_into()?)),
TxType::Eip4844 => Ok(Self::Eip4844(tx.try_into()?)),
Expand Down
10 changes: 6 additions & 4 deletions crates/consensus/src/transaction/legacy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ use alloy_primitives::{keccak256, Bytes, ChainId, Signature, TxKind, U256};
use alloy_rlp::{length_of_length, BufMut, Decodable, Encodable, Header, Result};
use std::mem;

use super::ConversionError;

/// Legacy transaction.
#[derive(Debug, Clone, PartialEq, Eq, Hash, Default)]
pub struct TxLegacy {
Expand Down Expand Up @@ -276,15 +278,15 @@ impl Decodable for TxLegacy {
}

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

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

let tx = TxLegacy {
chain_id: tx.chain_id.map(|c| c.to()),
chain_id: tx.chain_id,
nonce: tx.nonce,
gas_price: tx.gas_price.ok_or(Eip2718Error::MissingGasPrice)?.to(),
gas_price: tx.gas_price.ok_or(ConversionError::MissingGasPrice)?.to(),
gas_limit: tx.gas.to(),
to: tx.to.into(),
value: tx.value,
Expand Down
23 changes: 23 additions & 0 deletions crates/consensus/src/transaction/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,3 +115,26 @@ impl<S: 'static> dyn SignableTransaction<S> {
}
}
}

/// Error variants when parsing a [BlockNumberOrTag]
#[derive(Debug, thiserror::Error)]
pub enum ConversionError {
#[error("missing `gasPrice` field for Legacy transaction")]
MissingGasPrice,
#[error("missing signature for transaction")]
MissingSignature,
#[error("missing `accessList` field for EIP-2930 transaction")]
MissingAccessList,
#[error("missing `maxFeePerGas` field for EIP-1559 transaction")]
MissingMaxFeePerGas,
#[error("missing `maxPriorityFeePerGas` field for EIP-1559 transaction")]
MissingMaxPriorityFeePerGas,
#[error("missing `maxFeePerBlobGas` field for EIP-1559 transaction")]
MissingMaxFeePerBlobGas,
#[error("missing `chainId` field for EIP-155 transaction")]
MissingChainId,
#[error(transparent)]
SignatureError(#[from] alloy_primitives::SignatureError),
#[error(transparent)]
Eip2718Error(#[from] alloy_eips::eip2718::Eip2718Error),
}
36 changes: 2 additions & 34 deletions crates/eips/src/eip2718.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#[cfg(not(feature = "std"))]
use crate::alloc::{vec, vec::Vec};

use alloy_primitives::{keccak256, Sealed, SignatureError, B256};
use alloy_primitives::{keccak256, Sealed, B256};
use alloy_rlp::{BufMut, Header, EMPTY_STRING_CODE};
use core::{
fmt,
Expand All @@ -18,51 +18,19 @@ const TX_TYPE_BYTE_MAX: u8 = 0x7f;
/// [EIP-2718] decoding errors.
///
/// [EIP-2718]: https://eips.ethereum.org/EIPS/eip-2718
#[derive(Debug)]
#[derive(Debug, Copy, Clone)]
pub enum Eip2718Error {
/// Rlp error from [`alloy_rlp`].
RlpError(alloy_rlp::Error),
/// Got an unexpected type flag while decoding.
UnexpectedType(u8),
/// Missing `gasPrice` field for Legacy transaction.
MissingGasPrice,
/// Missing signature for transaction.
MissingSignature,
/// Missing `accessList` field for EIP-2930 transaction.
MissingAccessList,
/// Missing `maxFeePerGas` field for EIP-1559 transaction.
MissingMaxFeePerGas,
/// Missing `maxPriorityFeePerGas` field for EIP-1559 transaction.
MissingMaxPriorityFeePerGas,
/// Missing `maxFeePerBlobGas` field for EIP-4844 transaction.
MissingMaxFeePerBlobGas,
/// Missing `chainId` field for EIP-155 transaction.
MissingChainId,
/// Signature error from [`alloy_primitives`].
SignatureError(SignatureError),
}

impl Display for Eip2718Error {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
match self {
Self::RlpError(err) => write!(f, "{err}"),
Self::UnexpectedType(t) => write!(f, "Unexpected type flag. Got {t}."),
Self::MissingGasPrice => write!(f, "Missing `gasPrice` field for Legacy transaction."),
Self::MissingSignature => write!(f, "Missing signature for transaction."),
Self::MissingAccessList => {
write!(f, "Missing `accessList` field for EIP-2930 transaction.")
}
Self::MissingMaxFeePerGas => {
write!(f, "Missing `maxFeePerGas` field for EIP-1559 transaction.")
}
Self::MissingMaxPriorityFeePerGas => {
write!(f, "Missing `maxPriorityFeePerGas` field for EIP-1559 transaction.")
}
Self::MissingMaxFeePerBlobGas => {
write!(f, "Missing `maxFeePerBlobGas` field for EIP-4844 transaction.")
}
Self::MissingChainId => write!(f, "Missing `chainId` field for EIP-155 transaction."),
Self::SignatureError(err) => write!(f, "{err}"),
}
}
}
Expand Down

0 comments on commit b1706e6

Please sign in to comment.