pepper-sync 0.7.0

Pepper-sync is a crate providing a sync engine for the zcash network.
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
//! Module for handling all connections to the server

use std::{
    ops::Range,
    sync::{
        Arc,
        atomic::{self, AtomicBool},
    },
    time::Duration,
};

use tokio::sync::{mpsc::UnboundedSender, oneshot};

use zcash_primitives::transaction::{Transaction, TxId};
use zcash_protocol::{
    PoolType, ShieldedPool,
    consensus::{self, BlockHeight},
};

use zingo_netutils::{
    Indexer, TransparentIndexer,
    lightwallet_protocol::{
        BlockId, CompactBlock, GetAddressUtxosReply, RawTransaction, TreeState,
    },
};

use crate::{
    error::{MempoolError, ServerError},
    witness::Frontiers,
};

use zingo_netutils::lightwallet_protocol::SubtreeRoot;

pub(crate) mod fetch;

const MAX_RETRIES: u8 = 3;

use zingo_netutils::time::STREAM_MSG_TIMEOUT;

async fn next_stream_item<T>(
    stream: &mut tonic::Streaming<T>,
    what: &'static str,
) -> Result<Option<T>, tonic::Status> {
    match tokio::time::timeout(STREAM_MSG_TIMEOUT, stream.message()).await {
        Ok(res) => res,
        Err(_) => Err(tonic::Status::deadline_exceeded(format!(
            "{what} stream message timeout"
        ))),
    }
}

/// Fetch requests are created and sent to the [`crate::client::fetch::fetch`] task when a connection to the server is required.
///
/// Each variant includes a [`tokio::sync::oneshot::Sender`] for returning the fetched data to the requester.
#[derive(Debug)]
pub enum FetchRequest {
    /// Gets the height of the blockchain from the server.
    ChainTip(oneshot::Sender<Result<BlockId, tonic::Status>>),
    /// Gets  a compact block of the given block height.
    CompactBlock(
        oneshot::Sender<Result<CompactBlock, tonic::Status>>,
        BlockHeight,
    ),
    /// Gets the specified range of compact blocks from the server (end exclusive).
    ///
    /// Compact blocks include transparent data if the `bool` is true, otherwise only shielded data.
    CompactBlockRange(
        oneshot::Sender<Result<tonic::Streaming<CompactBlock>, tonic::Status>>,
        Range<BlockHeight>,
        bool,
    ),
    /// Gets the specified range of nullifiers from the server (end exclusive).
    NullifierRange(
        oneshot::Sender<Result<tonic::Streaming<CompactBlock>, tonic::Status>>,
        Range<BlockHeight>,
    ),
    /// Gets the tree states for a specified block height.
    TreeState(
        oneshot::Sender<Result<TreeState, tonic::Status>>,
        BlockHeight,
    ),
    /// Get a full transaction by txid.
    Transaction(oneshot::Sender<Result<RawTransaction, tonic::Status>>, TxId),
    /// Get a list of unspent transparent output metadata for a given list of transparent addresses and start height.
    #[allow(dead_code)]
    UtxoMetadata(
        oneshot::Sender<Result<Vec<GetAddressUtxosReply>, tonic::Status>>,
        (Vec<String>, BlockHeight),
    ),
    /// Get a list of transactions for a given transparent address and block range.
    TransparentAddressTxs(
        oneshot::Sender<Result<tonic::Streaming<RawTransaction>, tonic::Status>>,
        (String, Range<BlockHeight>),
    ),
    /// Get a stream of shards.
    SubtreeRoots(
        oneshot::Sender<Result<tonic::Streaming<SubtreeRoot>, tonic::Status>>,
        u32,
        i32,
        u32,
    ),
}

/// Minimum lightwallet protocol version the server must serve. v0.4.0 added transparent data to compact blocks and
/// v0.5.0 added the Ironwood pool.
const MIN_LIGHTWALLET_PROTOCOL_VERSION: (u64, u64, u64) = (0, 5, 0);

/// Checks the server's lightwallet protocol version is at least [`MIN_LIGHTWALLET_PROTOCOL_VERSION`] so that it serves
/// the transparent and Ironwood data in compact blocks required for sync.
///
/// Servers that pre-date the protocol version field do not set it and are rejected.
pub(crate) async fn check_lightwallet_protocol_version<C>(client: &mut C) -> Result<(), ServerError>
where
    C: Indexer,
{
    let version = fetch::get_lightd_info(client)
        .await?
        .lightwallet_protocol_version;

    if parse_protocol_version(&version)
        .is_some_and(|version| version >= MIN_LIGHTWALLET_PROTOCOL_VERSION)
    {
        Ok(())
    } else {
        Err(ServerError::UnsupportedProtocolVersion { version })
    }
}

