zebrad 7.2.0

The Zcash Foundation's independent, consensus-compatible implementation of a Zcash node
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
//! Check the relationship between various sync timeouts and delays.

#![allow(clippy::unwrap_in_result)]

use std::sync::{
    atomic::{AtomicU8, Ordering},
    Arc,
};

use futures::future;
use tokio::time::{timeout, Duration};

use zebra_chain::{
    block::Height,
    parameters::{Network, POST_BLOSSOM_POW_TARGET_SPACING},
};
use zebra_network::constants::{
    DEFAULT_CRAWL_NEW_PEER_INTERVAL, FIND_BUSY_SERVING_PEER_WAIT_TIMEOUT, HANDSHAKE_TIMEOUT,
    INVENTORY_ROTATION_INTERVAL, REQUEST_TIMEOUT,
};
use zebra_state::ChainTipSender;

use crate::{
    components::sync::{
        ChainSync, BLOCK_DOWNLOAD_RETRY_LIMIT, BLOCK_DOWNLOAD_ROUND_TIMEOUT,
        BLOCK_DOWNLOAD_TIMEOUT, BLOCK_VERIFY_TIMEOUT, GENESIS_TIMEOUT_RETRY, SYNC_RESTART_DELAY,
        TIPS_RESPONSE_TIMEOUT,
    },
    config::ZebradConfig,
};

/// Make sure the timeout values are consistent with each other.
#[test]
fn ensure_timeouts_consistent() {
    let _init_guard = zebra_test::init();

    // This constraint clears the download pipeline during a restart
    assert!(
        SYNC_RESTART_DELAY.as_secs() > 2 * BLOCK_DOWNLOAD_TIMEOUT.as_secs(),
        "Sync restart should allow for pending and buffered requests to complete"
    );

    // This constraint avoids spurious failures due to block download timeouts
    assert!(
        BLOCK_VERIFY_TIMEOUT.as_secs()
            > SYNC_RESTART_DELAY.as_secs()
                + BLOCK_DOWNLOAD_ROUND_TIMEOUT.as_secs()
                + BLOCK_DOWNLOAD_TIMEOUT.as_secs(),
        "Block verify should allow for a block timeout, a sync restart, and some block fetches"
    );

    // The minimum recommended network speed for Zebra, in bytes per second.
    const MIN_NETWORK_SPEED_BYTES_PER_SEC: u64 = 10 * 1024 * 1024 / 8;

    // This constraint avoids spurious failures when restarting large checkpoints
    assert!(
       BLOCK_VERIFY_TIMEOUT.as_secs() > SYNC_RESTART_DELAY.as_secs() + 2 * zebra_consensus::MAX_CHECKPOINT_BYTE_COUNT / MIN_NETWORK_SPEED_BYTES_PER_SEC,
       "Block verify should allow for a full checkpoint download, a sync restart, then a full checkpoint re-download"
    );

    // This constraint avoids spurious failures after checkpointing has finished
    assert!(
        BLOCK_VERIFY_TIMEOUT.as_secs()
            > 2 * zebra_chain::parameters::NetworkUpgrade::Blossom
                .target_spacing()
                .num_seconds() as u64,
        "Block verify should allow for at least one new block to be generated and distributed"
    );

    // This constraint makes genesis retries more likely to succeed
    assert!(
        GENESIS_TIMEOUT_RETRY.as_secs() > HANDSHAKE_TIMEOUT.as_secs()
            && GENESIS_TIMEOUT_RETRY.as_secs() < BLOCK_DOWNLOAD_TIMEOUT.as_secs(),
        "Genesis retries should wait for new peers, but they shouldn't wait too long"
    );

    assert!(
        SYNC_RESTART_DELAY.as_secs() < POST_BLOSSOM_POW_TARGET_SPACING.into(),
        "a syncer tip crawl should complete before most new blocks"
    );

    // This is a compromise between two failure modes:
    // - some peers have the inventory, but they weren't ready last time we checked,
    //   so we want to retry soon
    // - all peers are missing the inventory, so we want to wait for a while before retrying
    assert!(
        INVENTORY_ROTATION_INTERVAL < SYNC_RESTART_DELAY,
        "we should expire some inventory every time the syncer resets"
    );
    assert!(
        SYNC_RESTART_DELAY < 2 * INVENTORY_ROTATION_INTERVAL,
        "we should give the syncer at least one retry attempt, \
         before we expire all inventory"
    );

    // A busy peer can finish its current wire response before the queued download is dispatched.
    assert!(
        BLOCK_DOWNLOAD_ROUND_TIMEOUT > REQUEST_TIMEOUT + BLOCK_DOWNLOAD_TIMEOUT,
        "the round deadline must leave time for both readiness and the block response"
    );

    // A find request waits for a busy serving peer, then is sent to any ready peer, all within the
    // syncer's tips response timeout. That peer needs most of the timeout to answer.
    assert!(
        FIND_BUSY_SERVING_PEER_WAIT_TIMEOUT < TIPS_RESPONSE_TIMEOUT / 3,
        "waiting for a busy serving peer should leave most of the tips response timeout \
         for the peer's answer, because each timed out tip extension round costs a full timeout"
    );

    // The default peer crawler interval should be at least
    // `HANDSHAKE_TIMEOUT` lower than all other crawler intervals.
    //
    // See `DEFAULT_CRAWL_NEW_PEER_INTERVAL` for details.
    assert!(
        DEFAULT_CRAWL_NEW_PEER_INTERVAL.as_secs() + HANDSHAKE_TIMEOUT.as_secs()
            < SYNC_RESTART_DELAY.as_secs(),
        "an address crawl and peer connections should complete before most syncer tips crawls"
    );
}

