zakura 1.8.9

Zakura, an 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
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
use eyre::eyre;
use tokio_util::sync::CancellationToken;
use zakura_chain::block;

use super::{error_exit_status, supervise_block_sync_driver, NodeTasks, StartCmd};
use crate::components::zcashd_compat;
use crate::config::ZakuradConfig;
use zakura_network::types::PeerServices;
use zakura_network::P2pStack;
use zakura_state::{PruningConfig, StorageMode};

#[tokio::test]
async fn shutdown_cannot_mask_a_ready_writer_failure() {
    let shutdown = CancellationToken::new();
    shutdown.cancel();
    let failure: zakura_state::BoxError = "terminal writer failure".into();
    // Force the interleaving where shutdown wins even though writer failure is ready.
    let result = tokio::select! {
        biased;
        _ = shutdown.cancelled() => Ok(()),
        _ = std::future::ready(()) => panic!("shutdown wins this interleaving"),
    };
    let error = StartCmd::finish_shutdown(result, Some(failure)).unwrap_err();
    assert!(error.to_string().contains("terminal writer failure"));
    assert!(StartCmd::finish_shutdown(Ok(()), None).is_ok());
    assert_eq!(
        StartCmd::finish_shutdown(Err(eyre!("task failed")), None)
            .unwrap_err()
            .to_string(),
        "task failed"
    );
}

#[test]
fn advertised_services_describe_storage_mode_with_or_without_zcashd_compat() {
    for compat in [false, true] {
        for (storage_mode, expected) in [
            (StorageMode::Archive, PeerServices::NODE_NETWORK),
            (
                StorageMode::Pruned(PruningConfig::default()),
                PeerServices::NODE_NETWORK | PeerServices::NODE_PRUNED,
            ),
        ] {
            let mut config = ZakuradConfig::default();
            config.state.storage_mode = storage_mode;
            config.zcashd_compat.enabled = compat;
            assert_eq!(StartCmd::advertised_services(&config), expected);
        }
    }
}

#[test]
fn zcashd_compat_flag_enables_mode() {
    let cmd = StartCmd {
        filters: Vec::new(),
        zcashd_compat: true,
        unsafe_low_specs: false,
    };
    let mut config = ZakuradConfig::default();
    config.zcashd_compat.manage_zcashd = false;

    let config = cmd
        .override_config(config)
        .expect("zcashd-compat override config should succeed");

    assert!(config.zcashd_compat.enabled);
}

#[test]
fn zcashd_compat_config_enables_mode() {
    let cmd = StartCmd {
        filters: Vec::new(),
        zcashd_compat: false,
        unsafe_low_specs: false,
    };
    let mut config = ZakuradConfig::default();
    config.zcashd_compat.enabled = true;
    config.zcashd_compat.manage_zcashd = false;

    let config = cmd
        .override_config(config)
        .expect("zcashd-compat override config should succeed");

    assert!(config.zcashd_compat.enabled);
}

#[test]
fn blocksync_throughput_probe_requires_v2_p2p() {
    let cmd = StartCmd {
        filters: Vec::new(),
        zcashd_compat: false,
        unsafe_low_specs: false,
    };
    let mut config = ZakuradConfig::default();
    config.network.p2p_stack = P2pStack::Legacy;
    config.sync.debug_blocksync_throughput_target_height = Some(100);

    let error = cmd
        .override_config(config)
        .expect_err("throughput probe should require v2 P2P");

    assert!(
        error.to_string().contains(
            "sync.debug_blocksync_throughput_target_height requires the Zakura P2P v2 stack"
        ),
        "unexpected error: {error}"
    );
}

#[test]
fn vct_fast_sync_requires_checkpoint_sync_at_startup() {
    let cmd = StartCmd {
        filters: Vec::new(),
        zcashd_compat: false,
        unsafe_low_specs: false,
    };
    let mut config = ZakuradConfig::default();
    config.consensus.checkpoint_sync = false;
    config.consensus.vct_fast_sync = Some(true);

    let error = cmd
        .override_config(config)
        .expect_err("startup should reject explicit VCT fast sync without checkpoint sync");

    assert!(
        error.to_string().contains(
            "invalid consensus configuration: consensus.vct_fast_sync = true requires consensus.checkpoint_sync = true"
        ),
        "unexpected error: {error}"
    );

    // A pre-VCT config that only disables checkpoint sync must keep working:
    // the unset knob is not a contradiction, it just has no effect.
    let cmd = StartCmd {
        filters: Vec::new(),
        zcashd_compat: false,
        unsafe_low_specs: false,
    };
    let mut config = ZakuradConfig::default();
    config.consensus.checkpoint_sync = false;
    config.consensus.vct_fast_sync = None;

    cmd.override_config(config)
        .expect("checkpoint_sync = false with vct_fast_sync unset is a valid config");
}