/// Parses a `major.minor.patch` version with an optional `v` prefix. Any pre-release or build suffix on the patch
/// version is ignored.
fn parse_protocol_version(version: &str) -> Option<(u64, u64, u64)> {
    let mut parts = version.trim().trim_start_matches('v').splitn(3, '.');
    let major = parts.next()?.parse().ok()?;
    let minor = parts.next()?.parse().ok()?;
    let patch = parts.next()?;
    let patch = patch[..patch
        .find(|c: char| !c.is_ascii_digit())
        .unwrap_or(patch.len())]
        .parse()
        .ok()?;

    Some((major, minor, patch))
}

/// Gets the height of the blockchain from the server.
///
/// Requires [`crate::client::fetch::fetch`] to be running concurrently, connected via the `fetch_request` channel.
pub(crate) async fn get_chain_height(
    fetch_request_sender: UnboundedSender<FetchRequest>,
) -> Result<BlockHeight, ServerError> {
    let (reply_sender, reply_receiver) = oneshot::channel();
    fetch_request_sender
        .send(FetchRequest::ChainTip(reply_sender))
        .map_err(|_| ServerError::FetcherDropped)?;

    let chain_tip = reply_receiver
        .await
        .map_err(|_| ServerError::FetcherDropped)?
        .map_err(ServerError::RequestFailed)?;

    Ok(BlockHeight::from_u32(chain_tip.height as u32))
}

/// Gets the specified range of compact blocks from the server (end exclusive).
///
/// Requires [`crate::client::fetch::fetch`] to be running concurrently, connected via the `fetch_request` channel.
pub(crate) async fn get_compact_block(
    fetch_request_sender: UnboundedSender<FetchRequest>,
    block_height: BlockHeight,
) -> Result<CompactBlock, ServerError> {
    let (reply_sender, reply_receiver) = oneshot::channel();
    fetch_request_sender
        .send(FetchRequest::CompactBlock(reply_sender, block_height))
        .map_err(|_| ServerError::FetcherDropped)?;

    let block = reply_receiver
        .await
        .map_err(|_| ServerError::FetcherDropped)?
        .map_err(ServerError::RequestFailed)?;

    Ok(block)
}

/// Gets the specified range of compact blocks from the server (end exclusive).
///
/// Requires [`crate::client::fetch::fetch`] to be running concurrently, connected via the `fetch_request` channel.
pub(crate) async fn get_compact_block_range(
    fetch_request_sender: UnboundedSender<FetchRequest>,
    block_range: Range<BlockHeight>,
    include_transparent: bool,
) -> Result<tonic::Streaming<CompactBlock>, ServerError> {
    let (reply_sender, reply_receiver) = oneshot::channel();
    fetch_request_sender
        .send(FetchRequest::CompactBlockRange(
            reply_sender,
            block_range,
            include_transparent,
        ))
        .map_err(|_| ServerError::FetcherDropped)?;

    let block_stream = reply_receiver
        .await
        .map_err(|_| ServerError::FetcherDropped)?
        .map_err(ServerError::RequestFailed)?;

    Ok(block_stream)
}

/// Gets the specified range of nullifiers from the server (end exclusive).
///
/// Nullifiers are stored in compact blocks where the actions contain only nullifiers.
///
/// Requires [`crate::client::fetch::fetch`] to be running concurrently, connected via the `fetch_request` channel.
pub(crate) async fn get_nullifier_range(
    fetch_request_sender: UnboundedSender<FetchRequest>,
    block_range: Range<BlockHeight>,
) -> Result<tonic::Streaming<CompactBlock>, ServerError> {
    let (reply_sender, reply_receiver) = oneshot::channel();
    fetch_request_sender
        .send(FetchRequest::NullifierRange(reply_sender, block_range))
        .map_err(|_| ServerError::FetcherDropped)?;

    let block_stream = reply_receiver
        .await
        .map_err(|_| ServerError::FetcherDropped)?
        .map_err(ServerError::RequestFailed)?;

    Ok(block_stream)
}

