diff --git a/crates/consensus/src/transaction/eip1559.rs b/crates/consensus/src/transaction/eip1559.rs index 92a02a3e566..a392418d114 100644 --- a/crates/consensus/src/transaction/eip1559.rs +++ b/crates/consensus/src/transaction/eip1559.rs @@ -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() @@ -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_ diff --git a/crates/consensus/src/transaction/eip2930.rs b/crates/consensus/src/transaction/eip2930.rs index 4a796b7cbce..f281138983a 100644 --- a/crates/consensus/src/transaction/eip2930.rs +++ b/crates/consensus/src/transaction/eip2930.rs @@ -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() @@ -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 { Ok(Self { chain_id: Decodable::decode(buf)?, diff --git a/crates/consensus/src/transaction/eip4844.rs b/crates/consensus/src/transaction/eip4844.rs index c0b5971f084..a92794fb816 100644 --- a/crates/consensus/src/transaction/eip4844.rs +++ b/crates/consensus/src/transaction/eip4844.rs @@ -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(), @@ -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 { let needle = &mut &**buf; @@ -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() @@ -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 { Ok(Self { chain_id: Decodable::decode(buf)?, @@ -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() } @@ -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 { let tx = TxEip4844::rlp_decode(buf)?; let sidecar = BlobTransactionSidecar::rlp_decode_fields(buf)?; diff --git a/crates/consensus/src/transaction/eip7702.rs b/crates/consensus/src/transaction/eip7702.rs index a2a861ef13f..2688f78674a 100644 --- a/crates/consensus/src/transaction/eip7702.rs +++ b/crates/consensus/src/transaction/eip7702.rs @@ -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 { @@ -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 { Ok(Self { chain_id: Decodable::decode(buf)?, diff --git a/crates/consensus/src/transaction/legacy.rs b/crates/consensus/src/transaction/legacy.rs index 9281fb7611a..98031daeccb 100644 --- a/crates/consensus/src/transaction/legacy.rs +++ b/crates/consensus/src/transaction/legacy.rs @@ -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() @@ -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 { Ok(Self { nonce: Decodable::decode(buf)?, diff --git a/crates/consensus/src/transaction/rlp.rs b/crates/consensus/src/transaction/rlp.rs index 31131268184..b6098324a63 100644 --- a/crates/consensus/src/transaction/rlp.rs +++ b/crates/consensus/src/transaction/rlp.rs @@ -1,6 +1,9 @@ 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}; @@ -8,10 +11,7 @@ use alloy_rlp::{Buf, BufMut, Decodable, Encodable, Header}; #[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 { /// Calculate the encoded length of the transaction's fields, without a RLP /// header. fn rlp_encoded_fields_length(&self) -> usize; @@ -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 @@ -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. @@ -109,7 +109,7 @@ 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()) } } @@ -117,6 +117,9 @@ pub trait RlpEcdsaEncodableTx: Sized { #[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. diff --git a/crates/consensus/src/transaction/typed.rs b/crates/consensus/src/transaction/typed.rs index de800d8437c..e1593ce1605 100644 --- a/crates/consensus/src/transaction/typed.rs +++ b/crates/consensus/src/transaction/typed.rs @@ -375,8 +375,6 @@ impl Typed2718 for EthereumTypedTransaction { impl RlpEcdsaEncodableTx for EthereumTypedTransaction { - const DEFAULT_TX_TYPE: u8 = 0; - fn rlp_encoded_fields_length(&self) -> usize { match self { Self::Legacy(tx) => tx.rlp_encoded_fields_length(),