zakura-state 1.0.0

State contextual verification and storage code for the Zakura node. Internal crate, published to support cargo install zakura
Documentation
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
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
//! Tests for the offline finalized-state rollback utility.
//!
//! The core test is a round-trip equivalence check: sync a finalized state to height `N`, roll it
//! back to `M`, and assert it is *semantically* equivalent to a state freshly synced to `M`. We
//! compare via queries (tip, value pool, note-commitment/history roots, transparent balances and
//! UTXOs, nullifier presence) rather than raw bytes. Reopening each database also runs the
//! de-duplicate-tree format check, which guards against rollback leaving a duplicate sapling or
//! orchard tree entry (it would brick the node on the next startup).

use std::{env, path::Path, sync::Arc};

use tempfile::TempDir;

use zakura_chain::{
    amount::{Amount, NonNegative, MAX_MONEY},
    block::{self, Block, Height},
    ironwood, orchard,
    parameters::{
        testnet::{ConfiguredActivationHeights, Parameters as TestnetParameters},
        Network, NetworkKind, NetworkUpgrade,
    },
    serialization::{BytesInDisplayOrder, ZcashDeserializeInto},
    subtree::NoteCommitmentSubtree,
    transaction::{LockTime, Transaction},
    transparent::{self, Address, Input, OutPoint, Output, Script},
    LedgerState,
};
use zakura_test::prelude::*;

use crate::{
    config::Config,
    constants::{state_database_format_version_in_code, STATE_DATABASE_KIND},
    preview_rollback_finalized_state, rollback_finalized_state,
    service::{
        arbitrary::PreparedChain,
        finalized_state::{CheckpointVerifiedBlock, FinalizedState, STATE_COLUMN_FAMILIES_IN_CODE},
    },
    DiskWriteBatch, RollbackFinalizedStateError, RollbackFinalizedStateOptions,
    SemanticallyVerifiedBlock, ZakuraDb,
};

/// Number of proptest cases. Each case syncs and rolls back several on-disk databases, so the
/// default is low; raise it with `PROPTEST_CASES` for deeper coverage.
const DEFAULT_ROLLBACK_PROPTEST_CASES: u32 = 1;

/// Cap on the synced height, to keep the genesis-to-target treestate rebuild fast.
const MAX_SYNCED_BLOCKS: usize = 12;

fn proptest_cases() -> u32 {
    env::var("PROPTEST_CASES")
        .ok()
        .and_then(|v| v.parse().ok())
        .unwrap_or(DEFAULT_ROLLBACK_PROPTEST_CASES)
}

/// A persistent (non-ephemeral) state config rooted at `dir`, so the rollback utility can reopen
/// the same database by path after the syncing handle is dropped.
fn config_at(dir: &Path) -> Config {
    Config {
        cache_dir: dir.to_path_buf(),
        ephemeral: false,
        ..Config::default()
    }
}

fn height(n: usize) -> Height {
    Height(u32::try_from(n).expect("test heights fit in u32"))
}

/// Syncs a fresh finalized state at `config` by committing `blocks` in order, then drops it so the
/// database lock is released for the rollback utility to reopen.
fn sync_to(config: &Config, network: &Network, blocks: &[SemanticallyVerifiedBlock]) {
    let mut state = FinalizedState::new(
        config,
        network,
        #[cfg(feature = "elasticsearch")]
        false,
    )
    .expect("opening an ephemeral database should succeed");

    for block in blocks {
        let checkpoint_verified = CheckpointVerifiedBlock::from(block.block.clone());
        state
            .commit_finalized_direct(checkpoint_verified.into(), None, None, "rollback test")
            .expect("committing a generated block to a fresh state succeeds");
    }
}

/// Reopens the finalized state at `config` for read queries.
fn reopen(config: &Config, network: &Network) -> FinalizedState {
    FinalizedState::new(
        config,
        network,
        #[cfg(feature = "elasticsearch")]
        false,
    )
    .expect("opening an ephemeral database should succeed")
}

/// Opens the database at `config` directly, skipping format upgrades and their validation. The
/// subtree test writes synthetic subtree entries that don't correspond to real notes, which the
/// add-subtrees format check would reject.
fn open_unchecked_db(config: &Config, network: &Network) -> ZakuraDb {
    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,
    )
    .expect("opening the finalized state database should succeed")
}

