slotstrike 1.0.0

Low-latency Solana slotstrike runtime for event-driven token execution
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
use std::{
    collections::BTreeMap,
    str::FromStr,
    sync::Arc,
    time::{Duration, Instant},
};

use sof::framework::{
    ControlPlaneSource, LeaderScheduleEntry, LeaderScheduleEvent, ObserverPlugin,
};
use sof_tx::{
    adapters::PluginHostTxProviderAdapter,
    submit::{TxFlowSafetyIssue, TxFlowSafetyQuality, TxFlowSafetySnapshot, TxFlowSafetySource},
};
use solana_client::nonblocking::rpc_client::RpcClient;
use solana_epoch_schedule::EpochSchedule;
use solana_sdk::pubkey::Pubkey;
use tokio::{task::JoinHandle, time::MissedTickBehavior};

const DIRECT_SCHEDULE_CURSOR_TICK: Duration = Duration::from_millis(250);
const DIRECT_SCHEDULE_RPC_RETRY: Duration = Duration::from_secs(5);
const MIN_LEADER_WINDOW_SLOTS: usize = 512;
const MAX_LEADER_WINDOW_SLOTS: usize = 2_048;
const LEADER_WINDOW_MULTIPLIER: usize = 256;
const MAX_RECENT_BLOCKHASH_SLOT_LAG: u64 = 32;
const MAX_CLUSTER_TOPOLOGY_SLOT_LAG: u64 = 64;
const MAX_LEADER_SCHEDULE_SLOT_LAG: u64 = 128;
const MAX_CONTROL_PLANE_SLOT_SPREAD: u64 = 32;

#[derive(Clone)]
pub struct DirectLeaderScheduleSafetySource {
    adapter: Arc<PluginHostTxProviderAdapter>,
}

impl DirectLeaderScheduleSafetySource {
    #[must_use]
    pub const fn new(adapter: Arc<PluginHostTxProviderAdapter>) -> Self {
        Self { adapter }
    }
}

impl TxFlowSafetySource for DirectLeaderScheduleSafetySource {
    fn toxic_flow_snapshot(&self) -> TxFlowSafetySnapshot {
        let snapshot = self.adapter.control_plane_snapshot();
        let effective_tip = snapshot
            .latest_recent_blockhash_slot
            .or(snapshot.leader_schedule_slot)
            .or(snapshot.cluster_topology_slot);

        let mut missing_control_plane = false;
        let mut stale_control_plane = false;
        let mut degraded_control_plane = false;

        if snapshot.latest_recent_blockhash_slot.is_none()
            || snapshot.cluster_topology_slot.is_none()
            || snapshot.leader_schedule_slot.is_none()
            || snapshot.known_leader_target_addrs == 0
        {
            missing_control_plane = true;
        }

        if let Some(tip_slot) = effective_tip {
            stale_control_plane |= is_slot_lag_stale(
                snapshot.latest_recent_blockhash_slot,
                tip_slot,
                MAX_RECENT_BLOCKHASH_SLOT_LAG,
            );
            stale_control_plane |= is_slot_lag_stale(
                snapshot.cluster_topology_slot,
                tip_slot,
                MAX_CLUSTER_TOPOLOGY_SLOT_LAG,
            );
            stale_control_plane |= is_slot_lag_stale(
                snapshot.leader_schedule_slot,
                tip_slot,
                MAX_LEADER_SCHEDULE_SLOT_LAG,
            );
            degraded_control_plane |=
                control_plane_slot_spread(snapshot, tip_slot) > MAX_CONTROL_PLANE_SLOT_SPREAD;
        } else {
            missing_control_plane = true;
        }

        let (quality, issues) = if missing_control_plane {
            (
                TxFlowSafetyQuality::IncompleteControlPlane,
                vec![TxFlowSafetyIssue::MissingControlPlane],
            )
        } else if stale_control_plane {
            (
                TxFlowSafetyQuality::Stale,
                vec![TxFlowSafetyIssue::StaleControlPlane],
            )
        } else if degraded_control_plane {
            (
                TxFlowSafetyQuality::Degraded,
                vec![TxFlowSafetyIssue::DegradedControlPlane],
            )
        } else {
            (TxFlowSafetyQuality::Stable, Vec::new())
        };

        TxFlowSafetySnapshot {
            quality,
            issues,
            current_state_version: effective_tip,
            replay_recovery_pending: false,
        }
    }
}

