carbium 0.1.0

Async client for Carbium: Solana RPC, DAS, quote and swap, WebSocket subscriptions, Carbon Stream, webhook checks, and the account, DCA and token APIs
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
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
//! Carbon Stream, the Yellowstone (Geyser) gRPC stream on services.carbium.sh:9443, on the stock
//! yellowstone-grpc-client. The key travels as x-token metadata. `subscribe_with_reconnect` follows the published
//! rules: stop on a key, plan or request refusal, re-seed on OUT_OF_RANGE, back off from 2 s to 30 s otherwise, and
//! resume from the last slot seen when the retained floor allows.

use std::collections::HashMap;
use std::fmt;
use std::time::Duration;

use futures_util::{SinkExt, StreamExt};
use tokio::sync::mpsc;
use yellowstone_grpc_client::{ClientTlsConfig, GeyserGrpcClient, GeyserGrpcClientError};
use yellowstone_grpc_proto::prelude::{
    CommitmentLevel, GetBlockHeightResponse, GetLatestBlockhashResponse, GetSlotResponse,
    GetVersionResponse, IsBlockhashValidResponse, PongResponse, SubscribeReplayInfoResponse,
    SubscribeRequest, SubscribeRequestAccountsDataSlice, SubscribeRequestFilterAccounts,
    SubscribeRequestFilterBlocks, SubscribeRequestFilterBlocksMeta, SubscribeRequestFilterEntry,
    SubscribeRequestFilterSlots, SubscribeRequestFilterTransactions, SubscribeRequestPing,
    SubscribeUpdate, subscribe_update::UpdateOneof,
};
use yellowstone_grpc_proto::tonic::{Code, Status};

/// The stock Yellowstone message types a caller needs. The generated client and server are not offered: they would
/// expose calls the service does not serve.
///
/// ```compile_fail
/// use carbium::stream::proto::geyser_client;
/// ```
///
/// ```compile_fail
/// use carbium::stream::proto::geyser_server;
/// ```
pub mod proto {
    pub use yellowstone_grpc_proto::geyser::{
        CommitmentLevel, GetBlockHeightResponse, GetLatestBlockhashResponse, GetSlotResponse,
        GetVersionResponse, IsBlockhashValidResponse, PongResponse, SlotStatus,
        SubscribeReplayInfoResponse, SubscribeRequest, SubscribeRequestAccountsDataSlice,
        SubscribeRequestFilterAccounts, SubscribeRequestFilterAccountsFilter,
        SubscribeRequestFilterAccountsFilterLamports, SubscribeRequestFilterAccountsFilterMemcmp,
        SubscribeRequestFilterBlocks, SubscribeRequestFilterBlocksMeta,
        SubscribeRequestFilterEntry, SubscribeRequestFilterSlots,
        SubscribeRequestFilterTransactions, SubscribeRequestPing, SubscribeUpdate,
        SubscribeUpdateAccount, SubscribeUpdateAccountInfo, SubscribeUpdateBlock,
        SubscribeUpdateBlockMeta, SubscribeUpdateEntry, SubscribeUpdatePing, SubscribeUpdatePong,
        SubscribeUpdateSlot, SubscribeUpdateTransaction, SubscribeUpdateTransactionInfo,
        SubscribeUpdateTransactionStatus, subscribe_request_filter_accounts_filter,
        subscribe_request_filter_accounts_filter_lamports,
        subscribe_request_filter_accounts_filter_memcmp, subscribe_update,
    };
    pub use yellowstone_grpc_proto::solana::storage::confirmed_block;
}

use crate::error::{Code as ErrorCode, Error, ErrorKind, Result, Secret};
use crate::transport::{is_key, refuse_key_in_url};

pub const DEFAULT_STREAM_ENDPOINT: &str = "https://services.carbium.sh:9443";

/// The filter kinds the service serves, as served.json names them. Nothing else can be sent.
pub const STREAM_FILTERS: &[&str] = &[
    "accounts",
    "blocks",
    "blocksMeta",
    "entry",
    "slots",
    "transactions",
    "transactionsStatus",
];

/// The unary calls the service serves, as served.json names them. The crate offers no other.
pub const STREAM_UNARY: &[&str] = &[
    "GetBlockHeight",
    "GetLatestBlockhash",
    "GetSlot",
    "GetVersion",
    "IsBlockhashValid",
    "Ping",
    "SubscribeReplayInfo",
];

