simulator-client 0.9.0

Async WebSocket client for the Solana simulator backtest API
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
use std::{collections::VecDeque, sync::Arc, time::Duration};

use simulator_api::{
    AgentStatsReport, BacktestError, BacktestStatus, ContinueParams, ContinueToParams,
    CreateBacktestSessionRequest, DiscoveryBatchEvent, PausedEvent, SessionSummary,
};
use solana_transaction_status::EncodedConfirmedTransactionWithStatusMeta;
use thiserror::Error;
use tokio::sync::watch;
use tokio_util::sync::CancellationToken;

use super::{
    ConnectionStatus, ControlEvent, ControlHandle, ReconnectCoordinator, SessionInfo,
    SubscriptionHandle, SubscriptionNotification, spawn_account_diff_subscription_manager,
    spawn_control_manager, spawn_transaction_subscription_manager,
};
use crate::subscriptions::AccountDiffNotification;

/// Error returned by the high-level managed session wrapper.
#[derive(Debug, Error)]
pub enum ManagedSessionError {
    #[error("session create failed: {0}")]
    Create(String),

    #[error("control channel closed")]
    ControlClosed,

    #[error("control failed: {0}")]
    ControlFailed(String),

    #[error("subscription failed: {0}")]
    SubscriptionFailed(String),

    #[error("cancelled")]
    Cancelled,

    #[error("control closed while sending continue: {0}")]
    ContinueSend(String),
}

#[derive(Debug)]
pub enum ManagedEvent {
    ReadyForContinue,
    /// Server paused at a `ContinueTo` target. The session is ready for
    /// another `Continue` or `ContinueTo` from this point.
    Paused(PausedEvent),
    /// Server discovered an upcoming batch matching a registered
    /// `DiscoveryFilter`. Send `send_continue_to(slot, batch_index)` to pause
    /// immediately before it executes.
    DiscoveryBatch(DiscoveryBatchEvent),
    Slot(u64),
    Status(BacktestStatus),
    /// `agent_stats` is always `None` for [`super::ParallelSubSession`] —
    /// the multiplexed wire carries only the summary.
    Completed {
        summary: Option<SessionSummary>,
        agent_stats: Option<Vec<AgentStatsReport>>,
    },
    Error(BacktestError),
    Transaction(Box<EncodedConfirmedTransactionWithStatusMeta>),
    AccountDiff(AccountDiffNotification),
}

/// Idle backstop for the completion drain: if no trailing notification arrives
/// for this long after `Completed`, assume the data plane is stalled and stop
/// waiting. Bounds the gap *between* notifications, not total drain time, so a
/// slow-but-flowing stream (large backlog over a slow link) is never truncated.
const DEFAULT_COMPLETION_DRAIN_TIMEOUT: Duration = Duration::from_secs(60);

/// Result of [`ManagedBacktestSession::drain_until_subscriptions_complete`].
pub(super) enum DrainOutcome {
    /// Every subscription delivered its terminal (channels closed), or the
    /// session was cancelled. The drained events are complete up to the stop.
    Complete(Vec<ManagedEvent>),
    /// The idle backstop fired with subscriptions still open — trailing
    /// notifications never arrived, so the run is incomplete. Carries whatever
    /// was drained before the stall.
    Stalled(Vec<ManagedEvent>),
}

/// High-level managed backtest session.
///
/// This wrapper owns the control manager, supported subscription managers,
/// cancellation, status gating, and shutdown order. Callers only need to react
/// to [`ManagedEvent`]s and send [`ContinueParams`] after `ReadyForContinue`.
pub struct ManagedBacktestSession {
    session_info: SessionInfo,
    control: Option<ControlHandle>,
    subscriptions: Vec<SubscriptionHandle>,
    session_cancel: CancellationToken,
    /// Notifications drained on `Completed`, followed by `Completed`; served in
    /// order by `next_event`. `None` until completion.
    post_completion: Option<VecDeque<ManagedEvent>>,
    /// Surfaced after `post_completion` drains: set when the completion drain
    /// stalled (idle backstop fired with subscriptions still open), so a
    /// silently-truncated run fails loudly instead of reporting `Completed`.
    post_completion_error: Option<ManagedSessionError>,
    completion_drain_timeout: Duration,
    /// Shared across a parallel batch so this session's subscription reconnects
    /// step aside for still-streaming siblings. `None` for a standalone session.
    reconnect_coordinator: Option<Arc<ReconnectCoordinator>>,
}

