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
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
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
//! State histories for the signed issuance counter, with and without ZIP 234 reissuance.

use std::sync::Arc;

use zakura_chain::{
    amount::{Amount, NonNegative},
    block::{Block, Height},
    parameters::{
        subsidy::{expected_issued_supply, is_zip234_active},
        testnet::{ConfiguredActivationHeights, RegtestParameters},
        Network, NetworkKind, NetworkUpgrade,
    },
    transaction::{LockTime, Transaction},
    transparent::{Address, Input, Output},
};

use crate::{
    service::{
        finalized_state::{CheckpointVerifiedBlock, FinalizedState},
        non_finalized_state::NonFinalizedState,
    },
    CommitBlockError, Config, SemanticallyVerifiedBlock, ValidateContextError,
};

use super::rollback::{child_block, coinbase_tx};

/// Include transaction IDs so competing fixtures have distinct block hashes.
pub(super) fn child_block_with_history_commitment(
    parent: &Block,
    transactions: Vec<Arc<Transaction>>,
    network: &Network,
    history: &zakura_chain::history_tree::HistoryTree,
) -> Arc<Block> {
    let mut block = super::rollback::child_block_with_history_commitment(
        parent,
        transactions,
        network,
        history,
    );
    let block_mut = Arc::make_mut(&mut block);
    Arc::make_mut(&mut block_mut.header).merkle_root = block_mut.transactions.iter().collect();
    block
}

/// The accounting fixture height on the test network, and its ZIP 234 start height when
/// reissuance is on.
pub(super) const START: Height = Height(3);

/// Returns a Regtest network with NU7 at height 2 and accounting fixtures from [`START`].
///
/// If `reissuance` is true, ZIP 234 reissuance starts at [`START`].
pub(super) fn accounting_network(reissuance: bool) -> Network {
    Network::new_regtest(RegtestParameters {
        // Regtest activates Heartwood at height 1, where the block commitment is reserved.
        // NU7 activates after it.
        activation_heights: ConfiguredActivationHeights {
            nu7: Some(2),
            ..Default::default()
        },
        test_nsm_reissuance_height: reissuance.then_some(START),
        initial_nsm_value_balance: Some(Amount::zero()),
        ..Default::default()
    })
}

/// Returns the ZIP 234 reissuance bonus for a block at `height` whose parent left
/// `deficit`, or zero where reissuance is inactive.
///
/// This oracle restates `ceil(deficit * BLOCK_SUBSIDY_FRACTION)` independently of
/// `block_subsidy`, using the fixed NU7 fraction on every network.
fn reissuance_bonus(network: &Network, height: Height, deficit: i128) -> i128 {
    if !is_zip234_active(network, height) {
        return 0;
    }
    (deficit * 1_375 + 9_999_999_999) / 10_000_000_000
}

/// Returns a finalized state with Regtest blocks up to the parent of [`START`], and that
/// parent block.
pub(super) fn state_below_start(network: &Network) -> (FinalizedState, Arc<Block>) {
    state_below_start_with_config(network, &Config::ephemeral())
}

pub(super) fn state_below_start_with_config(
    network: &Network,
    config: &Config,
) -> (FinalizedState, Arc<Block>) {
    let address = Address::from_script_hash(NetworkKind::Regtest, [0x42; 20]);
    let dust = Amount::<NonNegative>::try_from(1).expect("1 fits in Amount<NonNegative>");

    let mut state =
        FinalizedState::new(config, network).expect("opening an ephemeral database should succeed");
    let mut parent = zakura_chain::block::genesis::regtest_genesis_block();
    commit(&mut state, &parent).expect("the genesis block commits");

    for height in 1..START.0 {
        let transactions = vec![coinbase_tx(Height(height), dust, &address)];
        let block = if height == 1 {
            // The Heartwood activation block has the reserved all-zero commitment.
            child_block(&parent, transactions)
        } else {
            child_block_with_history_commitment(
                &parent,
                transactions,
                network,
                &state.db.history_tree(),
            )
        };
        commit(&mut state, &block).expect("a block below the start height commits");
        parent = block;
    }

    (state, parent)
}