/// Asserts that `rolled` (a state rolled back to the tip of `retained`) is semantically equivalent
/// to `fresh` (a state synced only up to that tip), and that all data from the `removed` blocks is
/// gone from `rolled`.
fn assert_equivalent(
    rolled: &FinalizedState,
    fresh: &FinalizedState,
    network: &Network,
    retained: &[SemanticallyVerifiedBlock],
    removed: &[SemanticallyVerifiedBlock],
) {
    let (rolled, fresh) = (&rolled.db, &fresh.db);

    // Aggregates that fold in every committed block and note.
    assert_eq!(rolled.tip(), fresh.tip(), "tip");
    assert_eq!(
        rolled.finalized_value_pool(),
        fresh.finalized_value_pool(),
        "value pool"
    );
    assert_eq!(
        rolled.sprout_tree_for_tip().root(),
        fresh.sprout_tree_for_tip().root(),
        "sprout root"
    );
    assert_eq!(
        rolled.sapling_tree_for_tip().root(),
        fresh.sapling_tree_for_tip().root(),
        "sapling root"
    );
    assert_eq!(
        rolled.orchard_tree_for_tip().root(),
        fresh.orchard_tree_for_tip().root(),
        "orchard root"
    );
    assert_eq!(
        rolled.history_tree().hash(),
        fresh.history_tree().hash(),
        "history root"
    );

    // Transparent indexes and shielded nullifiers in the retained range must match a fresh sync.
    for block in retained {
        for transaction in &block.block.transactions {
            let tx_hash = transaction.hash();

            for (index, output) in transaction.outputs().iter().enumerate() {
                let outpoint = transparent::OutPoint::from_usize(tx_hash, index);
                assert_eq!(
                    rolled.utxo(&outpoint).is_some(),
                    fresh.utxo(&outpoint).is_some(),
                    "utxo presence for {outpoint:?}"
                );

                if let Some(address) = output.address(network) {
                    assert_eq!(
                        rolled.address_balance(&address),
                        fresh.address_balance(&address),
                        "balance for {address:?}"
                    );
                }
            }

            for nullifier in transaction.sprout_nullifiers() {
                assert!(
                    rolled.contains_sprout_nullifier(nullifier),
                    "retained sprout nullifier"
                );
            }
            for nullifier in transaction.sapling_nullifiers() {
                assert!(
                    rolled.contains_sapling_nullifier(nullifier),
                    "retained sapling nullifier"
                );
            }
            for nullifier in transaction.orchard_nullifiers() {
                assert!(
                    rolled.contains_orchard_nullifier(nullifier),
                    "retained orchard nullifier"
                );
            }
        }
    }

    // Everything from the removed blocks must be gone.
    for block in removed {
        assert!(
            rolled.height(block.hash).is_none(),
            "removed block {:?} is still present",
            block.hash
        );

        for transaction in &block.block.transactions {
            for nullifier in transaction.sprout_nullifiers() {
                assert!(
                    !rolled.contains_sprout_nullifier(nullifier),
                    "removed sprout nullifier"
                );
            }
            for nullifier in transaction.sapling_nullifiers() {
                assert!(
                    !rolled.contains_sapling_nullifier(nullifier),
                    "removed sapling nullifier"
                );
            }
            for nullifier in transaction.orchard_nullifiers() {
                assert!(
                    !rolled.contains_orchard_nullifier(nullifier),
                    "removed orchard nullifier"
                );
            }
        }
    }
}

/// Counts non-coinbase transparent inputs and shielded nullifiers across `blocks`, used to confirm
/// that a rolled-back range actually exercises the spend-reversal and nullifier-deletion paths
/// rather than passing over coinbase-only blocks.
fn count_reversible_features(blocks: &[SemanticallyVerifiedBlock]) -> (usize, usize) {
    let mut transparent_spends = 0;
    let mut shielded_nullifiers = 0;

    for block in blocks {
        for transaction in &block.block.transactions {
            transparent_spends += transaction
                .inputs()
                .iter()
                .filter(|input| input.outpoint().is_some())
                .count();
            shielded_nullifiers += transaction.sprout_nullifiers().count()
                + transaction.sapling_nullifiers().count()
                + transaction.orchard_nullifiers().count();
        }
    }

    (transparent_spends, shielded_nullifiers)
}

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

    proptest!(
        ProptestConfig::with_cases(proptest_cases()),
        // no_shrink: a failure points at the rollback logic, not the chain shape, and re-syncing
        // databases to shrink would be slow with little diagnostic value.
        |((chain, _count, network, _history_tree) in PreparedChain::default().no_shrink())| {
            // Use the whole generated chain. Transparent outputs only become spendable once the
            // coinbase maturity (100 blocks) has elapsed, so spends appear only near the tip; a
            // short prefix would never exercise the transparent spend-reversal path.
            let synced: Vec<SemanticallyVerifiedBlock> = chain.iter().cloned().collect();
            prop_assume!(synced.len() > 2);
            let tip = synced.len() - 1;

            // Roll back to genesis (reverses every block) and to the middle. Both ranges include
            // the spend-bearing tail near the tip.
            for target in [0, tip / 2] {
                let removed = &synced[target + 1..];

                // Guard against a vacuous pass: both ranges must actually reverse transparent
                // spends and delete shielded nullifiers, not just drop coinbase-only blocks.
                let (transparent_spends, shielded_nullifiers) = count_reversible_features(removed);
                prop_assert!(
                    transparent_spends > 0,
                    "rolled-back range must exercise transparent spend reversal"
                );
                prop_assert!(
                    shielded_nullifiers > 0,
                    "rolled-back range must exercise shielded nullifier deletion"
                );

                let synced_dir = TempDir::new().expect("temp dir");
                let fresh_dir = TempDir::new().expect("temp dir");
                let synced_config = config_at(synced_dir.path());
                let fresh_config = config_at(fresh_dir.path());

                sync_to(&synced_config, &network, &synced);
                sync_to(&fresh_config, &network, &synced[..=target]);

                let summary = rollback_finalized_state(
                    synced_config.clone(),
                    &network,
                    RollbackFinalizedStateOptions {
                        target_height: height(target),
                        keep_rolled_back_blocks: false,
                        max_checkpoint_height: None,
                    },
                )
                .expect("rollback to a valid lower height succeeds");

                prop_assert_eq!(summary.new_tip.0, height(target));
                prop_assert_eq!(summary.rolled_back_count, height(tip).0 - height(target).0);

                assert_equivalent(
                    &reopen(&synced_config, &network),
                    &reopen(&fresh_config, &network),
                    &network,
                    &synced[..=target],
                    removed,
                );
            }
        }
    );

    Ok(())
}