/// Gets the stream of shards (subtree roots)
/// from the server.
///
/// Requires [`crate::client::fetch::fetch`] to be running concurrently, connected via the `fetch_request` channel.
pub(crate) async fn get_subtree_roots(
    fetch_request_sender: UnboundedSender<FetchRequest>,
    mut start_index: u32,
    shielded_protocol: i32,
    max_entries: u32,
) -> Result<Vec<SubtreeRoot>, ServerError> {
    let mut subtree_roots = Vec::new();
    let mut retry_count = 0;

    'retry: loop {
        let roots_before_pass = subtree_roots.len();
        let (reply_sender, reply_receiver) = oneshot::channel();

        fetch_request_sender
            .send(FetchRequest::SubtreeRoots(
                reply_sender,
                start_index,
                shielded_protocol,
                max_entries,
            ))
            .map_err(|_| ServerError::FetcherDropped)?;

        let mut subtree_root_stream = reply_receiver
            .await
            .map_err(|_| ServerError::FetcherDropped)?
            .map_err(ServerError::RequestFailed)?;

        while let Some(subtree_root) =
            match next_stream_item(&mut subtree_root_stream, "SubtreeRoots").await {
                Ok(s) => s,
                Err(e)
                    if (e.code() == tonic::Code::DeadlineExceeded
                        || e.message().contains("Unexpected EOF decoding stream."))
                        && retry_count < MAX_RETRIES =>
                {
                    tokio::time::sleep(Duration::from_secs(3)).await;
                    retry_count += 1;
                    continue 'retry;
                }
                Err(e) => return Err(e.into()),
            }
        {
            subtree_roots.push(subtree_root);
            start_index += 1;
        }

        // For an unbounded request, a clean stream end is only trusted once
        // a resume pass from the current index comes back empty: a stream
        // cut mid-flight (proxy, flow control) also ends cleanly, and
        // accepting it here silently truncates the wallet's shard tree. The
        // confirmation costs one extra empty round-trip on the happy path.
        // A bounded request (max_entries != 0) keeps single-pass semantics,
        // because resuming would fetch past the caller's cap.
        if max_entries != 0 || subtree_roots.len() == roots_before_pass {
            break 'retry;
        }
    }

    Ok(subtree_roots)
}

/// Gets the frontiers for a specified block height.
///
/// Requires [`crate::client::fetch::fetch`] to be running concurrently, connected via the `fetch_request` channel.
pub(crate) async fn get_frontiers(
    fetch_request_sender: UnboundedSender<FetchRequest>,
    consensus_parameters: &impl consensus::Parameters,
    block_height: BlockHeight,
) -> Result<Frontiers, ServerError> {
    let (reply_sender, reply_receiver) = oneshot::channel();
    fetch_request_sender
        .send(FetchRequest::TreeState(reply_sender, block_height))
        .map_err(|_| ServerError::FetcherDropped)?;

    let tree_state = reply_receiver
        .await
        .map_err(|_| ServerError::FetcherDropped)?
        .map_err(ServerError::RequestFailed)?;

    // servers omit a pool's tree state below the pool's activation height. at and above the activation height, an
    // empty tree is served as a serialized empty tree, so an omitted tree state means the server does not serve the
    // pool.
    for (pool, tree, network_upgrade) in [
        (
            ShieldedPool::Sapling,
            &tree_state.sapling_tree,
            consensus::NetworkUpgrade::Sapling,
        ),
        (
            ShieldedPool::Orchard,
            &tree_state.orchard_tree,
            consensus::NetworkUpgrade::Nu5,
        ),
        (
            ShieldedPool::Ironwood,
            &tree_state.ironwood_tree,
            consensus::NetworkUpgrade::Nu6_3,
        ),
    ] {
        if tree.is_empty() && consensus_parameters.is_nu_active(network_upgrade, block_height) {
            return Err(ServerError::TreeStateNotServed {
                pool: PoolType::Shielded(pool),
                height: block_height,
            });
        }
    }

    tree_state.try_into().map_err(ServerError::InvalidFrontier)
}

const SNAPSHOT_RETRY_HINT: &str = "retry with a fresh snapshot";

