sof 0.17.1

Solana Observer Framework for low-latency shred ingestion and plugin-driven transaction observation
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
#[cfg(feature = "gossip-bootstrap")]
use solana_net_utils::PortRange;
#[cfg(feature = "gossip-bootstrap")]
use solana_pubkey::Pubkey;
#[cfg(feature = "gossip-bootstrap")]
use std::collections::HashSet;
#[cfg(feature = "gossip-bootstrap")]
use std::num::NonZeroUsize;
#[cfg(feature = "gossip-bootstrap")]
use std::num::ParseIntError;
#[cfg(feature = "gossip-bootstrap")]
use thiserror::Error;

#[cfg(not(feature = "gossip-bootstrap"))]
use super::read_env_var;
#[cfg(feature = "gossip-bootstrap")]
use super::{read_bool_env, read_env_var};

#[cfg(feature = "gossip-bootstrap")]
const DEFAULT_MAINNET_GOSSIP_ENTRYPOINT_HOSTNAME: &str = "entrypoint.mainnet-beta.solana.com:8001";
#[cfg(feature = "gossip-bootstrap")]
const DEFAULT_MAINNET_GOSSIP_ENTRYPOINT_IPS: [&str; 4] = [
    "104.204.142.108:8001",
    "64.130.54.173:8001",
    "85.195.118.195:8001",
    "160.202.131.177:8001",
];
// Runtime defaults for gossip mode; keep these aligned with docs/operations/advanced-env.md.
#[cfg(feature = "gossip-bootstrap")]
const DEFAULT_PORT_RANGE_START: u16 = 12_000;
#[cfg(feature = "gossip-bootstrap")]
const DEFAULT_PORT_RANGE_END: u16 = 12_100;
#[cfg(feature = "gossip-bootstrap")]
const TVU_SOCKETS_FALLBACK_PARALLELISM: usize = 1;
#[cfg(feature = "gossip-bootstrap")]
const TVU_SOCKETS_MIN: usize = 1;
#[cfg(feature = "gossip-bootstrap")]
const TVU_SOCKETS_MAX: usize = 16;
#[cfg(feature = "gossip-bootstrap")]
const DEFAULT_RUNTIME_SWITCH_STALL_MS: u64 = 2_000;
#[cfg(feature = "gossip-bootstrap")]
const DEFAULT_RUNTIME_SWITCH_DATASET_STALL_MS: u64 = 12_000;
#[cfg(feature = "gossip-bootstrap")]
const DEFAULT_RUNTIME_SWITCH_COOLDOWN_MS: u64 = 15_000;
#[cfg(feature = "gossip-bootstrap")]
const DEFAULT_RUNTIME_SWITCH_WARMUP_MS: u64 = 10_000;
#[cfg(feature = "gossip-bootstrap")]
const DEFAULT_RUNTIME_SWITCH_OVERLAP_MS: u64 = 1_250;
#[cfg(feature = "gossip-bootstrap")]
const DEFAULT_RUNTIME_SWITCH_SUSTAIN_MS: u64 = 1_500;
#[cfg(feature = "gossip-bootstrap")]
const DEFAULT_RUNTIME_SWITCH_NO_TRAFFIC_GRACE_MS: u64 = 120_000;
#[cfg(feature = "gossip-bootstrap")]
const DEFAULT_RUNTIME_SWITCH_STABILIZE_MS: u64 = 1_000;
#[cfg(feature = "gossip-bootstrap")]
const DEFAULT_RUNTIME_SWITCH_STABILIZE_MIN_PACKETS: u64 = 8;
#[cfg(feature = "gossip-bootstrap")]
const DEFAULT_RUNTIME_SWITCH_STABILIZE_MAX_WAIT_MS: u64 = 8_000;
#[cfg(feature = "gossip-bootstrap")]
const DEFAULT_RUNTIME_SWITCH_STABILIZE_MIN_PEERS: usize = 8;
#[cfg(feature = "gossip-bootstrap")]
const DEFAULT_BOOTSTRAP_STABILIZE_MAX_WAIT_MS: u64 = 20_000;
#[cfg(feature = "gossip-bootstrap")]
const DEFAULT_BOOTSTRAP_STABILIZE_MIN_PEERS: usize = 128;
#[cfg(feature = "gossip-bootstrap")]
const DEFAULT_RUNTIME_SWITCH_PEER_CANDIDATES: usize = 64;
#[cfg(feature = "gossip-bootstrap")]
const DEFAULT_RUNTIME_SWITCH_PROACTIVE_EVAL_MS: u64 = 30_000;
#[cfg(feature = "gossip-bootstrap")]
const DEFAULT_RUNTIME_SWITCH_PROACTIVE_ACTIVE_RANK_MAX: usize = 1;
#[cfg(feature = "gossip-bootstrap")]
const DEFAULT_RUNTIME_SWITCH_PRIORITIZED_CANDIDATES_MAX: usize = 8;
#[cfg(feature = "gossip-bootstrap")]
const DEFAULT_RUNTIME_SWITCH_PROACTIVE_STABLE_EVALS: usize = 2;
#[cfg(feature = "gossip-bootstrap")]
const DEFAULT_RUNTIME_SWITCH_PROACTIVE_MIN_RUNTIME_AGE_MS: u64 = 120_000;
#[cfg(feature = "gossip-bootstrap")]
const DEFAULT_GOSSIP_LOAD_SHED_QUEUE_PRESSURE_PCT: u64 = 8;
#[cfg(feature = "gossip-bootstrap")]
const DEFAULT_VERIFY_STRICT_UNKNOWN_QUEUE_PRESSURE_PCT: u64 = 4;
#[cfg(feature = "gossip-bootstrap")]
const DEFAULT_GOSSIP_LOAD_SHED_KEEP_TOP_SOURCES: usize = 4;
#[cfg(feature = "gossip-bootstrap")]
const GOSSIP_VALIDATORS_MAX: usize = 2_048;

