zakura-state 11.0.0

State contextual verification and storage code for the Zakura node. Internal crate, published to support cargo install zakura
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
//! Fixed test vectors for block queues.

use std::sync::Arc;

use tokio::sync::oneshot;

use zakura_chain::{block::Block, serialization::ZcashDeserializeInto};
use zakura_test::prelude::*;

use crate::{
    arbitrary::Prepare,
    service::queued_blocks::{
        QueuedBlocks, QueuedSemanticallyVerified, SentHashes, MAX_QUEUED_BLOCKS,
    },
    tests::FakeChainHelper,
    CommitBlockError, CommitSemanticallyVerifiedError,
};

// Quick helper trait for making queued blocks with throw away channels
trait IntoQueued {
    fn into_queued(self) -> QueuedSemanticallyVerified;
}

impl IntoQueued for Arc<Block> {
    fn into_queued(self) -> QueuedSemanticallyVerified {
        let (rsp_tx, _) = oneshot::channel();
        (self.prepare(), rsp_tx, None, 0)
    }
}

#[test]
fn dequeue_gives_right_children() -> Result<()> {
    let _init_guard = zakura_test::init();

    let block1: Arc<Block> =
        zakura_test::vectors::BLOCK_MAINNET_419200_BYTES.zcash_deserialize_into()?;
    let child1: Arc<Block> =
        zakura_test::vectors::BLOCK_MAINNET_419201_BYTES.zcash_deserialize_into()?;
    let child2 = block1.make_fake_child();

    let parent = block1.header.previous_block_hash;

    let mut queue = QueuedBlocks::default();
    // Empty to start
    assert_eq!(0, queue.blocks.len());
    assert_eq!(0, queue.by_parent.len());
    assert_eq!(0, queue.by_height.len());
    assert_eq!(0, queue.known_utxos.len());

    // Inserting the first block gives us 1 in each table, and some UTXOs
    queue.queue(block1.clone().into_queued());
    assert_eq!(1, queue.blocks.len());
    assert_eq!(1, queue.by_parent.len());
    assert_eq!(1, queue.by_height.len());
    assert_eq!(2, queue.known_utxos.len());

    // The second gives us another in each table because its a child of the first,
    // and a lot of UTXOs
    queue.queue(child1.clone().into_queued());
    assert_eq!(2, queue.blocks.len());
    assert_eq!(2, queue.by_parent.len());
    assert_eq!(2, queue.by_height.len());
    assert_eq!(632, queue.known_utxos.len());

    // The 3rd only increments blocks, because it is also a child of the
    // first block, so for the second and third tables it gets added to the
    // existing HashSet value
    queue.queue(child2.clone().into_queued());
    assert_eq!(3, queue.blocks.len());
    assert_eq!(2, queue.by_parent.len());
    assert_eq!(2, queue.by_height.len());
    assert_eq!(634, queue.known_utxos.len());

    // Dequeueing the first block removes 1 block from each list
    let children = queue.dequeue_children(parent);
    assert_eq!(1, children.len());
    assert_eq!(block1, children[0].0.block);
    assert_eq!(2, queue.blocks.len());
    assert_eq!(1, queue.by_parent.len());
    assert_eq!(1, queue.by_height.len());
    assert_eq!(632, queue.known_utxos.len());

    // Dequeueing the children of the first block removes both of the other
    // blocks, and empties all lists
    let parent = children[0].0.block.hash();
    let children = queue.dequeue_children(parent);
    assert_eq!(2, children.len());
    assert!(children
        .iter()
        .any(|(block, _, _, _)| block.hash == child1.hash()));
    assert!(children
        .iter()
        .any(|(block, _, _, _)| block.hash == child2.hash()));
    assert_eq!(0, queue.blocks.len());
    assert_eq!(0, queue.by_parent.len());
    assert_eq!(0, queue.by_height.len());
    assert_eq!(0, queue.known_utxos.len());

    Ok(())
}