const PING_EVERY: Duration = Duration::from_secs(10);

/// Options for [`CarbiumStream::connect`].
#[derive(Clone)]
pub struct StreamOptions {
    /// The full service key (c_serv_…) with the grpc scope.
    pub api_key: String,
    /// Default https://services.carbium.sh:9443.
    pub endpoint: String,
    /// The largest message the client reads. Default 64 MiB.
    pub max_decoding_message_size: usize,
}

impl StreamOptions {
    pub fn new(api_key: impl Into<String>) -> Self {
        Self {
            api_key: api_key.into(),
            endpoint: DEFAULT_STREAM_ENDPOINT.into(),
            max_decoding_message_size: 64 * 1024 * 1024,
        }
    }
}

impl fmt::Debug for StreamOptions {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("StreamOptions")
            .field("api_key", &Secret::new(self.api_key.clone()))
            .field("endpoint", &self.endpoint)
            .finish()
    }
}

/// The served filter kinds and the request-level fields. Unset commitment means processed.
#[derive(Debug, Clone, Default, PartialEq)]
pub struct StreamFilters {
    pub accounts: HashMap<String, SubscribeRequestFilterAccounts>,
    pub slots: HashMap<String, SubscribeRequestFilterSlots>,
    pub transactions: HashMap<String, SubscribeRequestFilterTransactions>,
    pub transactions_status: HashMap<String, SubscribeRequestFilterTransactions>,
    pub blocks: HashMap<String, SubscribeRequestFilterBlocks>,
    pub blocks_meta: HashMap<String, SubscribeRequestFilterBlocksMeta>,
    pub entry: HashMap<String, SubscribeRequestFilterEntry>,
    pub commitment: Option<CommitmentLevel>,
    pub accounts_data_slice: Vec<SubscribeRequestAccountsDataSlice>,
    /// Replay from this slot: processed commitment only.
    pub from_slot: Option<u64>,
}

fn refused(what: &str) -> Error {
    Error::new(
        ErrorKind::Request,
        format!("{what} is refused by Carbon Stream: remove the field"),
    )
}

/// The SubscribeRequest for `filters`, refusing before anything is sent what the service refuses.
pub fn build_request(filters: &StreamFilters) -> Result<SubscribeRequest> {
    for (name, f) in &filters.accounts {
        if f.cuckoo_accounts_filter.is_some() {
            return Err(refused(&format!("accounts.{name}.cuckooAccountsFilter")));
        }
    }
    for (kind, map) in [
        ("transactions", &filters.transactions),
        ("transactionsStatus", &filters.transactions_status),
    ] {
        for (name, t) in map {
            if t.token_accounts.is_some() {
                return Err(refused(&format!("{kind}.{name}.tokenAccounts")));
            }
            if t.cuckoo_account_include.is_some() {
                return Err(refused(&format!("{kind}.{name}.cuckooAccountInclude")));
            }
            let bare = t.vote.is_none()
                && t.failed.is_none()
                && t.signature.is_none()
                && t.account_include.is_empty()
                && t.account_exclude.is_empty()
                && t.account_required.is_empty();
            if bare {
                return Err(Error::new(
                    ErrorKind::Request,
                    format!(
                        "{kind}.{name} is a bare transaction filter: name accountInclude, accountRequired, accountExclude, signature, vote or failed"
                    ),
                ));
            }
        }
    }
    for (name, f) in &filters.blocks {
        if f.cuckoo_account_include.is_some() {
            return Err(refused(&format!("blocks.{name}.cuckooAccountInclude")));
        }
    }
    for (name, f) in &filters.entry {
        if f.include_update_parent == Some(true) {
            return Err(refused(&format!("entry.{name}.includeUpdateParent")));
        }
    }
    if filters.from_slot.is_some()
        && filters
            .commitment
            .is_some_and(|c| c != CommitmentLevel::Processed)
    {
        return Err(Error::new(
            ErrorKind::Request,
            "fromSlot replay is available only at processed commitment",
        ));
    }
    Ok(SubscribeRequest {
        accounts: filters.accounts.clone(),
        slots: filters.slots.clone(),
        transactions: filters.transactions.clone(),
        transactions_status: filters.transactions_status.clone(),
        blocks: filters.blocks.clone(),
        blocks_meta: filters.blocks_meta.clone(),
        entry: filters.entry.clone(),
        commitment: filters.commitment.map(|c| c as i32),
        accounts_data_slice: filters.accounts_data_slice.clone(),
        ping: None,
        from_slot: filters.from_slot,
        block_footer: HashMap::new(),
    })
}

