thruput-json-rpc-server 0.43.0

Reusable JSON-RPC server layer for Thruput
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
//! Submission pre-checks and the shared db-tip cache behind them, driven
//! through the native `sendTransaction` path.

use std::sync::Arc;
use std::time::Duration;

use tokio::sync::Mutex;

use super::*;
use crate::native::transaction::{NativeSubmitPlane, SubmissionAttempt};
use crate::tx::test_fixtures::{canonical_signed_transaction, signed_tx_bytes_with_contracts};
use crate::{RpcServerError, TipReading, ValidatedTransaction};
use jsonrpsee::types::ErrorObjectOwned;
use thruput_json_rpc_types::base58::Base58;
use thruput_json_rpc_types::block::ChainTip;
use thruput_json_rpc_types::error::native::codes;
use thruput_json_rpc_types::hex::HexBytes;
use thruput_json_rpc_types::transaction::TxHash;
use thruput_json_rpc_types::{BlockHeight, requests};
use xdr_common::VecM;

type Server<S, C> = NativeTransactionService<S, C, ()>;

fn server<S, C: ChainPlane>(submit: S, chain: C, config: RpcServerConfig) -> Server<S, C> {
    NativeTransactionService::new(submit, chain, (), config)
}

async fn submit<S: NativeSubmitPlane, C: ChainPlane>(
    server: &Server<S, C>,
    tx: Vec<u8>,
) -> Result<TxHash, ErrorObjectOwned> {
    server
        .send_transaction(requests::SendTransactionRequest {
            signed_transaction: HexBytes(tx),
        })
        .await
        .map(|result| result.transaction_hash)
}

fn canonical_tx() -> Vec<u8> {
    xdrs::txs::encode(&canonical_signed_transaction()).expect("bug: fixture should encode")
}

fn data(error: &ErrorObjectOwned) -> serde_json::Value {
    serde_json::from_str(error.data().expect("error should carry data").get()).unwrap()
}

/// A tip reading whose network config states `params`.
fn tip_reading_with_params(
    height: i64,
    params: Vec<thruput_xdr::network_config::ConfigParam>,
) -> TipReading {
    TipReading::new(
        ChainTip {
            height: BlockHeight::from(height),
            block_hash: Base58(thruput_xdr::types::SHA256Hash::from([0u8; 32])),
        },
        thruput_xdr::network_config::NetworkConfig::_0 {
            v0: thruput_xdr::network_config::NetworkConfigV0 {
                params: VecM(params),
            },
        },
    )
}

fn tip_reading(height: i64) -> TipReading {
    tip_reading_with_params(height, vec![])
}

/// A config stating only `maxNewContractsPerTx`.
fn max_contracts_params(
    max_new_contracts_per_tx: u32,
) -> Vec<thruput_xdr::network_config::ConfigParam> {
    vec![
        thruput_xdr::network_config::ConfigParam::kConfigParamMaxNewContractsPerTx {
            max_new_contracts_per_tx,
        },
    ]
}

fn rv32_contract(size: usize, fill: u8) -> thruput_xdr::contract::Contract {
    thruput_xdr::contract::Contract::kRv32 {
        rv32_contract: thruput_xdr::contract::ContractBlob {
            data: xdr_common::BytesM(vec![fill; size]),
        },
    }
}

#[derive(Clone)]
struct SubmitOnly {
    submitted: Arc<Mutex<Vec<ValidatedTransaction>>>,
}

impl SubmitOnly {
    fn new() -> Self {
        Self {
            submitted: Arc::new(Mutex::new(Vec::new())),
        }
    }
}

#[async_trait::async_trait]
impl NativeSubmitPlane for SubmitOnly {
    async fn native_submit(
        &self,
        _context: RpcRequestContext,
        tx: ValidatedTransaction,
        _attempt: SubmissionAttempt,
    ) -> Result<(), RpcServerError> {
        self.submitted.lock().await.push(tx);
        Ok(())
    }
}

#[derive(Clone)]
struct RecordingSubmit {
    contexts: Arc<Mutex<Vec<RpcRequestContext>>>,
    delay: Duration,
}