#[must_use]
pub fn direct_leader_window_slots(routing_next_leaders: usize) -> usize {
    routing_next_leaders
        .saturating_mul(LEADER_WINDOW_MULTIPLIER)
        .clamp(MIN_LEADER_WINDOW_SLOTS, MAX_LEADER_WINDOW_SLOTS)
}

#[must_use]
pub fn spawn_direct_leader_schedule_task(
    rpc_url: String,
    routing_next_leaders: usize,
    adapter: Arc<PluginHostTxProviderAdapter>,
) -> JoinHandle<()> {
    tokio::spawn(async move {
        let rpc = Arc::new(RpcClient::new(rpc_url));
        let window_slots = direct_leader_window_slots(routing_next_leaders);
        let mut state = DirectLeaderScheduleState::new(rpc, adapter, window_slots);
        state.run().await;
    })
}

struct DirectLeaderScheduleState {
    rpc: Arc<RpcClient>,
    adapter: Arc<PluginHostTxProviderAdapter>,
    window_slots: usize,
    epoch_schedule: Option<EpochSchedule>,
    cached_epochs: BTreeMap<u64, Vec<LeaderScheduleEntry>>,
    last_emitted_slot: Option<u64>,
    next_rpc_retry_at: Instant,
}

impl DirectLeaderScheduleState {
    fn new(
        rpc: Arc<RpcClient>,
        adapter: Arc<PluginHostTxProviderAdapter>,
        window_slots: usize,
    ) -> Self {
        Self {
            rpc,
            adapter,
            window_slots,
            epoch_schedule: None,
            cached_epochs: BTreeMap::new(),
            last_emitted_slot: None,
            next_rpc_retry_at: Instant::now(),
        }
    }

    async fn run(&mut self) {
        let mut tick = tokio::time::interval(DIRECT_SCHEDULE_CURSOR_TICK);
        tick.set_missed_tick_behavior(MissedTickBehavior::Skip);

        loop {
            tick.tick().await;

            let observed_slot = self.observed_slot();
            let Some(observed_slot) = observed_slot else {
                continue;
            };

            if self.epoch_schedule.is_none() && self.rpc_retry_ready() {
                match self.rpc.get_epoch_schedule().await {
                    Ok(epoch_schedule) => {
                        self.epoch_schedule = Some(epoch_schedule);
                    }
                    Err(error) => {
                        log::warn!(
                            "SOF-TX direct leader-schedule bootstrap failed to fetch epoch schedule: {}",
                            error
                        );
                        self.defer_rpc_retry();
                        continue;
                    }
                }
            }

            let Some(epoch_schedule) = self.epoch_schedule.clone() else {
                continue;
            };

            let current_epoch = epoch_schedule.get_epoch(observed_slot);
            if self.rpc_retry_ready()
                && self
                    .ensure_epoch_cache(&epoch_schedule, current_epoch, observed_slot)
                    .await
                    .is_err()
            {
                self.defer_rpc_retry();
                continue;
            }

            if self.last_emitted_slot == Some(observed_slot) {
                continue;
            }

            let snapshot_leaders =
                build_window_snapshot(&self.cached_epochs, observed_slot, self.window_slots);
            if snapshot_leaders.is_empty() {
                continue;
            }

            self.adapter
                .on_leader_schedule(LeaderScheduleEvent {
                    source: ControlPlaneSource::Direct,
                    slot: Some(observed_slot),
                    epoch: Some(current_epoch),
                    added_leaders: Vec::new(),
                    removed_slots: Vec::new(),
                    updated_leaders: Vec::new(),
                    snapshot_leaders,
                    provider_source: None,
                })
                .await;
            self.last_emitted_slot = Some(observed_slot);
        }
    }

    fn observed_slot(&self) -> Option<u64> {
        let snapshot = self.adapter.control_plane_snapshot();
        snapshot
            .latest_recent_blockhash_slot
            .or(snapshot.leader_schedule_slot)
            .or(snapshot.cluster_topology_slot)
    }

    fn rpc_retry_ready(&self) -> bool {
        Instant::now() >= self.next_rpc_retry_at
    }

    fn defer_rpc_retry(&mut self) {
        self.next_rpc_retry_at = Instant::now()
            .checked_add(DIRECT_SCHEDULE_RPC_RETRY)
            .unwrap_or_else(Instant::now);
    }

