diff --git a/crates/primitives/src/receipt.rs b/crates/primitives/src/receipt.rs index 2a25b2de81f1..63955a1d13b1 100644 --- a/crates/primitives/src/receipt.rs +++ b/crates/primitives/src/receipt.rs @@ -2,7 +2,7 @@ use crate::compression::{RECEIPT_COMPRESSOR, RECEIPT_DECOMPRESSOR}; use crate::{logs_bloom, Bloom, Bytes, PruneSegmentError, TxType, B256}; use alloy_primitives::Log; -use alloy_rlp::{length_of_length, Decodable, Encodable}; +use alloy_rlp::{length_of_length, Decodable, Encodable, RlpDecodable, RlpEncodable}; use bytes::{Buf, BufMut}; #[cfg(any(test, feature = "arbitrary"))] use proptest::strategy::Strategy; @@ -18,7 +18,8 @@ use std::{ #[cfg_attr(feature = "zstd-codec", main_codec(no_arbitrary, zstd))] #[cfg_attr(not(feature = "zstd-codec"), main_codec(no_arbitrary))] #[add_arbitrary_tests] -#[derive(Clone, Debug, PartialEq, Eq, Default)] +#[derive(Clone, Debug, PartialEq, Eq, Default, RlpEncodable, RlpDecodable)] +#[rlp(trailing)] pub struct Receipt { /// Receipt type. pub tx_type: TxType, diff --git a/crates/primitives/src/transaction/tx_type.rs b/crates/primitives/src/transaction/tx_type.rs index 11df417d4bd4..d203ecf77327 100644 --- a/crates/primitives/src/transaction/tx_type.rs +++ b/crates/primitives/src/transaction/tx_type.rs @@ -1,4 +1,5 @@ use crate::{U64, U8}; +use alloy_rlp::{Decodable, Encodable}; use bytes::Buf; use reth_codecs::{derive_arbitrary, Compact}; use serde::{Deserialize, Serialize}; @@ -181,8 +182,30 @@ impl PartialEq for u8 { } } +impl Encodable for TxType { + fn encode(&self, out: &mut dyn bytes::BufMut) { + (*self as u8).encode(out); + } + + fn length(&self) -> usize { + 1 + } +} + +impl Decodable for TxType { + fn decode(buf: &mut &[u8]) -> alloy_rlp::Result { + let ty = u8::decode(buf)?; + + TxType::try_from(ty).map_err(alloy_rlp::Error::Custom) + } +} + #[cfg(test)] mod tests { + use rand::Rng; + + use crate::hex; + use super::*; #[test] @@ -249,4 +272,36 @@ mod tests { ); } } + + #[test] + fn decode_tx_type() { + // Test for Legacy transaction + let tx_type = TxType::decode(&mut &hex!("80")[..]).unwrap(); + assert_eq!(tx_type, TxType::Legacy); + + // Test for EIP2930 transaction + let tx_type = TxType::decode(&mut &[1u8][..]).unwrap(); + assert_eq!(tx_type, TxType::Eip2930); + + // Test for EIP1559 transaction + let tx_type = TxType::decode(&mut &[2u8][..]).unwrap(); + assert_eq!(tx_type, TxType::Eip1559); + + // Test for EIP4844 transaction + let tx_type = TxType::decode(&mut &[3u8][..]).unwrap(); + assert_eq!(tx_type, TxType::Eip4844); + + // Test random byte not in range + let buf = [rand::thread_rng().gen_range(4..=u8::MAX)]; + println!("{buf:?}"); + assert!(TxType::decode(&mut &buf[..]).is_err()); + + // Test for Deposit transaction + #[cfg(feature = "optimism")] + { + let buf = [126u8]; + let tx_type = TxType::decode(&mut &buf[..]).unwrap(); + assert_eq!(tx_type, TxType::Deposit); + } + } }