From 36ef0a80d76e6705a4e7ade669824c9878ff2e64 Mon Sep 17 00:00:00 2001 From: Soubhik Singha Mahapatra <160333583+Soubhik-10@users.noreply.github.com> Date: Fri, 2 May 2025 16:41:44 +0530 Subject: [PATCH] chore: made `Envelope: FromTxCompact` public (#16038) Co-authored-by: Matthias Seitz --- crates/storage/codecs/src/alloy/mod.rs | 3 +- .../codecs/src/alloy/transaction/ethereum.rs | 38 ++++++++----------- .../codecs/src/alloy/transaction/mod.rs | 2 + .../codecs/src/alloy/transaction/optimism.rs | 11 ++---- crates/storage/codecs/src/lib.rs | 2 +- 5 files changed, 24 insertions(+), 32 deletions(-) diff --git a/crates/storage/codecs/src/alloy/mod.rs b/crates/storage/codecs/src/alloy/mod.rs index 697bac901e..34fcd6fdc2 100644 --- a/crates/storage/codecs/src/alloy/mod.rs +++ b/crates/storage/codecs/src/alloy/mod.rs @@ -19,12 +19,13 @@ cond_mod!( header, log, signature, - transaction, trie, txkind, withdrawal ); +pub mod transaction; + #[cfg(test)] mod tests { use crate::{ diff --git a/crates/storage/codecs/src/alloy/transaction/ethereum.rs b/crates/storage/codecs/src/alloy/transaction/ethereum.rs index 93b9d28400..14d51b866f 100644 --- a/crates/storage/codecs/src/alloy/transaction/ethereum.rs +++ b/crates/storage/codecs/src/alloy/transaction/ethereum.rs @@ -14,7 +14,7 @@ use bytes::{Buf, BufMut}; /// serialized separately. /// /// See [`ToTxCompact::to_tx_compact`]. -pub(super) trait ToTxCompact { +pub trait ToTxCompact { /// Serializes inner transaction using [`Compact`] encoding. Writes the result into `buf`. /// /// The written bytes do not contain signature and transaction type. This information be needs @@ -30,7 +30,8 @@ pub(super) trait ToTxCompact { /// separately. /// /// See [`FromTxCompact::from_tx_compact`]. -pub(super) trait FromTxCompact { +pub trait FromTxCompact { + /// The transaction type that represents the set of transactions. type TxType; /// Deserializes inner transaction using [`Compact`] encoding. The concrete type is determined @@ -39,11 +40,7 @@ pub(super) trait FromTxCompact { /// Returns a tuple of 2 elements. The first element is the deserialized value and the second /// is a byte slice created from `buf` with a starting position advanced by the exact amount /// of bytes consumed for this process. - fn from_tx_compact( - buf: &[u8], - tx_type: Self::TxType, - signature: Signature, - ) -> (Self, &[u8]) + fn from_tx_compact(buf: &[u8], tx_type: Self::TxType, signature: Signature) -> (Self, &[u8]) where Self: Sized; } @@ -63,11 +60,7 @@ impl ToTxCompact for EthereumTxEnvelope impl FromTxCompact for EthereumTxEnvelope { type TxType = TxType; - fn from_tx_compact( - buf: &[u8], - tx_type: TxType, - signature: Signature, - ) -> (Self, &[u8]) { + fn from_tx_compact(buf: &[u8], tx_type: TxType, signature: Signature) -> (Self, &[u8]) { match tx_type { TxType::Legacy => { let (tx, buf) = TxLegacy::from_compact(buf, buf.len()); @@ -98,8 +91,12 @@ impl FromTxCompact for EthereumTxEnvelope { +/// A trait for types convertible from a compact transaction type. +pub trait Envelope: FromTxCompact { + ///Returns the signature fn signature(&self) -> &Signature; + + ///Returns the tx type fn tx_type(&self) -> Self::TxType; } @@ -144,7 +141,7 @@ impl CompactEnvelope for let sig_bit = self.signature().to_compact(buf) as u8; let zstd_bit = self.input().len() >= 32; - let tx_bits = if zstd_bit { + let tx_bits = if zstd_bit { // compress the tx prefixed with txtype let mut tx_buf = Vec::with_capacity(256); let tx_bits = self.tx_type().to_compact(&mut tx_buf) as u8; @@ -167,11 +164,11 @@ impl CompactEnvelope for } .expect("Failed to compress"), ); - tx_bits + tx_bits } else { - let tx_bits = self.tx_type().to_compact(buf) as u8; + let tx_bits = self.tx_type().to_compact(buf) as u8; self.to_tx_compact(buf); - tx_bits + tx_bits }; let flags = sig_bit | (tx_bits << 1) | ((zstd_bit as u8) << 3); @@ -189,7 +186,6 @@ impl CompactEnvelope for let (signature, buf) = Signature::from_compact(buf, sig_bit); - let (transaction, buf) = if zstd_bit != 0 { #[cfg(feature = "std")] { @@ -198,8 +194,7 @@ impl CompactEnvelope for let decompressed = decompressor.decompress(buf); let (tx_type, tx_buf) = T::TxType::from_compact(decompressed, tx_bits); - let (tx, _) = - Self::from_tx_compact(tx_buf, tx_type, signature); + let (tx, _) = Self::from_tx_compact(tx_buf, tx_type, signature); (tx, buf) }) @@ -209,8 +204,7 @@ impl CompactEnvelope for let mut decompressor = reth_zstd_compressors::create_tx_decompressor(); let decompressed = decompressor.decompress(buf); let (tx_type, tx_buf) = T::TxType::from_compact(decompressed, tx_bits); - let (tx, _) = - Self::from_tx_compact(tx_buf, tx_type, signature); + let (tx, _) = Self::from_tx_compact(tx_buf, tx_type, signature); (tx, buf) } diff --git a/crates/storage/codecs/src/alloy/transaction/mod.rs b/crates/storage/codecs/src/alloy/transaction/mod.rs index d21e97f4c4..47881b6f87 100644 --- a/crates/storage/codecs/src/alloy/transaction/mod.rs +++ b/crates/storage/codecs/src/alloy/transaction/mod.rs @@ -56,6 +56,8 @@ where cond_mod!(eip1559, eip2930, eip4844, eip7702, legacy, txtype); mod ethereum; +pub use ethereum::{Envelope, FromTxCompact, ToTxCompact}; + #[cfg(all(feature = "test-utils", feature = "op"))] pub mod optimism; #[cfg(all(not(feature = "test-utils"), feature = "op"))] diff --git a/crates/storage/codecs/src/alloy/transaction/optimism.rs b/crates/storage/codecs/src/alloy/transaction/optimism.rs index f40dd76d5f..6e009a1136 100644 --- a/crates/storage/codecs/src/alloy/transaction/optimism.rs +++ b/crates/storage/codecs/src/alloy/transaction/optimism.rs @@ -12,7 +12,7 @@ use crate::{ use alloy_consensus::{ constants::EIP7702_TX_TYPE_ID, Signed, TxEip1559, TxEip2930, TxEip7702, TxLegacy, }; -use alloy_primitives::{Address, Bytes, Signature, Sealed, TxKind, B256, U256}; +use alloy_primitives::{Address, Bytes, Sealed, Signature, TxKind, B256, U256}; use bytes::BufMut; use op_alloy_consensus::{OpTxEnvelope, OpTxType, OpTypedTransaction, TxDeposit as AlloyTxDeposit}; use reth_codecs_derive::add_arbitrary_tests; @@ -183,11 +183,7 @@ impl ToTxCompact for OpTxEnvelope { impl FromTxCompact for OpTxEnvelope { type TxType = OpTxType; - fn from_tx_compact( - buf: &[u8], - tx_type: OpTxType, - signature: Signature, - ) -> (Self, &[u8]) { + fn from_tx_compact(buf: &[u8], tx_type: OpTxType, signature: Signature) -> (Self, &[u8]) { match tx_type { OpTxType::Legacy => { let (tx, buf) = TxLegacy::from_compact(buf, buf.len()); @@ -218,8 +214,7 @@ impl FromTxCompact for OpTxEnvelope { } } -const DEPOSIT_SIGNATURE: Signature = - Signature::new(U256::ZERO, U256::ZERO, false); +const DEPOSIT_SIGNATURE: Signature = Signature::new(U256::ZERO, U256::ZERO, false); impl Envelope for OpTxEnvelope { fn signature(&self) -> &Signature { diff --git a/crates/storage/codecs/src/lib.rs b/crates/storage/codecs/src/lib.rs index cbd0f7f690..a9cb7f2fcd 100644 --- a/crates/storage/codecs/src/lib.rs +++ b/crates/storage/codecs/src/lib.rs @@ -35,7 +35,7 @@ pub mod alloy; #[cfg(not(feature = "test-utils"))] #[cfg(any(test, feature = "alloy"))] -mod alloy; +pub mod alloy; pub mod txtype;