#[test]
fn same_hash_replacement_preserves_receipts_only_for_identical_bodies() -> Result<()> {
    let block: Arc<Block> =
        zakura_test::vectors::BLOCK_MAINNET_419200_BYTES.zcash_deserialize_into()?;
    for (original_order, different_body) in [(Some(1), false), (None, false), (Some(1), true)] {
        let mut replacement_block = (*block).clone();
        if different_body {
            replacement_block
                .transactions
                .push(block.transactions[0].clone());
        }
        let replacement_block = Arc::new(replacement_block);
        assert_eq!(block.hash(), replacement_block.hash());
        let mut queue = QueuedBlocks::default();
        let mut original = block.clone().into_queued();
        original.0.receipt_order = original_order;
        queue.queue(original);

        // Model a redelivery that received a new verifier receipt.
        let mut replacement = replacement_block.clone().into_queued();
        replacement.0.receipt_order = Some(9);
        let old = queue.replace(block.hash(), replacement);
        assert!(Arc::ptr_eq(&old.0.block, &block));
        let retained = queue.get_mut(&block.hash()).unwrap();
        assert!(Arc::ptr_eq(&retained.0.block, &replacement_block));
        assert_eq!(
            retained.0.receipt_order,
            if different_body {
                Some(9)
            } else {
                original_order
            }
        );
        let children = queue.dequeue_children(block.header.previous_block_hash);
        assert_eq!(children.len(), 1);
        assert_eq!(
            children[0].0.receipt_order,
            if different_body {
                Some(9)
            } else {
                original_order
            }
        );
    }
    Ok(())
}

/// The orphan queue holds a fixed number of entries.
///
/// `MAX_QUEUED_BLOCKS` also sizes the non-finalized write slots, and optimistic relay reads an
/// idle writer off that count, so this bound is load-bearing beyond the queue itself.
#[test]
fn orphan_queue_has_a_fixed_entry_bound() -> Result<()> {
    let block: Arc<Block> =
        zakura_test::vectors::BLOCK_MAINNET_419200_BYTES.zcash_deserialize_into()?;
    let mut queue = QueuedBlocks::default();
    assert!(!queue.is_full());

    for index in 0..MAX_QUEUED_BLOCKS {
        let mut queued = block.clone().into_queued();
        let index = u64::try_from(index).expect("the queue bound fits in u64");
        queued.0.hash.0[..8].copy_from_slice(&index.to_le_bytes());
        queue.blocks.insert(queued.0.hash, queued);
    }
    assert!(queue.is_full());
    Ok(())
}

#[test]
fn prune_removes_right_children() -> Result<()> {
    let _init_guard = zakura_test::init();

    let block1: Arc<Block> =
        zakura_test::vectors::BLOCK_MAINNET_419200_BYTES.zcash_deserialize_into()?;
    let child1: Arc<Block> =
        zakura_test::vectors::BLOCK_MAINNET_419201_BYTES.zcash_deserialize_into()?;
    let child2 = block1.make_fake_child();

    let mut queue = QueuedBlocks::default();
    queue.queue(block1.clone().into_queued());
    queue.queue(child1.clone().into_queued());
    queue.queue(child2.clone().into_queued());
    assert_eq!(3, queue.blocks.len());
    assert_eq!(2, queue.by_parent.len());
    assert_eq!(2, queue.by_height.len());
    assert_eq!(634, queue.known_utxos.len());

    // Pruning the first height removes only block1
    queue.prune_by_height(block1.coinbase_height().unwrap());
    assert_eq!(2, queue.blocks.len());
    assert_eq!(1, queue.by_parent.len());
    assert_eq!(1, queue.by_height.len());
    assert!(queue.get_mut(&block1.hash()).is_none());
    assert!(queue.get_mut(&child1.hash()).is_some());
    assert!(queue.get_mut(&child2.hash()).is_some());
    assert_eq!(632, queue.known_utxos.len());

    // Pruning the children of the first block removes both of the other
    // blocks, and empties all lists
    queue.prune_by_height(child1.coinbase_height().unwrap());
    assert_eq!(0, queue.blocks.len());
    assert_eq!(0, queue.by_parent.len());
    assert_eq!(0, queue.by_height.len());
    assert!(queue.get_mut(&child1.hash()).is_none());
    assert!(queue.get_mut(&child2.hash()).is_none());
    assert_eq!(0, queue.known_utxos.len());

    Ok(())
}