#[test]
fn block_gossip_peer_ips_require_zcashd_compat() {
    let cmd = StartCmd {
        filters: Vec::new(),
        zcashd_compat: false,
        unsafe_low_specs: false,
    };
    let mut config = ZakuradConfig::default();
    config.zcashd_compat.block_gossip_peer_ips =
        vec![std::net::IpAddr::V4(std::net::Ipv4Addr::LOCALHOST)];

    let error = cmd
        .override_config(config)
        .expect_err("block gossip peers should require zcashd-compat");

    assert!(
        error
            .to_string()
            .contains("zcashd_compat.block_gossip_peer_ips requires"),
        "error should explain the zcashd-compat requirement: {error}"
    );
}

#[test]
fn zcashd_compat_config_rejects_disabled_legacy_p2p() {
    let cmd = StartCmd {
        filters: Vec::new(),
        zcashd_compat: false,
        unsafe_low_specs: false,
    };
    let mut config = ZakuradConfig::default();
    config.zcashd_compat.enabled = true;
    config.zcashd_compat.manage_zcashd = false;
    config.network.p2p_stack = P2pStack::Zakura;

    let error = cmd
        .override_config(config)
        .expect_err("the P2P sidecar should require the legacy P2P listener");
    assert!(
        error
            .to_string()
            .contains("requires the legacy Zcash P2P stack"),
        "unexpected error: {error}"
    );
}

#[test]
fn zcashd_compat_config_rejects_peer_selection_extra_args() {
    let cmd = StartCmd {
        filters: Vec::new(),
        zcashd_compat: false,
        unsafe_low_specs: false,
    };
    let mut config = ZakuradConfig::default();
    config.zcashd_compat.enabled = true;
    config.zcashd_compat.manage_zcashd = true;
    config.zcashd_compat.zcashd_source = zcashd_compat::ConfigZcashdBinarySource::Embedded;
    config.zcashd_compat.zcashd_extra_args = vec!["-addnode=1.2.3.4".to_string()];

    let error = cmd
        .override_config(config)
        .expect_err("peer-selection extra args should be rejected");
    assert!(
        error.to_string().contains("peer-selection"),
        "unexpected error: {error}"
    );
}

#[test]
fn zcashd_compat_manage_zcashd_requires_resolvable_path() {
    let cmd = StartCmd {
        filters: Vec::new(),
        zcashd_compat: true,
        unsafe_low_specs: false,
    };
    let mut config = ZakuradConfig::default();
    config.zcashd_compat.manage_zcashd = true;
    config.zcashd_compat.zcashd_path = Some("/definitely/missing/zcashd-compat".into());

    let error = cmd
        .override_config(config)
        .expect_err("zcashd-compat override should fail for an unresolvable zcashd path");

    assert!(
        error
            .to_string()
            .contains("zcashd-compat mode could not resolve zcashd_path"),
        "unexpected error: {error}"
    );
}

#[test]
fn zcashd_compat_path_source_requires_explicit_path() {
    let cmd = StartCmd {
        filters: Vec::new(),
        zcashd_compat: true,
        unsafe_low_specs: false,
    };
    let mut config = ZakuradConfig::default();
    config.zcashd_compat.manage_zcashd = true;
    config.zcashd_compat.zcashd_source = zcashd_compat::ConfigZcashdBinarySource::Path;
    config.zcashd_compat.zcashd_path = None;

    let error = cmd
        .override_config(config)
        .expect_err("path source should require explicit zcashd_path");
    assert!(
        error.to_string().contains("zcashd_source=path"),
        "unexpected error: {error}"
    );
}

#[test]
fn zcashd_compat_embedded_source_allows_missing_local_path() {
    let cmd = StartCmd {
        filters: Vec::new(),
        zcashd_compat: true,
        unsafe_low_specs: false,
    };
    let mut config = ZakuradConfig::default();
    config.zcashd_compat.manage_zcashd = true;
    config.zcashd_compat.zcashd_source = zcashd_compat::ConfigZcashdBinarySource::Embedded;
    config.zcashd_compat.zcashd_path = None;

    cmd.override_config(config)
        .expect("embedded source should be validated at runtime, not override-time");
}

#[test]
fn zcashd_compat_config_manage_zcashd_requires_resolvable_path() {
    let cmd = StartCmd {
        filters: Vec::new(),
        zcashd_compat: false,
        unsafe_low_specs: false,
    };
    let mut config = ZakuradConfig::default();
    config.zcashd_compat.enabled = true;
    config.zcashd_compat.manage_zcashd = true;
    config.zcashd_compat.zcashd_path = Some("/definitely/missing/zcashd-compat".into());

    let error = cmd
        .override_config(config)
        .expect_err("zcashd-compat config should fail for an unresolvable zcashd path");

    assert!(
        error
            .to_string()
            .contains("zcashd-compat mode could not resolve zcashd_path"),
        "unexpected error: {error}"
    );
}

