sof 0.18.2

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
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
#[cfg(feature = "gossip-bootstrap")]
use super::*;
#[cfg(feature = "gossip-bootstrap")]
use sof_support::time_support::current_unix_ms;
#[cfg(feature = "gossip-bootstrap")]
use thiserror::Error;

#[cfg(feature = "gossip-bootstrap")]
const GOSSIP_RECEIVER_STOP_TIMEOUT: Duration = Duration::from_millis(750);
#[cfg(feature = "gossip-bootstrap")]
const GOSSIP_RUNTIME_DROP_TIMEOUT: Duration = Duration::from_millis(2_000);

#[cfg(feature = "gossip-bootstrap")]
pub(super) fn is_bind_conflict_error(error: &impl std::fmt::Display) -> bool {
    let rendered = error.to_string();
    rendered.contains("Address already in use")
        || rendered.contains("bind_to port")
        || rendered.contains("gossip_addr")
}

#[cfg(feature = "gossip-bootstrap")]
pub(crate) async fn stop_gossip_runtime_components(
    receiver_handles: Vec<JoinHandle<()>>,
    mut gossip_runtime: Option<GossipRuntime>,
) {
    if let Some(runtime) = gossip_runtime.as_ref() {
        runtime.exit.store(true, Ordering::Relaxed);
    }
    for handle in &receiver_handles {
        handle.abort();
    }
    for handle in receiver_handles {
        match tokio::time::timeout(GOSSIP_RECEIVER_STOP_TIMEOUT, handle).await {
            Ok(joined) => {
                if joined.is_err() {
                    // Receiver task was already aborted/cancelled.
                }
            }
            Err(_) => {
                tracing::warn!("timed out waiting for aborted gossip receiver task to stop");
            }
        }
    }
    if let Some(gossip_runtime) = gossip_runtime.take() {
        match tokio::time::timeout(
            GOSSIP_RUNTIME_DROP_TIMEOUT,
            tokio::task::spawn_blocking(move || drop(gossip_runtime)),
        )
        .await
        {
            Ok(joined) => {
                if let Err(error) = joined {
                    tracing::warn!(
                        ?error,
                        "failed to join blocking gossip runtime shutdown task"
                    );
                }
            }
            Err(_) => {
                tracing::warn!("timed out waiting for gossip runtime control plane shutdown");
            }
        }
    }
}

#[cfg(feature = "gossip-bootstrap")]
#[derive(Debug, Clone, Copy)]
pub(super) struct RuntimeStabilization {
    pub(super) stabilized: bool,
    pub(super) stabilized_by_peers: bool,
    pub(super) elapsed: Duration,
    pub(super) packets_seen: u64,
    pub(super) discovered_peers: usize,
}

#[cfg(feature = "gossip-bootstrap")]
pub(super) async fn wait_for_runtime_stabilization(
    telemetry: ingest::ReceiverTelemetry,
    sustain: Duration,
    min_packets: u64,
    max_wait: Duration,
) -> RuntimeStabilization {
    let started_at = Instant::now();
    let (baseline_packets, _) = telemetry.snapshot();
    let mut first_packet_at: Option<Instant> = None;
    let mut ticker = tokio::time::interval(Duration::from_millis(100));
    ticker.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Skip);

    loop {
        let (packets, last_packet_unix_ms) = telemetry.snapshot();
        let packets_seen = packets.saturating_sub(baseline_packets);
        if packets_seen > 0 && first_packet_at.is_none() {
            first_packet_at = Some(Instant::now());
        }
        let elapsed = started_at.elapsed();
        let last_packet_age_ms = if last_packet_unix_ms == 0 {
            u64::MAX
        } else {
            current_unix_ms().saturating_sub(last_packet_unix_ms)
        };
        let sustained = first_packet_at
            .map(|first_seen| Instant::now().saturating_duration_since(first_seen) >= sustain)
            .unwrap_or(false);
        let fresh = last_packet_age_ms <= 500;
        if sustained && fresh && packets_seen >= min_packets {
            return RuntimeStabilization {
                stabilized: true,
                stabilized_by_peers: false,
                elapsed,
                packets_seen,
                discovered_peers: 0,
            };
        }
        if elapsed >= max_wait {
            return RuntimeStabilization {
                stabilized: false,
                stabilized_by_peers: false,
                elapsed,
                packets_seen,
                discovered_peers: 0,
            };
        }
        ticker.tick().await;
    }
}