/// `SentHashes::remove` must drop the hash, its unshared outpoints from `known_utxos`,
/// and the corresponding `(hash, height)` entry from `curr_buf` (or whichever
/// batch buffer holds it). Without this, a rejected same-hash block would
/// keep a later honest re-delivery of a block at the same hash locked out as
/// a "duplicate" forever.
#[test]
fn sent_hashes_remove_drops_rejected_hash_and_utxos() -> Result<()> {
    let _init_guard = zakura_test::init();

    let block1: Arc<Block> =
        zakura_test::vectors::BLOCK_MAINNET_419200_BYTES.zcash_deserialize_into()?;
    let block2: Arc<Block> =
        zakura_test::vectors::BLOCK_MAINNET_419201_BYTES.zcash_deserialize_into()?;

    let prepared1 = block1.clone().prepare();
    let prepared2 = block2.clone().prepare();

    let mut sent = SentHashes::default();
    sent.add(&prepared1);
    sent.add(&prepared2);

    // Both hashes are present, and `known_utxos` contains every outpoint from
    // both blocks' coinbase + transparent outputs.
    let utxos_after_add = sent.known_utxos.len();
    assert!(sent.contains(&prepared1.hash));
    assert!(sent.contains(&prepared2.hash));
    assert!(utxos_after_add > 0);

    // Remove block1. block1's hash disappears, block2's stays, and the
    // total number of known utxos shrinks by exactly block1's contribution.
    let block1_utxos = prepared1.new_outputs.len();
    sent.remove(&prepared1.hash);

    assert!(
        !sent.contains(&prepared1.hash),
        "removed hash must not satisfy contains()"
    );
    assert!(sent.contains(&prepared2.hash));
    assert_eq!(
        sent.known_utxos.len(),
        utxos_after_add - block1_utxos,
        "remove must drop only the removed block's outpoints"
    );

    // The (hash, height) entry must be gone from the batch buffer too,
    // otherwise a later `prune_by_height` could re-insert into `sent`.
    assert!(
        !sent.curr_buf.iter().any(|(h, _)| h == &prepared1.hash),
        "remove must drop the (hash, height) entry from curr_buf"
    );
    assert!(sent.curr_buf.iter().any(|(h, _)| h == &prepared2.hash));

    // Removing a hash that isn't tracked is a no-op.
    let block3 = block1.make_fake_child();
    sent.remove(&block3.hash());
    assert!(sent.contains(&prepared2.hash));

    Ok(())
}

#[test]
fn sent_hashes_remove_many_preserves_other_blocks_across_batches() -> Result<()> {
    let _init_guard = zakura_test::init();
    let earlier: Arc<Block> =
        zakura_test::vectors::BLOCK_MAINNET_419200_BYTES.zcash_deserialize_into()?;
    let block: Arc<Block> =
        zakura_test::vectors::BLOCK_MAINNET_419201_BYTES.zcash_deserialize_into()?;
    let earlier = earlier.prepare();
    let siblings = block.make_fake_siblings(3);
    let removed = siblings[0].clone().prepare();
    let retained = siblings[1].clone().prepare();
    let removed_current = siblings[2].clone().prepare();
    let unknown_hash = block.make_fake_child().hash();

    let mut sent = SentHashes::default();
    sent.add(&earlier);
    sent.finish_batch();
    sent.add(&removed);
    sent.add(&retained);
    sent.finish_batch();
    sent.add(&removed_current);

    // Empty input must leave both finished and unfinished batches untouched.
    sent.remove_many(&[]);
    assert_eq!(sent.sent.len(), 4);
    assert_eq!(sent.bufs.len(), 2);
    assert_eq!(sent.curr_buf.len(), 1);

    sent.remove_many(&[
        earlier.hash,
        removed.hash,
        removed_current.hash,
        removed.hash,
        unknown_hash,
    ]);

    assert_eq!(sent.sent.len(), 1);
    assert!(sent.contains(&retained.hash));
    assert_eq!(sent.known_utxos.len(), retained.new_outputs.len());
    for outpoint in retained.new_outputs.keys() {
        assert!(sent.utxo(outpoint).is_some());
    }
    assert!(sent.curr_buf.is_empty());
    assert_eq!(sent.bufs.len(), 1);
    assert_eq!(
        sent.bufs[0].iter().copied().collect::<Vec<_>>(),
        vec![(retained.hash, retained.height)]
    );

    // Redelivery acquires a new owner after the previous batch entry was removed.
    sent.add(&removed_current);
    sent.remove_many(&[retained.hash, removed_current.hash]);
    assert!(sent.sent.is_empty());
    assert!(sent.known_utxos.is_empty());
    assert!(sent.curr_buf.is_empty());
    assert!(sent.bufs.is_empty());
    Ok(())
}