#[test]
fn zcashd_compat_supervisor_shutdown_timeout_matches_config() {
    let mut config = ZakuradConfig::default();

    config.zcashd_compat.enabled = true;
    config.zcashd_compat.manage_zcashd = true;
    config.zcashd_compat.shutdown_grace_period = std::time::Duration::from_secs(42);
    assert_eq!(
        StartCmd::zcashd_compat_supervisor_shutdown_timeout(&config),
        Some(std::time::Duration::from_secs(42) + StartCmd::ZCASHD_COMPAT_SHUTDOWN_TIMEOUT_MARGIN),
        "outer supervisor wait must exceed the child grace period so task \
         abort cannot preempt graceful termination",
    );

    config.zcashd_compat.manage_zcashd = false;
    assert_eq!(
        StartCmd::zcashd_compat_supervisor_shutdown_timeout(&config),
        None
    );

    config.zcashd_compat.enabled = false;
    config.zcashd_compat.manage_zcashd = true;
    assert_eq!(
        StartCmd::zcashd_compat_supervisor_shutdown_timeout(&config),
        None
    );
}

#[test]
fn zcashd_compat_supervisor_ok_exit_does_not_exit_zakura() {
    assert!(!StartCmd::zcashd_compat_supervisor_should_exit(Ok(Ok(()))));
}

#[test]
fn zcashd_compat_supervisor_error_does_not_exit_zakura() {
    assert!(!StartCmd::zcashd_compat_supervisor_should_exit(Ok(Err(
        eyre!("simulated zcashd supervisor runtime failure"),
    ))));
}

#[tokio::test]
async fn zcashd_compat_supervisor_panic_does_not_exit_zakura() {
    let join_err = tokio::spawn(async {
        panic!("simulated zcashd supervisor panic");
    })
    .await
    .expect_err("task should panic");

    assert!(!StartCmd::zcashd_compat_supervisor_should_exit(Err(
        join_err
    )));
}

#[test]
fn header_sync_fatal_event_returns_an_error_and_starts_shutdown() {
    let authority = zakura_header_chain::BodyWorkAuthority {
        header: zakura_header_chain::HeaderWorkAuthority {
            header_generation: zakura_header_chain::HeaderGeneration::new(3),
            branch: zakura_header_chain::BranchId::new(block::Hash([1; 32]), block::Hash([2; 32])),
        },
        verified_generation: zakura_header_chain::VerifiedGeneration::new(4),
        body_work_epoch: zakura_header_chain::BodyWorkEpoch::default(),
    };
    let owner = authority
        .bind(
            5,
            std::num::NonZeroU64::new(6).expect("the test request ID is nonzero"),
        )
        .into();
    let event = zakura_network::zakura::HeaderSyncFatalEvent {
        phase: "apply",
        owner,
        repair_generation: 7,
        target: zakura_header_chain::Frontier::new(block::Height(8), block::Hash([2; 32])),
        elapsed: std::time::Duration::from_secs(30 * 60),
    };
    let shutdown = CancellationToken::new();

    let error = StartCmd::handle_header_sync_fatal_event(event, &shutdown)
        .expect_err("a fatal VCT operation must stop the node");

    assert!(shutdown.is_cancelled(), "the node starts normal shutdown");
    assert!(error.to_string().contains("VCT local apply operation"));
}

#[tokio::test]
async fn block_sync_driver_exit_returns_an_error_and_starts_shutdown() {
    let shutdown = CancellationToken::new();
    let (fatal_tx, mut fatal_rx) = tokio::sync::mpsc::unbounded_channel();

    supervise_block_sync_driver(async {}, fatal_tx, shutdown.clone()).await;
    fatal_rx
        .recv()
        .await
        .expect("an unexpected driver exit must notify the node root");

    let error = StartCmd::handle_block_sync_driver_exit(&shutdown)
        .expect_err("a critical block-sync driver exit must stop the node");

    assert!(shutdown.is_cancelled(), "the node starts normal shutdown");
    assert!(error.to_string().contains("block-sync driver exited"));
}

#[tokio::test]
async fn block_sync_driver_panic_notifies_the_node_root() {
    let shutdown = CancellationToken::new();
    let (fatal_tx, mut fatal_rx) = tokio::sync::mpsc::unbounded_channel();
    let driver = tokio::spawn(supervise_block_sync_driver(
        async { panic!("simulated block-sync driver panic") },
        fatal_tx,
        shutdown,
    ));

    fatal_rx
        .recv()
        .await
        .expect("a driver panic must notify the node root");
    let error = driver
        .await
        .expect_err("the simulated driver panic propagates");
    assert!(error.is_panic(), "the driver task reports a panic");
}

