reth_network/transactions/blob/
role.rs1use super::fetcher::MAX_PROVIDERS_PER_TRANSACTION;
5use alloy_eips::eip7594::BlobCellMask;
6use rand::Rng;
7use reth_transaction_pool::blobstore::DATA_COLUMNS;
8
9const MAX_SAMPLED_CUSTODY: usize = DATA_COLUMNS.count() - 1 - MAX_PROVIDERS_PER_TRANSACTION;
12
13#[derive(Debug, Clone, Copy, PartialEq, Eq)]
16pub(super) enum Role {
17 Provider,
19 Sampler { custody: BlobCellMask, extra: BlobCellMask, available: bool },
22}
23
24impl Role {
25 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 pub(super) const fn keep(self) -> BlobCellMask {
35 match self {
36 Self::Provider => DATA_COLUMNS,
37 Self::Sampler { custody, .. } => custody,
38 }
39 }
40
41 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 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
76pub(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 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 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 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 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}