    async fn ensure_epoch_cache(
        &mut self,
        epoch_schedule: &EpochSchedule,
        current_epoch: u64,
        observed_slot: u64,
    ) -> Result<(), ()> {
        if !self.cached_epochs.contains_key(&current_epoch) {
            let entries =
                fetch_epoch_schedule_window(self.rpc.as_ref(), epoch_schedule, current_epoch)
                    .await
                    .map_err(|error| {
                        log::warn!(
                            "SOF-TX direct leader-schedule refresh failed for epoch {}: {}",
                            current_epoch,
                            error
                        );
                    })?;
            log::info!(
                "SOF-TX direct loaded leader-schedule cache for epoch {} ({} slots) at observed slot {}",
                current_epoch,
                entries.len(),
                observed_slot
            );
            let _ = self.cached_epochs.insert(current_epoch, entries);
            self.next_rpc_retry_at = Instant::now();
        }

        let current_epoch_end = epoch_schedule.get_last_slot_in_epoch(current_epoch);
        let should_prefetch_next_epoch =
            current_epoch_end.saturating_sub(observed_slot) <= self.window_slots as u64;
        let next_epoch = current_epoch.saturating_add(1);
        if should_prefetch_next_epoch && !self.cached_epochs.contains_key(&next_epoch) {
            match fetch_epoch_schedule_window(self.rpc.as_ref(), epoch_schedule, next_epoch).await {
                Ok(entries) => {
                    log::info!(
                        "SOF-TX direct loaded leader-schedule cache for epoch {} ({} slots) at observed slot {}",
                        next_epoch,
                        entries.len(),
                        observed_slot
                    );
                    let _ = self.cached_epochs.insert(next_epoch, entries);
                    self.next_rpc_retry_at = Instant::now();
                }
                Err(error) => {
                    log::debug!(
                        "SOF-TX direct next-epoch leader-schedule refresh deferred for epoch {}: {}",
                        next_epoch,
                        error
                    );
                }
            }
        }

        self.cached_epochs
            .retain(|epoch, _| *epoch >= current_epoch.saturating_sub(1));
        Ok(())
    }
}

async fn fetch_epoch_schedule_window(
    rpc: &RpcClient,
    epoch_schedule: &EpochSchedule,
    epoch: u64,
) -> Result<Vec<LeaderScheduleEntry>, String> {
    let first_slot = epoch_schedule.get_first_slot_in_epoch(epoch);
    let schedule = rpc
        .get_leader_schedule(Some(first_slot))
        .await
        .map_err(|error| error.to_string())?;
    let Some(schedule) = schedule else {
        return Ok(Vec::new());
    };

    let mut entries = Vec::new();
    for (identity, relative_slots) in schedule {
        let pubkey = Pubkey::from_str(&identity)
            .map_err(|error| format!("invalid leader identity '{identity}': {error}"))?;
        let leader = pubkey.into();
        for relative_slot in relative_slots {
            let slot = first_slot.saturating_add(relative_slot as u64);
            entries.push(LeaderScheduleEntry { slot, leader });
        }
    }
    entries.sort_unstable_by_key(|entry| entry.slot);
    Ok(entries)
}

fn build_window_snapshot(
    cached_epochs: &BTreeMap<u64, Vec<LeaderScheduleEntry>>,
    observed_slot: u64,
    window_slots: usize,
) -> Vec<LeaderScheduleEntry> {
    let end_slot = observed_slot.saturating_add(window_slots as u64);
    let mut snapshot = Vec::with_capacity(window_slots);

    for entries in cached_epochs.values() {
        let start_index = entries.partition_point(|entry| entry.slot < observed_slot);
        let Some(window) = entries.get(start_index..) else {
            continue;
        };
        for entry in window {
            if entry.slot >= end_slot {
                break;
            }
            snapshot.push(*entry);
            if snapshot.len() >= window_slots {
                return snapshot;
            }
        }
    }

    snapshot
}

const fn is_slot_lag_stale(observed_slot: Option<u64>, tip_slot: u64, max_lag: u64) -> bool {
    match observed_slot {
        Some(observed_slot) => tip_slot.saturating_sub(observed_slot) > max_lag,
        None => false,
    }
}