/// Returns an accounting fixture that overdraws the eligible deficit by `excess` zatoshi.
pub(super) fn start_block(
    state: &FinalizedState,
    network: &Network,
    parent: &Block,
    excess: i64,
) -> Arc<Block> {
    let address = Address::from_script_hash(NetworkKind::Regtest, [0x42; 20]);
    let scheduled =
        zakura_chain::parameters::subsidy::halving_block_subsidy(START, network).unwrap();
    let deficit = state.db.finalized_value_pool().nsm_value_balance_amount();
    let coinbase_value =
        Amount::<NonNegative>::try_from(i64::from(scheduled) + i64::from(deficit) + excess)
            .expect("valid coinbase value");

    // The non-finalized state only accepts transaction versions that are valid after
    // Canopy.
    let coinbase = Transaction::V5 {
        network_upgrade: NetworkUpgrade::Nu7,
        lock_time: LockTime::unlocked(),
        expiry_height: START,
        inputs: vec![Input::Coinbase {
            height: START,
            data: vec![0x00; 8],
            sequence: u32::MAX,
        }],
        outputs: vec![Output::new(coinbase_value, address.script())],
        sapling_shielded_data: None,
        orchard_shielded_data: None,
    };

    child_block_with_history_commitment(
        parent,
        vec![Arc::new(coinbase)],
        network,
        &state.db.history_tree(),
    )
}

/// Commits `block` to the finalized state as a checkpoint-verified block.
pub(super) fn commit(
    state: &mut FinalizedState,
    block: &Arc<Block>,
) -> Result<(), CommitBlockError> {
    state
        .commit_finalized_direct(
            CheckpointVerifiedBlock::from(block.clone()).into(),
            None,
            None,
            "NSM value balance test",
        )
        .map(|_| ())
        .map_err(|error| error.inner().clone())
}

/// Check historical exclusion and the first permitted claim after NU7.
#[test]
fn nsm_value_balance_matches_the_schedule() {
    let _init_guard = zakura_test::init();

    for reissuance in [false, true] {
        nsm_value_balance_matches_the_schedule_on(&accounting_network(reissuance));
    }
}

fn nsm_value_balance_matches_the_schedule_on(network: &Network) {
    let network = network.clone();
    let (mut state, parent) = state_below_start(&network);

    let baseline_info = state.db.block_info(Height(1).into()).unwrap();
    let baseline = i64::from(expected_issued_supply(Height(1), &network).unwrap())
        - i64::from(baseline_info.value_pools().issued_supply());

    // `state_below_start` mines dust coinbases, so every block so far under-claimed its
    // subsidy and the deficit has been accumulating.
    for height in 0..START.0 {
        let height = Height(height);
        let block_info = state
            .db
            .block_info(height.into())
            .expect("every committed block has block info");

        let expected = expected_issued_supply(height, &network)
            .expect("the halving schedule is a valid amount at every height");
        let issued = block_info.value_pools().issued_supply();
        let derived = if height < Height(2) {
            0
        } else {
            i64::from(expected) - i64::from(issued) - baseline
        };

        assert_eq!(
            i64::from(block_info.value_pools().nsm_value_balance_amount()),
            derived,
            "the stored deficit must exclude the pre-NU7 baseline at {height:?}",
        );
    }

    let deficit_below_start = state.db.finalized_value_pool().nsm_value_balance_amount();
    assert!(
        deficit_below_start > Amount::<NonNegative>::zero(),
        "under-claiming coinbases must leave a deficit to reissue, got {deficit_below_start:?}",
    );

    // A full permitted claim leaves the deficit lower by exactly the reissuance bonus.
    let block = permitted_start_block(&state, &network, &parent);
    commit(&mut state, &block).expect("the permitted claim commits");

    let expected = expected_issued_supply(START, &network).expect("valid expected issued supply");
    let pools = state.db.finalized_value_pool();
    assert_eq!(
        i64::from(pools.nsm_value_balance_amount()),
        i64::from(expected) - i64::from(pools.issued_supply()) - baseline,
        "the identity must still hold after a block that claims the permitted subsidy",
    );
}

#[test]
fn claim_fixture_obeys_subsidy_limit() {
    use zakura_chain::parameters::subsidy::block_subsidy;

    let _init_guard = zakura_test::init();
    for reissuance in [false, true] {
        let network = accounting_network(reissuance);
        let (state, parent) = state_below_start(&network);
        let deficit = state.db.finalized_value_pool().nsm_value_balance_amount();
        let allowed = block_subsidy(START, &network, Some(deficit.constrain().unwrap())).unwrap();
        let block = permitted_start_block(&state, &network, &parent);
        let change = block
            .chain_value_pool_change(&network, &Default::default(), None)
            .unwrap();
        assert_eq!(
            change.total().unwrap(),
            allowed,
            "the acceptance fixture must claim the exact subsidy"
        );
    }
}

