reth_network_api/
custody.rs1use alloy_eips::eip7594::BlobCellMask;
4use std::sync::{
5 atomic::{AtomicU64, AtomicU8, Ordering},
6 Arc,
7};
8
9#[derive(Debug, Clone, Default)]
18pub struct CellCustody {
19 inner: Arc<CellCustodyInner>,
20}
21
22impl CellCustody {
23 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 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}