fn parse_comma_separated_endpoints(value: &str) -> Vec<String> {
    value
        .split(',')
        .map(str::trim)
        .filter(|segment| !segment.is_empty())
        .map(str::to_owned)
        .collect()
}

#[cfg(feature = "gossip-bootstrap")]
/// Errors returned when parsing `SOF_PORT_RANGE`.
#[derive(Debug, Error)]
pub enum PortRangeParseError {
    /// `SOF_PORT_RANGE` did not contain a `start-end` pair.
    #[error("invalid SOF_PORT_RANGE `{value}`, expected start-end")]
    InvalidFormat { value: String },
    /// `SOF_PORT_RANGE` start value was not a valid `u16`.
    #[error("invalid SOF_PORT_RANGE start `{value}`: {source}")]
    InvalidStart {
        value: String,
        source: ParseIntError,
    },
    /// `SOF_PORT_RANGE` end value was not a valid `u16`.
    #[error("invalid SOF_PORT_RANGE end `{value}`: {source}")]
    InvalidEnd {
        value: String,
        source: ParseIntError,
    },
    /// `SOF_PORT_RANGE` start value was greater than end value.
    #[error("invalid SOF_PORT_RANGE, start must be <= end (got {start}-{end})")]
    StartGreaterThanEnd { start: u16, end: u16 },
}

#[cfg(feature = "gossip-bootstrap")]
/// Reads the gossip/runtime bind range from `SOF_PORT_RANGE`.
pub fn read_port_range() -> Result<PortRange, PortRangeParseError> {
    match read_env_var("SOF_PORT_RANGE") {
        Some(value) => {
            let (start, end) =
                value
                    .split_once('-')
                    .ok_or_else(|| PortRangeParseError::InvalidFormat {
                        value: value.clone(),
                    })?;
            let start =
                start
                    .parse::<u16>()
                    .map_err(|source| PortRangeParseError::InvalidStart {
                        value: start.to_owned(),
                        source,
                    })?;
            let end = end
                .parse::<u16>()
                .map_err(|source| PortRangeParseError::InvalidEnd {
                    value: end.to_owned(),
                    source,
                })?;
            if start > end {
                return Err(PortRangeParseError::StartGreaterThanEnd { start, end });
            }
            Ok((start, end))
        }
        None => Ok((DEFAULT_PORT_RANGE_START, DEFAULT_PORT_RANGE_END)),
    }
}