proptest::proptest! {
    #![proptest_config(proptest::test_runner::Config::with_cases(32))]

    #[test]
    fn checkpoint_claim_sequences_preserve_balance(
        claims in proptest::collection::vec(0u64..=1_000_000, 1..32),
        reissuance in proptest::bool::ANY,
    ) {
        use zakura_chain::parameters::subsidy::{block_subsidy, halving_block_subsidy};

        let _init_guard = zakura_test::init();
        let network = accounting_network(reissuance);
        let (mut state, mut parent) = state_below_start(&network);
        let address = Address::from_script_hash(NetworkKind::Regtest, [0x42; 20]);
        let mut issued = i64::from(state.db.finalized_value_pool().issued_supply());
        let mut expected = issued + i64::from(state.db.finalized_value_pool().nsm_value_balance_amount());

        for (index, claim_fraction) in claims.into_iter().enumerate() {
            let height = Height(START.0 + u32::try_from(index).unwrap());
            let before = expected - issued;
            let scheduled = i64::from(halving_block_subsidy(height, &network).unwrap());
            let bonus = reissuance_bonus(&network, height, i128::from(before));
            let allowed = block_subsidy(height, &network, Some(Amount::try_from(before).unwrap())).unwrap();
            proptest::prop_assert_eq!(i128::from(i64::from(allowed)), i128::from(scheduled) + bonus);
            let claimed = i64::try_from(i128::from(i64::from(allowed)) * i128::from(claim_fraction) / 1_000_000).unwrap();
            let block = child_block_with_history_commitment(
                &parent,
                vec![coinbase_tx(height, Amount::try_from(claimed).unwrap(), &address)],
                &network,
                &state.db.history_tree(),
            );
            commit(&mut state, &block).unwrap();
            expected += scheduled;
            issued += claimed;
            let pools = state.db.finalized_value_pool();
            proptest::prop_assert_eq!(i64::from(pools.issued_supply()), issued);
            proptest::prop_assert_eq!(i64::from(pools.nsm_value_balance_amount()), expected - issued);
            proptest::prop_assert!(expected >= issued);
            proptest::prop_assert_eq!(*state.db.block_info(height.into()).unwrap().value_pools(), pools);
            parent = block;
        }
    }
}

/// Construct an accounting fixture with the exact permitted subsidy.
/// Semantic subsidy validation lives in zakura-consensus block tests.
pub(super) fn permitted_start_block(
    state: &FinalizedState,
    network: &Network,
    parent: &Block,
) -> Arc<Block> {
    use zakura_chain::parameters::subsidy::halving_block_subsidy;

    let deficit = i64::from(state.db.finalized_value_pool().nsm_value_balance_amount());
    let bonus = i64::try_from(reissuance_bonus(network, START, i128::from(deficit))).unwrap();
    let allowed = i64::from(halving_block_subsidy(START, network).unwrap()) + bonus;
    let address = Address::from_script_hash(NetworkKind::Regtest, [0x42; 20]);
    child_block_with_history_commitment(
        parent,
        vec![coinbase_tx(
            START,
            Amount::try_from(allowed).unwrap(),
            &address,
        )],
        network,
        &state.db.history_tree(),
    )
}

#[test]
fn mining_template_uses_the_selected_parents_balance() {
    use crate::{service::read::difficulty::get_block_template_chain_info, tests::FakeChainHelper};

    let _guard = zakura_test::init();
    let network = accounting_network(true);
    let (state, parent) = state_below_start(&network);
    let mut forks = NonFinalizedState::new(&network);
    let balance = i64::from(state.db.finalized_value_pool().nsm_value_balance_amount());
    assert!(balance > 0);

    let assert_parent = |forks: &NonFinalizedState, parent: &Block, expected_balance: i64| {
        let info = get_block_template_chain_info(forks, &state.db, &network).unwrap();
        assert_eq!(info.tip_hash, parent.hash());
        assert_eq!(info.tip_height, parent.coinbase_height().unwrap());
        assert_eq!(
            i64::from(info.value_pools.nsm_value_balance_amount()),
            expected_balance,
        );
    };

    // With no non-finalized chain, the template must use the finalized tip's balance.
    assert_parent(&forks, &parent, balance);

    let bonus = i64::try_from(reissuance_bonus(&network, START, i128::from(balance))).unwrap();
    assert!(bonus > 0);
    // These accounting fixtures bypass semantic subsidy validation. The shortfall
    // gives the competing branch a distinct balance, and its higher work selects it.
    for (work, shortfall) in [(10, 0), (20, 1_000)] {
        let block =
            start_block(&state, &network, &parent, bonus - balance - shortfall).set_work(work);
        forks
            .commit_new_chain(SemanticallyVerifiedBlock::from(block.clone()), &state.db)
            .unwrap();
        assert_parent(&forks, &block, balance - bonus + shortfall);
    }
    assert_eq!(forks.chain_count(), 2);
}