fn control_plane_slot_spread(
    snapshot: sof_tx::adapters::TxProviderControlPlaneSnapshot,
    tip_slot: u64,
) -> u64 {
    let mut min_slot = tip_slot;
    let mut max_slot = tip_slot;

    for slot in [
        snapshot.latest_recent_blockhash_slot,
        snapshot.cluster_topology_slot,
        snapshot.leader_schedule_slot,
    ]
    .into_iter()
    .flatten()
    {
        min_slot = min_slot.min(slot);
        max_slot = max_slot.max(slot);
    }

    max_slot.saturating_sub(min_slot)
}

#[cfg(test)]
mod tests {
    use std::net::SocketAddr;

    use sof::framework::{ClusterNodeInfo, ClusterTopologyEvent};
    use solana_sdk::pubkey::Pubkey;

    use super::*;

    fn addr(port: u16) -> SocketAddr {
        SocketAddr::from(([127, 0, 0, 1], port))
    }

    #[test]
    fn direct_window_slots_scale_with_route_depth() {
        assert_eq!(direct_leader_window_slots(0), MIN_LEADER_WINDOW_SLOTS);
        assert_eq!(direct_leader_window_slots(2), MIN_LEADER_WINDOW_SLOTS);
        assert_eq!(direct_leader_window_slots(8), MAX_LEADER_WINDOW_SLOTS);
    }

    #[test]
    fn window_snapshot_starts_at_observed_slot() {
        let entries = vec![
            LeaderScheduleEntry {
                slot: 100,
                leader: Pubkey::new_unique().into(),
            },
            LeaderScheduleEntry {
                slot: 101,
                leader: Pubkey::new_unique().into(),
            },
            LeaderScheduleEntry {
                slot: 102,
                leader: Pubkey::new_unique().into(),
            },
        ];
        let snapshot = build_window_snapshot(&BTreeMap::from([(1, entries)]), 101, 2);

        assert_eq!(snapshot.len(), 2);
        assert_eq!(snapshot.first().map(|entry| entry.slot), Some(101));
        assert_eq!(snapshot.get(1).map(|entry| entry.slot), Some(102));
    }

    #[tokio::test]
    async fn safety_source_requires_recent_blockhash_topology_and_schedule() {
        let adapter = Arc::new(PluginHostTxProviderAdapter::topology_only(
            Default::default(),
        ));
        let safety = DirectLeaderScheduleSafetySource::new(Arc::clone(&adapter));
        let snapshot = safety.toxic_flow_snapshot();

        assert_eq!(
            snapshot.quality,
            TxFlowSafetyQuality::IncompleteControlPlane
        );

        let leader = Pubkey::new_unique().into();
        adapter
            .on_cluster_topology(ClusterTopologyEvent {
                source: ControlPlaneSource::Direct,
                slot: Some(200),
                epoch: Some(0),
                active_entrypoint: None,
                total_nodes: 1,
                added_nodes: Vec::new(),
                removed_pubkeys: Vec::new(),
                updated_nodes: Vec::new(),
                snapshot_nodes: vec![ClusterNodeInfo {
                    pubkey: leader,
                    wallclock: 0,
                    shred_version: 0,
                    gossip: None,
                    tpu: Some(addr(9001)),
                    tpu_quic: None,
                    tpu_forwards: None,
                    tpu_forwards_quic: None,
                    tpu_vote: None,
                    tvu: None,
                    rpc: None,
                }],
                provider_source: None,
            })
            .await;
        adapter
            .on_recent_blockhash(sof::framework::ObservedRecentBlockhashEvent {
                slot: 200,
                recent_blockhash: [9_u8; 32],
                dataset_tx_count: 1,
                provider_source: None,
            })
            .await;
        adapter
            .on_leader_schedule(LeaderScheduleEvent {
                source: ControlPlaneSource::Direct,
                slot: Some(200),
                epoch: Some(0),
                added_leaders: Vec::new(),
                removed_slots: Vec::new(),
                updated_leaders: Vec::new(),
                snapshot_leaders: vec![LeaderScheduleEntry { slot: 200, leader }],
                provider_source: None,
            })
            .await;

        let stable_snapshot = safety.toxic_flow_snapshot();
        assert_eq!(stable_snapshot.quality, TxFlowSafetyQuality::Stable);
        assert_eq!(stable_snapshot.current_state_version, Some(200));
    }
}