#[cfg(feature = "gossip-bootstrap")]
pub fn read_gossip_port() -> Option<u16> {
    read_env_var("SOF_GOSSIP_PORT").and_then(|value| value.parse::<u16>().ok())
}

#[cfg(feature = "gossip-bootstrap")]
pub fn read_shred_version_override() -> Option<u16> {
    read_env_var("SOF_SHRED_VERSION").and_then(|value| value.parse::<u16>().ok())
}

#[cfg(feature = "gossip-bootstrap")]
pub fn read_tvu_receive_sockets() -> usize {
    // Keep fanout bounded to avoid over-fragmenting packet ingestion on large hosts.
    let suggested = std::thread::available_parallelism()
        .map(NonZeroUsize::get)
        .unwrap_or(TVU_SOCKETS_FALLBACK_PARALLELISM)
        .clamp(TVU_SOCKETS_MIN, TVU_SOCKETS_MAX);
    read_env_var("SOF_TVU_SOCKETS")
        .and_then(|value| value.parse::<usize>().ok())
        .filter(|value| *value > 0)
        .unwrap_or(suggested)
}

#[cfg(feature = "gossip-bootstrap")]
pub fn read_gossip_entrypoints() -> Vec<String> {
    read_env_var("SOF_GOSSIP_ENTRYPOINT").map_or_else(
        || {
            let mut defaults = Vec::with_capacity(
                DEFAULT_MAINNET_GOSSIP_ENTRYPOINT_IPS
                    .len()
                    .saturating_add(1),
            );
            defaults.extend(
                DEFAULT_MAINNET_GOSSIP_ENTRYPOINT_IPS
                    .iter()
                    .map(|entrypoint| (*entrypoint).to_owned()),
            );
            defaults.push(DEFAULT_MAINNET_GOSSIP_ENTRYPOINT_HOSTNAME.to_owned());
            defaults
        },
        |value| parse_comma_separated_endpoints(&value),
    )
}

#[cfg(not(feature = "gossip-bootstrap"))]
pub fn read_gossip_entrypoints() -> Vec<String> {
    read_env_var("SOF_GOSSIP_ENTRYPOINT")
        .map_or_else(Vec::new, |value| parse_comma_separated_endpoints(&value))
}

#[cfg(feature = "gossip-bootstrap")]
pub fn read_gossip_entrypoint_probe_enabled() -> bool {
    read_bool_env("SOF_GOSSIP_ENTRYPOINT_PROBE", false)
}

#[cfg(feature = "gossip-bootstrap")]
pub fn read_gossip_validators() -> Option<HashSet<Pubkey>> {
    let validators = read_env_var("SOF_GOSSIP_VALIDATORS")
        .map(|value| {
            parse_comma_separated_endpoints(&value)
                .into_iter()
                .take(GOSSIP_VALIDATORS_MAX)
                .filter_map(|value| value.parse::<Pubkey>().ok())
                .collect::<HashSet<_>>()
        })
        .unwrap_or_default();
    (!validators.is_empty()).then_some(validators)
}

#[cfg(feature = "gossip-bootstrap")]
pub fn read_gossip_runtime_switch_enabled() -> bool {
    read_bool_env("SOF_GOSSIP_RUNTIME_SWITCH_ENABLED", false)
}

#[cfg(feature = "gossip-bootstrap")]
pub fn read_gossip_entrypoint_pinned() -> bool {
    read_bool_env("SOF_GOSSIP_ENTRYPOINT_PINNED", false)
}