/// Returns the lowest height below the tip whose sapling and orchard trees match the previous
/// block — i.e. the block added no shielded notes, so the forward write stored no new tree there.
/// Rolling back to such a height is the case that would leave a duplicate de-duplicated tree entry
/// if the rollback re-wrote the tip trees.
fn first_unchanged_tree_height(state: &FinalizedState, tip: usize) -> Option<Height> {
    (1..tip).find_map(|n| {
        let (h, prev) = (height(n), height(n - 1));
        let sapling_unchanged = state.db.sapling_tree_by_height(&h).map(|tree| tree.root())
            == state
                .db
                .sapling_tree_by_height(&prev)
                .map(|tree| tree.root());
        let orchard_unchanged = state.db.orchard_tree_by_height(&h).map(|tree| tree.root())
            == state
                .db
                .orchard_tree_by_height(&prev)
                .map(|tree| tree.root());
        (sapling_unchanged && orchard_unchanged).then_some(h)
    })
}

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

    proptest!(
        ProptestConfig::with_cases(proptest_cases()),
        |((chain, _count, network, _history_tree) in PreparedChain::default().no_shrink())| {
            let synced: Vec<SemanticallyVerifiedBlock> = chain.iter().cloned().collect();
            prop_assume!(synced.len() > 2);
            let tip = synced.len() - 1;

            let dir = TempDir::new().expect("temp dir");
            let config = config_at(dir.path());
            sync_to(&config, &network, &synced);

            // Roll back to a height whose block added no shielded notes. The de-duplicated tree
            // column families store no entry there, so re-writing the tip tree at that height
            // would duplicate the most recent stored tree.
            let target = {
                let synced_state = reopen(&config, &network);
                first_unchanged_tree_height(&synced_state, tip)
            };
            prop_assume!(target.is_some());
            let target = target.expect("checked is_some above");

            rollback_finalized_state(
                config.clone(),
                &network,
                RollbackFinalizedStateOptions {
                    target_height: target,
                    keep_rolled_back_blocks: false,
                    max_checkpoint_height: None,
                },
            )
            .expect("rollback to an unchanged-tree height succeeds");

            // Reopening runs the de-duplicate-tree format check, which panics on a duplicate entry.
            let rolled = reopen(&config, &network);
            prop_assert_eq!(rolled.db.tip().map(|(tip_height, _)| tip_height), Some(target));
        }
    );

    Ok(())
}

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

    proptest!(
        ProptestConfig::with_cases(proptest_cases()),
        |((chain, count, network, _history_tree) in PreparedChain::default().no_shrink())| {
            let synced: Vec<SemanticallyVerifiedBlock> =
                chain.iter().take(count.min(MAX_SYNCED_BLOCKS)).cloned().collect();
            prop_assume!(synced.len() >= 2);
            let tip = synced.len() - 1;

            let synced_dir = TempDir::new().expect("temp dir");
            let synced_config = config_at(synced_dir.path());
            sync_to(&synced_config, &network, &synced);

            // The validation errors surface through the read-only preview path, so none of these
            // mutate the database.
            let preview = |target: usize, keep: bool, max: Option<Height>| {
                preview_rollback_finalized_state(
                    synced_config.clone(),
                    &network,
                    RollbackFinalizedStateOptions {
                        target_height: height(target),
                        keep_rolled_back_blocks: keep,
                        max_checkpoint_height: max,
                    },
                )
            };

            prop_assert!(
                matches!(
                    preview(tip, false, None),
                    Err(RollbackFinalizedStateError::TargetIsTip { .. })
                ),
                "rolling back to the tip is rejected"
            );
            prop_assert!(
                matches!(
                    preview(tip + 1, false, None),
                    Err(RollbackFinalizedStateError::TargetAboveTip { .. })
                ),
                "rolling back above the tip is rejected"
            );
            // Bug #2: keeping blocks below the max checkpoint height would silently drop them.
            prop_assert!(
                matches!(
                    preview(tip - 1, true, Some(height(tip))),
                    Err(RollbackFinalizedStateError::KeepBelowMaxCheckpoint { .. })
                ),
                "keeping blocks below the max checkpoint is rejected"
            );
            // The same target is allowed once it is at/above the max checkpoint height.
            prop_assert!(
                preview(tip - 1, true, Some(height(tip - 1))).is_ok(),
                "keeping blocks at the max checkpoint is allowed"
            );

            // An empty database has no tip to roll back from.
            let empty_dir = TempDir::new().expect("temp dir");
            let empty_config = config_at(empty_dir.path());
            sync_to(&empty_config, &network, &[]);
            prop_assert!(
                matches!(
                    preview_rollback_finalized_state(
                        empty_config,
                        &network,
                        RollbackFinalizedStateOptions {
                            target_height: Height(0),
                            keep_rolled_back_blocks: false,
                            max_checkpoint_height: None,
                        },
                    ),
                    Err(RollbackFinalizedStateError::EmptyState)
                ),
                "rolling back an empty state is rejected"
            );
        }
    );

    Ok(())
}

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

    proptest!(
        ProptestConfig::with_cases(proptest_cases()),
        |((chain, count, network, _history_tree) in PreparedChain::default().no_shrink())| {
            let synced: Vec<SemanticallyVerifiedBlock> =
                chain.iter().take(count.min(MAX_SYNCED_BLOCKS)).cloned().collect();
            prop_assume!(synced.len() >= 3);
            let tip = synced.len() - 1;
            let target = tip - 2;

            let synced_dir = TempDir::new().expect("temp dir");
            let synced_config = config_at(synced_dir.path());
            sync_to(&synced_config, &network, &synced);

            let summary = rollback_finalized_state(
                synced_config,
                &network,
                RollbackFinalizedStateOptions {
                    target_height: height(target),
                    keep_rolled_back_blocks: true,
                    // A zero max checkpoint height keeps the request above the checkpoint guard.
                    max_checkpoint_height: Some(Height(0)),
                },
            )
            .expect("keeping rolled-back blocks succeeds above the max checkpoint");

            let backup = summary
                .backup
                .expect("keep_rolled_back_blocks records a backup summary");
            prop_assert_eq!(backup.block_count, tip - target);
            prop_assert!(backup.path.exists(), "backup directory was written");
        }
    );

    Ok(())
}

