feat(rlp): custom rlp encoding tx type (#7968)

This commit is contained in:
Emilia Hane
2024-04-29 22:20:51 +02:00
committed by GitHub
parent 0819780027
commit 593b2b6d04
2 changed files with 58 additions and 2 deletions

View File

@ -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,

View File

@ -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<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]
@ -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);
}
}
}