Skip to main content

reth_network_api/
custody.rs

1//! Shared blob cell custody state.
2
3use alloy_eips::eip7594::BlobCellMask;
4use std::sync::{
5    atomic::{AtomicU64, AtomicU8, Ordering},
6    Arc,
7};
8
9/// Shared blob cell custody bitmap for [EIP-8070] sparse blobpool sampling.
10///
11/// This stores the latest `custodyColumns` value received from
12/// `engine_forkchoiceUpdatedV4`, so network components can align blob cell sampling with the
13/// consensus client's custody set. The value is a lightweight hint for sampling decisions, not a
14/// consensus-critical guard.
15///
16/// [EIP-8070]: https://eips.ethereum.org/EIPS/eip-8070
17#[derive(Debug, Clone, Default)]
18pub struct CellCustody {
19    inner: Arc<CellCustodyInner>,
20}
21
22impl CellCustody {
23    /// Returns the currently configured blob cell custody bitmap.
24    ///
25    /// Reads are intentionally cheap and eventually consistent. Concurrent custom-to-custom
26    /// updates may briefly expose an in-flight bitmap, which is acceptable because custody is only
27    /// used to steer future sampling requests.
28    pub fn get(&self) -> BlobCellMask {
29        match self.inner.state.load(Ordering::Relaxed) {
30            CELL_CUSTODY_NONE => BlobCellMask::default(),
31            CELL_CUSTODY_FULL => BlobCellMask::ALL,
32            _ => {
33                let high = self.inner.high.load(Ordering::Relaxed) as u128;
34                let low = self.inner.low.load(Ordering::Relaxed) as u128;
35                BlobCellMask::from_bits((high << 64) | low)
36            }
37        }
38    }
39
40    /// Updates the blob cell custody bitmap.
41    pub fn set(&self, custody_columns: BlobCellMask) {
42        match custody_columns.bits() {
43            0 => self.inner.state.store(CELL_CUSTODY_NONE, Ordering::Relaxed),
44            u128::MAX => self.inner.state.store(CELL_CUSTODY_FULL, Ordering::Relaxed),
45            custody_columns => {
46                self.inner.high.store((custody_columns >> 64) as u64, Ordering::Relaxed);
47                self.inner.low.store(custody_columns as u64, Ordering::Relaxed);
48                self.inner.state.store(CELL_CUSTODY_CUSTOM, Ordering::Relaxed);
49            }
50        }
51    }
52}
53
54const CELL_CUSTODY_NONE: u8 = 0;
55const CELL_CUSTODY_FULL: u8 = 1;
56const CELL_CUSTODY_CUSTOM: u8 = 2;
57
58#[derive(Debug, Default)]
59struct CellCustodyInner {
60    state: AtomicU8,
61    high: AtomicU64,
62    low: AtomicU64,
63}
64
65#[cfg(test)]
66mod tests {
67    use super::*;
68
69    #[test]
70    fn stores_empty_full_and_partial_masks() {
71        let custody = CellCustody::default();
72        for mask in [BlobCellMask::from_bits(0b1010), BlobCellMask::ALL, BlobCellMask::default()] {
73            custody.set(mask);
74            assert_eq!(custody.get(), mask);
75        }
76    }
77}