/// A V1 coinbase transaction at `height` paying `value` to `address`. The miner data pads the
/// coinbase script past `MIN_COINBASE_SCRIPT_LEN` so it round-trips through the database.
fn coinbase_tx(height: Height, value: Amount<NonNegative>, address: &Address) -> Arc<Transaction> {
    Arc::new(Transaction::V1 {
        inputs: vec![Input::Coinbase {
            height,
            data: vec![0x00; 8],
            sequence: u32::MAX,
        }],
        outputs: vec![Output::new(value, address.script())],
        lock_time: LockTime::unlocked(),
    })
}

/// A V1 transaction spending `outpoint` and paying `value` to `address`.
fn spend_tx(outpoint: OutPoint, value: Amount<NonNegative>, address: &Address) -> Arc<Transaction> {
    Arc::new(Transaction::V1 {
        inputs: vec![Input::PrevOut {
            outpoint,
            unlock_script: Script::new(&[]),
            sequence: u32::MAX,
        }],
        outputs: vec![Output::new(value, address.script())],
        lock_time: LockTime::unlocked(),
    })
}

/// Builds a child of `parent` containing `transactions` (whose coinbase encodes the new height).
/// The checkpoint commit doesn't validate the pre-Sapling commitment or merkle root, so the
/// parent's header is reused with only the parent hash updated.
fn child_block(parent: &Block, transactions: Vec<Arc<Transaction>>) -> Arc<Block> {
    let header = block::Header {
        previous_block_hash: parent.hash(),
        ..*parent.header
    };
    Arc::new(Block {
        header: Arc::new(header),
        transactions,
    })
}

/// A V4 transaction with one dummy Sprout JoinSplit whose two commitments grow the Sprout note
/// commitment tree. The checkpoint commit path doesn't validate the JoinSplit, so the rest is
/// zeroed.
fn sprout_joinsplit_tx() -> Arc<Transaction> {
    let joinsplit = zakura_chain::sprout::JoinSplit::<zakura_chain::primitives::Groth16Proof> {
        vpub_old: Amount::zero(),
        vpub_new: Amount::zero(),
        anchor: zakura_chain::sprout::tree::Root::default(),
        nullifiers: [
            zakura_chain::sprout::note::Nullifier::from([0; 32]),
            zakura_chain::sprout::note::Nullifier::from([1; 32]),
        ],
        commitments: [
            zakura_chain::sprout::commitment::NoteCommitment::from([2; 32]),
            zakura_chain::sprout::commitment::NoteCommitment::from([3; 32]),
        ],
        ephemeral_key: zakura_chain::primitives::x25519::PublicKey::from([0; 32]),
        random_seed: zakura_chain::sprout::RandomSeed::from([0; 32]),
        vmacs: [
            zakura_chain::sprout::note::Mac::from([0; 32]),
            zakura_chain::sprout::note::Mac::from([0; 32]),
        ],
        zkproof: zakura_chain::primitives::Groth16Proof::from([0; 192]),
        enc_ciphertexts: [
            zakura_chain::sprout::note::EncryptedNote([0; 601]),
            zakura_chain::sprout::note::EncryptedNote([0; 601]),
        ],
    };

    Arc::new(Transaction::V4 {
        inputs: Vec::new(),
        outputs: Vec::new(),
        lock_time: LockTime::unlocked(),
        expiry_height: Height(0),
        joinsplit_data: Some(zakura_chain::transaction::JoinSplitData {
            first: joinsplit,
            rest: Vec::new(),
            pub_key: zakura_chain::primitives::ed25519::VerificationKeyBytes::from([0; 32]),
            sig: zakura_chain::primitives::ed25519::Signature::from([0; 64]),
        }),
        sapling_shielded_data: None,
    })
}

fn ironwood_v6_tx(expiry_height: Height) -> (Arc<Transaction>, ironwood::Nullifier) {
    use proptest::{prelude::any, strategy::ValueTree, test_runner::TestRunner};
    use zakura_chain::{
        at_least_one,
        orchard::{self, tree},
        primitives::Halo2Proof,
    };

    let mut runner = TestRunner::default();
    let action = any::<ironwood::Action>()
        .new_tree(&mut runner)
        .expect("test action strategy creates a value")
        .current();
    let nullifier = action.nullifier;

    let ironwood_shielded_data = ironwood::ShieldedData {
        flags: orchard::Flags::ENABLE_SPENDS,
        value_balance: Amount::zero(),
        shared_anchor: tree::Root::default(),
        proof: Halo2Proof(vec![0; 4992]),
        actions: at_least_one![ironwood::AuthorizedAction {
            action,
            spend_auth_sig: [0u8; 64].into(),
        }],
        binding_sig: [0u8; 64].into(),
    };

    (
        Arc::new(Transaction::V6 {
            network_upgrade: NetworkUpgrade::Nu6_3,
            lock_time: LockTime::unlocked(),
            expiry_height,
            inputs: Vec::new(),
            outputs: Vec::new(),
            sapling_shielded_data: None,
            orchard_shielded_data: None,
            ironwood_shielded_data: Some(ironwood_shielded_data),
        }),
        nullifier,
    )
}