/// - Sends a transaction request to the fetcher, and sends it again after each
///   refusal that carries [`SNAPSHOT_RETRY_HINT`], at most [`MAX_RETRIES`] times.
pub(crate) async fn get_transaction_and_block_height(
    fetch_request_sender: UnboundedSender<FetchRequest>,
    consensus_parameters: &impl consensus::Parameters,
    txid: TxId,
) -> Result<(Transaction, BlockHeight), ServerError> {
    let mut retry_count = 0;
    let raw_transaction = loop {
        let (reply_sender, reply_receiver) = oneshot::channel();
        fetch_request_sender
            .send(FetchRequest::Transaction(reply_sender, txid))
            .map_err(|_| ServerError::FetcherDropped)?;

        match reply_receiver
            .await
            .map_err(|_| ServerError::FetcherDropped)?
        {
            Ok(raw_transaction) => break raw_transaction,
            Err(status)
                if status.message().contains(SNAPSHOT_RETRY_HINT) && retry_count < MAX_RETRIES =>
            {
                retry_count += 1;
            }
            Err(status) => return Err(ServerError::RequestFailed(status)),
        }
    };
    let block_height =
        BlockHeight::from_u32(u32::try_from(raw_transaction.height).expect("should be valid u32"));
    let transaction = Transaction::read(
        &raw_transaction.data[..],
        consensus::BranchId::for_height(consensus_parameters, block_height),
    )
    .map_err(ServerError::InvalidTransaction)?;

    Ok((transaction, block_height))
}

/// Gets unspent transparent output metadata for a list of `transparent addresses` from the specified `start_height`.
///
/// Requires [`crate::client::fetch::fetch`] to be running concurrently, connected via the `fetch_request` channel.
#[allow(dead_code)]
pub(crate) async fn get_utxo_metadata(
    fetch_request_sender: UnboundedSender<FetchRequest>,
    transparent_addresses: Vec<String>,
    start_height: BlockHeight,
) -> Result<Vec<GetAddressUtxosReply>, ServerError> {
    if transparent_addresses.is_empty() {
        return Ok(Vec::new());
    }

    let (reply_sender, reply_receiver) = oneshot::channel();

    fetch_request_sender
        .send(FetchRequest::UtxoMetadata(
            reply_sender,
            (transparent_addresses, start_height),
        ))
        .map_err(|_| ServerError::FetcherDropped)?;

    reply_receiver
        .await
        .map_err(|_| ServerError::FetcherDropped)?
        .map_err(ServerError::RequestFailed)
}

/// Gets transactions relevant to a given `transparent address` in the specified `block_range`.
///
/// Requires [`crate::client::fetch::fetch`] to be running concurrently, connected via the `fetch_request` channel.
pub(crate) async fn get_transparent_address_transactions(
    fetch_request_sender: UnboundedSender<FetchRequest>,
    consensus_parameters: &impl consensus::Parameters,
    transparent_address: String,
    block_range: Range<BlockHeight>,
) -> Result<Vec<(BlockHeight, Transaction)>, ServerError> {
    let mut raw_transactions: Vec<RawTransaction> = Vec::new();
    let mut retry_count = 0;

    'retry: loop {
        let (reply_sender, reply_receiver) = oneshot::channel();

        fetch_request_sender
            .send(FetchRequest::TransparentAddressTxs(
                reply_sender,
                (transparent_address.clone(), block_range.clone()),
            ))
            .map_err(|_| ServerError::FetcherDropped)?;

        let mut raw_transaction_stream = reply_receiver
            .await
            .map_err(|_| ServerError::FetcherDropped)?
            .map_err(ServerError::RequestFailed)?;

        while let Some(raw_tx) =
            match next_stream_item(&mut raw_transaction_stream, "TransparentAddressTxs").await {
                Ok(s) => s,
                Err(e)
                    if (e.code() == tonic::Code::DeadlineExceeded
                        || e.message().contains("Unexpected EOF decoding stream."))
                        && retry_count < MAX_RETRIES =>
                {
                    tokio::time::sleep(Duration::from_secs(3)).await;
                    retry_count += 1;
                    raw_transactions.clear();
                    continue 'retry;
                }
                Err(e) => return Err(e.into()),
            }
        {
            raw_transactions.push(raw_tx);
        }

        break 'retry;
    }

    let transactions = raw_transactions
        .into_iter()
        .map(|raw_transaction| {
            let block_height = BlockHeight::from_u32(
                u32::try_from(raw_transaction.height).expect("should be valid u32"),
            );

            let transaction = Transaction::read(
                &raw_transaction.data[..],
                consensus::BranchId::for_height(consensus_parameters, block_height),
            )
            .map_err(ServerError::InvalidTransaction)?;

            Ok((block_height, transaction))
        })
        .collect::<Result<Vec<(BlockHeight, Transaction)>, ServerError>>()?;

    Ok(transactions)
}

