Skip to content

Commit

Permalink
chore: add rlp codec for Sidecar (paradigmxyz#5390)
Browse files Browse the repository at this point in the history
  • Loading branch information
mattsse authored Nov 13, 2023
1 parent 79abf2b commit 1bd79a4
Showing 1 changed file with 23 additions and 2 deletions.
25 changes: 23 additions & 2 deletions crates/primitives/src/transaction/sidecar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down Expand Up @@ -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);
}
Expand All @@ -340,6 +342,7 @@ impl BlobTransactionSidecar {
/// - `blobs`
/// - `commitments`
/// - `proofs`
#[inline]
pub(crate) fn decode_inner(buf: &mut &[u8]) -> alloy_rlp::Result<Self> {
Ok(BlobTransactionSidecarRlp::decode(buf)?.unwrap())
}
Expand Down Expand Up @@ -367,6 +370,24 @@ impl From<BlobTransactionSidecar> 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> {
Self::decode_inner(buf)
}
}

// Wrapper for c-kzg rlp
#[repr(C)]
struct BlobTransactionSidecarRlp {
Expand Down Expand Up @@ -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<String>;
type Strategy = BoxedStrategy<BlobTransactionSidecar>;

fn arbitrary_with(_args: Self::Parameters) -> Self::Strategy {
proptest_vec(proptest_vec(proptest_any::<u8>(), BYTES_PER_BLOB), 1..=5)
.prop_map(move |blobs| {
Expand All @@ -462,6 +481,8 @@ impl proptest::arbitrary::Arbitrary for BlobTransactionSidecar {
})
.boxed()
}

type Strategy = BoxedStrategy<BlobTransactionSidecar>;
}

#[cfg(any(test, feature = "arbitrary"))]
Expand Down

0 comments on commit 1bd79a4

Please sign in to comment.