#[cfg(feature = "gossip-bootstrap")]
pub(super) async fn wait_for_runtime_stabilization_or_peer_discovery<F>(
    telemetry: ingest::ReceiverTelemetry,
    sustain: Duration,
    min_packets: u64,
    max_wait: Duration,
    peer_counter: F,
    min_peers: usize,
) -> RuntimeStabilization
where
    F: Fn() -> usize,
{
    let started_at = Instant::now();
    let (baseline_packets, _) = telemetry.snapshot();
    let mut first_packet_at: Option<Instant> = None;
    let mut ticker = tokio::time::interval(Duration::from_millis(100));
    ticker.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Skip);

    loop {
        let (packets, last_packet_unix_ms) = telemetry.snapshot();
        let packets_seen = packets.saturating_sub(baseline_packets);
        if packets_seen > 0 && first_packet_at.is_none() {
            first_packet_at = Some(Instant::now());
        }
        let discovered_peers = peer_counter();
        let elapsed = started_at.elapsed();
        let last_packet_age_ms = if last_packet_unix_ms == 0 {
            u64::MAX
        } else {
            current_unix_ms().saturating_sub(last_packet_unix_ms)
        };
        let sustained = first_packet_at
            .map(|first_seen| Instant::now().saturating_duration_since(first_seen) >= sustain)
            .unwrap_or(false);
        let fresh = last_packet_age_ms <= 500;
        if sustained && fresh && packets_seen >= min_packets {
            return RuntimeStabilization {
                stabilized: true,
                stabilized_by_peers: false,
                elapsed,
                packets_seen,
                discovered_peers,
            };
        }
        if discovered_peers >= min_peers {
            return RuntimeStabilization {
                stabilized: false,
                stabilized_by_peers: true,
                elapsed,
                packets_seen,
                discovered_peers,
            };
        }
        if elapsed >= max_wait {
            return RuntimeStabilization {
                stabilized: false,
                stabilized_by_peers: false,
                elapsed,
                packets_seen,
                discovered_peers,
            };
        }
        ticker.tick().await;
    }
}

#[cfg(all(test, feature = "gossip-bootstrap"))]
mod tests {
    use super::wait_for_runtime_stabilization_or_peer_discovery;
    use crate::ingest::ReceiverTelemetry;
    use std::sync::{
        Arc,
        atomic::{AtomicUsize, Ordering},
    };
    use std::time::Duration;

    #[tokio::test]
    async fn control_plane_only_stabilization_returns_on_peer_discovery_without_packets() {
        let telemetry = ReceiverTelemetry::default();
        let discovered_peers = Arc::new(AtomicUsize::new(0));
        let peers_for_probe = discovered_peers.clone();

        let peer_task = tokio::spawn(async move {
            tokio::time::sleep(Duration::from_millis(50)).await;
            peers_for_probe.store(1, Ordering::Relaxed);
        });

        let stabilization = wait_for_runtime_stabilization_or_peer_discovery(
            telemetry,
            Duration::from_millis(250),
            8,
            Duration::from_secs(2),
            || discovered_peers.load(Ordering::Relaxed),
            1,
        )
        .await;

        peer_task.await.expect("peer task should complete");

        assert!(!stabilization.stabilized);
        assert!(stabilization.stabilized_by_peers);
        assert_eq!(stabilization.packets_seen, 0);
        assert_eq!(stabilization.discovered_peers, 1);
        assert!(stabilization.elapsed < Duration::from_secs(2));
    }
}

