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
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
//! Property tests for known hashes in discovery responses.

use std::collections::{HashMap, HashSet};

use proptest::{collection::btree_set, prelude::*};
use tokio::runtime::Builder;

use zebra_chain::block::Hash;
use zebra_state as zs;

use super::{assert_downloaded_hashes, known_hash_strategy::KnownHashStrategy, TestScenario};
use crate::components::sync::CheckedTip;

proptest! {
    /// Mixed discovery responses download exactly their unknown hashes without aborting.
    #[test]
    fn known_suffix_preserves_unknown_downloads(
        entries in hashes_with_known_flags(KnownHashStrategy::new(".*uK.*", 10)),
    ) {
        let _test_guard = zebra_test::init();

        let runtime = Builder::new_current_thread().enable_all().build().unwrap();

        runtime.block_on(async {
            let mut test = TestScenario::new();
            let hashes = Vec::from_iter(entries.iter().map(|(hash, _)| *hash));
            let remaining_hashes = HashSet::<Hash>::from_iter(
                entries.iter().filter_map(|(hash, is_known)| (!is_known).then_some(*hash)),
            );
            let is_known_by_hash = HashMap::<_, _>::from_iter(entries);

            let result = test
                .obtain_tips_with_hashes(hashes, |hash, _| {
                    is_known_by_hash[&hash].then_some(zs::KnownBlock::BestChain)
                })
                .await;

            prop_assert!(result.is_ok(), "a known suffix must not abort discovery: {result:?}");
            assert_downloaded_hashes(&mut test, remaining_hashes).await;

            Ok(())
        })?;
    }

    /// A later known-successor response preserves the earlier eligible pair for any prefix.
    #[test]
    fn known_successor_does_not_replace_earlier_tip(
        entries in hashes_with_known_flags(KnownHashStrategy::new(".*uuK", 10)),
    ) {
        let _test_guard = zebra_test::init();
        let runtime = Builder::new_current_thread().enable_all().build().unwrap();

        runtime.block_on(async {
            let mut test = TestScenario::new();
            let hashes = Vec::from_iter(entries.iter().map(|(hash, _)| *hash));
            let eligible_hashes = hashes[..hashes.len() - 1].to_vec();
            let expected_tip = CheckedTip {
                tip: hashes[hashes.len() - 3],
                expected_next: hashes[hashes.len() - 2],
            };
            let unknown_hashes = Vec::from_iter(
                entries.iter().filter_map(|(hash, is_known)| (!is_known).then_some(*hash)),
            );
            let is_known_by_hash = HashMap::<_, _>::from_iter(entries);

            test.obtain_tips_with_responses(vec![eligible_hashes, hashes], |hash, _| {
                is_known_by_hash[&hash].then_some(zs::KnownBlock::BestChain)
            })
            .await
            .expect("an ineligible response must not abort discovery");

            prop_assert_eq!(
                &test.sync.prospective_tips,
                &HashSet::from_iter([expected_tip]),
                "the original eligible tip must survive the later ineligible response",
            );
            assert_downloaded_hashes(&mut test, unknown_hashes).await;

            Ok(())
        })?;
    }

    /// An earlier known-successor response preserves the later eligible pair for any prefix.
    #[test]
    fn known_successor_does_not_suppress_later_tip(
        entries in hashes_with_known_flags(KnownHashStrategy::new(".*uuK", 10)),
    ) {
        let _test_guard = zebra_test::init();
        let runtime = Builder::new_current_thread().enable_all().build().unwrap();

        runtime.block_on(async {
            let mut test = TestScenario::new();
            let hashes = Vec::from_iter(entries.iter().map(|(hash, _)| *hash));
            let eligible_hashes = hashes[..hashes.len() - 1].to_vec();
            let expected_tip = CheckedTip {
                tip: hashes[hashes.len() - 3],
                expected_next: hashes[hashes.len() - 2],
            };
            let unknown_hashes = Vec::from_iter(
                entries.iter().filter_map(|(hash, is_known)| (!is_known).then_some(*hash)),
            );
            let is_known_by_hash = HashMap::<_, _>::from_iter(entries);

            test.obtain_tips_with_responses(vec![hashes, eligible_hashes], |hash, _| {
                is_known_by_hash[&hash].then_some(zs::KnownBlock::BestChain)
            })
            .await
            .expect("an ineligible response must not abort discovery");

            prop_assert_eq!(
                &test.sync.prospective_tips,
                &HashSet::from_iter([expected_tip]),
                "the later eligible tip must survive the earlier ineligible response",
            );
            assert_downloaded_hashes(&mut test, unknown_hashes).await;

            Ok(())
        })?;
    }

    /// Eligible overlapping responses keep the further tip for any length and either order.
    #[test]
    fn eligible_overlap_keeps_further_tip_in_both_orders(
        hashes in btree_set(any::<[u8; 32]>(), 3..=10)
            .prop_map(|hashes| Vec::from_iter(hashes.into_iter().map(Hash)))
            .prop_shuffle(),
        longer_response_first in any::<bool>(),
    ) {
        let _test_guard = zebra_test::init();
        let runtime = Builder::new_current_thread().enable_all().build().unwrap();

        runtime.block_on(async {
            let expected_tip = CheckedTip {
                tip: hashes[hashes.len() - 2],
                expected_next: hashes[hashes.len() - 1],
            };

            let mut test = TestScenario::new();
            let mut responses = vec![hashes[..hashes.len() - 1].to_vec(), hashes.clone()];
            if longer_response_first {
                responses.reverse();
            }

            test.obtain_tips_with_responses(responses, |_, _| None)
                .await
                .expect("eligible overlapping responses must not abort discovery");

            prop_assert_eq!(
                &test.sync.prospective_tips,
                &HashSet::from_iter([expected_tip]),
                "eligible merging must preserve the further tip, longer response first: {}",
                longer_response_first,
            );
            assert_downloaded_hashes(&mut test, hashes.iter().copied()).await;

            Ok(())
        })?;
    }

    /// A later known-locator response preserves the earlier eligible pair for any prefix.
    #[test]
    fn known_locator_does_not_replace_earlier_tip(
        entries in hashes_with_known_flags(KnownHashStrategy::new(".*uuKu", 10)),
    ) {
        let _test_guard = zebra_test::init();
        let runtime = Builder::new_current_thread().enable_all().build().unwrap();

        runtime.block_on(async {
            let mut test = TestScenario::new();
            let hashes = Vec::from_iter(entries.iter().map(|(hash, _)| *hash));
            let eligible_hashes = hashes[..hashes.len() - 2].to_vec();
            let expected_tip = CheckedTip {
                tip: hashes[hashes.len() - 4],
                expected_next: hashes[hashes.len() - 3],
            };
            let unknown_hashes = Vec::from_iter(
                entries.iter().filter_map(|(hash, is_known)| (!is_known).then_some(*hash)),
            );
            let is_known_by_hash = HashMap::<_, _>::from_iter(entries);

            test.obtain_tips_with_responses(vec![eligible_hashes, hashes], |hash, _| {
                is_known_by_hash[&hash].then_some(zs::KnownBlock::BestChain)
            })
            .await
            .expect("an ineligible response must not abort discovery");

            prop_assert_eq!(
                &test.sync.prospective_tips,
                &HashSet::from_iter([expected_tip]),
                "a known locator must not let a later response erase an eligible tip",
            );
            assert_downloaded_hashes(&mut test, unknown_hashes).await;

            Ok(())
        })?;
    }

    /// An earlier known-locator response preserves the later eligible pair for any prefix.
    #[test]
    fn known_locator_does_not_suppress_later_tip(
        entries in hashes_with_known_flags(KnownHashStrategy::new(".*uuKu", 10)),
    ) {
        let _test_guard = zebra_test::init();
        let runtime = Builder::new_current_thread().enable_all().build().unwrap();

        runtime.block_on(async {
            let mut test = TestScenario::new();
            let hashes = Vec::from_iter(entries.iter().map(|(hash, _)| *hash));
            let eligible_hashes = hashes[..hashes.len() - 2].to_vec();
            let expected_tip = CheckedTip {
                tip: hashes[hashes.len() - 4],
                expected_next: hashes[hashes.len() - 3],
            };
            let unknown_hashes = Vec::from_iter(
                entries.iter().filter_map(|(hash, is_known)| (!is_known).then_some(*hash)),
            );
            let is_known_by_hash = HashMap::<_, _>::from_iter(entries);

            test.obtain_tips_with_responses(vec![hashes, eligible_hashes], |hash, _| {
                is_known_by_hash[&hash].then_some(zs::KnownBlock::BestChain)
            })
            .await
            .expect("an ineligible response must not abort discovery");

            prop_assert_eq!(
                &test.sync.prospective_tips,
                &HashSet::from_iter([expected_tip]),
                "a known locator must not let an earlier response suppress an eligible tip",
            );
            assert_downloaded_hashes(&mut test, unknown_hashes).await;

            Ok(())
        })?;
    }

    /// A known trailing successor is discarded regardless of the preceding hashes.
    #[test]
    fn known_successor_is_not_a_prospective_tip(
        entries in hashes_with_known_flags(KnownHashStrategy::new(".*uK", 10)),
    ) {
        let _test_guard = zebra_test::init();

        let runtime = Builder::new_current_thread().enable_all().build().unwrap();

        runtime.block_on(async {
            let mut test = TestScenario::new();
            let hashes = Vec::from_iter(entries.iter().map(|(hash, _)| *hash));
            let is_known_by_hash = HashMap::<_, _>::from_iter(entries);

            test.obtain_tips_with_hashes(hashes, |hash, _| {
                is_known_by_hash[&hash].then_some(zs::KnownBlock::BestChain)
            })
            .await
            .unwrap();

            prop_assert!(
                test.sync.prospective_tips.is_empty(),
                "a known successor must not guide extension",
            );

            Ok(())
        })?;
    }

    /// Removing a known trailing locator never joins separated unknown hashes into a tip.
    #[test]
    fn filtering_does_not_manufacture_tip_pairs(
        entries in hashes_with_known_flags(KnownHashStrategy::new(".*u.*Ku", 10)),
    ) {
        let _test_guard = zebra_test::init();

        let runtime = Builder::new_current_thread().enable_all().build().unwrap();

        runtime.block_on(async {
            let mut test = TestScenario::new();
            let hashes = Vec::from_iter(entries.iter().map(|(hash, _)| *hash));
            let unknown_count = entries.iter().filter(|(_, is_known)| !is_known).count();
            let is_known_by_hash = HashMap::<_, _>::from_iter(entries);

            test.obtain_tips_with_hashes(hashes, |hash, _| {
                is_known_by_hash[&hash].then_some(zs::KnownBlock::BestChain)
            })
            .await
            .unwrap();

            prop_assert_eq!(test.sync.downloads.in_flight(), unknown_count);
            prop_assert!(
                test.sync.prospective_tips.is_empty(),
                "neither the known locator nor a manufactured pair may guide extension",
            );

            Ok(())
        })?;
    }

    /// Filtering a mixed prefix preserves the exact original unknown trailing pair.
    #[test]
    fn filtering_preserves_unknown_trailing_pair(
        entries in hashes_with_known_flags(KnownHashStrategy::new(".*u.*K.*uu", 10)),
    ) {
        let _test_guard = zebra_test::init();

        let runtime = Builder::new_current_thread().enable_all().build().unwrap();

        runtime.block_on(async {
            let mut test = TestScenario::new();
            let hashes = Vec::from_iter(entries.iter().map(|(hash, _)| *hash));
            let tip = hashes[hashes.len() - 2];
            let expected_next = hashes[hashes.len() - 1];
            let unknown_count = entries.iter().filter(|(_, is_known)| !is_known).count();
            let is_known_by_hash = HashMap::<_, _>::from_iter(entries);

            test.obtain_tips_with_hashes(hashes, |hash, _| {
                is_known_by_hash[&hash].then_some(zs::KnownBlock::Finalized)
            })
            .await
            .unwrap();

            prop_assert_eq!(test.sync.downloads.in_flight(), unknown_count);
            prop_assert_eq!(
                test.sync.prospective_tips,
                HashSet::from_iter([CheckedTip { tip, expected_next }]),
            );

            Ok(())
        })?;
    }

    /// Progress on the first candidate preserves every unknown suffix download.
    ///
    /// An empty suffix also checks that filtering all candidates succeeds.
    #[test]
    fn progress_on_first_candidate_preserves_unknown_suffix(
        hashes in btree_set(any::<[u8; 32]>(), 1..=10)
            .prop_map(|hashes| Vec::from_iter(hashes.into_iter().map(Hash)))
            .prop_shuffle(),
    ) {
        let _test_guard = zebra_test::init();

        let runtime = Builder::new_current_thread().enable_all().build().unwrap();

        runtime.block_on(async {
            let mut test = TestScenario::new();
            let transitioning_hash = hashes[0];
            let remaining_hashes = HashSet::<Hash>::from_iter(hashes.iter().skip(1).copied());

            // The first candidate is queried by both the initial scan and filtering.
            test.obtain_tips_with_hashes(hashes, |hash, observation| {
                (hash == transitioning_hash && observation > 1)
                    .then_some(zs::KnownBlock::WriteChannel)
            })
            .await
            .expect("concurrent state progress must not abort discovery");

            if remaining_hashes.is_empty() {
                prop_assert!(test.sync.prospective_tips.is_empty());
            }

            assert_downloaded_hashes(&mut test, remaining_hashes).await;

            Ok(())
        })?;
    }

    /// Progress after an unknown prefix preserves all other candidate downloads.
    ///
    /// The initial scan stops in the prefix, so the transitioning candidate is
    /// first observed in state during filtering, when it is already known.
    #[test]
    fn progress_after_unknown_prefix_preserves_other_downloads(
        (prefix_length, hashes) in (1usize..=5, 0usize..=5)
            .prop_flat_map(|(prefix_length, suffix_length)| {
                let hash_count = prefix_length + 1 + suffix_length;
                let hashes = btree_set(any::<[u8; 32]>(), hash_count)
                    .prop_map(|hashes| Vec::from_iter(hashes.into_iter().map(Hash)))
                    .prop_shuffle();

                (Just(prefix_length), hashes)
            }),
    ) {
        let _test_guard = zebra_test::init();

        let runtime = Builder::new_current_thread().enable_all().build().unwrap();

        runtime.block_on(async {
            let mut test = TestScenario::new();
            let transitioning_hash = hashes[prefix_length];
            let mut remaining_hashes = HashSet::<Hash>::from_iter(hashes.iter().copied());
            remaining_hashes.remove(&transitioning_hash);

            // Unlike the first candidate, this hash has no initial-scan lookup.
            test.obtain_tips_with_hashes(hashes, |hash, _| {
                (hash == transitioning_hash).then_some(zs::KnownBlock::WriteChannel)
            })
            .await
            .expect("concurrent state progress must not abort discovery");

            assert_downloaded_hashes(&mut test, remaining_hashes).await;
        });
    }

    /// Progress after filtering the first candidate must not abort any downloads.
    ///
    /// The candidate is unknown during the initial scan and filtering. Only
    /// a redundant third lookup observes it committed and exposes the old abort.
    #[test]
    fn progress_after_filtering_first_candidate_preserves_all_downloads(
        hashes in btree_set(any::<[u8; 32]>(), 1..=10)
            .prop_map(|hashes| Vec::from_iter(hashes.into_iter().map(Hash)))
            .prop_shuffle(),
    ) {
        let _test_guard = zebra_test::init();

        let runtime = Builder::new_current_thread().enable_all().build().unwrap();

        runtime.block_on(async {
            let mut test = TestScenario::new();
            let transitioning_hash = hashes[0];
            let remaining_hashes = HashSet::<Hash>::from_iter(hashes.iter().copied());

            test.obtain_tips_with_hashes(hashes, |hash, observation| {
                (hash == transitioning_hash && observation > 2)
                    .then_some(zs::KnownBlock::BestChain)
            })
            .await
            .expect("progress after filtering must not abort discovery");

            assert_downloaded_hashes(&mut test, remaining_hashes).await;
        });
    }

    /// Progress after filtering a later candidate must not abort any downloads.
    ///
    /// The initial scan stops in the unknown prefix. The candidate's first
    /// lookup is filtering; a redundant second lookup exposes the old abort.
    #[test]
    fn progress_after_filtering_later_candidate_preserves_all_downloads(
        (prefix_length, hashes) in (1usize..=5, 0usize..=5)
            .prop_flat_map(|(prefix_length, suffix_length)| {
                let hash_count = prefix_length + 1 + suffix_length;
                let hashes = btree_set(any::<[u8; 32]>(), hash_count)
                    .prop_map(|hashes| Vec::from_iter(hashes.into_iter().map(Hash)))
                    .prop_shuffle();

                (Just(prefix_length), hashes)
            }),
    ) {
        let _test_guard = zebra_test::init();

        let runtime = Builder::new_current_thread().enable_all().build().unwrap();

        runtime.block_on(async {
            let mut test = TestScenario::new();
            let transitioning_hash = hashes[prefix_length];
            let remaining_hashes = HashSet::<Hash>::from_iter(hashes.iter().copied());

            test.obtain_tips_with_hashes(hashes, |hash, observation| {
                (hash == transitioning_hash && observation > 1)
                    .then_some(zs::KnownBlock::BestChain)
            })
            .await
            .expect("progress after filtering must not abort discovery");

            assert_downloaded_hashes(&mut test, remaining_hashes).await;
        });
    }

    /// State-queued responses retain every download and their original trailing pair.
    #[test]
    fn queued_hashes_remain_downloadable(
        hashes in btree_set(any::<[u8; 32]>(), 2..=10)
            .prop_map(|hashes| Vec::from_iter(hashes.into_iter().map(Hash)))
            .prop_shuffle(),
    ) {
        let _test_guard = zebra_test::init();

        let runtime = Builder::new_current_thread().enable_all().build().unwrap();

        runtime.block_on(async {
            let mut test = TestScenario::new();
            let hash_count = hashes.len();
            let tip = hashes[hash_count - 2];
            let expected_next = hashes[hash_count - 1];

            test.obtain_tips_with_hashes(hashes, |_, _| Some(zs::KnownBlock::Queue))
                .await
                .unwrap();

            prop_assert_eq!(test.sync.downloads.in_flight(), hash_count);
            prop_assert_eq!(
                test.sync.prospective_tips,
                HashSet::from_iter([CheckedTip { tip, expected_next }]),
            );

            Ok(())
        })?;
    }
}

/// Pairs unique, shuffled hashes with flags in the order produced by `flags`.
fn hashes_with_known_flags(
    flags: impl Strategy<Value = Vec<bool>>,
) -> impl Strategy<Value = Vec<(Hash, bool)>> {
    flags.prop_flat_map(|flags| {
        btree_set(any::<[u8; 32]>(), flags.len())
            .prop_map(|hashes| Vec::from_iter(hashes.into_iter().map(Hash)))
            .prop_shuffle()
            .prop_map(move |hashes| Vec::from_iter(hashes.into_iter().zip(flags.iter().copied())))
    })
}