From b83e7a88be4e581a6b7b003710db0f90c93a8401 Mon Sep 17 00:00:00 2001 From: Thomas Coratger <60488569+tcoratger@users.noreply.github.com> Date: Wed, 25 Sep 2024 18:48:36 +0200 Subject: [PATCH] consensus: add `Receipts` struct (#1247) * consensus: add Receipts struct * fix cfg * fix comments * fix cargo file * fix imports * fix imports * Update crates/consensus/src/receipt/receipts.rs Co-authored-by: Matthias Seitz * fix nit --------- Co-authored-by: Matthias Seitz --- crates/consensus/Cargo.toml | 8 ++++- crates/consensus/src/lib.rs | 3 +- crates/consensus/src/receipt/mod.rs | 2 +- crates/consensus/src/receipt/receipts.rs | 42 +++++++++++++++++++++++- 4 files changed, 51 insertions(+), 4 deletions(-) diff --git a/crates/consensus/Cargo.toml b/crates/consensus/Cargo.toml index 6f38f6a0669..18d3729f33d 100644 --- a/crates/consensus/Cargo.toml +++ b/crates/consensus/Cargo.toml @@ -26,7 +26,6 @@ alloy-serde = { workspace = true, optional = true } # kzg c-kzg = { workspace = true, features = ["serde"], optional = true } -derive_more = { workspace = true, features = ["deref", "deref_mut", "from", "into_iterator"] } # arbitrary arbitrary = { workspace = true, features = ["derive"], optional = true } @@ -34,6 +33,13 @@ arbitrary = { workspace = true, features = ["derive"], optional = true } # serde serde = { workspace = true, features = ["derive"], optional = true } +derive_more = { workspace = true, features = [ + "from", + "deref", + "deref_mut", + "into_iterator" +], default-features = false } + [dev-dependencies] alloy-primitives = { workspace = true, features = ["arbitrary", "rand"] } alloy-eips = { workspace = true, features = ["arbitrary"] } diff --git a/crates/consensus/src/lib.rs b/crates/consensus/src/lib.rs index 5d24cca9999..cf204de8d6a 100644 --- a/crates/consensus/src/lib.rs +++ b/crates/consensus/src/lib.rs @@ -25,7 +25,8 @@ pub use header::{BlockHeader, Header, EMPTY_OMMER_ROOT_HASH, EMPTY_ROOT_HASH}; mod receipt; pub use receipt::{ - AnyReceiptEnvelope, Eip658Value, Receipt, ReceiptEnvelope, ReceiptWithBloom, TxReceipt, + AnyReceiptEnvelope, Eip658Value, Receipt, ReceiptEnvelope, ReceiptWithBloom, Receipts, + TxReceipt, }; mod request; diff --git a/crates/consensus/src/receipt/mod.rs b/crates/consensus/src/receipt/mod.rs index b9f172baa06..ce85aea01c2 100644 --- a/crates/consensus/src/receipt/mod.rs +++ b/crates/consensus/src/receipt/mod.rs @@ -7,7 +7,7 @@ mod envelope; pub use envelope::ReceiptEnvelope; mod receipts; -pub use receipts::{Receipt, ReceiptWithBloom}; +pub use receipts::{Receipt, ReceiptWithBloom, Receipts}; mod status; pub use status::Eip658Value; diff --git a/crates/consensus/src/receipt/receipts.rs b/crates/consensus/src/receipt/receipts.rs index 63fa657e5d7..5feab9f66fd 100644 --- a/crates/consensus/src/receipt/receipts.rs +++ b/crates/consensus/src/receipt/receipts.rs @@ -1,8 +1,9 @@ use crate::receipt::{Eip658Value, TxReceipt}; -use alloc::vec::Vec; +use alloc::{vec, vec::Vec}; use alloy_primitives::{Bloom, Log}; use alloy_rlp::{length_of_length, BufMut, Decodable, Encodable}; use core::borrow::Borrow; +use derive_more::{DerefMut, From, IntoIterator}; /// Receipt containing result of transaction execution. #[derive(Clone, Debug, Default, PartialEq, Eq)] @@ -100,6 +101,45 @@ impl From> for Receipt { } } +/// Receipt containing result of transaction execution. +#[derive( + Clone, Debug, PartialEq, Eq, Default, From, derive_more::Deref, DerefMut, IntoIterator, +)] +#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] +pub struct Receipts { + /// A two-dimensional vector of [`Receipt`] instances. + pub receipt_vec: Vec>, +} + +impl Receipts { + /// Returns the length of the [`Receipts`] vector. + pub fn len(&self) -> usize { + self.receipt_vec.len() + } + + /// Returns `true` if the [`Receipts`] vector is empty. + pub fn is_empty(&self) -> bool { + self.receipt_vec.is_empty() + } + + /// Push a new vector of receipts into the [`Receipts`] collection. + pub fn push(&mut self, receipts: Vec) { + self.receipt_vec.push(receipts); + } +} + +impl From> for Receipts { + fn from(block_receipts: Vec) -> Self { + Self { receipt_vec: vec![block_receipts] } + } +} + +impl FromIterator> for Receipts { + fn from_iter>>(iter: I) -> Self { + Self { receipt_vec: iter.into_iter().collect() } + } +} + /// [`Receipt`] with calculated bloom filter. /// /// This convenience type allows us to lazily calculate the bloom filter for a