From 1bd79a401d82dbd2ff22689c80892a330c809119 Mon Sep 17 00:00:00 2001 From: Matthias Seitz Date: Mon, 13 Nov 2023 19:25:59 +0100 Subject: [PATCH] chore: add rlp codec for Sidecar (#5390) --- crates/primitives/src/transaction/sidecar.rs | 25 ++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/crates/primitives/src/transaction/sidecar.rs b/crates/primitives/src/transaction/sidecar.rs index ac98c0af9..0a2963365 100644 --- a/crates/primitives/src/transaction/sidecar.rs +++ b/crates/primitives/src/transaction/sidecar.rs @@ -9,6 +9,7 @@ use crate::kzg::{ self, Blob, Bytes48, KzgSettings, BYTES_PER_BLOB, BYTES_PER_COMMITMENT, BYTES_PER_PROOF, }; use alloy_rlp::{Decodable, Encodable, Error as RlpError, Header}; +use bytes::BufMut; use serde::{Deserialize, Serialize}; @@ -325,6 +326,7 @@ impl BlobTransactionSidecar { /// - `blobs` /// - `commitments` /// - `proofs` + #[inline] pub(crate) fn encode_inner(&self, out: &mut dyn bytes::BufMut) { BlobTransactionSidecarRlp::wrap_ref(self).encode(out); } @@ -340,6 +342,7 @@ impl BlobTransactionSidecar { /// - `blobs` /// - `commitments` /// - `proofs` + #[inline] pub(crate) fn decode_inner(buf: &mut &[u8]) -> alloy_rlp::Result { Ok(BlobTransactionSidecarRlp::decode(buf)?.unwrap()) } @@ -367,6 +370,24 @@ impl From for reth_rpc_types::BlobTransactionSidecar { } } +impl Encodable for BlobTransactionSidecar { + /// Encodes the inner [BlobTransactionSidecar] fields as RLP bytes, without a RLP header. + fn encode(&self, out: &mut dyn BufMut) { + self.encode_inner(out) + } + + fn length(&self) -> usize { + self.fields_len() + } +} + +impl Decodable for BlobTransactionSidecar { + /// Decodes the inner [BlobTransactionSidecar] fields from RLP bytes, without a RLP header. + fn decode(buf: &mut &[u8]) -> alloy_rlp::Result { + Self::decode_inner(buf) + } +} + // Wrapper for c-kzg rlp #[repr(C)] struct BlobTransactionSidecarRlp { @@ -437,8 +458,6 @@ impl<'a> arbitrary::Arbitrary<'a> for BlobTransactionSidecar { #[cfg(any(test, feature = "arbitrary"))] impl proptest::arbitrary::Arbitrary for BlobTransactionSidecar { type Parameters = ParamsFor; - type Strategy = BoxedStrategy; - fn arbitrary_with(_args: Self::Parameters) -> Self::Strategy { proptest_vec(proptest_vec(proptest_any::(), BYTES_PER_BLOB), 1..=5) .prop_map(move |blobs| { @@ -462,6 +481,8 @@ impl proptest::arbitrary::Arbitrary for BlobTransactionSidecar { }) .boxed() } + + type Strategy = BoxedStrategy; } #[cfg(any(test, feature = "arbitrary"))]