#[test]
fn sent_hashes_remove_keeps_outputs_shared_with_sent_sibling() -> Result<()> {
    let _init_guard = zakura_test::init();
    let block: Arc<Block> =
        zakura_test::vectors::BLOCK_MAINNET_419201_BYTES.zcash_deserialize_into()?;
    let siblings = block.make_fake_siblings(2);
    let evicted = siblings[0].clone().prepare();
    let in_flight = siblings[1].clone().prepare();
    assert_ne!(evicted.hash, in_flight.hash);

    let mut sent = SentHashes::default();
    sent.add(&evicted);
    sent.add(&in_flight);
    sent.remove(&evicted.hash);

    assert!(sent.contains(&in_flight.hash));
    for outpoint in in_flight.new_outputs.keys() {
        assert!(
            sent.utxo(outpoint).is_some(),
            "removing one sibling must retain shared output {outpoint:?} for the other"
        );
    }
    Ok(())
}

#[test]
fn sent_hashes_shared_outputs_release_after_last_distinct_block() -> Result<()> {
    let _init_guard = zakura_test::init();
    let block: Arc<Block> =
        zakura_test::vectors::BLOCK_MAINNET_419201_BYTES.zcash_deserialize_into()?;
    let siblings = block.make_fake_siblings(2);
    let first = siblings[0].clone().prepare();
    let second = siblings[1].clone().prepare();
    let mut sent = SentHashes::default();
    sent.add(&first);
    sent.add(&first);
    sent.add_finalized(&crate::CheckpointVerifiedBlock::from(siblings[0].clone()));
    sent.add_finalized(&crate::CheckpointVerifiedBlock::from(siblings[1].clone()));
    sent.add(&second);
    sent.finish_batch();

    sent.remove(&first.hash);
    sent.remove(&first.hash);
    for outpoint in second.new_outputs.keys() {
        assert!(sent.utxo(outpoint).is_some());
    }
    sent.remove(&second.hash);
    assert!(sent.known_utxos.is_empty());
    assert!(sent.bufs.iter().all(|batch| batch.is_empty()));

    sent.add(&second);
    sent.prune_by_height(second.height);
    assert!(sent.known_utxos.is_empty());
    assert!(sent.sent.is_empty());
    Ok(())
}