impl ManagedBacktestSession {
    /// Start a managed session with an internally owned cancellation token.
    pub async fn start(
        url: String,
        api_key: String,
        create: CreateBacktestSessionRequest,
    ) -> Result<Self, ManagedSessionError> {
        Self::start_with_cancel(url, api_key, create, CancellationToken::new(), None).await
    }

    /// Start a managed session tied to a caller-owned cancellation token.
    ///
    /// Cancelling `parent_cancel` aborts startup and stops manager tasks.
    ///
    /// `reconnect_coordinator` is an optional coordinator shared across sibling
    /// sessions in a parallel batch; a dropped subscription parks on it until
    /// streaming siblings finish, then reconnects into the freed bandwidth. It
    /// is handed to the subscription managers spawned by `subscribe_*`. Pass
    /// `None` for a standalone session.
    pub async fn start_with_cancel(
        url: String,
        api_key: String,
        create: CreateBacktestSessionRequest,
        parent_cancel: CancellationToken,
        reconnect_coordinator: Option<Arc<ReconnectCoordinator>>,
    ) -> Result<Self, ManagedSessionError> {
        let session_cancel = parent_cancel.child_token();
        let mut control = spawn_control_manager(url, api_key, create, session_cancel.clone());

        let session_info = tokio::select! {
            biased;
            _ = parent_cancel.cancelled() => {
                session_cancel.cancel();
                control.join().await;
                return Err(ManagedSessionError::Cancelled);
            }
            result = control.wait_for_session() => {
                result.map_err(ManagedSessionError::Create)?
            }
        };

        Ok(Self {
            session_info,
            control: Some(control),
            subscriptions: Vec::new(),
            session_cancel,
            post_completion: None,
            post_completion_error: None,
            completion_drain_timeout: DEFAULT_COMPLETION_DRAIN_TIMEOUT,
            reconnect_coordinator,
        })
    }

    /// Metadata reported by the server when the session was created.
    pub fn session_info(&self) -> &SessionInfo {
        &self.session_info
    }

    /// Override the completion-drain idle timeout (default
    /// [`DEFAULT_COMPLETION_DRAIN_TIMEOUT`]). The drain gives up only after this
    /// long with no trailing notification; it does not cap total drain time.
    pub fn set_completion_drain_timeout(&mut self, idle_timeout: std::time::Duration) {
        self.completion_drain_timeout = idle_timeout;
    }

    /// Subscribe to transaction notifications for the configured programs.
    pub fn subscribe_transactions(&mut self, program_ids: Vec<String>) {
        self.subscriptions
            .push(spawn_transaction_subscription_manager(
                self.session_info.rpc_endpoint.clone(),
                program_ids,
                self.session_cancel.clone(),
                self.reconnect_coordinator.clone(),
            ));
    }

    /// Subscribe to account-diff notifications for the configured programs.
    pub fn subscribe_account_diffs(&mut self, program_ids: Vec<String>) {
        self.subscriptions
            .push(spawn_account_diff_subscription_manager(
                self.session_info.rpc_endpoint.clone(),
                program_ids,
                self.session_cancel.clone(),
                self.reconnect_coordinator.clone(),
            ));
    }

    /// Drain trailing notifications until every subscription delivers its
    /// end-of-stream terminal (closing its channel), the session is cancelled,
    /// or the data plane is found to have failed. See the `idle_timeout` arm for
    /// how reconnecting/parked subscriptions are distinguished from a stall.
    async fn drain_until_subscriptions_complete(
        &mut self,
        idle_timeout: std::time::Duration,
    ) -> DrainOutcome {
        drain_subscriptions_until_complete(
            &mut self.subscriptions,
            &self.session_cancel,
            idle_timeout,
        )
        .await
    }

