Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: replace receipt envelope encoded with trait #11742

Merged
merged 22 commits into from
Oct 28, 2024
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
82 changes: 72 additions & 10 deletions crates/primitives/src/receipt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ 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};
use bytes::{Buf, BufMut};
use core::{cmp::Ordering, ops::Deref};
Expand Down Expand Up @@ -204,14 +205,20 @@ 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()
impl Encodable2718 for ReceiptWithBloom {
fn type_flag(&self) -> Option<u8> {
match self.receipt.tx_type {
TxType::Legacy => None,
tx_type => Some(tx_type as u8),
}
}

fn encode_2718_len(&self) -> usize {
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
}
}

/// Encodes the receipt into its "raw" format.
Expand All @@ -223,10 +230,18 @@ impl ReceiptWithBloom {
/// 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_2718(&self, out: &mut dyn BufMut) {
self.encode_inner(out, false)
}

fn encoded_2718(&self) -> Vec<u8> {
let mut out = vec![];
self.encode_2718(&mut out);
out
}
}

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)
Expand Down Expand Up @@ -501,6 +516,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
Expand Down Expand Up @@ -649,4 +665,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"
);
}
}
2 changes: 1 addition & 1 deletion crates/rpc/rpc/src/debug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -913,7 +913,7 @@ where
.to_rpc_result()?
.unwrap_or_default()
.into_iter()
.map(|receipt| receipt.with_bloom().envelope_encoded())
.map(|receipt| receipt.with_bloom().encoded_2718().into())
.collect())
}

Expand Down
Loading