/// Test that calls to [`ChainSync::request_genesis`] are rate limited.
#[tokio::test(start_paused = true)]
async fn request_genesis_is_rate_limited() {
    let _init_guard = zebra_test::init();

    // The number of calls to `request_genesis()` we are going to be testing for
    const RETRIES_TO_RUN: u8 = 3;

    // create some counters that will be updated inside async blocks
    let peer_requests_counter = Arc::new(AtomicU8::new(0));
    let peer_requests_counter_in_service = Arc::clone(&peer_requests_counter);
    let state_requests_counter = Arc::new(AtomicU8::new(0));
    let state_requests_counter_in_service = Arc::clone(&state_requests_counter);

    // create a fake peer service that responds with `Error` to `BlockDownload` or
    // panic in any other type of request.
    let peer_service = tower::service_fn(move |request| {
        match request {
            zebra_network::Request::BlockDownload { .. } => {
                // Track the call
                peer_requests_counter_in_service.fetch_add(1, Ordering::SeqCst);
                // Respond with `Error`
                future::err("block not found".into())
            }
            _ => unreachable!("no other request is allowed"),
        }
    });

    // create a state service that respond with `None` to `Depth` or
    // panic in any other type of request.
    let state_service = tower::service_fn(move |request| {
        match request {
            zebra_state::Request::KnownBlock(_) => {
                // Track the call
                state_requests_counter_in_service.fetch_add(1, Ordering::SeqCst);
                // Respond with `None`
                future::ok(zebra_state::Response::KnownBlock(None))
            }
            _ => unreachable!("no other request is allowed"),
        }
    });

    // create a read-only state service that is never called: the genesis download fails before any
    // block body is available to check
    let read_state_service = tower::service_fn(move |_request| async move {
        unreachable!("no request to this service is allowed")
    });

    // create an empty latest chain tip
    let (_sender, latest_chain_tip, _change) = ChainTipSender::new(None, &Network::Mainnet);

    // create a verifier service that will always panic as it will never be called
    let verifier_service =
        tower::service_fn(
            move |_| async move { unreachable!("no request to this service is allowed") },
        );

    // start the sync
    let (misbehavior_tx, _misbehavior_rx) = tokio::sync::mpsc::channel(1);
    let (mut chain_sync, _) = ChainSync::new(
        &ZebradConfig::default(),
        Height(0),
        peer_service,
        verifier_service,
        state_service,
        read_state_service,
        latest_chain_tip,
        misbehavior_tx,
    );

    // Observe three genesis attempts, stopping halfway to the fourth retry.
    let retries_timeout = (RETRIES_TO_RUN - 1) as u64 * GENESIS_TIMEOUT_RETRY.as_secs()
        + GENESIS_TIMEOUT_RETRY.as_secs() / 2;
    let _ = timeout(
        Duration::from_secs(retries_timeout),
        chain_sync.request_genesis(),
    )
    .await;

    let peer_requests_counter = peer_requests_counter.load(Ordering::SeqCst);
    assert!(peer_requests_counter >= RETRIES_TO_RUN);
    // The retry limit is the constant 3, so conversion to u8 is lossless.
    assert!(peer_requests_counter <= RETRIES_TO_RUN * (BLOCK_DOWNLOAD_RETRY_LIMIT as u8) * 2);
    assert_eq!(
        state_requests_counter.load(Ordering::SeqCst),
        RETRIES_TO_RUN
    );
}