#[cfg(feature = "gossip-bootstrap")]
pub fn read_gossip_bootstrap_only() -> bool {
    read_bool_env("SOF_GOSSIP_BOOTSTRAP_ONLY", false)
}

#[cfg(feature = "gossip-bootstrap")]
pub fn read_gossip_runtime_switch_stall_ms() -> u64 {
    read_env_var("SOF_GOSSIP_RUNTIME_SWITCH_STALL_MS")
        .and_then(|value| value.parse::<u64>().ok())
        .filter(|value| *value > 0)
        .unwrap_or(DEFAULT_RUNTIME_SWITCH_STALL_MS)
}

#[cfg(feature = "gossip-bootstrap")]
pub fn read_gossip_runtime_switch_dataset_stall_ms() -> u64 {
    read_env_var("SOF_GOSSIP_RUNTIME_SWITCH_DATASET_STALL_MS")
        .and_then(|value| value.parse::<u64>().ok())
        .filter(|value| *value > 0)
        .unwrap_or(DEFAULT_RUNTIME_SWITCH_DATASET_STALL_MS)
}

#[cfg(feature = "gossip-bootstrap")]
pub fn read_gossip_runtime_switch_cooldown_ms() -> u64 {
    read_env_var("SOF_GOSSIP_RUNTIME_SWITCH_COOLDOWN_MS")
        .and_then(|value| value.parse::<u64>().ok())
        .filter(|value| *value > 0)
        .unwrap_or(DEFAULT_RUNTIME_SWITCH_COOLDOWN_MS)
}

#[cfg(feature = "gossip-bootstrap")]
pub fn read_gossip_runtime_switch_warmup_ms() -> u64 {
    read_env_var("SOF_GOSSIP_RUNTIME_SWITCH_WARMUP_MS")
        .and_then(|value| value.parse::<u64>().ok())
        .filter(|value| *value > 0)
        .unwrap_or(DEFAULT_RUNTIME_SWITCH_WARMUP_MS)
}

#[cfg(feature = "gossip-bootstrap")]
pub fn read_gossip_runtime_switch_overlap_ms() -> u64 {
    read_env_var("SOF_GOSSIP_RUNTIME_SWITCH_OVERLAP_MS")
        .and_then(|value| value.parse::<u64>().ok())
        .unwrap_or(DEFAULT_RUNTIME_SWITCH_OVERLAP_MS)
}

#[cfg(feature = "gossip-bootstrap")]
pub fn read_gossip_runtime_switch_port_range() -> Option<PortRange> {
    read_env_var("SOF_GOSSIP_RUNTIME_SWITCH_PORT_RANGE")
        .and_then(|value| {
            value
                .split_once('-')
                .map(|(start, end)| (start.to_owned(), end.to_owned()))
        })
        .and_then(|(start, end)| {
            let start = start.parse::<u16>().ok()?;
            let end = end.parse::<u16>().ok()?;
            (start <= end).then_some((start, end))
        })
}

#[cfg(feature = "gossip-bootstrap")]
pub fn read_gossip_runtime_switch_sustain_ms() -> u64 {
    read_env_var("SOF_GOSSIP_RUNTIME_SWITCH_SUSTAIN_MS")
        .and_then(|value| value.parse::<u64>().ok())
        .filter(|value| *value > 0)
        .unwrap_or(DEFAULT_RUNTIME_SWITCH_SUSTAIN_MS)
}

#[cfg(feature = "gossip-bootstrap")]
pub fn read_gossip_runtime_switch_no_traffic_grace_ms() -> u64 {
    read_env_var("SOF_GOSSIP_RUNTIME_SWITCH_NO_TRAFFIC_GRACE_MS")
        .and_then(|value| value.parse::<u64>().ok())
        .filter(|value| *value > 0)
        .unwrap_or(DEFAULT_RUNTIME_SWITCH_NO_TRAFFIC_GRACE_MS)
}