#[test]
fn mining_template_requires_parent_pools_at_reissuance_activation() {
    use crate::service::{
        finalized_state::DiskWriteBatch, read::difficulty::get_block_template_chain_info,
    };

    let _guard = zakura_test::init();
    for start in [START, START.next().unwrap()] {
        let network = Network::new_regtest(RegtestParameters {
            activation_heights: ConfiguredActivationHeights {
                nu7: Some(2),
                ..Default::default()
            },
            test_nsm_reissuance_height: Some(start),
            ..Default::default()
        });
        let (state, parent) = state_below_start(&network);
        let forks = NonFinalizedState::new(&network);
        let parent_height = parent.coinbase_height().unwrap();
        let parent_info = state.db.block_info(parent_height.into()).unwrap();
        let mut batch = DiskWriteBatch::new();
        let _ = state
            .db
            .block_info_cf()
            .with_batch_for_writing(&mut batch)
            .zs_delete(&parent_height);
        state.db.write_batch(batch).unwrap();

        let result = get_block_template_chain_info(&forks, &state.db, &network);
        // Restore the fixture before the database's shutdown integrity check.
        let mut batch = DiskWriteBatch::new();
        let _ = state
            .db
            .block_info_cf()
            .with_batch_for_writing(&mut batch)
            .zs_insert(&parent_height, &parent_info);
        state.db.write_batch(batch).unwrap();

        if start == START {
            assert_eq!(
                result.unwrap_err().to_string(),
                "missing chain value pools for the ZIP 234 candidate block parent",
            );
        } else {
            let info = result.unwrap();
            assert_eq!(info.tip_hash, parent.hash());
            assert_eq!(info.value_pools, Default::default());
        }
    }
}

proptest::proptest! {
    #![proptest_config(proptest::test_runner::Config::with_cases(std::env::var("NSM_HISTORY_CASES").ok().and_then(|value| value.parse().ok()).unwrap_or(8)))]

    /// Exercise accounting transitions through real databases. Partial claims intentionally
    /// bypass semantic subsidy validation; this property tests the state accounting layer.
    /// Pull requests run eight cases; the extended reissuance CI job sets
    /// `NSM_HISTORY_CASES` for a broader search.
    #[test]
    fn balance_rollback_replay_and_fork_equivalence(
        claims in proptest::collection::vec(0u32..=1_000_000, 2..12),
        target_offset in 0usize..12,
        reissuance in proptest::bool::ANY,
    ) {
        use zakura_chain::parameters::subsidy::halving_block_subsidy;
        use crate::RollbackFinalizedStateOptions;
        let _guard = zakura_test::init();
        let network = accounting_network(reissuance);
        let dir = tempfile::tempdir().unwrap();
        let config = Config { cache_dir: dir.path().to_owned(), ephemeral: false, ..Config::default() };
        let (mut state, mut parent) = state_below_start_with_config(&network, &config);
        let initial = state.db.finalized_value_pool();
        let mut snapshots = vec![initial];
        let mut blocks = vec![parent.clone()];
        let address = Address::from_script_hash(NetworkKind::Regtest, [0x42; 20]);
        let mut deficit = i128::from(i64::from(initial.nsm_value_balance_amount()));
        let mut issued = i128::from(i64::from(initial.issued_supply()));
        for (index, fraction) in claims.iter().enumerate() {
            let height = Height(START.0 + u32::try_from(index).unwrap());
            let scheduled = i128::from(i64::from(halving_block_subsidy(height, &network).unwrap()));
            let bonus = reissuance_bonus(&network, height, deficit);
            let claim = (scheduled + bonus) * i128::from(*fraction) / 1_000_000;
            let block = child_block_with_history_commitment(&parent,
                vec![coinbase_tx(height, Amount::try_from(i64::try_from(claim).unwrap()).unwrap(), &address)],
                &network, &state.db.history_tree());
            commit(&mut state, &block).unwrap();
            deficit += scheduled - claim;
            issued += claim;
            let pools = state.db.finalized_value_pool();
            proptest::prop_assert_eq!(i128::from(i64::from(pools.nsm_value_balance_amount())), deficit);
            proptest::prop_assert_eq!(i128::from(i64::from(pools.issued_supply())), issued);
            snapshots.push(pools);
            blocks.push(block.clone());
            parent = block;
        }
        let target = target_offset % claims.len();
        let target_height = Height(START.0 - 1 + u32::try_from(target).unwrap());
        // Keep this history's database open for rollback and replay. Offline
        // reopen/lock handling is covered by the rollback tests; both modes use
        // the same rollback implementation here.
        state.db.rollback_for_test(&network, RollbackFinalizedStateOptions {
            target_height, keep_rolled_back_blocks: true, max_checkpoint_height: Some(Height(0)),
        }).unwrap();
        proptest::prop_assert_eq!(state.db.finalized_value_pool(), snapshots[target]);
        for (index, block) in blocks.iter().enumerate().skip(target + 1) {
            commit(&mut state, block).unwrap();
            proptest::prop_assert_eq!(state.db.finalized_value_pool(), snapshots[index]);
            proptest::prop_assert_eq!(*state.db.block_info(block.coinbase_height().unwrap().into()).unwrap().value_pools(), snapshots[index]);
        }
        state.db.rollback_for_test(&network, RollbackFinalizedStateOptions {
            target_height, keep_rolled_back_blocks: false, max_checkpoint_height: Some(Height(0)),
        }).unwrap();
        // Retain a reopen check for persisted rollback state and format validity.
        drop(state);
        let mut state = FinalizedState::new(&config, &network).unwrap();
        proptest::prop_assert_eq!(state.db.finalized_value_pool(), snapshots[target]);
        let height = target_height.next().unwrap();
        let deficit = i128::from(i64::from(snapshots[target].nsm_value_balance_amount()));
        let bonus = reissuance_bonus(&network, height, deficit);
        let scheduled = i128::from(i64::from(halving_block_subsidy(height, &network).unwrap()));
        let fork = child_block_with_history_commitment(&blocks[target],
            vec![coinbase_tx(height, Amount::try_from(i64::try_from(scheduled + bonus).unwrap()).unwrap(), &Address::from_script_hash(NetworkKind::Regtest, [0x43; 20]))],
            &network, &state.db.history_tree());
        proptest::prop_assert_ne!(fork.hash(), blocks[target + 1].hash());
        commit(&mut state, &fork).unwrap();
        proptest::prop_assert_eq!(i128::from(i64::from(state.db.finalized_value_pool().nsm_value_balance_amount())), deficit - bonus);
        let (mut fresh, _) = state_below_start(&network);
        for block in blocks.iter().take(target + 1).skip(1) {
            commit(&mut fresh, block).unwrap();
        }
        commit(&mut fresh, &fork).unwrap();
        proptest::prop_assert_eq!(state.db.finalized_value_pool(), fresh.db.finalized_value_pool());
    }
}

