Skip to content

Commit 5fe237d

Browse files
authored
chore: sidecar helper fns (#2700)
added fn
1 parent fbc8133 commit 5fe237d

2 files changed

Lines changed: 40 additions & 0 deletions

File tree

crates/eips/src/eip4844/sidecar.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,18 @@ impl BlobTransactionSidecar {
296296
self.commitments.get(blob_index).map(|c| kzg_to_versioned_hash(c.as_slice()))
297297
}
298298

299+
/// Returns the index of the versioned hash in the commitments vector.
300+
pub fn versioned_hash_index(&self, hash: &B256) -> Option<usize> {
301+
self.commitments
302+
.iter()
303+
.position(|commitment| kzg_to_versioned_hash(commitment.as_slice()) == *hash)
304+
}
305+
306+
/// Returns the blob corresponding to the versioned hash, if it exists.
307+
pub fn blob_by_versioned_hash(&self, hash: &B256) -> Option<&Blob> {
308+
self.versioned_hash_index(hash).and_then(|index| self.blobs.get(index))
309+
}
310+
299311
/// Calculates a size heuristic for the in-memory size of the [BlobTransactionSidecar].
300312
#[inline]
301313
pub fn size(&self) -> usize {

crates/eips/src/eip7594/sidecar.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,22 @@ impl BlobTransactionSidecarVariant {
107107
VersionedHashIter::new(self.commitments())
108108
}
109109

110+
/// Returns the index of the versioned hash in the commitments vector.
111+
pub fn versioned_hash_index(&self, hash: &B256) -> Option<usize> {
112+
match self {
113+
Self::Eip4844(s) => s.versioned_hash_index(hash),
114+
Self::Eip7594(s) => s.versioned_hash_index(hash),
115+
}
116+
}
117+
118+
/// Returns the blob corresponding to the versioned hash, if it exists.
119+
pub fn blob_by_versioned_hash(&self, hash: &B256) -> Option<&Blob> {
120+
match self {
121+
Self::Eip4844(s) => s.blob_by_versioned_hash(hash),
122+
Self::Eip7594(s) => s.blob_by_versioned_hash(hash),
123+
}
124+
}
125+
110126
/// Outputs the RLP length of the [BlobTransactionSidecarVariant] fields, without a RLP header.
111127
#[doc(hidden)]
112128
pub fn rlp_encoded_fields_length(&self) -> usize {
@@ -414,6 +430,18 @@ impl BlobTransactionSidecarEip7594 {
414430
VersionedHashIter::new(&self.commitments)
415431
}
416432

433+
/// Returns the index of the versioned hash in the commitments vector.
434+
pub fn versioned_hash_index(&self, hash: &B256) -> Option<usize> {
435+
self.commitments.iter().position(|commitment| {
436+
crate::eip4844::kzg_to_versioned_hash(commitment.as_slice()) == *hash
437+
})
438+
}
439+
440+
/// Returns the blob corresponding to the versioned hash, if it exists.
441+
pub fn blob_by_versioned_hash(&self, hash: &B256) -> Option<&Blob> {
442+
self.versioned_hash_index(hash).and_then(|index| self.blobs.get(index))
443+
}
444+
417445
/// Matches versioned hashes and returns an iterator of (index, [`BlobAndProofV2`]) pairs
418446
/// where index is the position in `versioned_hashes` that matched the versioned hash in the
419447
/// sidecar.

0 commit comments

Comments
 (0)