#[async_trait::async_trait]
impl NativeSubmitPlane for RecordingSubmit {
    async fn native_submit(
        &self,
        context: RpcRequestContext,
        _tx: ValidatedTransaction,
        _attempt: SubmissionAttempt,
    ) -> Result<(), RpcServerError> {
        self.contexts.lock().await.push(context);
        tokio::time::sleep(self.delay).await;
        Ok(())
    }
}

/// Implement `ChainPlane` from a `chain_tip` body; the submit path reads
/// nothing else from the chain plane.
macro_rules! chain_plane {
    ($ty:ty, |$self:ident, $context:ident| $body:expr) => {
        #[async_trait::async_trait]
        impl ChainPlane for $ty {
            async fn chain_tip(
                &$self,
                $context: RpcRequestContext,
            ) -> Result<TipReading, RpcServerError> {
                $body
            }

            async fn pending_transaction_count(
                &self,
                _context: RpcRequestContext,
            ) -> Result<u64, RpcServerError> {
                unreachable!("the submit path does not read the mempool")
            }
        }
    };
}

/// Tip at the canonical vector's `block_expiry_height` of 1,000,000.
#[derive(Clone, Copy)]
struct ExpiredTipChain;
chain_plane!(ExpiredTipChain, |self, _context| Ok(tip_reading(1_000_000)));

/// Chain plane whose `chain_tip` always fails.
#[derive(Clone, Copy)]
struct UnavailableChain;
chain_plane!(UnavailableChain, |self, _context| Err(
    RpcServerError::BackendUnavailable("scheduler down".into())
));

/// Chain plane whose `chain_tip` never completes in test time.
#[derive(Clone, Copy)]
struct StalledTipChain;
chain_plane!(StalledTipChain, |self, _context| {
    tokio::time::sleep(Duration::from_hours(1)).await;
    unreachable!("bug: stalled tip probe should have been abandoned")
});

/// Counts `chain_tip` calls and reports a fixed pre-expiry tip, optionally
/// taking `probe_duration` to answer.
#[derive(Clone)]
struct CountingTipChain {
    tip_calls: Arc<Mutex<u32>>,
    probe_duration: Duration,
}

impl CountingTipChain {
    fn new(tip_calls: Arc<Mutex<u32>>) -> Self {
        Self::slow(tip_calls, Duration::ZERO)
    }

    fn slow(tip_calls: Arc<Mutex<u32>>, probe_duration: Duration) -> Self {
        Self {
            tip_calls,
            probe_duration,
        }
    }
}

chain_plane!(CountingTipChain, |self, _context| {
    *self.tip_calls.lock().await += 1;
    if !self.probe_duration.is_zero() {
        tokio::time::sleep(self.probe_duration).await;
    }
    Ok(tip_reading(42))
});

#[derive(Clone)]
struct RecordingTipChain {
    contexts: Arc<Mutex<Vec<RpcRequestContext>>>,
    delay: Duration,
}

chain_plane!(RecordingTipChain, |self, context| {
    self.contexts.lock().await.push(context);
    tokio::time::sleep(self.delay).await;
    Ok(tip_reading(42))
});

/// Pre-expiry tip carrying chosen network config parameters; counts how
/// often it is asked.
#[derive(Clone)]
struct ConfigChain {
    params: Arc<Mutex<Vec<thruput_xdr::network_config::ConfigParam>>>,
    tip_calls: Arc<Mutex<u32>>,
}

impl ConfigChain {
    /// A tip whose config states only `maxNewContractsPerTx`.
    fn new(max_new_contracts_per_tx: u32) -> Self {
        Self::with_params(max_contracts_params(max_new_contracts_per_tx))
    }

    fn with_params(params: Vec<thruput_xdr::network_config::ConfigParam>) -> Self {
        Self {
            params: Arc::new(Mutex::new(params)),
            tip_calls: Arc::new(Mutex::new(0)),
        }
    }