    /// Receive the next control or subscription event.
    ///
    /// On `Completed`, trailing subscription notifications are drained and
    /// delivered before the `Completed` event.
    pub async fn next_event(&mut self) -> Result<ManagedEvent, ManagedSessionError> {
        // Serve buffered post-completion events (trailing notifications, then
        // `Completed`); the control stream is gone once they're exhausted.
        if let Some(buffered) = self.post_completion.as_mut() {
            if let Some(event) = buffered.pop_front() {
                return Ok(event);
            }
            // Buffer drained: surface a pending stall error so an incomplete
            // run fails loudly, else the control stream is simply done.
            return Err(self
                .post_completion_error
                .take()
                .unwrap_or(ManagedSessionError::ControlClosed));
        }

        if let Some(event) = try_next_subscription_event(&mut self.subscriptions) {
            return Ok(event);
        }

        // Scope the borrows to the `select!` so the completion drain below can
        // re-borrow `self`.
        let event = {
            let cancel = self.session_cancel.clone();
            let control = self
                .control
                .as_mut()
                .ok_or(ManagedSessionError::ControlClosed)?;
            let subscriptions = &mut self.subscriptions;
            tokio::select! {
                biased;
                _ = cancel.cancelled() => return Err(ManagedSessionError::Cancelled),
                event = control.events.recv() => {
                    event.map(ManagedEvent::from).ok_or(ManagedSessionError::ControlClosed)?
                }
                event = wait_any_subscription_event(subscriptions) => event,
            }
        };

        // Bind the payload so the re-emitted `Completed` below carries it.
        let ManagedEvent::Completed {
            summary,
            agent_stats,
        } = event
        else {
            return Ok(event);
        };

        // Flush trailing notifications up to each subscription's terminal,
        // delivering them before `Completed` so none are dropped.
        let (mut buffered, terminal): (VecDeque<ManagedEvent>, _) = match self
            .drain_until_subscriptions_complete(self.completion_drain_timeout)
            .await
        {
            DrainOutcome::Complete(events) => (
                events.into(),
                Ok(ManagedEvent::Completed {
                    summary,
                    agent_stats,
                }),
            ),
            // The data plane stalled before every subscription finished:
            // trailing notifications are missing, so report failure rather
            // than a silently-truncated `Completed`. Deliver whatever was
            // drained first, then surface the error once the buffer empties.
            DrainOutcome::Stalled(events) => (
                events.into(),
                Err(ManagedSessionError::SubscriptionFailed(
                    "completion drain stalled: subscriptions did not deliver their \
                     end-of-stream terminals; the captured stream is incomplete"
                        .to_string(),
                )),
            ),
        };
        match terminal {
            Ok(completed) => buffered.push_back(completed),
            Err(err) => self.post_completion_error = Some(err),
        }
        let first = buffered.pop_front();
        self.post_completion = Some(buffered);
        match first {
            Some(event) => Ok(event),
            // Nothing buffered and the run failed: surface the error now.
            None => Err(self
                .post_completion_error
                .take()
                .unwrap_or(ManagedSessionError::ControlClosed)),
        }
    }

    /// Wait until the control connection and all subscription connections are
    /// up, then send a `Continue` request.
    ///
    /// Call this after receiving [`ManagedEvent::ReadyForContinue`] or
    /// [`ManagedEvent::Paused`]. If there are no subscriptions, only the
    /// control connection is gated.
    pub async fn send_continue(
        &mut self,
        params: ContinueParams,
    ) -> Result<(), ManagedSessionError> {
        self.wait_all_up().await?;
        self.control_mut()?
            .send_continue(params)
            .await
            .map_err(|e| ManagedSessionError::ContinueSend(e.to_string()))
    }

    /// Wait until the control connection and all subscription connections are
    /// up, then send a `ContinueTo` request to step to a specific slot/batch
    /// boundary.
    ///
    /// Pair with [`ManagedEvent::DiscoveryBatch`] to drive a discovery-paced
    /// loop: receive a discovery event, send `ContinueTo(slot, batch_index)`,
    /// and wait for [`ManagedEvent::Paused`] before inspecting state.
    pub async fn send_continue_to(
        &mut self,
        params: ContinueToParams,
    ) -> Result<(), ManagedSessionError> {
        self.wait_all_up().await?;
        self.control_mut()?
            .send_continue_to(params)
            .await
            .map_err(|e| ManagedSessionError::ContinueSend(e.to_string()))
    }

