Skip to main content

reth_network/transactions/blob/
role.rs

1//! The [EIP-8070](https://eips.ethereum.org/EIPS/eip-8070) role of an eth/72 blob transaction:
2//! fetched in full, or sampled at the custody columns.
3
4use super::fetcher::MAX_PROVIDERS_PER_TRANSACTION;
5use alloy_eips::eip7594::BlobCellMask;
6use rand::Rng;
7use reth_transaction_pool::blobstore::DATA_COLUMNS;
8
9// Most custody columns sampled, so a retry to each provider still fits an extra column in the
10// data columns the body is charged for.
11const MAX_SAMPLED_CUSTODY: usize = DATA_COLUMNS.count() - 1 - MAX_PROVIDERS_PER_TRANSACTION;
12
13/// Whether a transaction's cells are fetched in full or sampled, chosen when it's first announced.
14/// A sample that doesn't become available in time is fetched in full.
15#[derive(Debug, Clone, Copy, PartialEq, Eq)]
16pub(super) enum Role {
17    /// Fetches the data columns, which hold the blobs.
18    Provider,
19    /// Fetches the `custody` columns and the `extra` one, which is checked and dropped. It starts
20    /// once `available`, set when enough peers announced every column.
21    Sampler { custody: BlobCellMask, extra: BlobCellMask, available: bool },
22}
23
24impl Role {
25    /// Returns the columns to fetch.
26    pub(super) const fn target(self) -> BlobCellMask {
27        match self {
28            Self::Provider => DATA_COLUMNS,
29            Self::Sampler { custody, extra, .. } => custody.union(extra),
30        }
31    }
32
33    /// Returns the columns kept once checked.
34    pub(super) const fn keep(self) -> BlobCellMask {
35        match self {
36            Self::Provider => DATA_COLUMNS,
37            Self::Sampler { custody, .. } => custody,
38        }
39    }
40
41    /// Returns the columns that can be asked from a peer that announced `announced`. A sample's
42    /// extra column is only asked from peers with every column, so it probes one, as EIP-8070 asks.
43    pub(super) fn requestable(self, announced: BlobCellMask) -> BlobCellMask {
44        match self {
45            Self::Sampler { extra, .. } if announced != BlobCellMask::ALL => {
46                announced.difference(extra)
47            }
48            _ => announced,
49        }
50    }
51
52    /// Adds a random column outside the custody and `held` to a sample's custody-only request to a
53    /// peer with every column, as EIP-8070 asks, unless it exceeds the charged data columns.
54    pub(super) fn with_extra(
55        self,
56        requested: BlobCellMask,
57        announced: BlobCellMask,
58        held: BlobCellMask,
59        rng: &mut impl Rng,
60    ) -> BlobCellMask {
61        let Self::Sampler { custody, .. } = self else { return requested };
62        if announced != BlobCellMask::ALL ||
63            !requested.is_subset_of(custody) ||
64            held.union(requested).count() >= DATA_COLUMNS.count()
65        {
66            return requested
67        }
68        let others = BlobCellMask::ALL.difference(custody.union(held));
69        others
70            .selected_indices()
71            .nth(rng.random_range(0..others.count()))
72            .map_or(requested, |extra| requested.union(BlobCellMask::from_bits(1 << extra)))
73    }
74}
75
76/// Returns a provider with `fetch_probability` percent, or for no custody or more than
77/// [`MAX_SAMPLED_CUSTODY`] columns, and otherwise a sampler with one random extra column.
78pub(super) fn fetch_role(custody: BlobCellMask, fetch_probability: u8, rng: &mut impl Rng) -> Role {
79    if custody.count() == 0 ||
80        custody.count() > MAX_SAMPLED_CUSTODY ||
81        rng.random_range(0..100) < fetch_probability
82    {
83        return Role::Provider
84    }
85    let others = BlobCellMask::ALL.difference(custody);
86    others.selected_indices().nth(rng.random_range(0..others.count())).map_or(
87        Role::Provider,
88        |extra| Role::Sampler {
89            custody,
90            extra: BlobCellMask::from_bits(1 << extra),
91            available: false,
92        },
93    )
94}
95
96#[cfg(test)]
97mod tests {
98    use super::*;
99    use crate::transactions::constants::tx_manager::MIN_BLOB_FETCH_PROBABILITY;
100    use rand::{rngs::StdRng, SeedableRng};
101
102    #[test]
103    fn samples_most_transactions_with_their_custody_and_one_other_column() {
104        let mut rng = StdRng::seed_from_u64(1);
105        // No custody, or too many columns to leave room for extra ones, fetches in full.
106        for custody in [BlobCellMask::default(), BlobCellMask::ALL.first(MAX_SAMPLED_CUSTODY + 1)] {
107            assert_eq!(fetch_role(custody, MIN_BLOB_FETCH_PROBABILITY, &mut rng), Role::Provider);
108        }
109        let largest = BlobCellMask::ALL.first(MAX_SAMPLED_CUSTODY);
110        assert!(matches!(fetch_role(largest, 0, &mut rng), Role::Sampler { .. }));
111
112        let custody = BlobCellMask::from_bits(0b1111 << 70);
113        let mut full = 0;
114        for _ in 0..1000 {
115            match fetch_role(custody, MIN_BLOB_FETCH_PROBABILITY, &mut rng) {
116                Role::Provider => full += 1,
117                Role::Sampler { custody: sampled, extra, available } => {
118                    assert_eq!((sampled, extra.count(), available), (custody, 1, false));
119                    assert!(!extra.is_subset_of(custody));
120                }
121            }
122        }
123        assert!((100..200).contains(&full), "{full} of 1000 fetched in full");
124    }
125
126    #[test]
127    fn requests_to_peers_with_every_column_carry_an_extra_column() {
128        let mut rng = StdRng::seed_from_u64(1);
129        let custody = BlobCellMask::from_bits(0b1111 << 70);
130        let role = Role::Sampler { custody, extra: BlobCellMask::from_bits(1), available: true };
131        let (held, requested) =
132            (BlobCellMask::from_bits(1 | 0b11 << 70), BlobCellMask::from_bits(0b11 << 72));
133        // Custody cells alone asked from a peer with every column add one column not held.
134        let added =
135            role.with_extra(requested, BlobCellMask::ALL, held, &mut rng).difference(requested);
136        assert_eq!(added.count(), 1);
137        assert!(!added.is_subset_of(custody.union(held)));
138        // Nothing is added for a peer without every column, or a request with the extra one.
139        assert_eq!(role.with_extra(requested, custody, held, &mut rng), requested);
140        let with_extra = requested.union(BlobCellMask::from_bits(1));
141        assert_eq!(role.with_extra(with_extra, BlobCellMask::ALL, custody, &mut rng), with_extra);
142        // Nor once they would exceed the data columns the body was charged for.
143        let filled = BlobCellMask::ALL.first(DATA_COLUMNS.count() - requested.count());
144        assert_eq!(role.with_extra(requested, BlobCellMask::ALL, filled, &mut rng), requested);
145    }
146}