/// What to do after a stream ends.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum StreamAction {
    Stop,
    Reseed,
    Retry,
}

/// A gRPC status read the published way: its name, its message, the action and the table wait.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct StreamErrorInfo {
    pub status: &'static str,
    pub message: String,
    pub action: StreamAction,
    pub retry_after_ms: u64,
}

fn status_name(code: Code) -> &'static str {
    match code {
        Code::Ok => "OK",
        Code::Cancelled => "CANCELLED",
        Code::Unknown => "UNKNOWN",
        Code::InvalidArgument => "INVALID_ARGUMENT",
        Code::DeadlineExceeded => "DEADLINE_EXCEEDED",
        Code::NotFound => "NOT_FOUND",
        Code::AlreadyExists => "ALREADY_EXISTS",
        Code::PermissionDenied => "PERMISSION_DENIED",
        Code::ResourceExhausted => "RESOURCE_EXHAUSTED",
        Code::FailedPrecondition => "FAILED_PRECONDITION",
        Code::Aborted => "ABORTED",
        Code::OutOfRange => "OUT_OF_RANGE",
        Code::Unimplemented => "UNIMPLEMENTED",
        Code::Internal => "INTERNAL",
        Code::Unavailable => "UNAVAILABLE",
        Code::DataLoss => "DATA_LOSS",
        Code::Unauthenticated => "UNAUTHENTICATED",
    }
}

/// Stop, re-seed or retry, and the wait the published table gives, for a status that ended a stream or a call.
pub fn classify(status: &Status) -> StreamErrorInfo {
    let name = status_name(status.code());
    let message = if status.message().is_empty() {
        "stream ended".to_string()
    } else {
        status.message().to_string()
    };
    let (mut action, mut retry_after_ms) = (StreamAction::Retry, 2000);
    match status.code() {
        Code::Unauthenticated
        | Code::PermissionDenied
        | Code::InvalidArgument
        | Code::Unimplemented => action = StreamAction::Stop,
        Code::OutOfRange => action = StreamAction::Reseed,
        Code::ResourceExhausted
            if message
                .to_ascii_lowercase()
                .replace('_', " ")
                .contains("credits exhausted") =>
        {
            action = StreamAction::Stop
        }
        Code::Unavailable if message.to_ascii_lowercase().contains("still loading") => {
            retry_after_ms = 1000
        }
        _ => {}
    }
    let hinted = status
        .metadata()
        .get("retry-after")
        .and_then(|v| v.to_str().ok())
        .and_then(|v| v.trim().parse::<u64>().ok());
    if let Some(seconds) = hinted {
        retry_after_ms = retry_after_ms.max(seconds.saturating_mul(1000));
    }
    StreamErrorInfo {
        status: name,
        message,
        action,
        retry_after_ms,
    }
}

fn stream_error(method: Option<&str>, info: &StreamErrorInfo) -> Error {
    let e = Error::new(ErrorKind::Stream, &info.message)
        .with_reason(info.status)
        .with_code(ErrorCode::Text(info.status.to_string()))
        .with_retryable(info.action != StreamAction::Stop)
        .with_retry_after(Some(info.retry_after_ms));
    match method {
        Some(m) => e.with_method(m),
        None => e,
    }
}

fn client_error(method: Option<&str>, e: GeyserGrpcClientError) -> Error {
    let status = match e {
        GeyserGrpcClientError::TonicStatus(s) => s,
        GeyserGrpcClientError::TransportError(t) => Status::unavailable(t.to_string()),
    };
    stream_error(method, &classify(&status))
}

/// The Carbon Stream client: the seven unary calls and Subscribe, with or without reconnect.
pub struct CarbiumStream {
    endpoint: String,
    client: GeyserGrpcClient,
}

impl fmt::Debug for CarbiumStream {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("CarbiumStream")
            .field("endpoint", &self.endpoint)
            .field("x_token", &"c_serv_…")
            .finish()
    }
}

/// One plain stream: both halves of the call, kept open. It offers the in-stream ping and nothing that changes the
/// filters, which the service ignores after the first message. Dropping it ends the stream.
pub struct StreamHandle {
    sink: yellowstone_grpc_client::SubscribeRequestSink,
    stream: yellowstone_grpc_client::GeyserStream,
    live: SubscribeRequest,
}