fn child_block_with_history_commitment(
    parent: &Block,
    transactions: Vec<Arc<Transaction>>,
    network: &Network,
    previous_history_tree: &zakura_chain::history_tree::HistoryTree,
) -> Arc<Block> {
    let mut block = child_block(parent, transactions);
    let height = block
        .coinbase_height()
        .expect("test block must have a coinbase height");
    let history_tree_root = previous_history_tree
        .hash()
        .expect("modern test block must have a previous history tree root");

    let commitment_bytes = if NetworkUpgrade::current(network, height) >= NetworkUpgrade::Nu5 {
        zakura_chain::block::ChainHistoryBlockTxAuthCommitmentHash::from_commitments(
            &history_tree_root,
            &block.auth_data_root(),
        )
        .bytes_in_serialized_order()
    } else {
        history_tree_root.bytes_in_serialized_order()
    };

    let block_mut = Arc::make_mut(&mut block);
    Arc::make_mut(&mut block_mut.header).commitment_bytes = commitment_bytes.into();

    block
}

/// Rolling back a same-address transparent self-spend chain must not transiently push the address
/// balance above `MAX_MONEY`. The buggy reversal re-added every spent input before subtracting the
/// created outputs, so the running balance reached `1.5 * MAX_MONEY` and aborted; the fix undoes
/// each transaction in block order (un-credit then un-debit), keeping it in `[0, MAX_MONEY]`.
#[test]
fn rollback_reverses_intra_block_self_spend() {
    let _init_guard = zakura_test::init();

    let network = Network::Mainnet;
    let address = Address::from_script_hash(NetworkKind::Mainnet, [0x42; 20]);
    let throwaway = Address::from_script_hash(NetworkKind::Mainnet, [0x17; 20]);
    let value = Amount::<NonNegative>::try_from(MAX_MONEY / 2)
        .expect("MAX_MONEY / 2 fits in Amount<NonNegative>");
    let dust = Amount::<NonNegative>::try_from(1).expect("1 fits in Amount<NonNegative>");

    let genesis: Arc<Block> = zakura_test::vectors::BLOCK_MAINNET_GENESIS_BYTES
        .zcash_deserialize_into()
        .expect("mainnet genesis test vector deserializes");

    // Height 1: a coinbase paying `value` to the address.
    let block1 = child_block(&genesis, vec![coinbase_tx(Height(1), value, &address)]);
    let block1_coinbase = OutPoint {
        hash: block1.transactions[0].hash(),
        index: 0,
    };

    // Height 2: spend the height-1 coinbase back to the same address, then spend that output to the
    // same address again. With the coinbase this is a same-address self-spend chain.
    let t0 = spend_tx(block1_coinbase, value, &address);
    let t0_output = OutPoint {
        hash: t0.hash(),
        index: 0,
    };
    let t1 = spend_tx(t0_output, value, &address);
    let block2 = child_block(
        &block1,
        vec![coinbase_tx(Height(2), dust, &throwaway), t0, t1],
    );

    let chain: Vec<SemanticallyVerifiedBlock> = [genesis, block1, block2]
        .into_iter()
        .map(SemanticallyVerifiedBlock::from)
        .collect();

    let dir = TempDir::new().expect("temp dir");
    let config = config_at(dir.path());
    sync_to(&config, &network, &chain);

    // Roll back to height 1. Reversing height 2 re-credits the address's two spent inputs
    // (2 * value) on top of its current balance (value); the buggy order reached 3 * value.
    rollback_finalized_state(
        config.clone(),
        &network,
        RollbackFinalizedStateOptions {
            target_height: Height(1),
            keep_rolled_back_blocks: false,
            max_checkpoint_height: None,
        },
    )
    .expect("reversing a same-address self-spend chain must not overflow the address balance");

    let rolled = reopen(&config, &network);
    assert_eq!(rolled.db.tip().map(|(h, _)| h), Some(Height(1)), "tip");
    assert_eq!(
        rolled
            .db
            .address_balance(&address)
            .map(|(balance, _)| balance),
        Some(value),
        "address balance is restored to its height-1 value",
    );
}

/// When a rolled-back block grew the Sprout tree, rollback must reset the Sprout tip to the target
/// height's tree, not leave it at the later tip.
#[test]
fn rollback_resets_sprout_tree_changed_in_range() {
    let _init_guard = zakura_test::init();

    let network = Network::Mainnet;
    let address = Address::from_script_hash(NetworkKind::Mainnet, [0x42; 20]);
    let dust = Amount::<NonNegative>::try_from(1).expect("1 fits in Amount<NonNegative>");

    let genesis: Arc<Block> = zakura_test::vectors::BLOCK_MAINNET_GENESIS_BYTES
        .zcash_deserialize_into()
        .expect("mainnet genesis test vector deserializes");
    let block1 = child_block(&genesis, vec![coinbase_tx(Height(1), dust, &address)]);
    let block2 = child_block(
        &block1,
        vec![
            coinbase_tx(Height(2), dust, &address),
            sprout_joinsplit_tx(),
        ],
    );

    let to_height_1: Vec<SemanticallyVerifiedBlock> = [genesis.clone(), block1.clone()]
        .into_iter()
        .map(SemanticallyVerifiedBlock::from)
        .collect();
    let to_height_2: Vec<SemanticallyVerifiedBlock> = [genesis, block1, block2]
        .into_iter()
        .map(SemanticallyVerifiedBlock::from)
        .collect();

    let sprout_root_at = |blocks: &[SemanticallyVerifiedBlock]| {
        let dir = TempDir::new().expect("temp dir");
        let config = config_at(dir.path());
        sync_to(&config, &network, blocks);
        open_unchecked_db(&config, &network)
            .sprout_tree_for_tip()
            .root()
    };
    let root_at_1 = sprout_root_at(&to_height_1);
    let root_at_2 = sprout_root_at(&to_height_2);
    assert_ne!(
        root_at_1, root_at_2,
        "the JoinSplit must grow the sprout tree in the rolled-back range"
    );

    let dir = TempDir::new().expect("temp dir");
    let config = config_at(dir.path());
    sync_to(&config, &network, &to_height_2);
    rollback_finalized_state(
        config.clone(),
        &network,
        RollbackFinalizedStateOptions {
            target_height: Height(1),
            keep_rolled_back_blocks: false,
            max_checkpoint_height: None,
        },
    )
    .expect("rollback succeeds");

    assert_eq!(
        open_unchecked_db(&config, &network)
            .sprout_tree_for_tip()
            .root(),
        root_at_1,
        "sprout tip reset to the target height, not left at the rolled-back tip",
    );
}

