Skip to content
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

consensus: add Receipts struct #1247

Merged
merged 10 commits into from
Sep 25, 2024
Merged
Show file tree
Hide file tree
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
8 changes: 7 additions & 1 deletion crates/consensus/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,20 @@ 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 }

# 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"] }
Expand Down
3 changes: 2 additions & 1 deletion crates/consensus/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion crates/consensus/src/receipt/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
42 changes: 41 additions & 1 deletion crates/consensus/src/receipt/receipts.rs
Original file line number Diff line number Diff line change
@@ -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)]
Expand Down Expand Up @@ -100,6 +101,45 @@ impl<T> From<ReceiptWithBloom<T>> for Receipt<T> {
}
}

/// 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<T> {
/// A two-dimensional vector of [`Receipt`] instances.
pub receipt_vec: Vec<Vec<T>>,
}

impl<T> Receipts<T> {
/// 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<T>) {
self.receipt_vec.push(receipts);
}
}

impl<T> From<Vec<T>> for Receipts<T> {
fn from(block_receipts: Vec<T>) -> Self {
Self { receipt_vec: vec![block_receipts] }
}
}

impl<T> FromIterator<Vec<T>> for Receipts<T> {
fn from_iter<I: IntoIterator<Item = Vec<T>>>(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
Expand Down