Skip to main content

reth_transaction_pool/blobstore/
cells.rs

1//! Helpers for blob stores that hold [`BlobTransactionCellSidecar`]s, which keep only some of
2//! each blob's cells.
3
4use 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
14// Fills the cells and proofs that `slot` lacks from `found`, since sidecars of different
15// transactions may keep different cells of the same blob.
16pub(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
32// Returns the requested versioned hashes not found yet.
33pub(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
41// Rebuilds the blobs and returns them with the sidecar's commitments and proofs, which must then
42// be validated like any full sidecar. Joins the data columns when kept, else recovers the blobs.
43pub(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        // Every blob keeps at least the data columns, so joining them always succeeds.
49        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            // The rebuilt blobs don't match their commitments.
55            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    // A full sidecar with two different blobs, and all of its cells.
74    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    // Keeps only the cells at `mask`.
91    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}