/// A configured testnet with early modern activation heights, so rollback tests can exercise
/// Canopy-and-later history roots without syncing hundreds of thousands of blocks.
fn modern_rollback_network() -> Network {
    TestnetParameters::build()
        .with_activation_heights(ConfiguredActivationHeights {
            before_overwinter: Some(1),
            overwinter: Some(2),
            sapling: Some(3),
            blossom: Some(4),
            heartwood: Some(5),
            canopy: Some(6),
            nu5: Some(7),
            nu6: Some(8),
            nu6_1: Some(9),
            nu6_2: Some(10),
            nu6_3: Some(11),
            nu7: Some(12),
        })
        .expect("configured activation heights are valid")
        .extend_funding_streams()
        .to_network()
        .expect("configured network is valid")
}

/// Modern rollback targets must not replay note commitment trees from genesis. This test deletes
/// the genesis block header after syncing; a genesis-to-target treestate rebuild would fail to load
/// height 0, while the modern path only rebuilds the history tree from Canopy activation.
#[test]
fn modern_rollback_does_not_rebuild_note_trees_from_genesis() -> Result<()> {
    let _init_guard = zakura_test::init();

    let network = modern_rollback_network();
    let target_height = NetworkUpgrade::Nu5
        .activation_height(&network)
        .expect("NU5 activation height is configured");
    let target_index = usize::try_from(target_height.0).expect("test height fits in usize");
    let ledger_strategy =
        LedgerState::genesis_strategy(Some(network), NetworkUpgrade::Nu5, Some(5), true);

    proptest!(
        ProptestConfig::with_cases(proptest_cases()),
        |((chain, _count, network, _history_tree) in PreparedChain::default()
            .with_ledger_strategy(ledger_strategy)
            .with_valid_commitments()
            .no_shrink())
            | {
            let synced: Vec<SemanticallyVerifiedBlock> = chain.iter().cloned().collect();
            prop_assume!(synced.len() > target_index + 1);
            prop_assert!(
                !synced[target_index + 1..]
                    .iter()
                    .any(|block| block.block.sprout_note_commitments().next().is_some()),
                "V5 blocks in the removed range must not contain Sprout commitments"
            );

            let synced_dir = TempDir::new().expect("temp dir");
            let fresh_dir = TempDir::new().expect("temp dir");
            let synced_config = config_at(synced_dir.path());
            let fresh_config = config_at(fresh_dir.path());

            sync_to(&synced_config, &network, &synced);
            sync_to(&fresh_config, &network, &synced[..=target_index]);

            let old_sprout_root = open_unchecked_db(&synced_config, &network)
                .sprout_tree_for_tip()
                .root();

            {
                let db = open_unchecked_db(&synced_config, &network);
                let mut batch = DiskWriteBatch::new();
                batch.delete_block_header(&db, Height(0));
                db.write_batch(batch)
                    .expect("database accepts the synthetic missing genesis header");
            }

            rollback_finalized_state(
                synced_config.clone(),
                &network,
                RollbackFinalizedStateOptions {
                    target_height,
                    keep_rolled_back_blocks: false,
                    max_checkpoint_height: None,
                },
            )
            .expect("modern rollback does not need the genesis block");

            let rolled = open_unchecked_db(&synced_config, &network);
            let fresh = reopen(&fresh_config, &network);

            prop_assert_eq!(rolled.tip(), fresh.db.tip(), "tip");
            prop_assert_eq!(
                rolled.finalized_value_pool(),
                fresh.db.finalized_value_pool(),
                "value pool"
            );
            prop_assert_eq!(
                rolled.sprout_tree_for_tip().root(),
                old_sprout_root,
                "modern rollback keeps the current Sprout tip tree"
            );
            prop_assert_eq!(
                rolled.sapling_tree_for_tip().root(),
                fresh.db.sapling_tree_for_tip().root(),
                "sapling root"
            );
            prop_assert_eq!(
                rolled.orchard_tree_for_tip().root(),
                fresh.db.orchard_tree_for_tip().root(),
                "orchard root"
            );
            prop_assert_eq!(
                rolled.history_tree().hash(),
                fresh.db.history_tree().hash(),
                "history root"
            );
        }
    );

    Ok(())
}