/// Repeated failed extensions back off without cancelling an unrelated live verification.
#[tokio::test(start_paused = true)]
async fn failed_tip_extensions_have_a_no_progress_deadline() {
    use std::sync::atomic::{AtomicBool, AtomicUsize};

    use futures::StreamExt;
    use zebra_chain::{
        block::{self, Block},
        chain_tip::mock::MockChainTip,
        serialization::ZcashDeserializeInto,
    };
    use zebra_test::mock_service::MockService;

    use crate::components::sync::FANOUT;

    let _init_guard = zebra_test::init();
    // Keep the pending verification outside the short post-checkpoint timeout window.
    let honest: Arc<Block> = zebra_test::vectors::BLOCK_MAINNET_202_BYTES
        .zcash_deserialize_into()
        .unwrap();
    let honest_hash = honest.hash();
    let genesis_hash = Network::Mainnet.genesis_hash();
    let a = block::Hash::from([0xA1; 32]);
    let b = block::Hash::from([0xB1; 32]);
    let started = tokio::time::Instant::now();
    let injected = Arc::new(AtomicBool::new(false));
    let requests = Arc::new(AtomicUsize::new(0));
    let network_requests = requests.clone();
    let network = tower::service_fn(move |request| {
        let block = honest.clone();
        let injected = injected.clone();
        let requests = network_requests.clone();
        async move {
            match request {
                zebra_network::Request::FindBlocks { known_blocks, .. } => {
                    requests.fetch_add(1, Ordering::SeqCst);
                    tokio::time::sleep(Duration::from_secs(1)).await;
                    let hashes = if known_blocks == vec![genesis_hash] {
                        vec![a, b]
                    } else if started.elapsed() >= BLOCK_VERIFY_TIMEOUT / 2
                        && !injected.swap(true, Ordering::SeqCst)
                    {
                        // Keep the repeated tip (a, b), but also admit an honest block late
                        // enough that its verification is still live when the round ends.
                        vec![b, honest_hash, a, b]
                    } else {
                        vec![b, a, b]
                    };
                    Ok(zebra_network::Response::BlockHashes(hashes))
                }
                zebra_network::Request::BlockDownload { hash, .. } if hash == honest_hash => {
                    Ok(zebra_network::Response::Blocks(vec![
                        zebra_network::InventoryResponse::Available((block, None)),
                    ]))
                }
                zebra_network::Request::BlockDownload { .. } => {
                    Err(crate::BoxError::from("connection reset"))
                }
                _ => unreachable!("only tip and block requests are expected"),
            }
        }
    });
    let state = tower::service_fn(move |request| {
        future::ok::<_, crate::BoxError>(match request {
            zebra_state::Request::BlockLocator => {
                zebra_state::Response::BlockLocator(vec![genesis_hash])
            }
            zebra_state::Request::KnownBlock(_) => zebra_state::Response::KnownBlock(None),
            _ => unreachable!("only known blocks and the locator are queried"),
        })
    });
    let mut verifier = MockService::build().for_unit_tests();
    let (tip, _tip_sender) = MockChainTip::new();
    let (misbehavior_tx, _misbehavior_rx) = tokio::sync::mpsc::channel(1);
    let (mut chain_sync, _status) = ChainSync::new(
        &ZebradConfig::default(),
        Height(0),
        network,
        verifier.clone(),
        state,
        MockService::build().for_unit_tests(),
        tip,
        misbehavior_tx,
    );
    timeout(
        BLOCK_VERIFY_TIMEOUT + Duration::from_secs(2),
        chain_sync.try_to_sync(),
    )
    .await
    .expect("repeated failing extensions must not run forever")
    .expect("the no-progress deadline must preserve unrelated pending downloads");
    assert!(started.elapsed() >= BLOCK_VERIFY_TIMEOUT);
    assert!(requests.load(Ordering::SeqCst) > 5 * FANOUT);

    verifier
        .expect_request_that(|request: &zebra_consensus::Request| {
            request.block().hash() == honest_hash
        })
        .await
        .respond(honest_hash);
    timeout(Duration::from_secs(1), async {
        loop {
            match chain_sync.downloads.next().await {
                Some(Ok((_, hash))) if hash == honest_hash => break,
                Some(result) => chain_sync.handle_block_response(result).unwrap(),
                None => panic!("the live honest verification must not be cancelled"),
            }
        }
    })
    .await
    .expect("the honest verification must finish after the round backs off");
    chain_sync.downloads.cancel_all();
}

