|
1 | | -use std::{ |
2 | | - collections::HashSet, |
3 | | - sync::{Arc, Mutex}, |
4 | | -}; |
5 | | - |
6 | | -use alloy::{primitives::B256, rpc::types::beacon::BlsPublicKey}; |
| 1 | +use alloy::rpc::types::beacon::BlsPublicKey; |
7 | 2 | use cb_common::{ |
8 | 3 | config::{PbsConfig, PbsModuleConfig}, |
9 | | - pbs::{BuilderEvent, GetHeaderResponse, RelayClient}, |
| 4 | + pbs::{BuilderEvent, RelayClient}, |
10 | 5 | }; |
11 | | -use dashmap::DashMap; |
12 | | -use uuid::Uuid; |
13 | 6 |
|
14 | 7 | pub trait BuilderApiState: Clone + Sync + Send + 'static {} |
15 | 8 | impl BuilderApiState for () {} |
16 | 9 |
|
17 | | -/// State for the Pbs module. It can be extended by adding extra data to the |
18 | | -/// state |
| 10 | +/// Config for the Pbs module. It can be extended by adding extra data to the |
| 11 | +/// state for modules that need it |
| 12 | +// TODO: consider remove state from the PBS module altogether |
19 | 13 | #[derive(Clone)] |
20 | 14 | pub struct PbsState<S: BuilderApiState = ()> { |
21 | 15 | /// Config data for the Pbs service |
22 | 16 | pub config: PbsModuleConfig, |
23 | 17 | /// Opaque extra data for library use |
24 | 18 | pub data: S, |
25 | | - /// Info about the latest slot and its uuid |
26 | | - current_slot_info: Arc<Mutex<(u64, Uuid)>>, |
27 | | - /// Keeps track of which relays delivered which block for which slot |
28 | | - bid_cache: Arc<DashMap<u64, Vec<GetHeaderResponse>>>, |
29 | 19 | } |
30 | 20 |
|
31 | 21 | impl PbsState<()> { |
32 | 22 | pub fn new(config: PbsModuleConfig) -> Self { |
33 | | - Self { |
34 | | - config, |
35 | | - data: (), |
36 | | - current_slot_info: Arc::new(Mutex::new((0, Uuid::new_v4()))), |
37 | | - bid_cache: Arc::new(DashMap::new()), |
38 | | - } |
| 23 | + Self { config, data: () } |
39 | 24 | } |
40 | 25 |
|
41 | 26 | pub fn with_data<S: BuilderApiState>(self, data: S) -> PbsState<S> { |
42 | | - PbsState { |
43 | | - data, |
44 | | - config: self.config, |
45 | | - current_slot_info: self.current_slot_info, |
46 | | - bid_cache: self.bid_cache, |
47 | | - } |
| 27 | + PbsState { data, config: self.config } |
48 | 28 | } |
49 | 29 | } |
50 | 30 |
|
|
58 | 38 | } |
59 | 39 | } |
60 | 40 |
|
61 | | - pub fn get_or_update_slot_uuid(&self, last_slot: u64) -> Uuid { |
62 | | - let mut guard = self.current_slot_info.lock().expect("poisoned"); |
63 | | - if guard.0 < last_slot { |
64 | | - // new slot |
65 | | - guard.0 = last_slot; |
66 | | - guard.1 = Uuid::new_v4(); |
67 | | - self.clear(last_slot); |
68 | | - } |
69 | | - guard.1 |
70 | | - } |
71 | | - |
72 | | - pub fn get_slot_and_uuid(&self) -> (u64, Uuid) { |
73 | | - let guard = self.current_slot_info.lock().expect("poisoned"); |
74 | | - *guard |
75 | | - } |
76 | | - |
77 | 41 | // Getters |
78 | 42 | pub fn pbs_config(&self) -> &PbsConfig { |
79 | 43 | &self.config.pbs_config |
@@ -102,35 +66,4 @@ where |
102 | 66 | pub fn extra_validation_enabled(&self) -> bool { |
103 | 67 | self.config.pbs_config.extra_validation_enabled |
104 | 68 | } |
105 | | - |
106 | | - /// Add some bids to the cache, the bids are all assumed to be for the |
107 | | - /// provided slot Returns the bid with the max value |
108 | | - pub fn add_bids(&self, slot: u64, bids: Vec<GetHeaderResponse>) -> Option<GetHeaderResponse> { |
109 | | - let mut slot_entry = self.bid_cache.entry(slot).or_default(); |
110 | | - slot_entry.extend(bids); |
111 | | - slot_entry.iter().max_by_key(|bid| bid.value()).cloned() |
112 | | - } |
113 | | - |
114 | | - /// Retrieves a list of relays pubkeys that delivered a given block hash |
115 | | - /// Returns None if we dont have bids for the slot or for the block hash |
116 | | - pub fn get_relays_by_block_hash( |
117 | | - &self, |
118 | | - slot: u64, |
119 | | - block_hash: B256, |
120 | | - ) -> Option<HashSet<BlsPublicKey>> { |
121 | | - self.bid_cache.get(&slot).and_then(|bids| { |
122 | | - let filtered: HashSet<_> = bids |
123 | | - .iter() |
124 | | - .filter(|&bid| (bid.block_hash() == block_hash)) |
125 | | - .map(|bid| bid.pubkey()) |
126 | | - .collect(); |
127 | | - |
128 | | - (!filtered.is_empty()).then_some(filtered) |
129 | | - }) |
130 | | - } |
131 | | - |
132 | | - /// Clear bids which are more than ~3 minutes old |
133 | | - fn clear(&self, last_slot: u64) { |
134 | | - self.bid_cache.retain(|slot, _| last_slot.saturating_sub(*slot) < 15) |
135 | | - } |
136 | 69 | } |
0 commit comments