Skip to content

Commit

Permalink
feat: add match_versioned_hashes (#1882)
Browse files Browse the repository at this point in the history
* feat: add match_versioned_hashes

* bet
  • Loading branch information
hoank101 authored Jan 4, 2025
1 parent c927bd8 commit f7f7ed8
Showing 1 changed file with 24 additions and 1 deletion.
25 changes: 24 additions & 1 deletion crates/eips/src/eip4844/sidecar.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
//! EIP-4844 sidecar type
use crate::eip4844::{
kzg_to_versioned_hash, Blob, Bytes48, BYTES_PER_BLOB, BYTES_PER_COMMITMENT, BYTES_PER_PROOF,
kzg_to_versioned_hash, Blob, BlobAndProofV1, Bytes48, BYTES_PER_BLOB, BYTES_PER_COMMITMENT,
BYTES_PER_PROOF,
};
use alloc::{boxed::Box, vec::Vec};
use alloy_primitives::{bytes::BufMut, B256};
Expand Down Expand Up @@ -54,6 +55,28 @@ impl core::fmt::Debug for BlobTransactionSidecar {
}
}

impl BlobTransactionSidecar {
/// Matches versioned hashes and returns an iterator of (index, BlobAndProofV1) pairs
/// where index is the position in versioned_hashes that matched.
pub fn match_versioned_hashes<'a>(
&'a self,
versioned_hashes: &'a [B256],
) -> impl Iterator<Item = (usize, BlobAndProofV1)> + 'a {
self.versioned_hashes().enumerate().flat_map(move |(i, blob_versioned_hash)| {
versioned_hashes.iter().enumerate().filter_map(move |(j, target_hash)| {
if blob_versioned_hash == *target_hash {
Some((
j,
BlobAndProofV1 { blob: Box::new(self.blobs[i]), proof: self.proofs[i] },
))
} else {
None
}
})
})
}
}

impl IntoIterator for BlobTransactionSidecar {
type Item = BlobTransactionSidecarItem;
type IntoIter = alloc::vec::IntoIter<BlobTransactionSidecarItem>;
Expand Down

0 comments on commit f7f7ed8

Please sign in to comment.