#[cfg(feature = "gossip-bootstrap")]
pub fn read_gossip_runtime_switch_stabilize_ms() -> u64 {
    read_env_var("SOF_GOSSIP_RUNTIME_SWITCH_STABILIZE_MS")
        .and_then(|value| value.parse::<u64>().ok())
        .filter(|value| *value > 0)
        .unwrap_or(DEFAULT_RUNTIME_SWITCH_STABILIZE_MS)
}

#[cfg(feature = "gossip-bootstrap")]
pub fn read_gossip_runtime_switch_stabilize_min_packets() -> u64 {
    read_env_var("SOF_GOSSIP_RUNTIME_SWITCH_STABILIZE_MIN_PACKETS")
        .and_then(|value| value.parse::<u64>().ok())
        .filter(|value| *value > 0)
        .unwrap_or(DEFAULT_RUNTIME_SWITCH_STABILIZE_MIN_PACKETS)
}

#[cfg(feature = "gossip-bootstrap")]
pub fn read_gossip_runtime_switch_stabilize_max_wait_ms() -> u64 {
    read_env_var("SOF_GOSSIP_RUNTIME_SWITCH_STABILIZE_MAX_WAIT_MS")
        .and_then(|value| value.parse::<u64>().ok())
        .filter(|value| *value > 0)
        .unwrap_or(DEFAULT_RUNTIME_SWITCH_STABILIZE_MAX_WAIT_MS)
}

#[cfg(feature = "gossip-bootstrap")]
pub fn read_gossip_runtime_switch_stabilize_min_peers() -> usize {
    read_env_var("SOF_GOSSIP_RUNTIME_SWITCH_STABILIZE_MIN_PEERS")
        .and_then(|value| value.parse::<usize>().ok())
        .filter(|value| *value > 0)
        .unwrap_or(DEFAULT_RUNTIME_SWITCH_STABILIZE_MIN_PEERS)
}

#[cfg(feature = "gossip-bootstrap")]
pub fn read_gossip_bootstrap_stabilize_max_wait_ms() -> u64 {
    read_env_var("SOF_GOSSIP_BOOTSTRAP_STABILIZE_MAX_WAIT_MS")
        .and_then(|value| value.parse::<u64>().ok())
        .filter(|value| *value > 0)
        .unwrap_or(DEFAULT_BOOTSTRAP_STABILIZE_MAX_WAIT_MS)
}

#[cfg(feature = "gossip-bootstrap")]
pub fn read_gossip_bootstrap_stabilize_min_peers() -> usize {
    read_env_var("SOF_GOSSIP_BOOTSTRAP_STABILIZE_MIN_PEERS")
        .and_then(|value| value.parse::<usize>().ok())
        .filter(|value| *value > 0)
        .unwrap_or(DEFAULT_BOOTSTRAP_STABILIZE_MIN_PEERS)
}

#[cfg(feature = "gossip-bootstrap")]
pub fn read_gossip_runtime_switch_peer_candidates() -> usize {
    read_env_var("SOF_GOSSIP_RUNTIME_SWITCH_PEER_CANDIDATES")
        .and_then(|value| value.parse::<usize>().ok())
        .filter(|value| *value > 0)
        .unwrap_or(DEFAULT_RUNTIME_SWITCH_PEER_CANDIDATES)
}

#[cfg(feature = "gossip-bootstrap")]
pub fn read_gossip_runtime_switch_proactive_enabled() -> bool {
    read_bool_env("SOF_GOSSIP_RUNTIME_SWITCH_PROACTIVE_ENABLED", false)
}

#[cfg(feature = "gossip-bootstrap")]
pub fn read_gossip_runtime_switch_proactive_eval_ms() -> u64 {
    read_env_var("SOF_GOSSIP_RUNTIME_SWITCH_PROACTIVE_EVAL_MS")
        .and_then(|value| value.parse::<u64>().ok())
        .filter(|value| *value > 0)
        .unwrap_or(DEFAULT_RUNTIME_SWITCH_PROACTIVE_EVAL_MS)
}