#[tokio::test]
async fn unpolled_block_sync_driver_cancellation_notifies_the_node_root() {
    let shutdown = CancellationToken::new();
    let (fatal_tx, mut fatal_rx) = tokio::sync::mpsc::unbounded_channel();
    let driver = supervise_block_sync_driver(std::future::pending(), fatal_tx, shutdown);

    drop(driver);

    fatal_rx
        .recv()
        .await
        .expect("canceling an unpolled driver must notify the node root");
}

#[tokio::test]
async fn requested_shutdown_suppresses_block_sync_driver_fatal_event() {
    let shutdown = CancellationToken::new();
    let (fatal_tx, mut fatal_rx) = tokio::sync::mpsc::unbounded_channel();
    shutdown.cancel();

    supervise_block_sync_driver(async {}, fatal_tx, shutdown).await;

    assert_eq!(
        fatal_rx.recv().await,
        None,
        "requested shutdown closes the channel without a fatal event",
    );
}

/// Spawns a task that never finishes, and a receiver that errors once the task is aborted.
fn spawn_until_aborted() -> (
    tokio::task::JoinHandle<Result<(), eyre::Report>>,
    tokio::sync::oneshot::Receiver<()>,
) {
    let (held_by_task, aborted) = tokio::sync::oneshot::channel();
    let task = tokio::spawn(async move {
        let _held_by_task = held_by_task;
        std::future::pending().await
    });
    (task, aborted)
}

async fn assert_aborted(aborted: tokio::sync::oneshot::Receiver<()>) {
    tokio::time::timeout(std::time::Duration::from_secs(10), aborted)
        .await
        .expect("an aborted task drops its sender")
        .expect_err("the task never sends");
}

#[tokio::test]
async fn supervised_task_exit_is_reported_with_its_exit_status() {
    let mut node_tasks = NodeTasks::default();
    assert!(
        node_tasks.next_supervised_exit().await.is_none(),
        "no task is supervised yet",
    );

    let (still_running, _aborted) = spawn_until_aborted();
    node_tasks.supervise("still running", still_running, |result| result);
    node_tasks.supervise("clean exit", tokio::spawn(async {}), Ok);
    node_tasks
        .next_supervised_exit()
        .await
        .expect("the clean exit task is supervised")
        .expect("a clean exit is not a node failure");

    node_tasks.supervise(
        "failing",
        tokio::spawn(async { Err(std::io::Error::other("task failed")) }),
        error_exit_status,
    );
    let error = node_tasks
        .next_supervised_exit()
        .await
        .expect("the failing task is supervised")
        .expect_err("a task error is a node failure");
    assert_eq!(error.to_string(), "task failed");
}

#[tokio::test]
async fn server_error_does_not_fail_the_node() {
    let mut node_tasks = NodeTasks::default();
    node_tasks.supervise_server(
        "test",
        tokio::spawn(async { Err(tower::BoxError::from("server failed")) }),
    );

    node_tasks
        .next_supervised_exit()
        .await
        .expect("the server task is supervised")
        .expect("a server error stops the node without an error");
}

#[tokio::test]
#[should_panic(expected = "unexpected panic in the panicking task")]
async fn supervised_task_panic_is_propagated() {
    let mut node_tasks = NodeTasks::default();
    node_tasks.supervise(
        "panicking",
        tokio::spawn(async { panic!("task panicked") }),
        Ok,
    );

    let _ = node_tasks.next_supervised_exit().await;
}

#[tokio::test]
async fn abort_supervised_leaves_tracked_tasks_running_until_drop() {
    let mut node_tasks = NodeTasks::default();
    let (supervised, supervised_aborted) = spawn_until_aborted();
    let (tracked, mut tracked_aborted) = spawn_until_aborted();
    node_tasks.supervise("supervised", supervised, |result| result);
    node_tasks.track(&tracked);

    node_tasks.abort_supervised();
    assert_aborted(supervised_aborted).await;
    assert!(
        matches!(
            tracked_aborted.try_recv(),
            Err(tokio::sync::oneshot::error::TryRecvError::Empty)
        ),
        "a tracked task keeps running after the supervised tasks are aborted",
    );

    drop(node_tasks);
    assert_aborted(tracked_aborted).await;
}

#[tokio::test]
async fn dropping_node_tasks_aborts_supervised_tasks() {
    let mut node_tasks = NodeTasks::default();
    let (supervised, supervised_aborted) = spawn_until_aborted();
    node_tasks.supervise("supervised", supervised, |result| result);

    drop(node_tasks);
    assert_aborted(supervised_aborted).await;
}