proptest::proptest! {
    #![proptest_config(proptest::test_runner::Config::with_cases(64))]

    #[test]
    fn non_finalized_forks_keep_independent_balances(
        shortfall in 1i64..1_000_000,
        reissuance in proptest::bool::ANY,
    ) {
        let _guard = zakura_test::init();
        let network = accounting_network(reissuance);
        let (mut state, parent) = state_below_start(&network);
        let before = state.db.finalized_value_pool();
        let deficit = i64::from(before.nsm_value_balance_amount());
        let bonus = i64::try_from(reissuance_bonus(&network, START, i128::from(deficit))).unwrap();
        // These fixtures isolate contextual accounting from semantic subsidy validation.
        let full = start_block(&state, &network, &parent, bonus - deficit);
        let partial = start_block(&state, &network, &parent, bonus - deficit - shortfall);
        let mut forks = NonFinalizedState::new(&network);
        forks.commit_new_chain(SemanticallyVerifiedBlock::from(full.clone()), &state.db).unwrap();
        forks.commit_new_chain(SemanticallyVerifiedBlock::from(partial.clone()), &state.db).unwrap();
        proptest::prop_assert_ne!(full.hash(), partial.hash());
        proptest::prop_assert_eq!(forks.chain_count(), 2);
        for (block, expected) in [(&full, deficit - bonus), (&partial, deficit - bonus + shortfall)] {
            let info = forks.chain_iter().find_map(|chain| chain.block_info(block.hash().into())).unwrap();
            proptest::prop_assert_eq!(i64::from(info.value_pools().nsm_value_balance_amount()), expected);
        }
        // From NU7, the state rejects a block that leaves the balance negative.
        let over = start_block(&state, &network, &parent, 1);
        proptest::prop_assert!(forks.commit_new_chain(SemanticallyVerifiedBlock::from(over), &state.db).is_err());
        proptest::prop_assert_eq!(forks.chain_count(), 2);
        proptest::prop_assert_eq!(state.db.finalized_value_pool(), before);
        for (index, (parent, parent_deficit)) in [(&full, deficit - bonus), (&partial, deficit - bonus + shortfall)].into_iter().enumerate() {
            let height = START.next().unwrap();
            let bonus = i64::try_from(reissuance_bonus(&network, height, i128::from(parent_deficit))).unwrap();
            let scheduled = i64::from(zakura_chain::parameters::subsidy::halving_block_subsidy(height, &network).unwrap());
            let mut coinbase = (*parent.transactions[0]).clone();
            let Transaction::V5 { inputs, outputs, expiry_height, .. } = &mut coinbase else { unreachable!() };
            *expiry_height = height;
            let Input::Coinbase { height: input_height, .. } = &mut inputs[0] else { unreachable!() };
            *input_height = height;
            outputs[0].value = Amount::try_from(scheduled + bonus).unwrap();
            let history = forks.chain_iter().find_map(|chain| chain.history_tree(parent.hash().into())).unwrap();
            let child = child_block_with_history_commitment(parent, vec![Arc::new(coinbase)], &network, &history);
            forks.commit_block(SemanticallyVerifiedBlock::from(child.clone()), &state.db).unwrap();
            let info = forks.chain_iter().find_map(|chain| chain.block_info(child.hash().into())).unwrap();
            proptest::prop_assert_eq!(i64::from(info.value_pools().nsm_value_balance_amount()), parent_deficit - bonus);
            if index == 0 {
                proptest::prop_assert_eq!(forks.best_chain().unwrap().non_finalized_tip_hash(), child.hash());
            }
        }
        let best = forks.best_chain().unwrap();
        let root_pools = *best.block_info(START.into()).unwrap().value_pools();
        let tip_before = best.non_finalized_tip_with_value_balance();
        state.commit_finalized_direct(forks.finalize(), None, None, "balance property").unwrap();
        proptest::prop_assert_eq!(state.db.finalized_value_pool(), root_pools);
        proptest::prop_assert_eq!(forks.chain_count(), 1);
        proptest::prop_assert_eq!(forks.best_chain().unwrap().non_finalized_tip_with_value_balance(), tip_before);
        state.commit_finalized_direct(forks.finalize(), None, None, "balance property").unwrap();
        proptest::prop_assert_eq!(state.db.finalized_value_pool(), tip_before.2);

    }
}