#[cfg(feature = "gossip-bootstrap")]
pub fn read_gossip_runtime_switch_proactive_active_rank_max() -> usize {
    read_env_var("SOF_GOSSIP_RUNTIME_SWITCH_PROACTIVE_ACTIVE_RANK_MAX")
        .and_then(|value| value.parse::<usize>().ok())
        .unwrap_or(DEFAULT_RUNTIME_SWITCH_PROACTIVE_ACTIVE_RANK_MAX)
}

#[cfg(feature = "gossip-bootstrap")]
pub fn read_gossip_runtime_switch_prioritized_candidates_max() -> usize {
    read_env_var("SOF_GOSSIP_RUNTIME_SWITCH_PRIORITIZED_CANDIDATES_MAX")
        .and_then(|value| value.parse::<usize>().ok())
        .filter(|value| *value > 0)
        .unwrap_or(DEFAULT_RUNTIME_SWITCH_PRIORITIZED_CANDIDATES_MAX)
}

#[cfg(feature = "gossip-bootstrap")]
pub fn read_gossip_runtime_switch_proactive_stable_evals() -> usize {
    read_env_var("SOF_GOSSIP_RUNTIME_SWITCH_PROACTIVE_STABLE_EVALS")
        .and_then(|value| value.parse::<usize>().ok())
        .filter(|value| *value > 0)
        .unwrap_or(DEFAULT_RUNTIME_SWITCH_PROACTIVE_STABLE_EVALS)
}

#[cfg(feature = "gossip-bootstrap")]
pub fn read_gossip_runtime_switch_proactive_min_runtime_age_ms() -> u64 {
    read_env_var("SOF_GOSSIP_RUNTIME_SWITCH_PROACTIVE_MIN_RUNTIME_AGE_MS")
        .and_then(|value| value.parse::<u64>().ok())
        .filter(|value| *value > 0)
        .unwrap_or(DEFAULT_RUNTIME_SWITCH_PROACTIVE_MIN_RUNTIME_AGE_MS)
}

#[cfg(feature = "gossip-bootstrap")]
pub fn read_gossip_load_shed_enabled() -> bool {
    read_bool_env("SOF_GOSSIP_LOAD_SHED_ENABLED", true)
}

#[cfg(feature = "gossip-bootstrap")]
pub fn read_gossip_load_shed_queue_pressure_pct() -> u64 {
    read_env_var("SOF_GOSSIP_LOAD_SHED_QUEUE_PRESSURE_PCT")
        .and_then(|value| value.parse::<u64>().ok())
        .map(|value| value.clamp(1, 100))
        .unwrap_or(DEFAULT_GOSSIP_LOAD_SHED_QUEUE_PRESSURE_PCT)
}

#[cfg(feature = "gossip-bootstrap")]
pub fn read_verify_strict_unknown_queue_pressure_pct() -> u64 {
    read_env_var("SOF_VERIFY_STRICT_UNKNOWN_QUEUE_PRESSURE_PCT")
        .and_then(|value| value.parse::<u64>().ok())
        .map(|value| value.clamp(1, 100))
        .unwrap_or(DEFAULT_VERIFY_STRICT_UNKNOWN_QUEUE_PRESSURE_PCT)
}

#[cfg(feature = "gossip-bootstrap")]
pub fn read_gossip_load_shed_keep_top_sources() -> usize {
    read_env_var("SOF_GOSSIP_LOAD_SHED_KEEP_TOP_SOURCES")
        .and_then(|value| value.parse::<usize>().ok())
        .filter(|value| *value > 0)
        .map(|value| value.min(32))
        .unwrap_or(DEFAULT_GOSSIP_LOAD_SHED_KEEP_TOP_SOURCES)
}