    /// Replace the config the next tip reports, as an upgrade block does.
    async fn set_params(&self, params: Vec<thruput_xdr::network_config::ConfigParam>) {
        *self.params.lock().await = params;
    }
}

chain_plane!(ConfigChain, |self, _context| {
    *self.tip_calls.lock().await += 1;
    Ok(tip_reading_with_params(
        42,
        self.params.lock().await.clone(),
    ))
});

#[tokio::test]
async fn expiry_precheck_probes_tip_at_most_once_per_refresh_interval() {
    let tip_calls = Arc::new(Mutex::new(0));
    let submitter = SubmitOnly::new();
    let server = server(
        submitter.clone(),
        CountingTipChain::new(tip_calls.clone()),
        RpcServerConfig::default(),
    );

    for _ in 0..3 {
        submit(&server, canonical_tx())
            .await
            .expect("bug: unexpired transaction should submit");
    }

    assert_eq!(
        *tip_calls.lock().await,
        1,
        "tip should be probed once within the refresh interval"
    );
    assert_eq!(submitter.submitted.lock().await.len(), 3);
}

/// The reuse window must never be shorter than the interval that refreshes it.
#[tokio::test(start_paused = true)]
async fn submit_inside_the_refresh_interval_does_not_fetch_its_own_tip() {
    const INTERVAL_MS: u64 = 10_000;

    let tip_calls = Arc::new(Mutex::new(0));
    let server = server(
        SubmitOnly::new(),
        CountingTipChain::new(tip_calls.clone()),
        RpcServerConfig::default().with_tip_refresh_interval_ms(INTERVAL_MS),
    );

    submit(&server, canonical_tx())
        .await
        .expect("bug: unexpired transaction should submit");
    assert_eq!(
        *tip_calls.lock().await,
        1,
        "the first submit warms the reading"
    );

    // Last instant inside the interval: any shorter window has lapsed, and
    // the first refresh tick has not run.
    tokio::time::advance(Duration::from_millis(INTERVAL_MS - 1)).await;

    submit(&server, canonical_tx())
        .await
        .expect("bug: unexpired transaction should submit");
    assert_eq!(
        *tip_calls.lock().await,
        1,
        "a submit inside the refresh interval must reuse the reading \
         rather than fetch one on its own latency path"
    );
}

#[tokio::test]
async fn expiry_precheck_probes_tip_again_once_the_window_lapses() {
    let tip_calls = Arc::new(Mutex::new(0));
    let server = server(
        SubmitOnly::new(),
        CountingTipChain::new(tip_calls.clone()),
        RpcServerConfig::default().with_tip_refresh_interval_ms(0),
    );

    for _ in 0..2 {
        submit(&server, canonical_tx())
            .await
            .expect("bug: unexpired transaction should submit");
    }

    assert_eq!(
        *tip_calls.lock().await,
        2,
        "an expired cache entry should trigger a fresh probe"
    );
}

#[tokio::test]
async fn stalled_tip_probe_is_abandoned_and_submit_proceeds() {
    let submitter = SubmitOnly::new();
    let server = server(
        submitter.clone(),
        StalledTipChain,
        RpcServerConfig::default().with_tip_probe_timeout_ms(50),
    );

    let started = std::time::Instant::now();
    submit(&server, canonical_tx())
        .await
        .expect("bug: a stalled tip probe must not block submission");

    assert!(
        started.elapsed() < Duration::from_secs(5),
        "submit should not ride out the stalled probe, took {:?}",
        started.elapsed()
    );
    assert_eq!(submitter.submitted.lock().await.len(), 1);
}

#[tokio::test]
async fn chain_tip_failure_fails_open_and_submits() {
    let submitter = SubmitOnly::new();
    let server = server(
        submitter.clone(),
        UnavailableChain,
        RpcServerConfig::default(),
    );

    submit(&server, canonical_tx())
        .await
        .expect("bug: tip unavailability should not block submission");

    assert_eq!(submitter.submitted.lock().await.len(), 1);
}