#[test]
fn startup_migration_failure_preserves_version_and_retry_matches_fresh_sync() {
    migration_retry_matches_fresh_sync(accounting_network(false));
}

#[test]
fn derived_nsm_seed_migration_retry_matches_fresh_sync() {
    migration_retry_matches_fresh_sync(Network::new_regtest(RegtestParameters {
        activation_heights: ConfiguredActivationHeights {
            nu7: Some(2),
            ..Default::default()
        },
        ..Default::default()
    }));
}

fn migration_retry_matches_fresh_sync(network: Network) {
    use crate::{
        constants::{state_database_format_version_in_code, STATE_DATABASE_KIND},
        service::finalized_state::{
            disk_format::{FromDisk, IntoDisk, RawBytes},
            DiskWriteBatch, ZakuraDb, STATE_COLUMN_FAMILIES_IN_CODE,
        },
    };
    let _guard = zakura_test::init();
    let dir = tempfile::tempdir().unwrap();
    let config = Config {
        cache_dir: dir.path().to_owned(),
        ephemeral: false,
        ..Config::default()
    };
    let (mut state, parent) = state_below_start_with_config(&network, &config);
    let block = permitted_start_block(&state, &network, &parent);
    commit(&mut state, &block).unwrap();
    let expected = state.db.finalized_value_pool();
    let baseline_bytes = state.db.raw_block_info_cf().zs_get(&Height(1)).unwrap();
    let mut batch = DiskWriteBatch::new();
    for height in 0..=START.0 {
        let mut bytes = state
            .db
            .raw_block_info_cf()
            .zs_get(&Height(height))
            .unwrap()
            .as_bytes();
        bytes.drain(48..56);
        let _ = state
            .db
            .raw_block_info_cf()
            .with_batch_for_writing(&mut batch)
            .zs_insert(&Height(height), &RawBytes::from_bytes(bytes));
    }
    let _ = state
        .db
        .raw_block_info_cf()
        .with_batch_for_writing(&mut batch)
        .zs_insert(&Height(1), &RawBytes::from_bytes(vec![0; 51]));
    let tip_bytes = state
        .db
        .raw_chain_value_pools_cf()
        .zs_get(&())
        .unwrap()
        .as_bytes();
    let _ = state
        .db
        .raw_chain_value_pools_cf()
        .with_batch_for_writing(&mut batch)
        .zs_insert(&(), &RawBytes::from_bytes(&tip_bytes[..48]));
    state.db.write_batch(batch).unwrap();
    let old_version = semver::Version::new(28, 2, 0);
    state
        .db
        .update_format_version_on_disk(&old_version)
        .unwrap();
    drop(state);
    let old_path = config.db_path(STATE_DATABASE_KIND, 28, &network);
    let new_path = config.db_path(
        STATE_DATABASE_KIND,
        state_database_format_version_in_code().major,
        &network,
    );
    std::fs::create_dir_all(old_path.parent().unwrap()).unwrap();
    std::fs::rename(&new_path, &old_path).unwrap();
    assert!(FinalizedState::new(&config, &network).is_err());
    assert!(
        !old_path.exists(),
        "the upgrader moves the legacy database out of the old binary's path"
    );
    assert!(new_path.exists());
    let db = ZakuraDb::new(
        &config,
        STATE_DATABASE_KIND,
        &state_database_format_version_in_code(),
        &network,
        true,
        STATE_COLUMN_FAMILIES_IN_CODE
            .iter()
            .map(ToString::to_string),
        false,
    )
    .unwrap();
    assert_eq!(db.format_version_on_disk().unwrap(), Some(old_version));
    let mut batch = DiskWriteBatch::new();
    let _ = db
        .raw_block_info_cf()
        .with_batch_for_writing(&mut batch)
        .zs_insert(&Height(1), &baseline_bytes);
    db.write_batch(batch).unwrap();
    drop(db);
    let upgraded = FinalizedState::new(&config, &network).unwrap();
    assert_eq!(upgraded.db.finalized_value_pool(), expected);
    assert_eq!(
        upgraded.db.format_version_on_disk().unwrap(),
        Some(state_database_format_version_in_code())
    );
    assert_eq!(
        *upgraded.db.block_info(START.into()).unwrap().value_pools(),
        expected
    );
}

