|
| 1 | +use std::{ |
| 2 | + collections::{HashMap, HashSet}, |
| 3 | + sync::Arc, |
| 4 | +}; |
| 5 | + |
| 6 | +use alloy::rpc::types::beacon::BlsPublicKey; |
| 7 | +use eyre::{bail, ensure, eyre}; |
| 8 | +use serde::{Deserialize, Serialize}; |
| 9 | + |
| 10 | +use super::{PbsConfig, RelayConfig}; |
| 11 | +use crate::pbs::{RelayClient, RelayEntry}; |
| 12 | + |
| 13 | +#[derive(Debug, Clone, Deserialize, Serialize)] |
| 14 | +pub struct PbsMuxes { |
| 15 | + /// List of PBS multiplexers |
| 16 | + #[serde(rename = "mux")] |
| 17 | + pub muxes: Vec<MuxConfig>, |
| 18 | +} |
| 19 | + |
| 20 | +#[derive(Debug, Clone)] |
| 21 | +pub struct RuntimeMuxConfig { |
| 22 | + pub config: Arc<PbsConfig>, |
| 23 | + pub relays: Vec<RelayClient>, |
| 24 | +} |
| 25 | + |
| 26 | +impl PbsMuxes { |
| 27 | + pub fn validate_and_fill( |
| 28 | + self, |
| 29 | + default_pbs: &PbsConfig, |
| 30 | + default_relays: &[RelayConfig], |
| 31 | + ) -> eyre::Result<HashMap<BlsPublicKey, RuntimeMuxConfig>> { |
| 32 | + // check that validator pubkeys are in disjoint sets |
| 33 | + let mut unique_pubkeys = HashSet::new(); |
| 34 | + for mux in self.muxes.iter() { |
| 35 | + for pubkey in mux.validator_pubkeys.iter() { |
| 36 | + if !unique_pubkeys.insert(pubkey) { |
| 37 | + bail!("duplicate validator pubkey in muxes: {pubkey}"); |
| 38 | + } |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + let mut configs = HashMap::new(); |
| 43 | + // fill the configs using the default pbs config and relay entries |
| 44 | + for mux in self.muxes { |
| 45 | + ensure!(!mux.relays.is_empty(), "mux config must have at least one relay"); |
| 46 | + ensure!( |
| 47 | + !mux.validator_pubkeys.is_empty(), |
| 48 | + "mux config must have at least one validator pubkey" |
| 49 | + ); |
| 50 | + |
| 51 | + let mut relay_clients = Vec::with_capacity(mux.relays.len()); |
| 52 | + for partial_relay in mux.relays.into_iter() { |
| 53 | + // create a new config overriding only the missing fields |
| 54 | + let partial_id = partial_relay.id()?; |
| 55 | + // assume that there is always a relay defined in the default config. If this |
| 56 | + // becomes too much of a burden, we can change this to allow defining relays |
| 57 | + // that are exclusively used by a mux |
| 58 | + let default_relay = default_relays |
| 59 | + .iter() |
| 60 | + .find(|r| r.id() == partial_id) |
| 61 | + .ok_or_else(|| eyre!("default relay config not found for: {}", partial_id))?; |
| 62 | + |
| 63 | + let full_config = RelayConfig { |
| 64 | + id: Some(partial_id.to_string()), |
| 65 | + entry: partial_relay.entry.unwrap_or(default_relay.entry.clone()), |
| 66 | + headers: partial_relay.headers.or(default_relay.headers.clone()), |
| 67 | + enable_timing_games: partial_relay |
| 68 | + .enable_timing_games |
| 69 | + .unwrap_or(default_relay.enable_timing_games), |
| 70 | + target_first_request_ms: partial_relay |
| 71 | + .target_first_request_ms |
| 72 | + .or(default_relay.target_first_request_ms), |
| 73 | + frequency_get_header_ms: partial_relay |
| 74 | + .frequency_get_header_ms |
| 75 | + .or(default_relay.frequency_get_header_ms), |
| 76 | + }; |
| 77 | + |
| 78 | + relay_clients.push(RelayClient::new(full_config)?); |
| 79 | + } |
| 80 | + |
| 81 | + let config = PbsConfig { |
| 82 | + timeout_get_header_ms: mux |
| 83 | + .timeout_get_header_ms |
| 84 | + .unwrap_or(default_pbs.timeout_get_header_ms), |
| 85 | + late_in_slot_time_ms: mux |
| 86 | + .late_in_slot_time_ms |
| 87 | + .unwrap_or(default_pbs.late_in_slot_time_ms), |
| 88 | + ..default_pbs.clone() |
| 89 | + }; |
| 90 | + let config = Arc::new(config); |
| 91 | + |
| 92 | + let runtime_config = RuntimeMuxConfig { config, relays: relay_clients }; |
| 93 | + for pubkey in mux.validator_pubkeys.iter() { |
| 94 | + configs.insert(*pubkey, runtime_config.clone()); |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + Ok(configs) |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +/// Configuration for the PBS Multiplexer |
| 103 | +#[derive(Debug, Clone, Deserialize, Serialize)] |
| 104 | +pub struct MuxConfig { |
| 105 | + /// Relays to use for this mux config |
| 106 | + pub relays: Vec<PartialRelayConfig>, |
| 107 | + /// Which validator pubkeys to match against this mux config |
| 108 | + pub validator_pubkeys: Vec<BlsPublicKey>, |
| 109 | + pub timeout_get_header_ms: Option<u64>, |
| 110 | + pub late_in_slot_time_ms: Option<u64>, |
| 111 | +} |
| 112 | + |
| 113 | +#[derive(Debug, Clone, Deserialize, Serialize)] |
| 114 | +/// A relay config with all optional fields. See [`RelayConfig`] for the |
| 115 | +/// description of the fields. |
| 116 | +pub struct PartialRelayConfig { |
| 117 | + pub id: Option<String>, |
| 118 | + #[serde(rename = "url")] |
| 119 | + pub entry: Option<RelayEntry>, |
| 120 | + pub headers: Option<HashMap<String, String>>, |
| 121 | + pub enable_timing_games: Option<bool>, |
| 122 | + pub target_first_request_ms: Option<u64>, |
| 123 | + pub frequency_get_header_ms: Option<u64>, |
| 124 | +} |
| 125 | + |
| 126 | +impl PartialRelayConfig { |
| 127 | + pub fn id(&self) -> eyre::Result<&str> { |
| 128 | + match &self.id { |
| 129 | + Some(id) => Ok(id.as_str()), |
| 130 | + None => { |
| 131 | + let entry = self.entry.as_ref().ok_or_else(|| { |
| 132 | + eyre!("relays in [[mux]] need to specifify either an `id` or a `url`") |
| 133 | + })?; |
| 134 | + Ok(entry.id.as_str()) |
| 135 | + } |
| 136 | + } |
| 137 | + } |
| 138 | +} |
0 commit comments