    /// Cancel the session and join all manager tasks.
    pub async fn shutdown(mut self) {
        self.session_cancel.cancel();
        if let Some(control) = self.control.take() {
            control.join().await;
        }
        for sub in std::mem::take(&mut self.subscriptions) {
            let _ = sub.join.await;
        }
    }

    fn control_mut(&mut self) -> Result<&mut ControlHandle, ManagedSessionError> {
        self.control
            .as_mut()
            .ok_or(ManagedSessionError::ControlClosed)
    }

    async fn wait_all_up(&self) -> Result<(), ManagedSessionError> {
        let control = self
            .control
            .as_ref()
            .ok_or(ManagedSessionError::ControlClosed)?
            .status
            .clone();
        let subscriptions = self
            .subscriptions
            .iter()
            .map(|s| s.status.clone())
            .collect();
        wait_connections_up(control, subscriptions, &self.session_cancel).await
    }
}

/// Block until the control connection and every subscription connection report
/// `Up`, returning an error if any reports `Failed` or the token is cancelled.
/// Shared by the single-session and parallel sub-session drivers.
pub(super) async fn wait_connections_up(
    mut control: watch::Receiver<ConnectionStatus>,
    mut subscriptions: Vec<watch::Receiver<ConnectionStatus>>,
    cancel: &CancellationToken,
) -> Result<(), ManagedSessionError> {
    loop {
        let control_status = control.borrow().clone();
        if let ConnectionStatus::Failed(why) = &control_status {
            return Err(ManagedSessionError::ControlFailed(why.clone()));
        }

        let mut all_subscriptions_up = true;
        for subscription in &subscriptions {
            match &*subscription.borrow() {
                ConnectionStatus::Failed(why) => {
                    return Err(ManagedSessionError::SubscriptionFailed(why.clone()));
                }
                ConnectionStatus::Up => {}
                _ => all_subscriptions_up = false,
            }
        }

        if control_status == ConnectionStatus::Up && all_subscriptions_up {
            return Ok(());
        }

        tokio::select! {
            _ = cancel.cancelled() => return Err(ManagedSessionError::Cancelled),
            _ = control.changed() => {}
            _ = wait_any_subscription_change(&mut subscriptions) => {}
        }
    }
}

/// Drain trailing notifications until every subscription delivers its
/// end-of-stream terminal (closing its channel), `cancel` fires, or the data
/// plane is found to have stalled. Shared by the single-session and parallel
/// sub-session drivers. See
/// [`ManagedBacktestSession::drain_until_subscriptions_complete`].
///
/// `idle_timeout` bounds the gap *between* notifications, not total drain time,
/// so a slow-but-flowing stream (large backlog over a slow link) is never
/// truncated. A subscription that's `Down` — reconnecting, or parked behind the
/// reconnect coordinator waiting for siblings to free bandwidth — is *not* a
/// stall: it resumes via `replayFromSlot` or exhausts its budget and reports
/// `Failed`. So the idle gap only declares a `Stalled` outcome when an open
/// subscription is still `Up` (connected but not delivering — a genuinely hung
/// data plane); a subscription that closed in `Failed` left a truncated stream.
pub(super) async fn drain_subscriptions_until_complete(
    subscriptions: &mut [SubscriptionHandle],
    cancel: &CancellationToken,
    idle_timeout: std::time::Duration,
) -> DrainOutcome {
    let mut events = Vec::new();
    if subscriptions.is_empty() {
        return DrainOutcome::Complete(events);
    }
    loop {
        while let Some(event) = try_next_subscription_event(subscriptions) {
            events.push(event);
        }
        if subscriptions.iter().all(|s| s.notifications.is_closed()) {
            let any_failed = subscriptions
                .iter()
                .any(|s| matches!(*s.status.borrow(), ConnectionStatus::Failed(_)));
            return if any_failed {
                DrainOutcome::Stalled(events)
            } else {
                DrainOutcome::Complete(events)
            };
        }
        tokio::select! {
            biased;
            // A cancel is an intentional stop, not a data-plane failure.
            _ = cancel.cancelled() => return DrainOutcome::Complete(events),
            // Idle gap elapsed. A still-`Up` subscription should be delivering
            // but isn't — a hung data plane (stall). If the open subscriptions
            // are all reconnecting/parked (`Down`), keep waiting: the
            // coordinator is deferring them on purpose, not failing.
            _ = tokio::time::sleep(idle_timeout) => {
                let any_up = subscriptions.iter().any(|s| {
                    !s.notifications.is_closed()
                        && matches!(*s.status.borrow(), ConnectionStatus::Up)
                });
                if any_up {
                    return DrainOutcome::Stalled(events);
                }
            }
            received = recv_any_open_subscription(subscriptions) => {
                // `None` means a channel closed; the loop re-checks all-closed.
                if let Some(event) = received {
                    events.push(event);
                }
            }
        }
    }
}