#[test]
fn legacy_migration_reopens_and_replays_across_activation() {
    use crate::{
        constants::state_database_format_version_in_code,
        rollback_finalized_state,
        service::finalized_state::{
            disk_format::{FromDisk, IntoDisk, RawBytes},
            DiskWriteBatch,
        },
        RollbackFinalizedStateOptions,
    };

    let _guard = zakura_test::init();
    let network = accounting_network(false);
    for migrate_before_activation in [false, true] {
        for excess in [-1, 0] {
            let dir = tempfile::tempdir().unwrap();
            let config = Config {
                cache_dir: dir.path().to_owned(),
                ephemeral: false,
                ..Config::default()
            };
            let (mut state, parent) = state_below_start_with_config(&network, &config);
            let block = start_block(&state, &network, &parent, excess);
            commit(&mut state, &block).unwrap();
            let expected = state.db.finalized_value_pool();
            let before_activation_info = state.db.block_info(Height(1).into()).unwrap();
            let before_activation = *before_activation_info.value_pools();
            let before_activation_size = before_activation_info.size();

            if migrate_before_activation {
                drop(state);
                rollback_finalized_state(
                    config.clone(),
                    &network,
                    RollbackFinalizedStateOptions {
                        target_height: Height(1),
                        keep_rolled_back_blocks: false,
                        max_checkpoint_height: Some(Height(0)),
                    },
                )
                .unwrap();
                state = FinalizedState::new(&config, &network).unwrap();
            }

            let tip = state.db.finalized_tip_height().unwrap();
            let mut batch = DiskWriteBatch::new();
            for height in 0..=tip.0 {
                let mut bytes = state
                    .db
                    .raw_block_info_cf()
                    .zs_get(&Height(height))
                    .unwrap()
                    .as_bytes();
                bytes.drain(48..56);
                let _ = state
                    .db
                    .raw_block_info_cf()
                    .with_batch_for_writing(&mut batch)
                    .zs_insert(&Height(height), &RawBytes::from_bytes(bytes));
            }
            let tip_bytes = state
                .db
                .raw_chain_value_pools_cf()
                .zs_get(&())
                .unwrap()
                .as_bytes();
            let _ = state
                .db
                .raw_chain_value_pools_cf()
                .with_batch_for_writing(&mut batch)
                .zs_insert(&(), &RawBytes::from_bytes(&tip_bytes[..48]));
            state.db.write_batch(batch).unwrap();
            state
                .db
                .update_format_version_on_disk(&semver::Version::new(28, 2, 0))
                .unwrap();
            drop(state);

            let upgraded = FinalizedState::new(&config, &network).unwrap();
            assert_eq!(
                upgraded.db.format_version_on_disk().unwrap(),
                Some(state_database_format_version_in_code())
            );
            let upgraded_anchor = upgraded.db.block_info(Height(1).into()).unwrap();
            assert_eq!(
                *upgraded_anchor.value_pools(),
                before_activation,
                "migration must preserve the seed-height pools semantically",
            );
            assert_eq!(upgraded_anchor.size(), before_activation_size);
            if migrate_before_activation {
                assert_eq!(
                    upgraded.db.finalized_value_pool(),
                    before_activation,
                    "migration must preserve the pre-activation tip pools semantically",
                );
            } else {
                assert_eq!(upgraded.db.finalized_value_pool(), expected);
            }
            drop(upgraded);

            if !migrate_before_activation {
                rollback_finalized_state(
                    config.clone(),
                    &network,
                    RollbackFinalizedStateOptions {
                        target_height: Height(1),
                        keep_rolled_back_blocks: false,
                        max_checkpoint_height: Some(Height(0)),
                    },
                )
                .unwrap();
            }
            let mut replayed = FinalizedState::new(&config, &network).unwrap();
            assert_eq!(replayed.db.finalized_value_pool(), before_activation);
            assert_eq!(
                replayed
                    .db
                    .finalized_value_pool()
                    .nsm_value_balance_amount(),
                Amount::<zakura_chain::amount::NegativeAllowed>::zero()
            );
            commit(&mut replayed, &parent).unwrap();
            commit(&mut replayed, &block).unwrap();
            assert_eq!(replayed.db.finalized_value_pool(), expected);
            drop(replayed);
            assert_eq!(
                FinalizedState::new(&config, &network)
                    .unwrap()
                    .db
                    .finalized_value_pool(),
                expected
            );
        }
    }
}

