Skip to content

Commit

Permalink
consensus: add Receipts struct (#1247)
Browse files Browse the repository at this point in the history
* 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 <matthias.seitz@outlook.de>

* fix nit

---------

Co-authored-by: Matthias Seitz <matthias.seitz@outlook.de>
  • Loading branch information
tcoratger and mattsse authored Sep 25, 2024
1 parent 36e3304 commit b83e7a8
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 4 deletions.
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

0 comments on commit b83e7a8

Please sign in to comment.