impl Drop for ManagedBacktestSession {
    fn drop(&mut self) {
        self.session_cancel.cancel();
    }
}

pub(super) async fn wait_any_subscription_change(
    subscriptions: &mut [watch::Receiver<ConnectionStatus>],
) {
    if subscriptions.is_empty() {
        std::future::pending::<()>().await;
        return;
    }
    let _ =
        futures::future::select_all(subscriptions.iter_mut().map(|s| Box::pin(s.changed()))).await;
}

pub(super) async fn wait_any_subscription_event(
    subscriptions: &mut [SubscriptionHandle],
) -> ManagedEvent {
    loop {
        if let Some(event) = try_next_subscription_event(subscriptions) {
            return event;
        }

        let futures: Vec<_> = subscriptions
            .iter_mut()
            .filter(|s| !s.notifications.is_closed())
            .map(|s| Box::pin(s.notifications.recv()))
            .collect();

        if futures.is_empty() {
            std::future::pending::<()>().await;
        }

        let (notification, _, _) = futures::future::select_all(futures).await;
        if let Some(notification) = notification {
            return notification.into();
        }
    }
}

/// Await the next notification from any still-open subscription channel,
/// returning `None` when one closes. Unlike [`wait_any_subscription_event`],
/// which never resolves on closure, this lets the completion drain observe
/// per-channel end-of-stream.
pub(super) async fn recv_any_open_subscription(
    subscriptions: &mut [SubscriptionHandle],
) -> Option<ManagedEvent> {
    let futures: Vec<_> = subscriptions
        .iter_mut()
        .filter(|s| !s.notifications.is_closed())
        .map(|s| Box::pin(s.notifications.recv()))
        .collect();

    if futures.is_empty() {
        return None;
    }

    let (notification, _, _) = futures::future::select_all(futures).await;
    notification.map(Into::into)
}

pub(super) fn try_next_subscription_event(
    subscriptions: &mut [SubscriptionHandle],
) -> Option<ManagedEvent> {
    for subscription in subscriptions {
        if let Ok(notification) = subscription.notifications.try_recv() {
            return Some(notification.into());
        }
    }
    None
}

impl From<ControlEvent> for ManagedEvent {
    fn from(event: ControlEvent) -> Self {
        match event {
            ControlEvent::ReadyForContinue => Self::ReadyForContinue,
            ControlEvent::Paused(event) => Self::Paused(event),
            ControlEvent::DiscoveryBatch(event) => Self::DiscoveryBatch(event),
            ControlEvent::Slot(slot) => Self::Slot(slot),
            ControlEvent::Status(status) => Self::Status(status),
            ControlEvent::Completed {
                summary,
                agent_stats,
            } => Self::Completed {
                summary,
                agent_stats,
            },
            ControlEvent::Error(error) => Self::Error(error),
        }
    }
}

impl From<SubscriptionNotification> for ManagedEvent {
    fn from(notification: SubscriptionNotification) -> Self {
        match notification {
            SubscriptionNotification::Transaction(transaction) => Self::Transaction(transaction),
            SubscriptionNotification::AccountDiff(diff) => Self::AccountDiff(diff),
        }
    }
}