impl StreamHandle {
    /// The next update, or None when the stream ended; an error ends it too.
    pub async fn next(&mut self) -> Option<Result<SubscribeUpdate>> {
        let item = self.stream.next().await?;
        Some(item.map_err(|s| stream_error(Some("Subscribe"), &classify(&s))))
    }

    /// Sends an in-stream ping; the service answers with a pong update carrying `id`.
    pub async fn ping(&mut self, id: i32) -> Result<()> {
        let ping = SubscribeRequest {
            ping: Some(SubscribeRequestPing { id }),
            ..self.live.clone()
        };
        self.sink.send(ping).await.map_err(|e| {
            stream_error(
                Some("Subscribe"),
                &classify(&Status::unavailable(e.to_string())),
            )
        })
    }
}

/// What a reconnecting subscription yields.
#[derive(Debug)]
pub enum StreamEvent {
    Update(Box<SubscribeUpdate>),
    /// A stream opened; `from_slot` is set when it resumed.
    Connected {
        attempt: u32,
        from_slot: Option<u64>,
    },
    /// The stream ended and is opened again after `delay_ms`.
    Retry {
        error: Error,
        delay_ms: u64,
        attempt: u32,
    },
    /// The service cannot replay what was missed (OUT_OF_RANGE, or the floor passed the last slot): re-read state,
    /// then the stream goes on live.
    Reseed {
        error: Option<Error>,
        last_slot: Option<u64>,
    },
    /// The stream stopped for good: a key, plan or request refusal, or None after `close`.
    Stopped(Option<Error>),
}

/// Options for [`CarbiumStream::subscribe_with_reconnect`].
#[derive(Debug, Clone, Copy)]
pub struct ReconnectOptions {
    /// Random source for the jitter, 0 ≤ r < 1.
    pub random: fn() -> f64,
    /// An in-stream ping this often, never with more than one pong outstanding. Default 10 s.
    pub ping_every: Duration,
}

impl Default for ReconnectOptions {
    fn default() -> Self {
        Self {
            random: crate::retry::default_random,
            ping_every: PING_EVERY,
        }
    }
}

/// A subscription that reconnects on its own. Dropping it, or `close`, ends it.
///
/// Updates wait in a small queue (32 frames): a slow reader slows the stream, and the service cuts a reader that takes
/// nothing for 10 s.
pub struct ReconnectingStream {
    events: mpsc::Receiver<StreamEvent>,
    task: tokio::task::JoinHandle<()>,
    closed: bool,
    stopped_sent: bool,
}

impl ReconnectingStream {
    /// The next update or status, or None once the stream stopped.
    pub async fn next(&mut self) -> Option<StreamEvent> {
        if self.closed {
            if self.stopped_sent {
                return None;
            }
            self.stopped_sent = true;
            return Some(StreamEvent::Stopped(None));
        }
        self.events.recv().await
    }

    /// Ends the stream: the next event is `Stopped(None)`, then the events end.
    pub fn close(&mut self) {
        self.task.abort();
        self.closed = true;
    }
}

impl Drop for ReconnectingStream {
    fn drop(&mut self) {
        self.task.abort();
    }
}

/// The wait before the next attempt: the larger of the table wait and the backoff, plus up to 1 s of jitter.
pub fn retry_delay_ms(retry_after_ms: u64, backoff_ms: u64, random: fn() -> f64) -> u64 {
    retry_after_ms
        .max(backoff_ms)
        .saturating_add((random() * 1000.0).floor() as u64)
}

/// The backoff after one more failure in a row: doubled, at most 30 s.
pub fn next_backoff_ms(backoff_ms: u64) -> u64 {
    backoff_ms.saturating_mul(2).min(30_000)
}

fn slot_of(update: &SubscribeUpdate) -> Option<u64> {
    match update.update_oneof.as_ref()? {
        UpdateOneof::Slot(s) => Some(s.slot),
        UpdateOneof::Transaction(t) => Some(t.slot),
        UpdateOneof::Account(a) => Some(a.slot),
        UpdateOneof::BlockMeta(b) => Some(b.slot),
        UpdateOneof::Block(b) => Some(b.slot),
        UpdateOneof::Entry(e) => Some(e.slot),
        UpdateOneof::TransactionStatus(t) => Some(t.slot),
        _ => None,
    }
}