/// Gets stream of mempool transactions until the next block is mined.
///
/// Checks at intervals if `shutdown_mempool` is set to prevent hanging on awating mempool monitor handle.
pub(crate) async fn get_mempool_transaction_stream<C>(
    client: &mut C,
    shutdown_mempool: Arc<AtomicBool>,
) -> Result<tonic::Streaming<RawTransaction>, MempoolError>
where
    C: Clone + Indexer + TransparentIndexer + Sync + Send + 'static,
{
    tracing::debug!("Fetching mempool stream");
    let mut interval = tokio::time::interval(Duration::from_secs(3));
    interval.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Delay);
    interval.tick().await;
    loop {
        tokio::select! {
            mempool_stream_response = fetch::get_mempool_stream(client) => {
                return mempool_stream_response.map_err(|e| MempoolError::ServerError(ServerError::RequestFailed(e)));
            }

            _ = interval.tick() => {
                if shutdown_mempool.load(atomic::Ordering::Acquire) {
                    return Err(MempoolError::ShutdownWithoutStream);
                }
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use tokio::sync::mpsc;
    use zcash_protocol::local_consensus::LocalNetwork;
    use zingo_netutils::lightwallet_protocol::TreeState;

    use super::*;

    const SAPLING_ACTIVATION: u32 = 100;
    const ORCHARD_ACTIVATION: u32 = 200;
    const IRONWOOD_ACTIVATION: u32 = 300;

    const NETWORK: LocalNetwork = LocalNetwork {
        overwinter: Some(BlockHeight::from_u32(1)),
        sapling: Some(BlockHeight::from_u32(SAPLING_ACTIVATION)),
        blossom: Some(BlockHeight::from_u32(SAPLING_ACTIVATION)),
        heartwood: Some(BlockHeight::from_u32(SAPLING_ACTIVATION)),
        canopy: Some(BlockHeight::from_u32(SAPLING_ACTIVATION)),
        nu5: Some(BlockHeight::from_u32(ORCHARD_ACTIVATION)),
        nu6: Some(BlockHeight::from_u32(ORCHARD_ACTIVATION)),
        nu6_1: Some(BlockHeight::from_u32(ORCHARD_ACTIVATION)),
        nu6_2: Some(BlockHeight::from_u32(ORCHARD_ACTIVATION)),
        nu6_3: Some(BlockHeight::from_u32(IRONWOOD_ACTIVATION)),
        nu7: Some(BlockHeight::from_u32(IRONWOOD_ACTIVATION)),
    };

    /// Serialized empty commitment tree, as served at and above a pool's activation height.
    const EMPTY_TREE: &str = "000000";

    /// Answers tree state requests with an empty tree for every pool except `omitted_pool`, whose tree state field
    /// is left empty.
    fn spawn_fetcher(omitted_pool: ShieldedPool) -> mpsc::UnboundedSender<FetchRequest> {
        let tree = |pool: ShieldedPool| {
            if pool == omitted_pool {
                String::new()
            } else {
                EMPTY_TREE.to_string()
            }
        };
        let (sapling_tree, orchard_tree, ironwood_tree) = (
            tree(ShieldedPool::Sapling),
            tree(ShieldedPool::Orchard),
            tree(ShieldedPool::Ironwood),
        );
        let (fetch_request_sender, mut fetch_request_receiver) = mpsc::unbounded_channel();
        tokio::spawn(async move {
            while let Some(fetch_request) = fetch_request_receiver.recv().await {
                match fetch_request {
                    FetchRequest::TreeState(reply_sender, block_height) => {
                        let _ignore_error = reply_sender.send(Ok(TreeState {
                            height: u64::from(block_height),
                            hash: "00".repeat(32),
                            sapling_tree: sapling_tree.clone(),
                            orchard_tree: orchard_tree.clone(),
                            ironwood_tree: ironwood_tree.clone(),
                            ..Default::default()
                        }));
                    }
                    _ => panic!("unexpected fetch request"),
                }
            }
        });

        fetch_request_sender
    }

    /// Servers omit a pool's tree state below the pool's activation height and serve an empty tree as `000000` at
    /// the activation height, so an omitted tree state at or above the activation height means the server does not
    /// serve the pool.
    #[tokio::test]
    async fn omitted_tree_state_is_rejected_at_or_above_activation() {
        for (pool, activation_height) in [
            (ShieldedPool::Sapling, SAPLING_ACTIVATION),
            (ShieldedPool::Orchard, ORCHARD_ACTIVATION),
            (ShieldedPool::Ironwood, IRONWOOD_ACTIVATION),
        ] {
            get_frontiers(
                spawn_fetcher(pool),
                &NETWORK,
                (activation_height - 1).into(),
            )
            .await
            .unwrap();

            for height in [activation_height, activation_height + 1] {
                assert!(matches!(
                    get_frontiers(spawn_fetcher(pool), &NETWORK, height.into()).await,
                    Err(ServerError::TreeStateNotServed { pool: PoolType::Shielded(p), height: h })
                        if p == pool && h == height.into()
                ));
            }
        }
    }

    /// Answers transaction requests with each status of `replies` in turn, then
    /// panics, so a caller that asks more often than the script allows fails.
    fn spawn_transaction_fetcher(
        replies: Vec<tonic::Status>,
    ) -> mpsc::UnboundedSender<FetchRequest> {
        let (fetch_request_sender, mut fetch_request_receiver) = mpsc::unbounded_channel();
        tokio::spawn(async move {
            let mut replies = replies.into_iter();
            while let Some(fetch_request) = fetch_request_receiver.recv().await {
                match fetch_request {
                    FetchRequest::Transaction(reply_sender, _) => {
                        let status = replies.next().expect("a request beyond the script");
                        let _ignore_error = reply_sender.send(Err(status));
                    }
                    _ => panic!("unexpected fetch request"),
                }
            }
        });
        fetch_request_sender
    }

    fn snapshot_race() -> tonic::Status {
        tonic::Status::unknown(
            "Chain index error: unavailable: mempool is not coherent with the requested \
             snapshot; retry with a fresh snapshot",
        )
    }

    fn status_of(result: Result<(Transaction, BlockHeight), ServerError>) -> tonic::Status {
        match result {
            Err(ServerError::RequestFailed(status)) => status,
            other => panic!("expected a failed request, got {other:?}"),
        }
    }

    /// HYPOTHESIS: zaino answers a transaction request caught between two
    /// mempool snapshots with an `Unknown` status that asks the client to
    /// retry, and the client obeys, up to the retry ceiling. Falsified if the
    /// first such status surfaces, or if the ceiling is not enforced.
    #[tokio::test]
    async fn a_snapshot_race_is_retried_up_to_the_ceiling() {
        let within_ceiling: Vec<tonic::Status> = (0..MAX_RETRIES)
            .map(|_| snapshot_race())
            .chain([tonic::Status::not_found("gone")])
            .collect();
        let status = status_of(
            get_transaction_and_block_height(
                spawn_transaction_fetcher(within_ceiling),
                &NETWORK,
                TxId::from_bytes([0; 32]),
            )
            .await,
        );
        assert_eq!(status.code(), tonic::Code::NotFound, "{status}");

        let beyond_ceiling: Vec<tonic::Status> = (0..=MAX_RETRIES)
            .map(|_| snapshot_race())
            .chain([tonic::Status::not_found("never asked")])
            .collect();
        let status = status_of(
            get_transaction_and_block_height(
                spawn_transaction_fetcher(beyond_ceiling),
                &NETWORK,
                TxId::from_bytes([0; 32]),
            )
            .await,
        );
        assert_eq!(status.code(), tonic::Code::Unknown, "{status}");
    }

    /// HYPOTHESIS: any other failed request surfaces at once. Falsified if a
    /// second request is made.
    #[tokio::test]
    async fn an_unrelated_failure_is_not_retried() {
        let status = status_of(
            get_transaction_and_block_height(
                spawn_transaction_fetcher(vec![tonic::Status::internal("broken")]),
                &NETWORK,
                TxId::from_bytes([0; 32]),
            )
            .await,
        );
        assert_eq!(status.code(), tonic::Code::Internal, "{status}");
    }

    /// Zaino reports the protocol version with a `v` prefix. Servers that pre-date the field report an empty string.
    #[test]
    fn protocol_version_is_parsed_and_compared() {
        for (version, supported) in [
            ("v0.5.0", true),
            ("0.5.0", true),
            ("v0.5.1-rc.1", true),
            ("v1.0.0", true),
            ("v0.4.1", false),
            ("v0.4.0", false),
            ("", false),
            ("v0.5", false),
            ("unknown", false),
        ] {
            assert_eq!(
                parse_protocol_version(version)
                    .is_some_and(|version| version >= MIN_LIGHTWALLET_PROTOCOL_VERSION),
                supported,
                "{version}"
            );
        }
    }
}