/// From NU7, both commit paths reject a block that overdraws the NSM value balance, and
/// agree on the balance after a block that does not.
#[test]
fn signed_balance_agrees_across_both_commit_paths() {
    let _guard = zakura_test::init();
    let network = accounting_network(false);
    for excess in [-1, 0, 1, 1_000_000] {
        let (mut state, parent) = state_below_start(&network);
        let block = start_block(&state, &network, &parent, excess);
        let mut forks = NonFinalizedState::new(&network);
        let fork_result =
            forks.commit_new_chain(SemanticallyVerifiedBlock::from(block.clone()), &state.db);
        let finalized_result = commit(&mut state, &block);

        if excess > 0 {
            assert!(
                matches!(fork_result,
                    Err(ValidateContextError::NegativeNsmValueBalance { height, .. }) if height == START
                ),
                "excess {excess}: {fork_result:?}"
            );
            assert!(
                matches!(finalized_result,
                    Err(CommitBlockError::ValidateContextError(ref error))
                        if matches!(**error, ValidateContextError::NegativeNsmValueBalance { height, .. } if height == START)
                ),
                "excess {excess}: {finalized_result:?}"
            );
            continue;
        }

        fork_result.unwrap();
        finalized_result.unwrap();
        let fork_pools = *forks
            .best_chain()
            .unwrap()
            .block_info(block.hash().into())
            .unwrap()
            .value_pools();
        assert_eq!(state.db.finalized_value_pool(), fork_pools);
        assert_eq!(i64::from(fork_pools.nsm_value_balance_amount()), -excess);
    }
}

#[test]
fn deficit_validation_accepts_block_info_rows_with_appended_fields() {
    use crate::service::finalized_state::{
        disk_format::{
            upgrade::{nsm_value_balance_pool, DiskFormatUpgrade},
            FromDisk, IntoDisk, RawBytes,
        },
        DiskWriteBatch,
    };

    let _guard = zakura_test::init();
    let network = accounting_network(false);
    let (mut state, parent) = state_below_start(&network);
    let block = permitted_start_block(&state, &network, &parent);
    commit(&mut state, &block).unwrap();

    // A later format can append fields to BlockInfo, which a downgrade to this format
    // must still validate.
    let mut bytes = state
        .db
        .raw_block_info_cf()
        .zs_get(&START)
        .unwrap()
        .as_bytes();
    bytes.extend_from_slice(&[0xa5; 4]);
    let mut batch = DiskWriteBatch::new();
    let _ = state
        .db
        .raw_block_info_cf()
        .with_batch_for_writing(&mut batch)
        .zs_insert(&START, &RawBytes::from_bytes(bytes));
    state.db.write_batch(batch).unwrap();

    let (_cancel_sender, cancel_receiver) =
        crate::service::finalized_state::disk_format::upgrade::cancel_channel();
    assert_eq!(
        nsm_value_balance_pool::Upgrade
            .validate(&state.db, &cancel_receiver)
            .unwrap(),
        Ok(())
    );
}