impl CarbiumStream {
    /// Connects with the key as x-token metadata. Refuses a malformed key and a key in the endpoint.
    pub async fn connect(options: StreamOptions) -> Result<Self> {
        if !is_key(&options.api_key, "c_serv_") {
            return Err(Error::new(
                ErrorKind::Auth,
                "api_key must be the full service key (c_serv_…) with the grpc scope",
            ));
        }
        refuse_key_in_url(&options.endpoint)?;
        let builder_error = |e: yellowstone_grpc_client::GeyserGrpcBuilderError| {
            Error::new(ErrorKind::Stream, e.to_string()).with_cause(&e)
        };
        let mut builder = GeyserGrpcClient::build_from_shared(options.endpoint.clone())
            .map_err(builder_error)?
            .x_token(Some(options.api_key.clone()))
            .map_err(|_| Error::new(ErrorKind::Auth, "api_key is not a metadata value"))?
            .max_decoding_message_size(options.max_decoding_message_size)
            .connect_timeout(Duration::from_secs(10));
        if options.endpoint.starts_with("https") {
            builder = builder
                .tls_config(ClientTlsConfig::new().with_native_roots())
                .map_err(builder_error)?;
        }
        let client = builder.connect().await.map_err(builder_error)?;
        Ok(Self {
            endpoint: options.endpoint,
            client,
        })
    }

    pub async fn get_slot(&self, commitment: Option<CommitmentLevel>) -> Result<GetSlotResponse> {
        self.client
            .clone()
            .get_slot(commitment)
            .await
            .map_err(|e| client_error(Some("GetSlot"), e))
    }
    pub async fn get_block_height(
        &self,
        commitment: Option<CommitmentLevel>,
    ) -> Result<GetBlockHeightResponse> {
        self.client
            .clone()
            .get_block_height(commitment)
            .await
            .map_err(|e| client_error(Some("GetBlockHeight"), e))
    }
    pub async fn get_latest_blockhash(
        &self,
        commitment: Option<CommitmentLevel>,
    ) -> Result<GetLatestBlockhashResponse> {
        self.client
            .clone()
            .get_latest_blockhash(commitment)
            .await
            .map_err(|e| client_error(Some("GetLatestBlockhash"), e))
    }
    pub async fn is_blockhash_valid(
        &self,
        blockhash: &str,
        commitment: Option<CommitmentLevel>,
    ) -> Result<IsBlockhashValidResponse> {
        self.client
            .clone()
            .is_blockhash_valid(blockhash.to_string(), commitment)
            .await
            .map_err(|e| client_error(Some("IsBlockhashValid"), e))
    }
    pub async fn get_version(&self) -> Result<GetVersionResponse> {
        self.client
            .clone()
            .get_version()
            .await
            .map_err(|e| client_error(Some("GetVersion"), e))
    }
    pub async fn ping(&self, count: i32) -> Result<PongResponse> {
        self.client
            .clone()
            .ping(count)
            .await
            .map_err(|e| client_error(Some("Ping"), e))
    }
    pub async fn subscribe_replay_info(&self) -> Result<SubscribeReplayInfoResponse> {
        self.client
            .clone()
            .subscribe_replay_info()
            .await
            .map_err(|e| client_error(Some("SubscribeReplayInfo"), e))
    }

    /// One plain stream with no reconnect: the request is checked first.
    pub async fn subscribe(&self, filters: &StreamFilters) -> Result<StreamHandle> {
        let request = build_request(filters)?;
        let live = SubscribeRequest {
            from_slot: None,
            ..request.clone()
        };
        let (sink, stream) = self
            .client
            .clone()
            .subscribe_with_request(Some(request))
            .await
            .map_err(|e| client_error(Some("Subscribe"), e))?;
        Ok(StreamHandle { sink, stream, live })
    }

    /// A stream that follows the published rules (see the module docs). The request is checked before any call.
    /// Updates and status changes come in order from `next`.
    pub fn subscribe_with_reconnect(
        &self,
        filters: &StreamFilters,
        options: ReconnectOptions,
    ) -> Result<ReconnectingStream> {
        let base = build_request(filters)?;
        let (tx, events) = mpsc::channel(32);
        let task = tokio::spawn(run(self.client.clone(), base, options, tx));
        Ok(ReconnectingStream {
            events,
            task,
            closed: false,
            stopped_sent: false,
        })
    }
}