#[cfg(feature = "gossip-bootstrap")]
#[derive(Clone, Copy, Debug)]
pub(super) struct GossipRuntimePortPlan {
    pub(super) primary: PortRange,
    pub(super) secondary: Option<PortRange>,
}

#[cfg(feature = "gossip-bootstrap")]
#[derive(Debug, Error)]
pub(super) enum GossipRuntimePortPlanError {
    #[error(transparent)]
    PortRange(#[from] PortRangeParseError),
}

#[cfg(feature = "gossip-bootstrap")]
pub(super) fn build_gossip_runtime_port_plan()
-> Result<GossipRuntimePortPlan, GossipRuntimePortPlanError> {
    let mut primary = read_port_range()?;
    let configured_overlap = read_gossip_runtime_switch_overlap_ms() > 0;
    let secondary = match read_gossip_runtime_switch_port_range() {
        Some(candidate)
            if !port_ranges_overlap(primary, candidate)
                && range_contains_port(candidate, read_gossip_port().unwrap_or(candidate.0)) =>
        {
            Some(candidate)
        }
        Some(candidate) => {
            tracing::warn!(
                primary_range = %format_port_range(primary),
                candidate_range = %format_port_range(candidate),
                "ignoring SOF_GOSSIP_RUNTIME_SWITCH_PORT_RANGE because it overlaps primary range or excludes SOF_GOSSIP_PORT"
            );
            None
        }
        None if configured_overlap => {
            let split = split_port_range_for_overlap(primary);
            if let Some((left, _)) = split {
                primary = left;
            }
            if split.is_none() {
                tracing::warn!(
                    primary_range = %format_port_range(primary),
                    "unable to auto-split SOF_PORT_RANGE for overlap switching; using break-before-make fallback"
                );
            }
            split.map(|(_, right)| right)
        }
        None => None,
    };
    Ok(GossipRuntimePortPlan { primary, secondary })
}

#[cfg(feature = "gossip-bootstrap")]
fn split_port_range_for_overlap(primary: PortRange) -> Option<(PortRange, PortRange)> {
    let span = u32::from(primary.1.saturating_sub(primary.0)).saturating_add(1);
    let min_span = u32::try_from(read_tvu_receive_sockets().saturating_add(16))
        .unwrap_or(u32::MAX)
        .max(24);
    if span < min_span.saturating_mul(2) {
        return None;
    }
    let primary_span = span / 2;
    if primary_span == 0 {
        return None;
    }
    let primary_end = u32::from(primary.0)
        .saturating_add(primary_span)
        .saturating_sub(1);
    let secondary_start = primary_end.saturating_add(1);
    let secondary_end = u32::from(primary.1);
    if secondary_end < secondary_start {
        return None;
    }
    let secondary_span = secondary_end
        .saturating_sub(secondary_start)
        .saturating_add(1);
    if secondary_span < min_span {
        return None;
    }
    let left = (primary.0, u16::try_from(primary_end).ok()?);
    let right = (
        u16::try_from(secondary_start).ok()?,
        u16::try_from(secondary_end).ok()?,
    );
    Some((left, right))
}

#[cfg(feature = "gossip-bootstrap")]
const fn port_ranges_overlap(left: PortRange, right: PortRange) -> bool {
    left.0 <= right.1 && right.0 <= left.1
}

#[cfg(feature = "gossip-bootstrap")]
const fn range_contains_port(range: PortRange, port: u16) -> bool {
    port >= range.0 && port <= range.1
}

#[cfg(feature = "gossip-bootstrap")]
pub(super) fn format_port_range(range: PortRange) -> String {
    format!("{}-{}", range.0, range.1)
}

#[cfg(feature = "gossip-bootstrap")]
fn apply_gossip_runtime_env_overrides() {
    const GOSSIP_OVERRIDE_KEYS: [&str; 10] = [
        "SOF_GOSSIP_RECEIVER_CHANNEL_CAPACITY",
        "SOF_GOSSIP_SOCKET_CONSUME_CHANNEL_CAPACITY",
        "SOF_GOSSIP_RESPONSE_CHANNEL_CAPACITY",
        "SOF_GOSSIP_CHANNEL_CONSUME_CAPACITY",
        "SOF_GOSSIP_CONSUME_THREADS",
        "SOF_GOSSIP_LISTEN_THREADS",
        "SOF_GOSSIP_RUN_THREADS",
        "SOF_GOSSIP_SOCKET_CONSUME_PARALLEL_PACKET_THRESHOLD",
        "SOF_GOSSIP_STATS_INTERVAL_SECS",
        "SOF_GOSSIP_SAMPLE_LOGS_ENABLED",
    ];

    let overrides = GOSSIP_OVERRIDE_KEYS
        .iter()
        .filter_map(|key| {
            crate::runtime_env::read_env_var(key).map(|value| ((*key).to_owned(), value))
        })
        .collect::<Vec<_>>();
    solana_gossip::set_runtime_env_overrides(overrides);
}

#[cfg(feature = "gossip-bootstrap")]
/// Runtime components produced by successful gossip bootstrap.
pub(super) type GossipBootstrapRuntime = (
    Vec<JoinHandle<()>>,
    GossipRuntime,
    Option<crate::repair::GossipRepairClient>,
);

#[cfg(feature = "gossip-bootstrap")]
/// Errors produced while starting one gossip-bootstrap runtime instance.
#[derive(Debug, Error)]
pub(super) enum GossipBootstrapStartError {
    /// Gossip entrypoint could not be resolved to a socket address.
    #[error(transparent)]
    ResolveEntrypoint(#[from] crate::app::runtime::bootstrap::relay::ResolveSocketAddrError),
    /// The blocking discovery task did not join.
    #[error("failed to join gossip bootstrap discovery task: {source}")]
    DiscoveryTaskJoin { source: tokio::task::JoinError },
    /// Public IP probe failed.
    #[error("failed to determine public IP via {entrypoint}: {reason}")]
    DiscoveryPublicIp {
        entrypoint: SocketAddr,
        reason: String,
    },
    /// `SOF_SHRED_VERSION` resolved to zero, which is invalid.
    #[error("shred version resolved to 0; set SOF_SHRED_VERSION explicitly")]
    ZeroShredVersion,
    /// Entrypoint shred-version discovery failed.
    #[error("failed to resolve shred version from entrypoint {entrypoint}: {reason}")]
    DiscoveryShredVersion {
        entrypoint: SocketAddr,
        reason: String,
    },
    /// Port-range parsing failed.
    #[error(transparent)]
    PortRange(#[from] PortRangeParseError),
    /// TVU socket count resolved to zero.
    #[error("SOF_TVU_SOCKETS must be non-zero")]
    ZeroTvuSockets,
    /// Retransmit socket count resolved to zero.
    #[error("internal error: retransmit sockets must be non-zero")]
    ZeroRetransmitSockets,
    /// QUIC endpoint count resolved to zero.
    #[error("DEFAULT_QUIC_ENDPOINTS must be non-zero")]
    ZeroQuicEndpoints,
    /// Bind-IP set construction failed.
    #[error("failed to build bind IP set: {reason}")]
    BindIpAddrs { reason: String },
    /// Gossip node did not allocate TVU sockets.
    #[error("node did not allocate tvu sockets")]
    NoTvuSocketsAllocated,
    /// Failed to clone a TVU socket.
    #[error("failed to clone tvu socket: {source}")]
    CloneTvuSocket { source: std::io::Error },
    /// Failed to set TVU socket nonblocking mode.
    #[error("failed to set tvu socket nonblocking: {source}")]
    SetTvuSocketNonblocking { source: std::io::Error },
    /// Failed to clone repair socket for receiver path.
    #[error("failed to clone repair socket for receiver: {source}")]
    CloneRepairReceiverSocket { source: std::io::Error },
    /// Failed to set repair receiver socket nonblocking mode.
    #[error("failed to set repair receiver socket nonblocking: {source}")]
    SetRepairReceiverSocketNonblocking { source: std::io::Error },
    /// Failed to clone repair socket for sender path.
    #[error("failed to clone repair socket for sender: {source}")]
    CloneRepairSenderSocket { source: std::io::Error },
    /// Failed to set repair sender socket nonblocking mode.
    #[error("failed to set repair sender socket nonblocking: {source}")]
    SetRepairSenderSocketNonblocking { source: std::io::Error },
    /// Failed to convert repair sender socket into Tokio UDP socket.
    #[error("failed to convert repair sender socket to tokio udp socket: {source}")]
    RepairSenderSocketToTokio { source: std::io::Error },
}

#[cfg(feature = "gossip-bootstrap")]
async fn resolve_shred_version(
    discovery_entrypoint: SocketAddr,
) -> Result<u16, GossipBootstrapStartError> {
    if let Some(shred_version_override) = read_shred_version_override() {
        if shred_version_override == 0 {
            return Err(GossipBootstrapStartError::ZeroShredVersion);
        }
        return Ok(shred_version_override);
    }

    let discovery_result =
        tokio::task::spawn_blocking(move || get_cluster_shred_version(&discovery_entrypoint))
            .await
            .map_err(|source| GossipBootstrapStartError::DiscoveryTaskJoin { source })?;
    match discovery_result {
        Ok(shred_version) if shred_version > 0 => Ok(shred_version),
        Ok(_) => Err(GossipBootstrapStartError::ZeroShredVersion),
        Err(reason) => Err(GossipBootstrapStartError::DiscoveryShredVersion {
            entrypoint: discovery_entrypoint,
            reason,
        }),
    }
}

#[cfg(feature = "gossip-bootstrap")]
/// Errors produced while spawning and guarding a gossip bootstrap task.
#[derive(Debug, Error)]
pub(super) enum GossipBootstrapGuardError {
    /// Inner bootstrap startup failed.
    #[error(transparent)]
    Bootstrap(#[from] GossipBootstrapStartError),
    /// Bootstrap task panicked.
    #[error("panic while bootstrapping gossip runtime from {entrypoint}: {source}")]
    BootstrapPanic {
        entrypoint: String,
        source: tokio::task::JoinError,
    },
    /// Bootstrap task failed to join.
    #[error("failed to join gossip bootstrap task for {entrypoint}: {source}")]
    TaskJoin {
        entrypoint: String,
        source: tokio::task::JoinError,
    },
}

#[cfg(feature = "gossip-bootstrap")]
async fn start_gossip_bootstrapped_receiver(
    entrypoint: &str,
    tx: ingest::RawPacketBatchSender,
    gossip_identity: Arc<Keypair>,
    port_range_override: Option<PortRange>,
    control_plane_only_bootstrap: bool,
) -> Result<GossipBootstrapRuntime, GossipBootstrapStartError> {
    let log_startup_steps = read_log_startup_steps();
    let entrypoint = resolve_socket_addr(entrypoint)?;
    let discovery_entrypoint = entrypoint;
    if log_startup_steps {
        tracing::info!(
            step = "gossip_discovery_probe_begin",
            entrypoint = %discovery_entrypoint,
            "probing shred version and public IP"
        );
    }
    let advertised_ip = tokio::task::spawn_blocking(move || {
        let advertised_ip = get_public_ip_addr_with_binding(
            &discovery_entrypoint,
            IpAddr::V4(Ipv4Addr::UNSPECIFIED),
        )
        .map_err(|source| GossipBootstrapStartError::DiscoveryPublicIp {
            entrypoint: discovery_entrypoint,
            reason: source.to_string(),
        })?;
        Ok::<IpAddr, GossipBootstrapStartError>(advertised_ip)
    })
    .await
    .map_err(|source| GossipBootstrapStartError::DiscoveryTaskJoin { source })??;
    let shred_version = resolve_shred_version(discovery_entrypoint).await?;
    if log_startup_steps {
        tracing::info!(
            step = "gossip_discovery_probe_complete",
            entrypoint = %entrypoint,
            shred_version,
            advertised_ip = %advertised_ip,
            "gossip discovery probe succeeded"
        );
    }
    let port_range = match port_range_override {
        Some(port_range) => port_range,
        None => read_port_range()?,
    };
    let configured_gossip_port = read_gossip_port();
    let gossip_port = configured_gossip_port
        .filter(|port| range_contains_port(port_range, *port))
        .unwrap_or(port_range.0);
    if configured_gossip_port.is_some() && gossip_port != configured_gossip_port.unwrap_or_default()
    {
        tracing::warn!(
            configured_gossip_port = configured_gossip_port.unwrap_or_default(),
            port_range = %format_port_range(port_range),
            "SOF_GOSSIP_PORT is outside selected port range; using range start instead"
        );
    }
    let gossip_addr = SocketAddr::new(advertised_ip, gossip_port);
    let num_tvu_receive_sockets = NonZeroUsize::new(read_tvu_receive_sockets())
        .ok_or(GossipBootstrapStartError::ZeroTvuSockets)?;
    let num_tvu_retransmit_sockets =
        NonZeroUsize::new(1).ok_or(GossipBootstrapStartError::ZeroRetransmitSockets)?;
    let num_quic_endpoints = NonZeroUsize::new(DEFAULT_QUIC_ENDPOINTS)
        .ok_or(GossipBootstrapStartError::ZeroQuicEndpoints)?;

    let config = NodeConfig {
        advertised_ip,
        gossip_port: gossip_addr.port(),
        port_range,
        bind_ip_addrs: solana_net_utils::multihomed_sockets::BindIpAddrs::new(vec![IpAddr::V4(
            Ipv4Addr::UNSPECIFIED,
        )])
        .map_err(|reason| GossipBootstrapStartError::BindIpAddrs { reason })?,
        public_tpu_addr: None,
        public_tpu_forwards_addr: None,
        vortexor_receiver_addr: None,
        num_tvu_receive_sockets,
        num_tvu_retransmit_sockets,
        num_quic_endpoints,
    };
    let mut node = Node::new_with_external_ip(&gossip_identity.pubkey(), config);
    node.info.set_shred_version(shred_version);

    if node.sockets.tvu.is_empty() {
        return Err(GossipBootstrapStartError::NoTvuSocketsAllocated);
    }

    tracing::info!(
        gossip = %node.info.gossip().unwrap_or(gossip_addr),
        tvu = %node.info.tvu(solana_gossip::contact_info::Protocol::UDP).unwrap_or_else(|| SocketAddr::new(IpAddr::V4(Ipv4Addr::UNSPECIFIED), 0)),
        tvu_sockets = node.sockets.tvu.len(),
        shred_version,
        "started gossip bootstrap node"
    );

    let cluster_info = ClusterInfo::new(
        node.info.clone(),
        gossip_identity,
        SocketAddrSpace::Unspecified,
    );
    cluster_info.set_entrypoint(ContactInfo::new_gossip_entry_point(&entrypoint));
    let gossip_validators = read_gossip_validators();
    if let Some(validators) = gossip_validators.as_ref() {
        tracing::info!(
            validator_count = validators.len(),
            "gossip validator allowlist enabled"
        );
    }
    let cluster_info = Arc::new(cluster_info);
    let exit = Arc::new(AtomicBool::new(false));
    apply_gossip_runtime_env_overrides();
    let gossip_service = GossipService::new(
        &cluster_info,
        None,
        node.sockets.gossip.clone(),
        gossip_validators,
        true,
        None,
        exit.clone(),
    );

    // Bootstrap/runtime switching should only consider TVU ingress. Repair/control
    // traffic can be present on otherwise dead runtimes and must not count as
    // "healthy" packet flow.
    let ingest_telemetry = ingest::ReceiverTelemetry::default();
    let mut receiver_handles = Vec::with_capacity(node.sockets.tvu.len().saturating_add(1));
    if !control_plane_only_bootstrap {
        for socket in &node.sockets.tvu {
            let std_socket = socket
                .try_clone()
                .map_err(|source| GossipBootstrapStartError::CloneTvuSocket { source })?;
            std_socket
                .set_nonblocking(true)
                .map_err(|source| GossipBootstrapStartError::SetTvuSocketNonblocking { source })?;
            receiver_handles.push(
                ingest::spawn_udp_receiver_from_std_with_telemetry_and_shutdown(
                    std_socket,
                    tx.clone(),
                    Some(ingest_telemetry.clone()),
                    Some(exit.clone()),
                ),
            );
        }
        let repair_receiver_socket =
            node.sockets.repair.try_clone().map_err(|source| {
                GossipBootstrapStartError::CloneRepairReceiverSocket { source }
            })?;
        repair_receiver_socket
            .set_nonblocking(true)
            .map_err(
                |source| GossipBootstrapStartError::SetRepairReceiverSocketNonblocking { source },
            )?;
        receiver_handles.push(
            ingest::spawn_udp_receiver_from_std_with_telemetry_and_shutdown(
                repair_receiver_socket,
                tx,
                None,
                Some(exit.clone()),
            ),
        );
    }

    let repair_client = if !control_plane_only_bootstrap && read_repair_enabled() {
        let repair_sender_socket = node
            .sockets
            .repair
            .try_clone()
            .map_err(|source| GossipBootstrapStartError::CloneRepairSenderSocket { source })?;
        repair_sender_socket
            .set_nonblocking(true)
            .map_err(
                |source| GossipBootstrapStartError::SetRepairSenderSocketNonblocking { source },
            )?;
        let repair_sender_socket = tokio::net::UdpSocket::from_std(repair_sender_socket)
            .map_err(|source| GossipBootstrapStartError::RepairSenderSocketToTokio { source })?;
        Some(crate::repair::GossipRepairClient::new(
            cluster_info.clone(),
            repair_sender_socket,
            cluster_info.keypair(),
            crate::repair::GossipRepairClientConfig {
                peer_cache_ttl: Duration::from_millis(read_repair_peer_cache_ttl_ms()),
                peer_cache_capacity: read_repair_peer_cache_capacity(),
                active_peer_count: read_repair_active_peers(),
                peer_sample_size: read_repair_peer_sample_size(),
                serve_max_bytes_per_sec: read_repair_serve_max_bytes_per_sec(),
                serve_unstaked_max_bytes_per_sec: read_repair_serve_unstaked_max_bytes_per_sec(),
                serve_max_requests_per_peer_per_sec:
                    read_repair_serve_max_requests_per_peer_per_sec(),
            },
        ))
    } else {
        None
    };
    Ok((
        receiver_handles,
        GossipRuntime {
            exit,
            gossip_service: Some(gossip_service),
            cluster_info,
            ingest_telemetry,
        },
        repair_client,
    ))
}

#[cfg(feature = "gossip-bootstrap")]
pub(super) async fn start_gossip_bootstrapped_receiver_guarded(
    entrypoint: &str,
    tx: ingest::RawPacketBatchSender,
    gossip_identity: Arc<Keypair>,
    port_range_override: Option<PortRange>,
    control_plane_only_bootstrap: bool,
) -> Result<GossipBootstrapRuntime, GossipBootstrapGuardError> {
    let entrypoint_owned = entrypoint.to_owned();
    let join = tokio::spawn(async move {
        start_gossip_bootstrapped_receiver(
            &entrypoint_owned,
            tx,
            gossip_identity,
            port_range_override,
            control_plane_only_bootstrap,
        )
        .await
    });
    match join.await {
        Ok(result) => result.map_err(GossipBootstrapGuardError::from),
        Err(source) if source.is_panic() => Err(GossipBootstrapGuardError::BootstrapPanic {
            entrypoint: entrypoint.to_owned(),
            source,
        }),
        Err(source) => Err(GossipBootstrapGuardError::TaskJoin {
            entrypoint: entrypoint.to_owned(),
            source,
        }),
    }
}