/// The same Sprout fallback must work for a modern target. If the removed range contains a Sprout
/// JoinSplit, rollback cannot use the Sprout tip tree as the target tree.
#[test]
fn modern_rollback_resets_sprout_tree_changed_in_range() -> Result<()> {
    let _init_guard = zakura_test::init();

    let network = modern_rollback_network();
    let target_height = NetworkUpgrade::Nu5
        .activation_height(&network)
        .expect("NU5 activation height is configured");
    let target_index = usize::try_from(target_height.0).expect("test height fits in usize");
    let ledger_strategy =
        LedgerState::genesis_strategy(Some(network), NetworkUpgrade::Nu5, Some(5), true);

    proptest!(
        ProptestConfig::with_cases(proptest_cases()),
        |((chain, _count, network, _history_tree) in PreparedChain::default()
            .with_ledger_strategy(ledger_strategy)
            .with_valid_commitments()
            .no_shrink())
            | {
            let prefix: Vec<SemanticallyVerifiedBlock> =
                chain.iter().take(target_index + 1).cloned().collect();
            prop_assume!(prefix.len() == target_index + 1);

            let target_dir = TempDir::new().expect("temp dir");
            let target_config = config_at(target_dir.path());
            sync_to(&target_config, &network, &prefix);

            let target_db = open_unchecked_db(&target_config, &network);
            let parent = prefix
                .last()
                .expect("target prefix has a tip block")
                .block
                .clone();
            let previous_history_tree = target_db.history_tree();
            let root_at_target = target_db.sprout_tree_for_tip().root();
            drop(target_db);

            let removed_height = (target_height + 1).expect("test target height is not max");
            let address = Address::from_script_hash(NetworkKind::Testnet, [0x42; 20]);
            let dust = Amount::<NonNegative>::try_from(1).expect("1 fits in Amount<NonNegative>");
            let sprout_block = child_block_with_history_commitment(
                &parent,
                vec![
                    coinbase_tx(removed_height, dust, &address),
                    sprout_joinsplit_tx(),
                ],
                &network,
                &previous_history_tree,
            );

            let mut synced = prefix;
            synced.push(SemanticallyVerifiedBlock::from(sprout_block));

            let sprout_dir = TempDir::new().expect("temp dir");
            let sprout_config = config_at(sprout_dir.path());
            sync_to(&sprout_config, &network, &synced);
            let root_at_tip = open_unchecked_db(&sprout_config, &network)
                .sprout_tree_for_tip()
                .root();
            prop_assert_ne!(
                root_at_target,
                root_at_tip,
                "the synthetic JoinSplit must grow Sprout after the modern target"
            );

            rollback_finalized_state(
                sprout_config.clone(),
                &network,
                RollbackFinalizedStateOptions {
                    target_height,
                    keep_rolled_back_blocks: false,
                    max_checkpoint_height: None,
                },
            )
            .expect("modern rollback with Sprout in the removed range succeeds");

            prop_assert_eq!(
                open_unchecked_db(&sprout_config, &network)
                    .sprout_tree_for_tip()
                    .root(),
                root_at_target,
                "modern rollback reset Sprout to the target height"
            );
        }
    );

    Ok(())
}

/// Rollback must prune note-commitment subtrees whose `end_height` is above the new tip, and keep
/// those at or below it. A subtree completes only every 2^16 notes, far more than any test chain,
/// so the subtree entries are inserted directly into the database.
#[test]
fn rollback_prunes_subtrees_above_target() {
    let _init_guard = zakura_test::init();

    let network = Network::Mainnet;
    let address = Address::from_script_hash(NetworkKind::Mainnet, [0x42; 20]);
    let dust = Amount::<NonNegative>::try_from(1).expect("1 fits in Amount<NonNegative>");

    let genesis: Arc<Block> = zakura_test::vectors::BLOCK_MAINNET_GENESIS_BYTES
        .zcash_deserialize_into()
        .expect("mainnet genesis test vector deserializes");
    let block1 = child_block(&genesis, vec![coinbase_tx(Height(1), dust, &address)]);
    let block2 = child_block(&block1, vec![coinbase_tx(Height(2), dust, &address)]);
    let block3 = child_block(&block2, vec![coinbase_tx(Height(3), dust, &address)]);

    let chain: Vec<SemanticallyVerifiedBlock> = [genesis, block1, block2, block3]
        .into_iter()
        .map(SemanticallyVerifiedBlock::from)
        .collect();

    let dir = TempDir::new().expect("temp dir");
    let config = config_at(dir.path());
    sync_to(&config, &network, &chain);

    // Insert subtrees straddling the rollback target (height 2): index 0 ends at or below it,
    // and index 1 ends above it.
    let sapling_node = sapling_crypto::Node::from_bytes([0; 32]).expect("dummy sapling node");
    let orchard_node = orchard::tree::Node::default();
    let ironwood_node = ironwood::tree::Node::default();
    {
        let db = open_unchecked_db(&config, &network);
        let mut batch = DiskWriteBatch::new();
        batch.insert_sapling_subtree(
            &db,
            &NoteCommitmentSubtree::new(0u16, Height(2), sapling_node),
        );
        batch.insert_sapling_subtree(
            &db,
            &NoteCommitmentSubtree::new(1u16, Height(3), sapling_node),
        );
        batch.insert_orchard_subtree(
            &db,
            &NoteCommitmentSubtree::new(0u16, Height(1), orchard_node),
        );
        batch.insert_orchard_subtree(
            &db,
            &NoteCommitmentSubtree::new(1u16, Height(3), orchard_node),
        );
        batch.insert_ironwood_subtree(
            &db,
            &NoteCommitmentSubtree::new(0u16, Height(2), ironwood_node),
        );
        batch.insert_ironwood_subtree(
            &db,
            &NoteCommitmentSubtree::new(1u16, Height(3), ironwood_node),
        );
        db.write_batch(batch)
            .expect("database accepts the synthetic subtree batch");
    }

    rollback_finalized_state(
        config.clone(),
        &network,
        RollbackFinalizedStateOptions {
            target_height: Height(2),
            keep_rolled_back_blocks: false,
            max_checkpoint_height: None,
        },
    )
    .expect("rollback succeeds");

    let rolled = open_unchecked_db(&config, &network);
    let sapling: Vec<u16> = rolled
        .sapling_subtree_list_by_index_range(..)
        .keys()
        .map(|index| index.0)
        .collect();
    let orchard: Vec<u16> = rolled
        .orchard_subtree_list_by_index_range(..)
        .keys()
        .map(|index| index.0)
        .collect();
    let ironwood: Vec<u16> = rolled
        .ironwood_subtree_list_by_index_range(..)
        .keys()
        .map(|index| index.0)
        .collect();

    assert_eq!(
        sapling,
        vec![0],
        "sapling subtree above the target is pruned"
    );
    assert_eq!(
        orchard,
        vec![0],
        "orchard subtree above the target is pruned"
    );
    assert_eq!(
        ironwood,
        vec![0],
        "ironwood subtree above the target is pruned"
    );
}

