reth_transaction_pool/blobstore/
cells.rs1use crate::blobstore::DATA_COLUMNS;
5use alloy_eips::{
6 eip4844::{env_settings::KzgSettings, BlobCellsAndProofsV1, BlobTransactionValidationError},
7 eip7594::{
8 blob_from_data_cells, BlobCellRecoveryError, BlobTransactionCellSidecar,
9 BlobTransactionSidecarEip7594,
10 },
11};
12use alloy_primitives::{map::B256Set, B256};
13
14pub(crate) fn merge_cells(slot: &mut Option<BlobCellsAndProofsV1>, found: BlobCellsAndProofsV1) {
17 let Some(slot) = slot else {
18 *slot = Some(found);
19 return
20 };
21 let cells = slot.blob_cells.iter_mut().zip(&mut slot.proofs);
22 for ((cell, proof), (found_cell, found_proof)) in
23 cells.zip(found.blob_cells.into_iter().zip(found.proofs))
24 {
25 if cell.is_none() {
26 *cell = found_cell;
27 *proof = found_proof;
28 }
29 }
30}
31
32pub(crate) fn missing_hashes<T>(versioned_hashes: &[B256], result: &[Option<T>]) -> B256Set {
34 versioned_hashes
35 .iter()
36 .zip(result)
37 .filter_map(|(hash, found)| found.is_none().then_some(*hash))
38 .collect()
39}
40
41pub(crate) fn to_full_sidecar(
44 cells: &BlobTransactionCellSidecar,
45 settings: &KzgSettings,
46) -> Result<BlobTransactionSidecarEip7594, BlobTransactionValidationError> {
47 let blobs = if DATA_COLUMNS.is_subset_of(cells.mask()) {
48 cells.cells().chunks_exact(cells.mask().count()).filter_map(blob_from_data_cells).collect()
50 } else {
51 match cells.recover(settings) {
52 Ok(sidecar) => sidecar.blobs,
53 Err(BlobCellRecoveryError::Kzg(err)) => return Err(err.into()),
54 Err(_) => return Err(BlobTransactionValidationError::InvalidProof),
56 }
57 };
58 Ok(BlobTransactionSidecarEip7594::new(
59 blobs,
60 cells.commitments().to_vec(),
61 cells.proofs().to_vec(),
62 ))
63}
64
65#[cfg(test)]
66pub(crate) mod tests {
67 use alloy_eips::{
68 eip4844::{env_settings::EnvKzgSettings, Blob},
69 eip7594::{BlobCellMask, BlobTransactionCellSidecar, BlobTransactionSidecarEip7594},
70 };
71 use std::sync::OnceLock;
72
73 pub(crate) fn fixture() -> &'static (BlobTransactionSidecarEip7594, BlobTransactionCellSidecar)
75 {
76 static FIXTURE: OnceLock<(BlobTransactionSidecarEip7594, BlobTransactionCellSidecar)> =
77 OnceLock::new();
78 FIXTURE.get_or_init(|| {
79 let mut second = Blob::default();
80 second[31] = 1;
81 let full = BlobTransactionSidecarEip7594::try_from_blobs(vec![Blob::default(), second])
82 .unwrap();
83 let cells =
84 BlobTransactionCellSidecar::try_from_sidecar(&full, EnvKzgSettings::Default.get())
85 .unwrap();
86 (full, cells)
87 })
88 }
89
90 pub(crate) fn subset(mask: BlobCellMask) -> BlobTransactionCellSidecar {
92 let (_, all) = fixture();
93 BlobTransactionCellSidecar::new(
94 all.commitments().to_vec(),
95 all.proofs().to_vec(),
96 all.matching_cells(mask).unwrap(),
97 mask,
98 )
99 .unwrap()
100 }
101}