#[test]
fn sent_hashes_pruning_keeps_outputs_owned_by_later_block() -> Result<()> {
    let _init_guard = zakura_test::init();
    let block: Arc<Block> =
        zakura_test::vectors::BLOCK_MAINNET_419201_BYTES.zcash_deserialize_into()?;
    let siblings = block.make_fake_siblings(2);
    let first = siblings[0].clone().prepare();
    // The other fork includes the shared transactions one block later.
    let mut other_parent = siblings[1].clone();
    Arc::make_mut(&mut other_parent).transactions.truncate(1);
    let mut later = other_parent.make_fake_child();
    Arc::make_mut(&mut later)
        .transactions
        .extend(block.transactions.iter().skip(1).cloned());
    let later = later.prepare();
    assert_eq!(later.height, (first.height + 1).unwrap());
    assert!(first.new_outputs.iter().any(|(outpoint, output)| {
        !output.utxo.from_coinbase && later.new_outputs.contains_key(outpoint)
    }));

    let mut sent = SentHashes::default();
    sent.add(&first);
    sent.finish_batch();
    sent.add(&later);
    sent.finish_batch();
    sent.prune_by_height(first.height);
    assert!(!sent.contains(&first.hash));
    assert!(sent.contains(&later.hash));
    for outpoint in later.new_outputs.keys() {
        assert!(sent.utxo(outpoint).is_some());
    }
    for outpoint in first.new_outputs.keys() {
        if !later.new_outputs.contains_key(outpoint) {
            assert!(sent.utxo(outpoint).is_none());
        }
    }

    sent.prune_by_height(later.height);
    assert!(sent.known_utxos.is_empty());
    assert!(sent.sent.is_empty());
    Ok(())
}

// Ensures `dequeue_children` does not remove same-height sibling blocks from other forks.
#[test]
fn dequeue_children_preserves_same_height_siblings() -> Result<()> {
    let _init_guard = zakura_test::init();

    let root_block: Arc<Block> =
        zakura_test::vectors::BLOCK_MAINNET_419200_BYTES.zcash_deserialize_into()?;

    let left_child: Arc<Block> =
        zakura_test::vectors::BLOCK_MAINNET_419201_BYTES.zcash_deserialize_into()?;
    let left_grandchild = left_child.make_fake_child();

    let right_child = root_block.make_fake_child();
    let right_grandchild = right_child.make_fake_child();

    let mut queue = QueuedBlocks::default();
    queue.queue(left_grandchild.clone().into_queued());
    queue.queue(right_grandchild.clone().into_queued());

    let height = left_grandchild.coinbase_height().unwrap();

    // Sanity check: both entries are indexed under the same height bucket
    assert_eq!(
        queue.by_height.get(&height).unwrap().len(),
        2,
        "expected both fork grandchildren to be in the same height bucket"
    );

    // Dequeue only one branch
    queue.dequeue_children(left_child.hash());

    assert!(
        queue.blocks.contains_key(&right_grandchild.hash()),
        "sibling block must remain in queue after unrelated dequeue"
    );

    assert!(
        queue
            .by_height
            .get(&height)
            .unwrap()
            .contains(&right_grandchild.hash()),
        "sibling must remain indexed by height after unrelated dequeue"
    );

    Ok(())
}

#[test]
fn dequeue_descendants_removes_the_complete_failed_subtree() -> Result<()> {
    let _init_guard = zakura_test::init();
    let root: Arc<Block> =
        zakura_test::vectors::BLOCK_MAINNET_419200_BYTES.zcash_deserialize_into()?;
    let failed_child: Arc<Block> =
        zakura_test::vectors::BLOCK_MAINNET_419201_BYTES.zcash_deserialize_into()?;
    let failed_grandchild = failed_child.make_fake_child();
    let sibling = root.make_fake_child();

    let mut queue = QueuedBlocks::default();
    let mut responses = Vec::new();
    for block in [failed_child, failed_grandchild, sibling] {
        let (response, receiver) = oneshot::channel();
        queue.queue((block.prepare(), response, None, 0));
        responses.push(receiver);
    }
    let error = CommitSemanticallyVerifiedError::from(CommitBlockError::HeaderChainError {
        error: format!("ancestor {} failed", root.hash()),
    });

    assert_eq!(queue.fail_descendants(root.hash(), error.clone()).len(), 3);
    for response in &mut responses {
        assert_eq!(response.try_recv(), Ok(Err(error.clone())));
    }
    assert!(queue.blocks.is_empty());
    assert!(queue.by_parent.is_empty());
    assert!(queue.by_height.is_empty());
    assert!(queue.known_utxos.is_empty());

    Ok(())
}