/// Finalized state stores the empty genesis Ironwood tree and anchor even before any Ironwood note
/// changes the tree. Rollback should preserve that genesis empty root.
#[test]
fn modern_rollback_preserves_empty_genesis_ironwood_tree() -> Result<()> {
    let _init_guard = zakura_test::init();

    let network = modern_rollback_network();
    let target_height = NetworkUpgrade::Nu6_3
        .activation_height(&network)
        .expect("NU6.3 activation height is configured");
    let target_index = usize::try_from(target_height.0).expect("test height fits in usize");
    let ledger_strategy =
        LedgerState::genesis_strategy(Some(network), NetworkUpgrade::Nu5, Some(5), true);

    proptest!(
        ProptestConfig::with_cases(proptest_cases()),
        |((chain, _count, network, _history_tree) in PreparedChain::default()
            .with_ledger_strategy(ledger_strategy)
            .with_valid_commitments()
            .no_shrink())
            | {
            let synced: Vec<SemanticallyVerifiedBlock> = chain.iter().cloned().collect();
            prop_assume!(synced.len() > target_index + 1);

            let dir = TempDir::new().expect("temp dir");
            let config = config_at(dir.path());
            sync_to(&config, &network, &synced);

            {
                let db = open_unchecked_db(&config, &network);
                let Some((height, ironwood_tree)) =
                    db.ironwood_tree_by_height_range(..=target_height).last()
                else {
                    prop_assert!(false, "genesis stores the empty Ironwood tree");
                    return Ok(());
                };

                prop_assert_eq!(height, Height::MIN);
                prop_assert_eq!(
                    ironwood_tree.root(),
                    ironwood::tree::NoteCommitmentTree::default().root(),
                    "genesis Ironwood tree is empty"
                );
                prop_assert!(
                    db.contains_ironwood_anchor(&ironwood_tree.root()),
                    "genesis Ironwood anchor is indexed"
                );
            }

            rollback_finalized_state(
                config.clone(),
                &network,
                RollbackFinalizedStateOptions {
                    target_height,
                    keep_rolled_back_blocks: false,
                    max_checkpoint_height: None,
                },
            )
            .expect("rollback succeeds while preserving the empty genesis Ironwood tree");

            let rolled = open_unchecked_db(&config, &network);
            prop_assert_eq!(rolled.tip().map(|(height, _hash)| height), Some(target_height));
            prop_assert_eq!(
                rolled.ironwood_tree_for_tip().root(),
                ironwood::tree::NoteCommitmentTree::default().root(),
                "rollback preserves the empty genesis Ironwood tree"
            );
        }
    );

    Ok(())
}

#[test]
fn rollback_prunes_ironwood_nullifiers_above_target() {
    let _init_guard = zakura_test::init();

    let network = Network::Mainnet;
    let address = Address::from_script_hash(NetworkKind::Mainnet, [0x42; 20]);
    let dust = Amount::<NonNegative>::try_from(1).expect("1 fits in Amount<NonNegative>");

    let genesis: Arc<Block> = zakura_test::vectors::BLOCK_MAINNET_GENESIS_BYTES
        .zcash_deserialize_into()
        .expect("mainnet genesis test vector deserializes");
    let block1 = child_block(&genesis, vec![coinbase_tx(Height(1), dust, &address)]);
    let (ironwood_tx, ironwood_nullifier) = ironwood_v6_tx(Height(2));
    let block2 = child_block(
        &block1,
        vec![coinbase_tx(Height(2), dust, &address), ironwood_tx],
    );

    let chain: Vec<SemanticallyVerifiedBlock> = [genesis, block1, block2]
        .into_iter()
        .map(SemanticallyVerifiedBlock::from)
        .collect();

    let dir = TempDir::new().expect("temp dir");
    let config = config_at(dir.path());
    sync_to(&config, &network, &chain);

    assert!(
        open_unchecked_db(&config, &network).contains_ironwood_nullifier(&ironwood_nullifier),
        "forward sync indexes the Ironwood nullifier"
    );

    rollback_finalized_state(
        config.clone(),
        &network,
        RollbackFinalizedStateOptions {
            target_height: Height(1),
            keep_rolled_back_blocks: false,
            max_checkpoint_height: None,
        },
    )
    .expect("rollback succeeds");

    assert!(
        !open_unchecked_db(&config, &network).contains_ironwood_nullifier(&ironwood_nullifier),
        "rollback prunes Ironwood nullifiers above the target"
    );
}