/// A tip advance renews the deadline without cancelling a pending tip extension.
#[tokio::test(start_paused = true)]
async fn tip_progress_renews_an_in_progress_sync_step() {
    use zebra_chain::{block, chain_tip::mock::MockChainTip};
    use zebra_test::mock_service::MockService;

    use crate::components::sync::RestartReason;

    let _init_guard = zebra_test::init();
    let genesis_hash = Network::Mainnet.genesis_hash();
    let a = block::Hash::from([0xA1; 32]);
    let b = block::Hash::from([0xB1; 32]);
    let started = tokio::time::Instant::now();
    let network = tower::service_fn(move |request| async move {
        match request {
            zebra_network::Request::FindBlocks { known_blocks, .. } => {
                let hashes = if known_blocks == vec![genesis_hash] {
                    vec![a, b]
                } else {
                    tokio::time::sleep(Duration::from_secs(5) - Duration::from_millis(1)).await;
                    if started.elapsed() > BLOCK_VERIFY_TIMEOUT {
                        Vec::new()
                    } else {
                        vec![b, a, b]
                    }
                };
                Ok(zebra_network::Response::BlockHashes(hashes))
            }
            zebra_network::Request::BlockDownload { .. } => {
                Err(crate::BoxError::from("connection reset"))
            }
            _ => unreachable!("only tip and block requests are expected"),
        }
    });
    let state = tower::service_fn(move |request| {
        future::ok::<_, crate::BoxError>(match request {
            zebra_state::Request::BlockLocator => {
                zebra_state::Response::BlockLocator(vec![genesis_hash])
            }
            zebra_state::Request::KnownBlock(_) => zebra_state::Response::KnownBlock(None),
            _ => unreachable!("only known blocks and the locator are queried"),
        })
    });
    let (tip, tip_sender) = MockChainTip::new();
    let advance_tip = tokio::spawn(async move {
        tokio::time::sleep(BLOCK_VERIFY_TIMEOUT - Duration::from_millis(1)).await;
        tip_sender.send_best_tip_height(Height(1));
        tip_sender
    });
    let (misbehavior_tx, _misbehavior_rx) = tokio::sync::mpsc::channel(1);
    let (mut chain_sync, _status) = ChainSync::new(
        &ZebradConfig::default(),
        Height(0),
        network,
        MockService::build().for_unit_tests(),
        state,
        MockService::build().for_unit_tests(),
        tip,
        misbehavior_tx,
    );
    let reason = timeout(
        BLOCK_VERIFY_TIMEOUT + Duration::from_secs(6),
        chain_sync.try_to_sync(),
    )
    .await
    .expect("the pending extension must finish within its original response timeout")
    .unwrap();
    assert_eq!(
        reason,
        RestartReason::ExhaustedTips,
        "recent chain progress must prevent the old deadline from cancelling the extension"
    );
    assert!(started.elapsed() > BLOCK_VERIFY_TIMEOUT);
    let _tip_sender = advance_tip.await.unwrap();
    chain_sync.downloads.cancel_all();
}

/// Verifier backpressure must expire even when no sync stage is polling the download.
#[tokio::test(start_paused = true)]
async fn verifier_readiness_times_out() {
    use futures::StreamExt;
    use tower::limit::ConcurrencyLimit;

    use zebra_chain::{
        block::Block, chain_tip::mock::MockChainTip, serialization::ZcashDeserializeInto,
    };
    use zebra_test::mock_service::MockService;

    use crate::components::sync::downloads::BlockDownloadVerifyError;

    let _init_guard = zebra_test::init();
    let block: Arc<Block> = zebra_test::vectors::BLOCK_MAINNET_1_BYTES
        .zcash_deserialize_into()
        .unwrap();
    let hash = block.hash();
    let network = tower::service_fn(move |request| {
        assert_eq!(
            request,
            zebra_network::Request::BlockDownload {
                hash,
                advertiser: None,
            }
        );
        future::ok::<_, crate::BoxError>(zebra_network::Response::Blocks(vec![
            zebra_network::InventoryResponse::Available((block.clone(), None)),
        ]))
    });
    let never_ready_verifier = ConcurrencyLimit::new(MockService::build().for_unit_tests(), 0);
    let (tip, _tip_sender) = MockChainTip::new();
    let (misbehavior_tx, _misbehavior_rx) = tokio::sync::mpsc::channel(1);
    let (mut chain_sync, _status) = ChainSync::new(
        &ZebradConfig::default(),
        Height(0),
        network,
        never_ready_verifier,
        MockService::build().for_unit_tests(),
        MockService::build().for_unit_tests(),
        tip,
        misbehavior_tx,
    );

    let started = tokio::time::Instant::now();
    chain_sync
        .downloads
        .download_and_verify(hash)
        .await
        .unwrap();
    let error = timeout(
        BLOCK_VERIFY_TIMEOUT + Duration::from_secs(1),
        chain_sync.downloads.next(),
    )
    .await
    .expect("verifier readiness must have its own deadline")
    .expect("the download must report its readiness timeout")
    .expect_err("a verifier with no permits never becomes ready");
    assert!(
        matches!(
            error,
            BlockDownloadVerifyError::VerifierServiceError { ref error }
                if error.is::<tokio::time::error::Elapsed>()
        ),
        "a readiness timeout must be reported as a verifier service error: {error:?}"
    );
    assert_eq!(tokio::time::Instant::now() - started, BLOCK_VERIFY_TIMEOUT);
    assert!(chain_sync.downloads.is_empty());
}