#[tokio::test]
async fn expired_transaction_is_rejected_before_submit() {
    let submitter = SubmitOnly::new();
    let server = server(
        submitter.clone(),
        ExpiredTipChain,
        RpcServerConfig::default(),
    );

    let error = submit(&server, canonical_tx())
        .await
        .expect_err("bug: expired transaction should be rejected");

    assert_eq!(error.code(), codes::TRANSACTION_REJECTED);
    assert_eq!(data(&error)["reason"], "expired");
    assert_eq!(data(&error)["admission"], "notAttempted");
    assert!(data(&error)["transactionHash"].is_string());
    assert!(submitter.submitted.lock().await.is_empty());
}

#[tokio::test]
async fn validation_expiry_probe_and_submit_share_one_request_deadline() {
    let submit_contexts = Arc::new(Mutex::new(Vec::new()));
    let tip_contexts = Arc::new(Mutex::new(Vec::new()));
    let server = server(
        RecordingSubmit {
            contexts: submit_contexts.clone(),
            delay: Duration::from_millis(40),
        },
        RecordingTipChain {
            contexts: tip_contexts.clone(),
            delay: Duration::from_millis(25),
        },
        RpcServerConfig::default()
            .with_request_timeout_ms(50)
            .with_tip_probe_timeout_ms(500),
    );

    let error = submit(&server, canonical_tx())
        .await
        .expect_err("the combined probe and submit work should exhaust one request budget");

    assert_eq!(error.code(), codes::BACKEND_TIMEOUT);
    let tip_context = tip_contexts.lock().await[0];
    let submit_context = submit_contexts.lock().await[0];
    assert_eq!(tip_context.started_at(), submit_context.started_at());
    assert_eq!(tip_context.deadline(), submit_context.deadline());
}

#[tokio::test]
async fn expiry_probe_uses_a_shorter_deadline_without_resetting_request_start() {
    let submit_contexts = Arc::new(Mutex::new(Vec::new()));
    let tip_contexts = Arc::new(Mutex::new(Vec::new()));
    let server = server(
        RecordingSubmit {
            contexts: submit_contexts.clone(),
            delay: Duration::ZERO,
        },
        RecordingTipChain {
            contexts: tip_contexts.clone(),
            delay: Duration::ZERO,
        },
        RpcServerConfig::default()
            .with_request_timeout_ms(500)
            .with_tip_probe_timeout_ms(50),
    );

    submit(&server, canonical_tx())
        .await
        .expect("the bounded expiry probe and submission should complete");

    let tip_context = tip_contexts.lock().await[0];
    let submit_context = submit_contexts.lock().await[0];
    assert_eq!(tip_context.started_at(), submit_context.started_at());
    assert!(
        tip_context.deadline().expect("probe deadline")
            < submit_context.deadline().expect("request deadline"),
        "the nested expiry probe should receive the shorter deadline"
    );
}

#[tokio::test]
async fn config_reading_rides_on_the_tip_probe() {
    let chain = ConfigChain::new(4);
    let submitter = SubmitOnly::new();
    let server = server(submitter.clone(), chain.clone(), RpcServerConfig::default());

    for _ in 0..3 {
        submit(
            &server,
            signed_tx_bytes_with_contracts(vec![rv32_contract(8, 1)]),
        )
        .await
        .expect("bug: one attached contract is within a cap of four");
    }

    assert_eq!(
        *chain.tip_calls.lock().await,
        1,
        "count check should use the tip reading, not a second backend call"
    );
    assert_eq!(submitter.submitted.lock().await.len(), 3);
}

#[tokio::test]
async fn too_many_contracts_is_rejected_with_structured_data_before_submit() {
    let submitter = SubmitOnly::new();
    let server = server(
        submitter.clone(),
        ConfigChain::new(1),
        RpcServerConfig::default(),
    );

    let error = submit(
        &server,
        signed_tx_bytes_with_contracts(vec![rv32_contract(8, 1), rv32_contract(8, 2)]),
    )
    .await
    .expect_err("bug: two contracts should exceed a cap of one");

    assert_eq!(error.code(), codes::TRANSACTION_INVALID);
    assert_eq!(
        error.message(),
        "invalid transaction: transaction attaches 2 contracts; chain permits 1"
    );
    assert_eq!(
        data(&error),
        serde_json::json!({
            "kind": "transactionInvalid",
            "reason": "executorBoundViolation",
            "admission": "notAttempted",
            "detail": {"reason": "tooManyContracts", "attached": 2, "max": 1}
        })
    );
    assert!(submitter.submitted.lock().await.is_empty());
}

