Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions crates/consensus/src/transaction/eip1559.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,6 @@ impl TxEip1559 {
}

impl RlpEcdsaEncodableTx for TxEip1559 {
const DEFAULT_TX_TYPE: u8 = { Self::tx_type() as u8 };

/// Outputs the length of the transaction's fields, without a RLP header.
fn rlp_encoded_fields_length(&self) -> usize {
self.chain_id.length()
Expand Down Expand Up @@ -129,6 +127,8 @@ impl RlpEcdsaEncodableTx for TxEip1559 {
}

impl RlpEcdsaDecodableTx for TxEip1559 {
const DEFAULT_TX_TYPE: u8 = { Self::tx_type() as u8 };

/// Decodes the inner [TxEip1559] fields from RLP bytes.
///
/// NOTE: This assumes a RLP header has already been decoded, and _just_
Expand Down
4 changes: 2 additions & 2 deletions crates/consensus/src/transaction/eip2930.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,6 @@ impl TxEip2930 {
}

impl RlpEcdsaEncodableTx for TxEip2930 {
const DEFAULT_TX_TYPE: u8 = { Self::tx_type() as u8 };

/// Outputs the length of the transaction's fields, without a RLP header.
fn rlp_encoded_fields_length(&self) -> usize {
self.chain_id.length()
Expand All @@ -110,6 +108,8 @@ impl RlpEcdsaEncodableTx for TxEip2930 {
}

impl RlpEcdsaDecodableTx for TxEip2930 {
const DEFAULT_TX_TYPE: u8 = { Self::tx_type() as u8 };

fn rlp_decode_fields(buf: &mut &[u8]) -> alloy_rlp::Result<Self> {
Ok(Self {
chain_id: Decodable::decode(buf)?,
Expand Down
12 changes: 6 additions & 6 deletions crates/consensus/src/transaction/eip4844.rs
Original file line number Diff line number Diff line change
Expand Up @@ -287,8 +287,6 @@ impl Typed2718 for TxEip4844 {
}

impl RlpEcdsaEncodableTx for TxEip4844Variant {
const DEFAULT_TX_TYPE: u8 = { Self::tx_type() as u8 };

fn rlp_encoded_fields_length(&self) -> usize {
match self {
Self::TxEip4844(inner) => inner.rlp_encoded_fields_length(),
Expand Down Expand Up @@ -326,6 +324,8 @@ impl RlpEcdsaEncodableTx for TxEip4844Variant {
}

impl RlpEcdsaDecodableTx for TxEip4844Variant {
const DEFAULT_TX_TYPE: u8 = { Self::tx_type() as u8 };

fn rlp_decode_fields(buf: &mut &[u8]) -> alloy_rlp::Result<Self> {
let needle = &mut &**buf;

Expand Down Expand Up @@ -542,8 +542,6 @@ impl TxEip4844 {
}

impl RlpEcdsaEncodableTx for TxEip4844 {
const DEFAULT_TX_TYPE: u8 = { Self::tx_type() as u8 };

fn rlp_encoded_fields_length(&self) -> usize {
self.chain_id.length()
+ self.nonce.length()
Expand Down Expand Up @@ -574,6 +572,8 @@ impl RlpEcdsaEncodableTx for TxEip4844 {
}

impl RlpEcdsaDecodableTx for TxEip4844 {
const DEFAULT_TX_TYPE: u8 = { Self::tx_type() as u8 };

fn rlp_decode_fields(buf: &mut &[u8]) -> alloy_rlp::Result<Self> {
Ok(Self {
chain_id: Decodable::decode(buf)?,
Expand Down Expand Up @@ -916,8 +916,6 @@ impl Typed2718 for TxEip4844WithSidecar {
}

impl RlpEcdsaEncodableTx for TxEip4844WithSidecar {
const DEFAULT_TX_TYPE: u8 = { Self::tx_type() as u8 };

fn rlp_encoded_fields_length(&self) -> usize {
self.sidecar.rlp_encoded_fields_length() + self.tx.rlp_encoded_length()
}
Expand Down Expand Up @@ -946,6 +944,8 @@ impl RlpEcdsaEncodableTx for TxEip4844WithSidecar {
}

impl RlpEcdsaDecodableTx for TxEip4844WithSidecar {
const DEFAULT_TX_TYPE: u8 = { Self::tx_type() as u8 };

fn rlp_decode_fields(buf: &mut &[u8]) -> alloy_rlp::Result<Self> {
let tx = TxEip4844::rlp_decode(buf)?;
let sidecar = BlobTransactionSidecar::rlp_decode_fields(buf)?;
Expand Down
4 changes: 2 additions & 2 deletions crates/consensus/src/transaction/eip7702.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,6 @@ impl TxEip7702 {
}

impl RlpEcdsaEncodableTx for TxEip7702 {
const DEFAULT_TX_TYPE: u8 = { Self::tx_type() as u8 };

/// Outputs the length of the transaction's fields, without a RLP header.
#[doc(hidden)]
fn rlp_encoded_fields_length(&self) -> usize {
Expand Down Expand Up @@ -139,6 +137,8 @@ impl RlpEcdsaEncodableTx for TxEip7702 {
}

impl RlpEcdsaDecodableTx for TxEip7702 {
const DEFAULT_TX_TYPE: u8 = { Self::tx_type() as u8 };

fn rlp_decode_fields(buf: &mut &[u8]) -> alloy_rlp::Result<Self> {
Ok(Self {
chain_id: Decodable::decode(buf)?,
Expand Down
4 changes: 2 additions & 2 deletions crates/consensus/src/transaction/legacy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,6 @@ impl TxLegacy {
// Legacy transaction network and 2718 encodings are identical to the RLP
// encoding.
impl RlpEcdsaEncodableTx for TxLegacy {
const DEFAULT_TX_TYPE: u8 = { Self::TX_TYPE as u8 };

fn rlp_encoded_fields_length(&self) -> usize {
self.nonce.length()
+ self.gas_price.length()
Expand Down Expand Up @@ -179,6 +177,8 @@ impl RlpEcdsaEncodableTx for TxLegacy {
}

impl RlpEcdsaDecodableTx for TxLegacy {
const DEFAULT_TX_TYPE: u8 = { Self::TX_TYPE as u8 };

fn rlp_decode_fields(buf: &mut &[u8]) -> alloy_rlp::Result<Self> {
Ok(Self {
nonce: Decodable::decode(buf)?,
Expand Down
19 changes: 11 additions & 8 deletions crates/consensus/src/transaction/rlp.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
use crate::Signed;
use alloc::vec::Vec;
use alloy_eips::eip2718::{Eip2718Error, Eip2718Result};
use alloy_eips::{
eip2718::{Eip2718Error, Eip2718Result},
Typed2718,
};
use alloy_primitives::{keccak256, PrimitiveSignature as Signature, TxHash};
use alloy_rlp::{Buf, BufMut, Decodable, Encodable, Header};

/// Helper trait for managing RLP encoding of transactions inside 2718
#[doc(hidden)]
#[doc(alias = "RlpEncodableTx", alias = "RlpTxEncoding")]
#[auto_impl::auto_impl(&, Arc)]
pub trait RlpEcdsaEncodableTx: Sized {
/// The default transaction type for this transaction.
const DEFAULT_TX_TYPE: u8;

pub trait RlpEcdsaEncodableTx: Sized + Typed2718 {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is great

/// Calculate the encoded length of the transaction's fields, without a RLP
/// header.
fn rlp_encoded_fields_length(&self) -> usize;
Expand Down Expand Up @@ -71,7 +71,7 @@ pub trait RlpEcdsaEncodableTx: Sized {
/// EIP-2718 encode the transaction with the given signature and the default
/// type flag.
fn eip2718_encode(&self, signature: &Signature, out: &mut dyn BufMut) {
self.eip2718_encode_with_type(signature, Self::DEFAULT_TX_TYPE, out);
self.eip2718_encode_with_type(signature, self.ty(), out);
}

/// Create an rlp header for the network encoded transaction. This will
Expand All @@ -97,7 +97,7 @@ pub trait RlpEcdsaEncodableTx: Sized {
/// Network encode the transaction with the given signature and the default
/// type flag.
fn network_encode(&self, signature: &Signature, out: &mut dyn BufMut) {
self.network_encode_with_type(signature, Self::DEFAULT_TX_TYPE, out);
self.network_encode_with_type(signature, self.ty(), out);
}

/// Calculate the transaction hash for the given signature and type.
Expand All @@ -109,14 +109,17 @@ pub trait RlpEcdsaEncodableTx: Sized {

/// Calculate the transaction hash for the given signature.
fn tx_hash(&self, signature: &Signature) -> TxHash {
self.tx_hash_with_type(signature, Self::DEFAULT_TX_TYPE)
self.tx_hash_with_type(signature, self.ty())
}
}

/// Helper trait for managing RLP decoding of transactions inside 2718 envelopes.
#[doc(hidden)]
#[doc(alias = "RlpDecodableTx", alias = "RlpTxDecoding")]
pub trait RlpEcdsaDecodableTx: RlpEcdsaEncodableTx {
/// The default transaction type for this transaction.
const DEFAULT_TX_TYPE: u8;

/// Decodes the fields of the transaction from RLP bytes. Do not decode a
/// header. You may assume the buffer is long enough to contain the
/// transaction.
Expand Down
2 changes: 0 additions & 2 deletions crates/consensus/src/transaction/typed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -375,8 +375,6 @@ impl<Eip4844: Typed2718> Typed2718 for EthereumTypedTransaction<Eip4844> {
impl<Eip4844: RlpEcdsaEncodableTx + Typed2718> RlpEcdsaEncodableTx
for EthereumTypedTransaction<Eip4844>
{
const DEFAULT_TX_TYPE: u8 = 0;

fn rlp_encoded_fields_length(&self) -> usize {
match self {
Self::Legacy(tx) => tx.rlp_encoded_fields_length(),
Expand Down