async fn run(
    client: GeyserGrpcClient,
    base: SubscribeRequest,
    options: ReconnectOptions,
    tx: mpsc::Sender<StreamEvent>,
) {
    let can_resume = base
        .commitment
        .is_none_or(|c| c == CommitmentLevel::Processed as i32);
    let live = SubscribeRequest {
        from_slot: None,
        ..base.clone()
    };
    let mut last_slot = base.from_slot;
    let mut request = base;
    let (mut backoff_ms, mut attempt) = (2000u64, 0u32);
    loop {
        let mut got_data = false;
        let mut ended: Option<Status> = None;
        let opened = client
            .clone()
            .subscribe_with_request(Some(request.clone()))
            .await;
        match opened {
            Err(GeyserGrpcClientError::TonicStatus(s)) => ended = Some(s),
            Err(GeyserGrpcClientError::TransportError(t)) => {
                ended = Some(Status::unavailable(t.to_string()))
            }
            Ok((mut sink, mut stream)) => {
                if tx
                    .send(StreamEvent::Connected {
                        attempt,
                        from_slot: request.from_slot,
                    })
                    .await
                    .is_err()
                {
                    return;
                }
                let mut pinger = tokio::time::interval_at(
                    tokio::time::Instant::now() + options.ping_every,
                    options.ping_every,
                );
                let (mut ping_id, mut pong_pending) = (0i32, false);
                loop {
                    tokio::select! {
                        _ = pinger.tick() => {
                            if pong_pending {
                                continue; // the control queue holds 8 messages: never pile pings up
                            }
                            pong_pending = true;
                            ping_id += 1;
                            let ping = SubscribeRequest { ping: Some(SubscribeRequestPing { id: ping_id }), ..live.clone() };
                            if let Err(e) = sink.send(ping).await {
                                ended = Some(Status::unavailable(e.to_string()));
                                break;
                            }
                        }
                        item = stream.next() => match item {
                            Some(Ok(update)) => match update.update_oneof {
                                Some(UpdateOneof::Pong(_)) => pong_pending = false,
                                Some(UpdateOneof::Ping(_)) => {}
                                _ => {
                                    got_data = true;
                                    if let Some(slot) = slot_of(&update) {
                                        last_slot = Some(slot);
                                    }
                                    if tx.send(StreamEvent::Update(Box::new(update))).await.is_err() {
                                        return;
                                    }
                                }
                            },
                            Some(Err(status)) => {
                                ended = Some(status);
                                break;
                            }
                            None => break,
                        },
                    }
                }
            }
        }
        let info = classify(&ended.unwrap_or_else(|| Status::unknown("stream ended")));
        let error = stream_error(None, &info);
        if info.action == StreamAction::Stop {
            let _ = tx.send(StreamEvent::Stopped(Some(error))).await;
            return;
        }
        if got_data {
            backoff_ms = 2000;
            attempt = 0;
        }
        attempt += 1;
        if info.action == StreamAction::Reseed {
            if tx
                .send(StreamEvent::Reseed {
                    error: Some(error),
                    last_slot,
                })
                .await
                .is_err()
            {
                return;
            }
            request = live.clone();
            continue;
        }
        let delay_ms = retry_delay_ms(info.retry_after_ms, backoff_ms, options.random);
        backoff_ms = next_backoff_ms(backoff_ms);
        if tx
            .send(StreamEvent::Retry {
                error,
                delay_ms,
                attempt,
            })
            .await
            .is_err()
        {
            return;
        }
        tokio::time::sleep(Duration::from_millis(delay_ms)).await;
        request = live.clone();
        if let (true, Some(slot)) = (can_resume, last_slot) {
            let floor = client
                .clone()
                .subscribe_replay_info()
                .await
                .map(|r| r.first_available.unwrap_or(0));
            match floor {
                Ok(first) if slot >= first => request.from_slot = Some(slot),
                Ok(_) => {
                    let gone = tx
                        .send(StreamEvent::Reseed {
                            error: None,
                            last_slot,
                        })
                        .await
                        .is_err();
                    if gone {
                        return;
                    }
                }
                Err(e) => {
                    // the floor cannot be read: the gap cannot be checked, so the caller re-reads before going live
                    let error = Some(client_error(Some("SubscribeReplayInfo"), e));
                    if tx
                        .send(StreamEvent::Reseed { error, last_slot })
                        .await
                        .is_err()
                    {
                        return;
                    }
                }
            }
        }
    }
}