#[tokio::test]
async fn oversized_contract_is_rejected_when_tip_is_unavailable() {
    // Without a tip reading the chain-configured count check is skipped,
    // but the size cap the protocol fixes for an unstated config is still in
    // force.
    let submitter = SubmitOnly::new();
    let server = server(
        submitter.clone(),
        UnavailableChain,
        RpcServerConfig::default(),
    );
    let oversized = thruput_xdr::contract::kDefaultMaxContractSizeBytes as usize + 1;

    let error = submit(
        &server,
        signed_tx_bytes_with_contracts(vec![rv32_contract(oversized, 1)]),
    )
    .await
    .expect_err("bug: oversized contract should be rejected");
    submit(
        &server,
        signed_tx_bytes_with_contracts((1..=8).map(|fill| rv32_contract(1, fill)).collect()),
    )
    .await
    .expect("bug: without a tip reading the count check is skipped");

    assert_eq!(error.code(), codes::TRANSACTION_INVALID);
    assert_eq!(data(&error)["detail"]["reason"], "contractTooLarge");
    assert_eq!(data(&error)["detail"]["index"], 0);
    assert_eq!(submitter.submitted.lock().await.len(), 1);
}

#[tokio::test]
async fn stated_contract_size_cap_from_the_tip_bounds_submissions() {
    // The tip's config states a cap far below the protocol default; the
    // submit path must apply the stated value, not the constant.
    let chain = ConfigChain::with_params(vec![
        thruput_xdr::network_config::ConfigParam::kConfigParamMaxContractSizeBytes {
            max_contract_size_bytes: 64,
        },
    ]);
    let submitter = SubmitOnly::new();
    let server = server(submitter.clone(), chain, RpcServerConfig::default());

    let error = submit(
        &server,
        signed_tx_bytes_with_contracts(vec![rv32_contract(65, 1)]),
    )
    .await
    .expect_err("bug: a 65-byte contract should exceed the stated 64-byte cap");
    submit(
        &server,
        signed_tx_bytes_with_contracts(vec![rv32_contract(64, 1)]),
    )
    .await
    .expect("bug: a contract at the stated cap should be admitted");

    assert_eq!(error.code(), codes::TRANSACTION_INVALID);
    assert_eq!(
        data(&error),
        serde_json::json!({
            "kind": "transactionInvalid",
            "reason": "executorBoundViolation",
            "admission": "notAttempted",
            "detail": {"reason": "contractTooLarge", "index": 0, "sizeBytes": 65, "maxBytes": 64}
        })
    );
    assert_eq!(submitter.submitted.lock().await.len(), 1);
}

#[tokio::test]
async fn bound_violation_is_rechecked_against_a_fresh_tip_before_rejecting() {
    // The cached config caps attachments at one; an upgrade then raises it to
    // two. A submit inside the window must not reject on the stale cap: it
    // reads the tip again and applies the raised one.
    let chain = ConfigChain::new(1);
    let submitter = SubmitOnly::new();
    let server = server(submitter.clone(), chain.clone(), RpcServerConfig::default());
    submit(
        &server,
        signed_tx_bytes_with_contracts(vec![rv32_contract(8, 1)]),
    )
    .await
    .expect("bug: one contract is within a cap of one");
    chain.set_params(max_contracts_params(2)).await;

    let two_contracts =
        signed_tx_bytes_with_contracts(vec![rv32_contract(8, 1), rv32_contract(8, 2)]);
    submit(&server, two_contracts.clone())
        .await
        .expect("bug: the raised cap should admit two contracts");

    assert_eq!(
        *chain.tip_calls.lock().await,
        2,
        "the violation should trigger exactly one fresh probe"
    );
    let sent = submitter.submitted.lock().await;
    assert_eq!(sent.len(), 2);
    assert_eq!(
        sent[1].bytes, two_contracts,
        "the recheck must validate and submit the same bytes"
    );
}

