Skip to content

feat: impl Encodable / Decodable for Receipts #1752

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 38 additions & 7 deletions crates/consensus/src/receipt/receipts.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use crate::receipt::{Eip658Value, RlpDecodableReceipt, RlpEncodableReceipt, TxReceipt};
use crate::receipt::{
Eip2718EncodableReceipt, Eip658Value, RlpDecodableReceipt, RlpEncodableReceipt, TxReceipt,
};
use alloc::{vec, vec::Vec};
use alloy_eips::eip2718::Encodable2718;
use alloy_primitives::{Bloom, Log};
use alloy_rlp::{BufMut, Decodable, Encodable, Header};
use core::{borrow::Borrow, fmt};
use derive_more::{DerefMut, From, IntoIterator};

use super::Eip2718EncodableReceipt;
use derive_more::{From, IntoIterator};

/// Receipt containing result of transaction execution.
#[derive(Clone, Debug, Default, PartialEq, Eq)]
Expand Down Expand Up @@ -148,9 +148,7 @@ impl<T> From<ReceiptWithBloom<Self>> for Receipt<T> {
}

/// Receipt containing result of transaction execution.
#[derive(
Clone, Debug, PartialEq, Eq, Default, From, derive_more::Deref, DerefMut, IntoIterator,
)]
#[derive(Clone, Debug, PartialEq, Eq, Default, From, IntoIterator)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Receipts<T> {
/// A two-dimensional vector of [`Receipt`] instances.
Expand Down Expand Up @@ -186,6 +184,22 @@ impl<T> FromIterator<Vec<T>> for Receipts<T> {
}
}

impl<T: Encodable> Encodable for Receipts<T> {
fn encode(&self, out: &mut dyn BufMut) {
self.receipt_vec.encode(out)
}

fn length(&self) -> usize {
self.receipt_vec.length()
}
}

impl<T: Decodable> Decodable for Receipts<T> {
fn decode(buf: &mut &[u8]) -> alloy_rlp::Result<Self> {
Ok(Self { receipt_vec: Decodable::decode(buf)? })
}
}

Comment on lines +187 to +201
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

aren't those derivable?

Copy link
Contributor Author

@Rjected Rjected Dec 4, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes this should mimic the rlpx format. I'm not sure how we can derive them, because it's a struct and not a newtype, we can't use RlpEncodableWrapper etc. If we derive RlpEncodable, it will insert an additional header which isn't correct.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah because RlpEncodableWrapper only works with unnamed and can't handle a single field?

/// [`Receipt`] with calculated bloom filter.
///
/// This convenience type allows us to lazily calculate the bloom filter for a
Expand Down Expand Up @@ -302,6 +316,9 @@ where

#[cfg(test)]
mod test {
use super::*;
use crate::ReceiptEnvelope;
use alloy_rlp::{Decodable, Encodable};

#[cfg(feature = "serde")]
#[test]
Expand Down Expand Up @@ -344,4 +361,18 @@ mod test {
))
);
}

#[test]
fn rountrip_encodable_eip1559() {
let receipts =
Receipts { receipt_vec: vec![vec![ReceiptEnvelope::Eip1559(Default::default())]] };

let mut out = vec![];
receipts.encode(&mut out);

let mut out = out.as_slice();
let decoded = Receipts::<ReceiptEnvelope>::decode(&mut out).unwrap();

assert_eq!(receipts, decoded);
}
}
Loading