Skip to content

Commit

Permalink
feat(rlp): custom rlp encoding tx type (paradigmxyz#7968)
Browse files Browse the repository at this point in the history
  • Loading branch information
emhane authored Apr 29, 2024
1 parent 0819780 commit 593b2b6
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 2 deletions.
5 changes: 3 additions & 2 deletions crates/primitives/src/receipt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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,
Expand Down
55 changes: 55 additions & 0 deletions crates/primitives/src/transaction/tx_type.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -181,8 +182,30 @@ impl PartialEq<TxType> 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<Self> {
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]
Expand Down Expand Up @@ -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);
}
}
}

0 comments on commit 593b2b6

Please sign in to comment.