#[tokio::test]
async fn bound_violation_on_a_fresh_reading_is_rejected_without_a_second_probe() {
    let chain = ConfigChain::new(1);
    let submitter = SubmitOnly::new();
    let server = server(submitter.clone(), chain.clone(), RpcServerConfig::default());

    let error = submit(
        &server,
        signed_tx_bytes_with_contracts(vec![rv32_contract(8, 1), rv32_contract(8, 2)]),
    )
    .await
    .expect_err("bug: two contracts should exceed a cap of one");

    assert_eq!(error.code(), codes::TRANSACTION_INVALID);
    assert_eq!(
        *chain.tip_calls.lock().await,
        1,
        "the reading this request fetched is already current"
    );
    assert!(submitter.submitted.lock().await.is_empty());
}

#[tokio::test]
async fn background_refresh_fetches_the_tip_without_submissions() {
    let chain = ConfigChain::new(4);
    let _server = server(
        SubmitOnly::new(),
        chain.clone(),
        RpcServerConfig::default().with_tip_refresh_interval_ms(10),
    );

    let started = std::time::Instant::now();
    while *chain.tip_calls.lock().await == 0 {
        assert!(
            started.elapsed() < Duration::from_secs(5),
            "background refresh should have probed the tip by now"
        );
        tokio::time::sleep(Duration::from_millis(5)).await;
    }
}

/// Refreshes must land on a fixed schedule: scheduling from the end of each
/// probe pushes every later one out by the time that probe took.
#[tokio::test(start_paused = true)]
async fn background_refresh_does_not_drift_by_the_time_a_probe_takes() {
    const INTERVAL_MS: u64 = 10_000;
    const PROBE: Duration = Duration::from_secs(1);

    // Advancing a paused clock does not itself poll the spawned task;
    // yielding afterwards lets it observe the tick.
    async fn advance(by: Duration) {
        tokio::time::advance(by).await;
        for _ in 0..8 {
            tokio::task::yield_now().await;
        }
    }

    let tip_calls = Arc::new(Mutex::new(0));
    let _server = server(
        SubmitOnly::new(),
        CountingTipChain::slow(tip_calls.clone(), PROBE),
        RpcServerConfig::default().with_tip_refresh_interval_ms(INTERVAL_MS),
    );
    // Anchor the task's schedule at zero before moving the clock.
    tokio::task::yield_now().await;

    // First tick at 10s. The probe counts on entry, then takes a second to
    // answer.
    advance(Duration::from_millis(INTERVAL_MS)).await;
    assert_eq!(
        *tip_calls.lock().await,
        1,
        "the first refresh is due at 10s"
    );
    advance(PROBE).await;

    // The second belongs at 20s. A schedule restarted when the first probe
    // answered would place it at 21s instead.
    advance(Duration::from_millis(8_900)).await;
    assert_eq!(*tip_calls.lock().await, 1, "not due before 20s");

    advance(Duration::from_millis(200)).await;
    assert_eq!(
        *tip_calls.lock().await,
        2,
        "the second refresh must land on the fixed schedule, not one probe later"
    );
}

#[tokio::test]
async fn background_refresh_stops_when_the_server_is_dropped() {
    let chain = ConfigChain::new(4);
    let server = server(
        SubmitOnly::new(),
        chain.clone(),
        RpcServerConfig::default().with_tip_refresh_interval_ms(5),
    );
    while *chain.tip_calls.lock().await == 0 {
        tokio::time::sleep(Duration::from_millis(5)).await;
    }

    drop(server);
    tokio::time::sleep(Duration::from_millis(20)).await;
    let after_drop = *chain.tip_calls.lock().await;
    tokio::time::sleep(Duration::from_millis(50)).await;

    assert_eq!(
        *chain.tip_calls.lock().await,
        after_drop,
        "no probes should run after the last server clone is dropped"
    );
}