refactor: store plain receipts instead Options in Receipts (#14010)

This commit is contained in:
Arsenii Kulikov
2025-01-27 20:05:48 +04:00
committed by GitHub
parent 0fa79c6c65
commit cde951732e
22 changed files with 88 additions and 128 deletions

View File

@ -24,7 +24,7 @@ pub use reth_ethereum_primitives::Receipt;
)]
pub struct Receipts<T = Receipt> {
/// A two-dimensional vector of optional `Receipt` instances.
pub receipt_vec: Vec<Vec<Option<T>>>,
pub receipt_vec: Vec<Vec<T>>,
}
impl<T> Receipts<T> {
@ -39,26 +39,25 @@ impl<T> Receipts<T> {
}
/// Push a new vector of receipts into the `Receipts` collection.
pub fn push(&mut self, receipts: Vec<Option<T>>) {
pub fn push(&mut self, receipts: Vec<T>) {
self.receipt_vec.push(receipts);
}
/// Retrieves all recorded receipts from index and calculates the root using the given closure.
pub fn root_slow(&self, index: usize, f: impl FnOnce(&[&T]) -> B256) -> Option<B256> {
let receipts =
self.receipt_vec[index].iter().map(Option::as_ref).collect::<Option<Vec<_>>>()?;
Some(f(receipts.as_slice()))
pub fn root_slow(&self, index: usize, f: impl FnOnce(&[&T]) -> B256) -> B256 {
let receipts = self.receipt_vec[index].iter().collect::<Vec<_>>();
f(receipts.as_slice())
}
}
impl<T> From<Vec<T>> for Receipts<T> {
fn from(block_receipts: Vec<T>) -> Self {
Self { receipt_vec: vec![block_receipts.into_iter().map(Option::Some).collect()] }
Self { receipt_vec: vec![block_receipts.into_iter().collect()] }
}
}
impl<T> FromIterator<Vec<Option<T>>> for Receipts<T> {
fn from_iter<I: IntoIterator<Item = Vec<Option<T>>>>(iter: I) -> Self {
impl<T> FromIterator<Vec<T>> for Receipts<T> {
fn from_iter<I: IntoIterator<Item = Vec<T>>>(iter: I) -> Self {
iter.into_iter().collect::<Vec<_>>().into()
}
}