From f9d6372a98d198b75f703896b04361588882bbc8 Mon Sep 17 00:00:00 2001 From: Khanh Hoa Date: Tue, 15 Oct 2024 16:45:11 +0700 Subject: [PATCH 01/18] bet --- crates/primitives/src/receipt.rs | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/crates/primitives/src/receipt.rs b/crates/primitives/src/receipt.rs index cfd831ed0f74..b3eccd4fa109 100644 --- a/crates/primitives/src/receipt.rs +++ b/crates/primitives/src/receipt.rs @@ -14,6 +14,11 @@ use derive_more::{DerefMut, From, IntoIterator}; use reth_codecs::{Compact, CompactZstd}; use serde::{Deserialize, Serialize}; +pub trait Encodable2718: Encodable { + fn encode_envelope(&self, out: &mut dyn BufMut); + fn envelope_length(&self) -> usize; +} + /// Receipt containing result of transaction execution. #[derive( Clone, Debug, PartialEq, Eq, Default, RlpEncodable, RlpDecodable, Serialize, Deserialize, @@ -205,14 +210,6 @@ impl<'a> arbitrary::Arbitrary<'a> for Receipt { } impl ReceiptWithBloom { - /// Returns the enveloped encoded receipt. - /// - /// See also [`ReceiptWithBloom::encode_enveloped`] - pub fn envelope_encoded(&self) -> Bytes { - let mut buf = Vec::new(); - self.encode_enveloped(&mut buf); - buf.into() - } /// Encodes the receipt into its "raw" format. /// This format is also referred to as "binary" encoding. @@ -289,6 +286,16 @@ impl ReceiptWithBloom { } } +impl Encodable2718 for ReceiptWithBloom { + fn encode_envelope(&self, out: &mut dyn BufMut) { + self.encode_enveloped(out) + } + + fn envelope_length(&self) -> usize { + self.as_encoder().length() + } +} + impl Encodable for ReceiptWithBloom { fn encode(&self, out: &mut dyn BufMut) { self.encode_inner(out, true) From 87933e687e51ac857eac1fbfae664a1104c0f9b3 Mon Sep 17 00:00:00 2001 From: Khanh Hoa Date: Tue, 15 Oct 2024 16:53:02 +0700 Subject: [PATCH 02/18] bet --- crates/rpc/rpc/src/debug.rs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/crates/rpc/rpc/src/debug.rs b/crates/rpc/rpc/src/debug.rs index acf215b3b2cc..134d7158412d 100644 --- a/crates/rpc/rpc/src/debug.rs +++ b/crates/rpc/rpc/src/debug.rs @@ -891,7 +891,11 @@ where .to_rpc_result()? .unwrap_or_default() .into_iter() - .map(|receipt| receipt.with_bloom().envelope_encoded()) + .map(|receipt| { + let mut buf = Vec::new(); + receipt.with_bloom().encode(&mut buf); + buf.into() + }) .collect()) } From e19e88705d5f3d698610bf33b3992e2262afe896 Mon Sep 17 00:00:00 2001 From: Khanh Hoa Date: Tue, 15 Oct 2024 17:24:45 +0700 Subject: [PATCH 03/18] bet --- crates/primitives/src/lib.rs | 2 +- crates/primitives/src/receipt.rs | 45 ++++++++++++++------------------ crates/rpc/rpc/src/debug.rs | 7 ++--- 3 files changed, 22 insertions(+), 32 deletions(-) diff --git a/crates/primitives/src/lib.rs b/crates/primitives/src/lib.rs index ec65cbf20e52..c6d42387de92 100644 --- a/crates/primitives/src/lib.rs +++ b/crates/primitives/src/lib.rs @@ -28,7 +28,7 @@ mod block; mod compression; pub mod constants; pub mod proofs; -mod receipt; +pub mod receipt; pub use reth_static_file_types as static_file; pub mod transaction; #[cfg(any(test, feature = "arbitrary"))] diff --git a/crates/primitives/src/receipt.rs b/crates/primitives/src/receipt.rs index b3eccd4fa109..4cf0a0bafcca 100644 --- a/crates/primitives/src/receipt.rs +++ b/crates/primitives/src/receipt.rs @@ -14,11 +14,6 @@ use derive_more::{DerefMut, From, IntoIterator}; use reth_codecs::{Compact, CompactZstd}; use serde::{Deserialize, Serialize}; -pub trait Encodable2718: Encodable { - fn encode_envelope(&self, out: &mut dyn BufMut); - fn envelope_length(&self) -> usize; -} - /// Receipt containing result of transaction execution. #[derive( Clone, Debug, PartialEq, Eq, Default, RlpEncodable, RlpDecodable, Serialize, Deserialize, @@ -209,21 +204,29 @@ impl<'a> arbitrary::Arbitrary<'a> for Receipt { } } -impl ReceiptWithBloom { +/// A trait for enveloped encoding of receipts. +pub trait EnvelopedEncoding { + /// Returns the enveloped encoded receipt. + fn envelope_encoded(&self) -> Bytes; /// Encodes the receipt into its "raw" format. - /// This format is also referred to as "binary" encoding. - /// - /// For legacy receipts, it encodes the RLP of the receipt into the buffer: - /// `rlp([status, cumulativeGasUsed, logsBloom, logs])` as per EIP-2718. - /// For EIP-2718 typed transactions, it encodes the type of the transaction followed by the rlp - /// of the receipt: - /// - EIP-1559, 2930 and 4844 transactions: `tx-type || rlp([status, cumulativeGasUsed, - /// logsBloom, logs])` - pub fn encode_enveloped(&self, out: &mut dyn bytes::BufMut) { + fn encode_enveloped(&self, out: &mut dyn bytes::BufMut); +} + +// Implement the trait for ReceiptWithBloom +impl EnvelopedEncoding for ReceiptWithBloom { + fn envelope_encoded(&self) -> Bytes { + let mut buf = Vec::new(); + self.encode_enveloped(&mut buf); + buf.into() + } + + fn encode_enveloped(&self, out: &mut dyn bytes::BufMut) { self.encode_inner(out, false) } +} +impl ReceiptWithBloom { /// Encode receipt with or without the header data. pub fn encode_inner(&self, out: &mut dyn BufMut, with_header: bool) { self.as_encoder().encode_inner(out, with_header) @@ -286,16 +289,6 @@ impl ReceiptWithBloom { } } -impl Encodable2718 for ReceiptWithBloom { - fn encode_envelope(&self, out: &mut dyn BufMut) { - self.encode_enveloped(out) - } - - fn envelope_length(&self) -> usize { - self.as_encoder().length() - } -} - impl Encodable for ReceiptWithBloom { fn encode(&self, out: &mut dyn BufMut) { self.encode_inner(out, true) @@ -656,4 +649,4 @@ mod tests { let (decoded, _) = Receipt::from_compact(&data[..], data.len()); assert_eq!(decoded, receipt); } -} +} \ No newline at end of file diff --git a/crates/rpc/rpc/src/debug.rs b/crates/rpc/rpc/src/debug.rs index 134d7158412d..d9987ee6ef41 100644 --- a/crates/rpc/rpc/src/debug.rs +++ b/crates/rpc/rpc/src/debug.rs @@ -11,6 +11,7 @@ use alloy_rpc_types_trace::geth::{ GethDebugTracerType, GethDebugTracingCallOptions, GethDebugTracingOptions, GethTrace, NoopFrame, TraceResult, }; +use reth_primitives::receipt::EnvelopedEncoding; use async_trait::async_trait; use jsonrpsee::core::RpcResult; use reth_chainspec::EthereumHardforks; @@ -891,11 +892,7 @@ where .to_rpc_result()? .unwrap_or_default() .into_iter() - .map(|receipt| { - let mut buf = Vec::new(); - receipt.with_bloom().encode(&mut buf); - buf.into() - }) + .map(|receipt| receipt.with_bloom().envelope_encoded()) .collect()) } From 7c476706f231c0b21766ee2be38790a0e28b78c7 Mon Sep 17 00:00:00 2001 From: Khanh Hoa Date: Tue, 15 Oct 2024 17:26:05 +0700 Subject: [PATCH 04/18] bet --- crates/primitives/src/receipt.rs | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/crates/primitives/src/receipt.rs b/crates/primitives/src/receipt.rs index 4cf0a0bafcca..024a234d16c5 100644 --- a/crates/primitives/src/receipt.rs +++ b/crates/primitives/src/receipt.rs @@ -215,6 +215,15 @@ pub trait EnvelopedEncoding { // Implement the trait for ReceiptWithBloom impl EnvelopedEncoding for ReceiptWithBloom { + /// Encodes the receipt into its "raw" format. + /// This format is also referred to as "binary" encoding. + /// + /// For legacy receipts, it encodes the RLP of the receipt into the buffer: + /// `rlp([status, cumulativeGasUsed, logsBloom, logs])` as per EIP-2718. + /// For EIP-2718 typed transactions, it encodes the type of the transaction followed by the rlp + /// of the receipt: + /// - EIP-1559, 2930 and 4844 transactions: `tx-type || rlp([status, cumulativeGasUsed, + /// logsBloom, logs])` fn envelope_encoded(&self) -> Bytes { let mut buf = Vec::new(); self.encode_enveloped(&mut buf); From 99016faa20f3bec24b150fe84451631bb145eae9 Mon Sep 17 00:00:00 2001 From: Khanh Hoa Date: Tue, 15 Oct 2024 17:35:37 +0700 Subject: [PATCH 05/18] bet --- crates/primitives/src/lib.rs | 4 ++-- crates/rpc/rpc/src/debug.rs | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/crates/primitives/src/lib.rs b/crates/primitives/src/lib.rs index c6d42387de92..4f0077311fcf 100644 --- a/crates/primitives/src/lib.rs +++ b/crates/primitives/src/lib.rs @@ -28,7 +28,7 @@ mod block; mod compression; pub mod constants; pub mod proofs; -pub mod receipt; +mod receipt; pub use reth_static_file_types as static_file; pub mod transaction; #[cfg(any(test, feature = "arbitrary"))] @@ -44,7 +44,7 @@ pub use constants::{ MAINNET_GENESIS_HASH, SEPOLIA_GENESIS_HASH, }; pub use receipt::{ - gas_spent_by_transactions, Receipt, ReceiptWithBloom, ReceiptWithBloomRef, Receipts, + gas_spent_by_transactions, Receipt, ReceiptWithBloom, ReceiptWithBloomRef, Receipts, EnvelopedEncoding }; pub use reth_primitives_traits::{ logs_bloom, Account, Bytecode, GotExpected, GotExpectedBoxed, Header, HeaderError, Log, diff --git a/crates/rpc/rpc/src/debug.rs b/crates/rpc/rpc/src/debug.rs index d9987ee6ef41..371fe36c86b8 100644 --- a/crates/rpc/rpc/src/debug.rs +++ b/crates/rpc/rpc/src/debug.rs @@ -11,7 +11,7 @@ use alloy_rpc_types_trace::geth::{ GethDebugTracerType, GethDebugTracingCallOptions, GethDebugTracingOptions, GethTrace, NoopFrame, TraceResult, }; -use reth_primitives::receipt::EnvelopedEncoding; +use reth_primitives::EnvelopedEncoding; use async_trait::async_trait; use jsonrpsee::core::RpcResult; use reth_chainspec::EthereumHardforks; From a352a2dc874ff24cd1616a890d9486374b487906 Mon Sep 17 00:00:00 2001 From: Khanh Hoa Date: Tue, 15 Oct 2024 19:10:55 +0700 Subject: [PATCH 06/18] bet --- crates/primitives/src/lib.rs | 3 ++- crates/primitives/src/receipt.rs | 2 +- crates/rpc/rpc/src/debug.rs | 5 +++-- 3 files changed, 6 insertions(+), 4 deletions(-) diff --git a/crates/primitives/src/lib.rs b/crates/primitives/src/lib.rs index 4f0077311fcf..0798c6fe5830 100644 --- a/crates/primitives/src/lib.rs +++ b/crates/primitives/src/lib.rs @@ -44,7 +44,8 @@ pub use constants::{ MAINNET_GENESIS_HASH, SEPOLIA_GENESIS_HASH, }; pub use receipt::{ - gas_spent_by_transactions, Receipt, ReceiptWithBloom, ReceiptWithBloomRef, Receipts, EnvelopedEncoding + gas_spent_by_transactions, EnvelopedEncoding, Receipt, ReceiptWithBloom, ReceiptWithBloomRef, + Receipts, }; pub use reth_primitives_traits::{ logs_bloom, Account, Bytecode, GotExpected, GotExpectedBoxed, Header, HeaderError, Log, diff --git a/crates/primitives/src/receipt.rs b/crates/primitives/src/receipt.rs index 024a234d16c5..40275b88bc66 100644 --- a/crates/primitives/src/receipt.rs +++ b/crates/primitives/src/receipt.rs @@ -658,4 +658,4 @@ mod tests { let (decoded, _) = Receipt::from_compact(&data[..], data.len()); assert_eq!(decoded, receipt); } -} \ No newline at end of file +} diff --git a/crates/rpc/rpc/src/debug.rs b/crates/rpc/rpc/src/debug.rs index 371fe36c86b8..aba07e3b24d5 100644 --- a/crates/rpc/rpc/src/debug.rs +++ b/crates/rpc/rpc/src/debug.rs @@ -11,7 +11,6 @@ use alloy_rpc_types_trace::geth::{ GethDebugTracerType, GethDebugTracingCallOptions, GethDebugTracingOptions, GethTrace, NoopFrame, TraceResult, }; -use reth_primitives::EnvelopedEncoding; use async_trait::async_trait; use jsonrpsee::core::RpcResult; use reth_chainspec::EthereumHardforks; @@ -19,7 +18,9 @@ use reth_evm::{ execute::{BlockExecutorProvider, Executor}, ConfigureEvmEnv, }; -use reth_primitives::{Block, BlockId, BlockNumberOrTag, TransactionSignedEcRecovered}; +use reth_primitives::{ + Block, BlockId, BlockNumberOrTag, EnvelopedEncoding, TransactionSignedEcRecovered, +}; use reth_provider::{ BlockReaderIdExt, ChainSpecProvider, EvmEnvProvider, HeaderProvider, StateProofProvider, StateProviderFactory, TransactionVariant, From 5ca410c02435285c71f875ef0a931f9cda363c5f Mon Sep 17 00:00:00 2001 From: Tuan Tran Date: Wed, 16 Oct 2024 11:46:53 +0700 Subject: [PATCH 07/18] bet --- crates/primitives/src/receipt.rs | 35 +++++++++++++++----------------- 1 file changed, 16 insertions(+), 19 deletions(-) diff --git a/crates/primitives/src/receipt.rs b/crates/primitives/src/receipt.rs index 40275b88bc66..a3d215ce1acf 100644 --- a/crates/primitives/src/receipt.rs +++ b/crates/primitives/src/receipt.rs @@ -5,7 +5,8 @@ use crate::{ EIP7702_TX_TYPE_ID, }; use alloc::{vec, vec::Vec}; -use alloy_primitives::{Bloom, Bytes, Log, B256}; +use alloy_eips::eip2718::Encodable2718; +use alloy_primitives::{Bloom, Log, B256}; use alloy_rlp::{length_of_length, Decodable, Encodable, RlpDecodable, RlpEncodable}; use bytes::{Buf, BufMut}; use core::{cmp::Ordering, ops::Deref}; @@ -204,17 +205,8 @@ impl<'a> arbitrary::Arbitrary<'a> for Receipt { } } -/// A trait for enveloped encoding of receipts. -pub trait EnvelopedEncoding { - /// Returns the enveloped encoded receipt. - fn envelope_encoded(&self) -> Bytes; - - /// Encodes the receipt into its "raw" format. - fn encode_enveloped(&self, out: &mut dyn bytes::BufMut); -} - // Implement the trait for ReceiptWithBloom -impl EnvelopedEncoding for ReceiptWithBloom { +impl Encodable2718 for ReceiptWithBloom { /// Encodes the receipt into its "raw" format. /// This format is also referred to as "binary" encoding. /// @@ -224,13 +216,18 @@ impl EnvelopedEncoding for ReceiptWithBloom { /// of the receipt: /// - EIP-1559, 2930 and 4844 transactions: `tx-type || rlp([status, cumulativeGasUsed, /// logsBloom, logs])` - fn envelope_encoded(&self) -> Bytes { - let mut buf = Vec::new(); - self.encode_enveloped(&mut buf); - buf.into() + fn type_flag(&self) -> Option { + match self.receipt.tx_type { + TxType::Legacy => None, + tx_type => Some(tx_type as u8), + } + } + + fn encode_2718_len(&self) -> usize { + self.as_encoder().length() } - fn encode_enveloped(&self, out: &mut dyn bytes::BufMut) { + fn encode_2718(&self, out: &mut dyn alloy_rlp::BufMut) { self.encode_inner(out, false) } } @@ -246,7 +243,7 @@ impl ReceiptWithBloom { let b = &mut &**buf; let rlp_head = alloy_rlp::Header::decode(b)?; if !rlp_head.list { - return Err(alloy_rlp::Error::UnexpectedString) + return Err(alloy_rlp::Error::UnexpectedString); } let started_len = b.len(); @@ -291,7 +288,7 @@ impl ReceiptWithBloom { return Err(alloy_rlp::Error::ListLengthMismatch { expected: rlp_head.payload_length, got: consumed, - }) + }); } *buf = *b; Ok(this) @@ -448,7 +445,7 @@ impl ReceiptWithBloomEncoder<'_> { fn encode_inner(&self, out: &mut dyn BufMut, with_header: bool) { if matches!(self.receipt.tx_type, TxType::Legacy) { self.encode_fields(out); - return + return; } let mut payload = Vec::new(); From 4926dec96d7e2e457e05f10a3c4f82cd2b508c73 Mon Sep 17 00:00:00 2001 From: Khanh Hoa Date: Wed, 16 Oct 2024 11:58:19 +0700 Subject: [PATCH 08/18] bet --- crates/primitives/src/lib.rs | 3 +- crates/primitives/src/receipt.rs | 71 ++++++++++++++++++-------------- crates/rpc/rpc/src/debug.rs | 10 +++-- 3 files changed, 47 insertions(+), 37 deletions(-) diff --git a/crates/primitives/src/lib.rs b/crates/primitives/src/lib.rs index 0798c6fe5830..ec65cbf20e52 100644 --- a/crates/primitives/src/lib.rs +++ b/crates/primitives/src/lib.rs @@ -44,8 +44,7 @@ pub use constants::{ MAINNET_GENESIS_HASH, SEPOLIA_GENESIS_HASH, }; pub use receipt::{ - gas_spent_by_transactions, EnvelopedEncoding, Receipt, ReceiptWithBloom, ReceiptWithBloomRef, - Receipts, + gas_spent_by_transactions, Receipt, ReceiptWithBloom, ReceiptWithBloomRef, Receipts, }; pub use reth_primitives_traits::{ logs_bloom, Account, Bytecode, GotExpected, GotExpectedBoxed, Header, HeaderError, Log, diff --git a/crates/primitives/src/receipt.rs b/crates/primitives/src/receipt.rs index 40275b88bc66..fd4f1172d987 100644 --- a/crates/primitives/src/receipt.rs +++ b/crates/primitives/src/receipt.rs @@ -5,6 +5,7 @@ use crate::{ EIP7702_TX_TYPE_ID, }; use alloc::{vec, vec::Vec}; +use alloy_eips::eip2718::Encodable2718; use alloy_primitives::{Bloom, Bytes, Log, B256}; use alloy_rlp::{length_of_length, Decodable, Encodable, RlpDecodable, RlpEncodable}; use bytes::{Buf, BufMut}; @@ -204,43 +205,16 @@ impl<'a> arbitrary::Arbitrary<'a> for Receipt { } } -/// A trait for enveloped encoding of receipts. -pub trait EnvelopedEncoding { - /// Returns the enveloped encoded receipt. - fn envelope_encoded(&self) -> Bytes; - - /// Encodes the receipt into its "raw" format. - fn encode_enveloped(&self, out: &mut dyn bytes::BufMut); -} - -// Implement the trait for ReceiptWithBloom -impl EnvelopedEncoding for ReceiptWithBloom { - /// Encodes the receipt into its "raw" format. - /// This format is also referred to as "binary" encoding. - /// - /// For legacy receipts, it encodes the RLP of the receipt into the buffer: - /// `rlp([status, cumulativeGasUsed, logsBloom, logs])` as per EIP-2718. - /// For EIP-2718 typed transactions, it encodes the type of the transaction followed by the rlp - /// of the receipt: - /// - EIP-1559, 2930 and 4844 transactions: `tx-type || rlp([status, cumulativeGasUsed, - /// logsBloom, logs])` - fn envelope_encoded(&self) -> Bytes { - let mut buf = Vec::new(); - self.encode_enveloped(&mut buf); - buf.into() - } - - fn encode_enveloped(&self, out: &mut dyn bytes::BufMut) { - self.encode_inner(out, false) - } -} - impl ReceiptWithBloom { /// Encode receipt with or without the header data. pub fn encode_inner(&self, out: &mut dyn BufMut, with_header: bool) { self.as_encoder().encode_inner(out, with_header) } + fn encode_inner_len(&self, _with_header: bool) -> usize { + self.as_encoder().length() + } + /// Decodes the receipt payload fn decode_receipt(buf: &mut &[u8], tx_type: TxType) -> alloy_rlp::Result { let b = &mut &**buf; @@ -298,6 +272,41 @@ impl ReceiptWithBloom { } } +impl Encodable2718 for ReceiptWithBloom { + /// Return the type flag for the receipt. + /// This could be `None` for legacy receipts or a specific byte for typed receipts. + fn type_flag(&self) -> Option { + match self.receipt.tx_type { + TxType::Legacy => None, + TxType::Eip2930 => Some(EIP2930_TX_TYPE_ID), + TxType::Eip1559 => Some(EIP1559_TX_TYPE_ID), + TxType::Eip4844 => Some(EIP4844_TX_TYPE_ID), + TxType::Eip7702 => Some(EIP7702_TX_TYPE_ID), + #[cfg(feature = "optimism")] + TxType::Deposit => Some(crate::transaction::DEPOSIT_TX_TYPE_ID), + } + } + + /// Calculate the length of the 2718 encoded envelope. + fn encode_2718_len(&self) -> usize { + let payload_len = self.encode_inner_len(false); + if self.type_flag().is_some() { + // Add 1 for the type flag byte + payload_len + 1 + } else { + payload_len + } + } + + /// Encode the transaction according to EIP-2718 rules. + fn encode_2718(&self, out: &mut dyn BufMut) { + if let Some(type_flag) = self.type_flag() { + out.put_u8(type_flag); + } + self.encode_inner(out, false); + } +} + impl Encodable for ReceiptWithBloom { fn encode(&self, out: &mut dyn BufMut) { self.encode_inner(out, true) diff --git a/crates/rpc/rpc/src/debug.rs b/crates/rpc/rpc/src/debug.rs index aba07e3b24d5..323a38207984 100644 --- a/crates/rpc/rpc/src/debug.rs +++ b/crates/rpc/rpc/src/debug.rs @@ -18,9 +18,7 @@ use reth_evm::{ execute::{BlockExecutorProvider, Executor}, ConfigureEvmEnv, }; -use reth_primitives::{ - Block, BlockId, BlockNumberOrTag, EnvelopedEncoding, TransactionSignedEcRecovered, -}; +use reth_primitives::{Block, BlockId, BlockNumberOrTag, TransactionSignedEcRecovered}; use reth_provider::{ BlockReaderIdExt, ChainSpecProvider, EvmEnvProvider, HeaderProvider, StateProofProvider, StateProviderFactory, TransactionVariant, @@ -893,7 +891,11 @@ where .to_rpc_result()? .unwrap_or_default() .into_iter() - .map(|receipt| receipt.with_bloom().envelope_encoded()) + .map(|receipt| { + let mut out = vec![]; + receipt.with_bloom().encode_2718(&mut out); + out.into() + }) .collect()) } From b4c9cd73e24dcf3665ec61a49978d4aefcc4c6cf Mon Sep 17 00:00:00 2001 From: Khanh Hoa Date: Wed, 16 Oct 2024 11:59:45 +0700 Subject: [PATCH 09/18] bet --- crates/primitives/src/receipt.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/primitives/src/receipt.rs b/crates/primitives/src/receipt.rs index fd4f1172d987..8d30e68ba9e2 100644 --- a/crates/primitives/src/receipt.rs +++ b/crates/primitives/src/receipt.rs @@ -6,7 +6,7 @@ use crate::{ }; use alloc::{vec, vec::Vec}; use alloy_eips::eip2718::Encodable2718; -use alloy_primitives::{Bloom, Bytes, Log, B256}; +use alloy_primitives::{Bloom, Log, B256}; use alloy_rlp::{length_of_length, Decodable, Encodable, RlpDecodable, RlpEncodable}; use bytes::{Buf, BufMut}; use core::{cmp::Ordering, ops::Deref}; From 24e10f3cf12f2e51eb7ba59e2fca5598fc9ce3ae Mon Sep 17 00:00:00 2001 From: Khanh Hoa Date: Wed, 16 Oct 2024 12:05:36 +0700 Subject: [PATCH 10/18] bet --- crates/primitives/src/receipt.rs | 39 -------------------------------- 1 file changed, 39 deletions(-) diff --git a/crates/primitives/src/receipt.rs b/crates/primitives/src/receipt.rs index cb6d723b5f23..a3d215ce1acf 100644 --- a/crates/primitives/src/receipt.rs +++ b/crates/primitives/src/receipt.rs @@ -238,10 +238,6 @@ impl ReceiptWithBloom { self.as_encoder().encode_inner(out, with_header) } - fn encode_inner_len(&self, _with_header: bool) -> usize { - self.as_encoder().length() - } - /// Decodes the receipt payload fn decode_receipt(buf: &mut &[u8], tx_type: TxType) -> alloy_rlp::Result { let b = &mut &**buf; @@ -299,41 +295,6 @@ impl ReceiptWithBloom { } } -impl Encodable2718 for ReceiptWithBloom { - /// Return the type flag for the receipt. - /// This could be `None` for legacy receipts or a specific byte for typed receipts. - fn type_flag(&self) -> Option { - match self.receipt.tx_type { - TxType::Legacy => None, - TxType::Eip2930 => Some(EIP2930_TX_TYPE_ID), - TxType::Eip1559 => Some(EIP1559_TX_TYPE_ID), - TxType::Eip4844 => Some(EIP4844_TX_TYPE_ID), - TxType::Eip7702 => Some(EIP7702_TX_TYPE_ID), - #[cfg(feature = "optimism")] - TxType::Deposit => Some(crate::transaction::DEPOSIT_TX_TYPE_ID), - } - } - - /// Calculate the length of the 2718 encoded envelope. - fn encode_2718_len(&self) -> usize { - let payload_len = self.encode_inner_len(false); - if self.type_flag().is_some() { - // Add 1 for the type flag byte - payload_len + 1 - } else { - payload_len - } - } - - /// Encode the transaction according to EIP-2718 rules. - fn encode_2718(&self, out: &mut dyn BufMut) { - if let Some(type_flag) = self.type_flag() { - out.put_u8(type_flag); - } - self.encode_inner(out, false); - } -} - impl Encodable for ReceiptWithBloom { fn encode(&self, out: &mut dyn BufMut) { self.encode_inner(out, true) From 678a187a2fd6b8a2ab39af9af79823ef2879ae4e Mon Sep 17 00:00:00 2001 From: Khanh Hoa Date: Wed, 16 Oct 2024 12:11:06 +0700 Subject: [PATCH 11/18] bet --- crates/primitives/src/receipt.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/crates/primitives/src/receipt.rs b/crates/primitives/src/receipt.rs index a3d215ce1acf..8101561f7612 100644 --- a/crates/primitives/src/receipt.rs +++ b/crates/primitives/src/receipt.rs @@ -507,6 +507,7 @@ impl Encodable for ReceiptWithBloomEncoder<'_> { #[cfg(test)] mod tests { use super::*; + use crate::revm_primitives::Bytes; use alloy_primitives::{address, b256, bytes, hex_literal::hex}; // Test vector from: https://eips.ethereum.org/EIPS/eip-2481 From 1ba36004be0c648c31d3c2b4c256bb73d5cd872e Mon Sep 17 00:00:00 2001 From: Hoa Nguyen Date: Wed, 16 Oct 2024 16:38:33 +0700 Subject: [PATCH 12/18] Update crates/primitives/src/receipt.rs Co-authored-by: Matthias Seitz --- crates/primitives/src/receipt.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/primitives/src/receipt.rs b/crates/primitives/src/receipt.rs index 8101561f7612..dba32dfc2766 100644 --- a/crates/primitives/src/receipt.rs +++ b/crates/primitives/src/receipt.rs @@ -288,7 +288,7 @@ impl ReceiptWithBloom { return Err(alloy_rlp::Error::ListLengthMismatch { expected: rlp_head.payload_length, got: consumed, - }); + }) } *buf = *b; Ok(this) From 78c006eea6d1d05e7e7afcf48751fa194af69afa Mon Sep 17 00:00:00 2001 From: Hoa Nguyen Date: Wed, 16 Oct 2024 16:38:43 +0700 Subject: [PATCH 13/18] Update crates/primitives/src/receipt.rs Co-authored-by: Matthias Seitz --- crates/primitives/src/receipt.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/primitives/src/receipt.rs b/crates/primitives/src/receipt.rs index dba32dfc2766..38c3fa60e4ac 100644 --- a/crates/primitives/src/receipt.rs +++ b/crates/primitives/src/receipt.rs @@ -445,7 +445,7 @@ impl ReceiptWithBloomEncoder<'_> { fn encode_inner(&self, out: &mut dyn BufMut, with_header: bool) { if matches!(self.receipt.tx_type, TxType::Legacy) { self.encode_fields(out); - return; + return } let mut payload = Vec::new(); From 03fa0099c8f8c7ac26a009676da17a55fd9b6e0e Mon Sep 17 00:00:00 2001 From: Hoa Nguyen Date: Wed, 16 Oct 2024 16:38:56 +0700 Subject: [PATCH 14/18] Update crates/primitives/src/receipt.rs Co-authored-by: Matthias Seitz --- crates/primitives/src/receipt.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/primitives/src/receipt.rs b/crates/primitives/src/receipt.rs index 38c3fa60e4ac..d2a0f550554c 100644 --- a/crates/primitives/src/receipt.rs +++ b/crates/primitives/src/receipt.rs @@ -243,7 +243,7 @@ impl ReceiptWithBloom { let b = &mut &**buf; let rlp_head = alloy_rlp::Header::decode(b)?; if !rlp_head.list { - return Err(alloy_rlp::Error::UnexpectedString); + return Err(alloy_rlp::Error::UnexpectedString) } let started_len = b.len(); From bccc9bcbc4c217dde7003be6bed9fff60c8cedcf Mon Sep 17 00:00:00 2001 From: Khanh Hoa Date: Wed, 16 Oct 2024 16:45:48 +0700 Subject: [PATCH 15/18] bet --- crates/primitives/src/receipt.rs | 8 +++++++- crates/rpc/rpc/src/debug.rs | 6 +----- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/crates/primitives/src/receipt.rs b/crates/primitives/src/receipt.rs index d2a0f550554c..f745868feaa7 100644 --- a/crates/primitives/src/receipt.rs +++ b/crates/primitives/src/receipt.rs @@ -227,9 +227,15 @@ impl Encodable2718 for ReceiptWithBloom { self.as_encoder().length() } - fn encode_2718(&self, out: &mut dyn alloy_rlp::BufMut) { + fn encode_2718(&self, out: &mut dyn BufMut) { self.encode_inner(out, false) } + + fn encoded_2718(&self) -> Vec { + let mut out = vec![]; + self.encode_2718(&mut out); + out + } } impl ReceiptWithBloom { diff --git a/crates/rpc/rpc/src/debug.rs b/crates/rpc/rpc/src/debug.rs index 323a38207984..dd823602f1b7 100644 --- a/crates/rpc/rpc/src/debug.rs +++ b/crates/rpc/rpc/src/debug.rs @@ -891,11 +891,7 @@ where .to_rpc_result()? .unwrap_or_default() .into_iter() - .map(|receipt| { - let mut out = vec![]; - receipt.with_bloom().encode_2718(&mut out); - out.into() - }) + .map(|receipt| receipt.with_bloom().encoded_2718().into()) .collect()) } From 8b71e4940b481337fcd23a4c3154a08f50804a38 Mon Sep 17 00:00:00 2001 From: Khanh Hoa Date: Fri, 18 Oct 2024 22:32:48 +0700 Subject: [PATCH 16/18] bet --- crates/primitives/src/receipt.rs | 53 ++++++++++++++++++++++++++++++-- 1 file changed, 51 insertions(+), 2 deletions(-) diff --git a/crates/primitives/src/receipt.rs b/crates/primitives/src/receipt.rs index 2c2b885b997c..454152d81d56 100644 --- a/crates/primitives/src/receipt.rs +++ b/crates/primitives/src/receipt.rs @@ -5,7 +5,6 @@ use alloc::{vec, vec::Vec}; use alloy_consensus::constants::{ EIP1559_TX_TYPE_ID, EIP2930_TX_TYPE_ID, EIP4844_TX_TYPE_ID, EIP7702_TX_TYPE_ID, }; -use alloy_primitives::{Bloom, Bytes, Log, B256}; use alloy_eips::eip2718::Encodable2718; use alloy_primitives::{Bloom, Log, B256}; use alloy_rlp::{length_of_length, Decodable, Encodable, RlpDecodable, RlpEncodable}; @@ -225,7 +224,11 @@ impl Encodable2718 for ReceiptWithBloom { } fn encode_2718_len(&self) -> usize { - self.as_encoder().length() + let encoder = self.as_encoder(); + match self.receipt.tx_type { + TxType::Legacy => encoder.receipt_length(), + _ => 1 + encoder.receipt_length(), // 1 byte for the type prefix + } } fn encode_2718(&self, out: &mut dyn BufMut) { @@ -663,4 +666,50 @@ mod tests { let (decoded, _) = Receipt::from_compact(&data[..], data.len()); assert_eq!(decoded, receipt); } + + #[test] + fn test_encode_2718_length() { + let receipt = ReceiptWithBloom { + receipt: Receipt { + tx_type: TxType::Eip1559, + success: true, + cumulative_gas_used: 21000, + logs: vec![], + #[cfg(feature = "optimism")] + deposit_nonce: None, + #[cfg(feature = "optimism")] + deposit_receipt_version: None, + }, + bloom: Bloom::default(), + }; + + let encoded = receipt.encoded_2718(); + assert_eq!( + encoded.len(), + receipt.encode_2718_len(), + "Encoded length should match the actual encoded data length" + ); + + // Test for legacy receipt as well + let legacy_receipt = ReceiptWithBloom { + receipt: Receipt { + tx_type: TxType::Legacy, + success: true, + cumulative_gas_used: 21000, + logs: vec![], + #[cfg(feature = "optimism")] + deposit_nonce: None, + #[cfg(feature = "optimism")] + deposit_receipt_version: None, + }, + bloom: Bloom::default(), + }; + + let legacy_encoded = legacy_receipt.encoded_2718(); + assert_eq!( + legacy_encoded.len(), + legacy_receipt.encode_2718_len(), + "Encoded length for legacy receipt should match the actual encoded data length" + ); + } } From 926cff66bb3f31ad3927988b5a8896d46161e382 Mon Sep 17 00:00:00 2001 From: Hoa Nguyen Date: Sat, 19 Oct 2024 09:58:51 +0700 Subject: [PATCH 17/18] Update crates/primitives/src/receipt.rs Co-authored-by: Dan Cline <6798349+Rjected@users.noreply.github.com> --- crates/primitives/src/receipt.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/crates/primitives/src/receipt.rs b/crates/primitives/src/receipt.rs index 454152d81d56..0cccca6c3716 100644 --- a/crates/primitives/src/receipt.rs +++ b/crates/primitives/src/receipt.rs @@ -205,7 +205,6 @@ impl<'a> arbitrary::Arbitrary<'a> for Receipt { } } -// Implement the trait for ReceiptWithBloom impl Encodable2718 for ReceiptWithBloom { /// Encodes the receipt into its "raw" format. /// This format is also referred to as "binary" encoding. From da4b4bd524b320c8dfeffed5d6630f97bf0d40b4 Mon Sep 17 00:00:00 2001 From: Khanh Hoa Date: Sat, 19 Oct 2024 10:02:19 +0700 Subject: [PATCH 18/18] bet --- crates/primitives/src/receipt.rs | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/crates/primitives/src/receipt.rs b/crates/primitives/src/receipt.rs index 0cccca6c3716..973b35fd4186 100644 --- a/crates/primitives/src/receipt.rs +++ b/crates/primitives/src/receipt.rs @@ -206,15 +206,6 @@ impl<'a> arbitrary::Arbitrary<'a> for Receipt { } impl Encodable2718 for ReceiptWithBloom { - /// Encodes the receipt into its "raw" format. - /// This format is also referred to as "binary" encoding. - /// - /// For legacy receipts, it encodes the RLP of the receipt into the buffer: - /// `rlp([status, cumulativeGasUsed, logsBloom, logs])` as per EIP-2718. - /// For EIP-2718 typed transactions, it encodes the type of the transaction followed by the rlp - /// of the receipt: - /// - EIP-1559, 2930 and 4844 transactions: `tx-type || rlp([status, cumulativeGasUsed, - /// logsBloom, logs])` fn type_flag(&self) -> Option { match self.receipt.tx_type { TxType::Legacy => None, @@ -230,6 +221,15 @@ impl Encodable2718 for ReceiptWithBloom { } } + /// Encodes the receipt into its "raw" format. + /// This format is also referred to as "binary" encoding. + /// + /// For legacy receipts, it encodes the RLP of the receipt into the buffer: + /// `rlp([status, cumulativeGasUsed, logsBloom, logs])` as per EIP-2718. + /// For EIP-2718 typed transactions, it encodes the type of the transaction followed by the rlp + /// of the receipt: + /// - EIP-1559, 2930 and 4844 transactions: `tx-type || rlp([status, cumulativeGasUsed, + /// logsBloom, logs])` fn encode_2718(&self, out: &mut dyn BufMut) { self.encode_inner(out, false) }