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
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
//! Tests for stopping optional transparent index writes in the existing state database.
//!
//! Pruned checkpoint sync keeps consensus state, stops index updates in place, and rejects
//! stale address queries. A durable marker preserves this across restarts and rollback.

use std::{
    collections::{BTreeMap, HashSet},
    path::Path,
    sync::Arc,
};

use crate::service::finalized_state::disk_format::upgrade::cancel_channel;
use tempfile::TempDir;
use tower::{Service, ServiceExt};

use zakura_chain::{
    amount::{Amount, NonNegative},
    block::{Block, Height},
    parameters::{Network, NetworkKind},
    serialization::ZcashDeserializeInto,
    transaction::{LockTime, Transaction},
    transparent::{Address, Input, OutPoint, Script},
};

use crate::{
    config::{Config, PruningConfig, StorageMode},
    constants::{state_database_format_version_in_code, STATE_DATABASE_KIND},
    init_read_only,
    request::{FinalizableBlock, Treestate},
    rollback_finalized_state,
    service::{
        check,
        finalized_state::{
            disk_format::upgrade::DbFormatChange,
            zakura_db::transparent::{BALANCE_BY_TRANSPARENT_ADDR, TX_LOC_BY_SPENT_OUT_LOC},
            CheckpointVerifiedBlock, FinalizedState, ZakuraDb, PRUNING_METADATA,
            STATE_COLUMN_FAMILIES_IN_CODE,
        },
        non_finalized_state::{CreatedUtxos, SharedMap},
    },
    ContextuallyVerifiedBlock, ReadRequest, ReadResponse, RollbackFinalizedStateError,
    RollbackFinalizedStateOptions, SemanticallyVerifiedBlock, StateInitError,
    TransparentIndexesUnavailable, ValidateContextError,
};

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

/// Column families that only hold transparent address or spender indexes.
const TRANSPARENT_INDEX_COLUMN_FAMILIES: [&str; 4] = [
    BALANCE_BY_TRANSPARENT_ADDR,
    "tx_loc_by_transparent_addr_loc",
    "utxo_loc_by_transparent_addr_loc",
    TX_LOC_BY_SPENT_OUT_LOC,
];

/// How a test node is configured.
#[derive(Clone, Copy, Debug)]
enum NodeConfig {
    /// Pruned storage with checkpoint sync: new databases skip transparent indexes.
    PrunedCheckpoint,
    /// Pruned storage with checkpoint sync disabled: new databases build transparent indexes.
    PrunedFullVerification,
    /// Archive storage with checkpoint sync.
    Archive,
}

fn config_at(dir: &Path, node: NodeConfig) -> Config {
    let (storage_mode, checkpoint_sync) = match node {
        NodeConfig::PrunedCheckpoint => (StorageMode::Pruned(PruningConfig::default()), true),
        NodeConfig::PrunedFullVerification => {
            (StorageMode::Pruned(PruningConfig::default()), false)
        }
        NodeConfig::Archive => (StorageMode::Archive, true),
    };

    Config {
        cache_dir: dir.to_path_buf(),
        ephemeral: false,
        storage_mode,
        checkpoint_sync,
        ..Config::default()
    }
}

fn amount(zatoshis: i64) -> Amount<NonNegative> {
    Amount::try_from(zatoshis).expect("test amounts are valid")
}

fn outpoint(transaction: &Transaction, index: u32) -> OutPoint {
    OutPoint {
        hash: transaction.hash(),
        index,
    }
}

/// A transaction spending `outpoints` with no transparent outputs, which is how a valid
/// transaction spends a coinbase output on networks that require coinbase spends to be shielded.
fn shielding_spend_tx(outpoints: &[OutPoint]) -> Arc<Transaction> {
    Arc::new(Transaction::V1 {
        inputs: outpoints
            .iter()
            .map(|outpoint| Input::PrevOut {
                outpoint: *outpoint,
                unlock_script: Script::new(&[]),
                sequence: u32::MAX,
            })
            .collect(),
        outputs: Vec::new(),
        lock_time: LockTime::unlocked(),
    })
}

/// A short Mainnet chain with coinbase outputs and transparent spends that cross blocks and stay
/// inside one block.
struct TestChain {
    network: Network,
    address: Address,
    other: Address,
    blocks: Vec<Arc<Block>>,
    /// The non-coinbase output created at height 3, unspent at the tip.
    unspent_output: OutPoint,
    /// The height 1 coinbase output, spent at height 2.
    spent_coinbase: OutPoint,
    /// The height 3 coinbase output, unspent at the tip.
    tip_coinbase: OutPoint,
}

fn test_chain() -> TestChain {
    let network = Network::Mainnet;
    let address = Address::from_script_hash(NetworkKind::Mainnet, [0x42; 20]);
    let other = Address::from_script_hash(NetworkKind::Mainnet, [0x17; 20]);

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

    let coinbase1 = coinbase_tx(Height(1), amount(50_000), &address);
    let block1 = child_block(&genesis, vec![coinbase1.clone()]);

    // Height 2 spends the height 1 coinbase, then spends that output again in the same block.
    let spend1 = spend_tx(outpoint(&coinbase1, 0), amount(40_000), &other);
    let spend2 = spend_tx(outpoint(&spend1, 0), amount(30_000), &address);
    let block2 = child_block(
        &block1,
        vec![
            coinbase_tx(Height(2), amount(1_000), &other),
            spend1,
            spend2.clone(),
        ],
    );

    // Height 3 spends an output from height 2.
    let coinbase3 = coinbase_tx(Height(3), amount(2_000), &address);
    let spend3 = spend_tx(outpoint(&spend2, 0), amount(20_000), &other);
    let block3 = child_block(&block2, vec![coinbase3.clone(), spend3.clone()]);

    TestChain {
        network,
        address,
        other,
        blocks: vec![genesis, block1, block2, block3],
        unspent_output: outpoint(&spend3, 0),
        spent_coinbase: outpoint(&coinbase1, 0),
        tip_coinbase: outpoint(&coinbase3, 0),
    }
}

fn open(config: &Config, network: &Network) -> FinalizedState {
    FinalizedState::new(config, network).expect("test state database opens")
}

fn commit_checkpoint(state: &mut FinalizedState, blocks: &[Arc<Block>]) {
    for block in blocks {
        state
            .commit_finalized_direct(
                CheckpointVerifiedBlock::from(block.clone()).into(),
                None,
                None,
                "transparent index tests",
            )
            .expect("committing a test block succeeds");
    }
}

/// Commits `block` the way a block above the checkpoint is finalized: its spends are checked
/// against the finalized UTXO set, then it is committed as a contextually verified block.
fn commit_contextual(state: &mut FinalizedState, block: Arc<Block>) {
    let network = state.network();
    let semantically_verified = SemanticallyVerifiedBlock::from(block);
    let spent_utxos = check::utxo::transparent_spend(
        &semantically_verified,
        &CreatedUtxos::default(),
        &SharedMap::default(),
        &state.db,
    )
    .expect("the test block only spends valid finalized outputs");
    let contextually_verified = ContextuallyVerifiedBlock::with_block_and_spent_utxos(
        &network,
        semantically_verified,
        spent_utxos,
    )
    .expect("the test block has a valid value balance");

    state
        .commit_finalized_direct(
            FinalizableBlock::new(contextually_verified, Treestate::default()),
            None,
            None,
            "transparent index tests",
        )
        .expect("committing a contextually verified test block succeeds");
}

/// Returns the raw contents of every column family that consensus or block serving reads, which
/// excludes the transparent index column families and the storage policy metadata.
fn consensus_column_families(db: &ZakuraDb) -> Vec<(&'static str, Vec<(Vec<u8>, Vec<u8>)>)> {
    STATE_COLUMN_FAMILIES_IN_CODE
        .iter()
        .filter(|cf| !TRANSPARENT_INDEX_COLUMN_FAMILIES.contains(cf) && **cf != PRUNING_METADATA)
        .map(|cf| (*cf, db.raw_column_family_entries_for_test(cf)))
        .collect()
}

fn transparent_index_entry_count(db: &ZakuraDb) -> usize {
    TRANSPARENT_INDEX_COLUMN_FAMILIES
        .iter()
        .map(|cf| db.raw_column_family_entries_for_test(cf).len())
        .sum()
}

/// Runs the UTXO contextual checks for a candidate block at `height` with `transactions`.
fn check_spends(
    db: &ZakuraDb,
    parent: &Block,
    height: Height,
    transactions: Vec<Arc<Transaction>>,
) -> Result<HashSet<OutPoint>, ValidateContextError> {
    let address = Address::from_script_hash(NetworkKind::Mainnet, [0x99; 20]);
    let mut all_transactions = vec![coinbase_tx(height, amount(1), &address)];
    all_transactions.extend(transactions);
    let block = SemanticallyVerifiedBlock::from(child_block(parent, all_transactions));

    check::utxo::transparent_spend(&block, &CreatedUtxos::default(), &SharedMap::default(), db)
        .map(|spent| spent.into_keys().collect())
}

/// Runs every spend check this file cares about, returning a comparable summary.
fn spend_check_results(db: &ZakuraDb, chain: &TestChain) -> Vec<String> {
    let tip = chain.blocks.last().expect("the chain has blocks");
    let next = Height(4);
    let missing = OutPoint {
        hash: zakura_chain::transaction::Hash([0xab; 32]),
        index: 0,
    };

    let valid = check_spends(
        db,
        tip,
        next,
        vec![spend_tx(
            chain.unspent_output,
            amount(10_000),
            &chain.address,
        )],
    );
    assert_eq!(
        valid,
        Ok(HashSet::from([chain.unspent_output])),
        "a valid transparent spend succeeds"
    );

    let double_spend = check_spends(
        db,
        tip,
        next,
        vec![spend_tx(chain.spent_coinbase, amount(1), &chain.address)],
    );
    assert!(
        matches!(double_spend, Err(ValidateContextError::MissingTransparentOutput { outpoint, .. }) if outpoint == chain.spent_coinbase),
        "spending an output a finalized block already spent is rejected: {double_spend:?}"
    );

    let same_block_double_spend = check_spends(
        db,
        tip,
        next,
        vec![
            spend_tx(chain.unspent_output, amount(1), &chain.address),
            spend_tx(chain.unspent_output, amount(1), &chain.other),
        ],
    );
    assert!(
        matches!(
            same_block_double_spend,
            Err(ValidateContextError::DuplicateTransparentSpend { .. })
        ),
        "spending one output twice in a block is rejected: {same_block_double_spend:?}"
    );

    let missing_spend = check_spends(
        db,
        tip,
        next,
        vec![spend_tx(missing, amount(1), &chain.address)],
    );
    assert!(
        matches!(missing_spend, Err(ValidateContextError::MissingTransparentOutput { outpoint, .. }) if outpoint == missing),
        "spending an output that never existed is rejected: {missing_spend:?}"
    );

    let immature = check_spends(
        db,
        tip,
        next,
        vec![shielding_spend_tx(&[chain.tip_coinbase])],
    );
    assert!(
        matches!(
            immature,
            Err(ValidateContextError::ImmatureTransparentCoinbaseSpend { .. })
        ),
        "spending an immature coinbase output is rejected: {immature:?}"
    );

    let mature_height = Height(3 + zakura_chain::transparent::MIN_TRANSPARENT_COINBASE_MATURITY);
    let mature = check_spends(
        db,
        tip,
        mature_height,
        vec![shielding_spend_tx(&[chain.tip_coinbase])],
    );
    assert_eq!(
        mature,
        Ok(HashSet::from([chain.tip_coinbase])),
        "spending a mature coinbase output succeeds"
    );

    [
        valid.map(|_| ()),
        double_spend.map(|_| ()),
        same_block_double_spend.map(|_| ()),
        missing_spend.map(|_| ()),
        immature.map(|_| ()),
        mature.map(|_| ()),
    ]
    .into_iter()
    .map(|result| format!("{result:?}"))
    .collect()
}

#[test]
fn index_writes_follow_pruned_checkpoint_sync_configuration() {
    let dir = TempDir::new().expect("temp dir");
    for (node, skips) in [
        (NodeConfig::PrunedCheckpoint, true),
        (NodeConfig::PrunedFullVerification, false),
        (NodeConfig::Archive, false),
    ] {
        let config = config_at(dir.path(), node);
        assert_eq!(config.skips_transparent_indexes(), skips, "{node:?}");
        assert_eq!(
            config.non_finalized_state_backup_dir(&Network::Mainnet),
            Some(dir.path().join("non_finalized_state").join("mainnet"))
        );
    }
}

/// The database without transparent indexes stores exactly the same consensus state as an
/// indexed database, answers the same spend checks, and keeps doing so after it restarts with
/// checkpoint sync disabled and commits another spending block above the checkpoint.
#[test]
fn skipped_and_indexed_databases_keep_identical_validation_state() {
    let _init_guard = zakura_test::init();
    let chain = test_chain();
    let network = chain.network.clone();

    let skipped_dir = TempDir::new().expect("temp dir");
    let indexed_dir = TempDir::new().expect("temp dir");
    let skipped_config = config_at(skipped_dir.path(), NodeConfig::PrunedCheckpoint);
    let indexed_config = config_at(indexed_dir.path(), NodeConfig::PrunedFullVerification);

    {
        let mut skipped = open(&skipped_config, &network);
        let mut indexed = open(&indexed_config, &network);
        assert!(skipped.db.skips_transparent_indexes());
        assert!(!indexed.db.skips_transparent_indexes());

        commit_checkpoint(&mut skipped, &chain.blocks);
        commit_checkpoint(&mut indexed, &chain.blocks);

        assert_eq!(
            consensus_column_families(&skipped.db),
            consensus_column_families(&indexed.db),
            "consensus column families are identical"
        );
        assert_eq!(transparent_index_entry_count(&skipped.db), 0);
        assert!(
            transparent_index_entry_count(&indexed.db) > 0,
            "the indexed database built transparent indexes"
        );
        assert_eq!(
            skipped.db.transparent_indexes_skipped_from(),
            Some(Height(0)),
            "the first committed block is marked"
        );
        assert_eq!(indexed.db.transparent_indexes_skipped_from(), None);

        assert_eq!(
            spend_check_results(&skipped.db, &chain),
            spend_check_results(&indexed.db, &chain),
            "both databases answer every spend check the same way"
        );
    }

    // Restart with checkpoint sync disabled. The existing database keeps skipping indexes.
    let next = child_block(
        chain.blocks.last().expect("the chain has blocks"),
        vec![
            coinbase_tx(Height(4), amount(3_000), &chain.other),
            spend_tx(chain.unspent_output, amount(5_000), &chain.address),
        ],
    );
    let restarted_config = config_at(skipped_dir.path(), NodeConfig::PrunedFullVerification);
    assert!(!restarted_config.skips_transparent_indexes());

    let mut skipped = open(&restarted_config, &network);
    let mut indexed = open(&indexed_config, &network);
    assert!(
        skipped.db.skips_transparent_indexes(),
        "restarting with checkpoint sync disabled keeps index writes disabled"
    );

    commit_contextual(&mut skipped, next.clone());
    commit_contextual(&mut indexed, next);

    assert_eq!(skipped.db.finalized_tip_height(), Some(Height(4)));
    assert_eq!(
        consensus_column_families(&skipped.db),
        consensus_column_families(&indexed.db),
        "consensus column families stay identical after a commit above the checkpoint"
    );
    assert_eq!(
        transparent_index_entry_count(&skipped.db),
        0,
        "the restarted database never resumes transparent index writes"
    );
    assert_eq!(
        skipped.db.transparent_indexes_skipped_from(),
        Some(Height(0)),
        "the marker keeps the first skipped height"
    );
    assert!(skipped.db.utxo(&chain.unspent_output).is_none());
    assert!(indexed.db.utxo(&chain.unspent_output).is_none());
}

/// The marker is written with the first skipped block, before raw transaction pruning starts.
#[test]
fn transparent_index_marker_precedes_raw_transaction_pruning() {
    let _init_guard = zakura_test::init();
    let network = Network::Mainnet;
    let chain = test_chain();
    let dir = TempDir::new().expect("temp dir");
    let config = Config {
        // A tiny retention, below the production floor, so this short chain is pruned.
        storage_mode: StorageMode::Pruned(PruningConfig { tx_retention: 1 }),
        ..config_at(dir.path(), NodeConfig::PrunedCheckpoint)
    };

    let mut state =
        FinalizedState::new_with_debug_without_storage_validation(&config, &network, false, false)
            .expect("test state database opens");

    commit_checkpoint(&mut state, &chain.blocks[..1]);
    assert_eq!(state.db.transparent_indexes_skipped_from(), Some(Height(0)));
    assert_eq!(
        state.db.lowest_retained_height(),
        None,
        "nothing is pruned when the marker is written"
    );

    commit_checkpoint(&mut state, &chain.blocks[1..]);
    assert!(state.db.is_pruned(), "later commits prune raw transactions");
    assert_eq!(
        state.db.transparent_indexes_skipped_from(),
        Some(Height(0)),
        "pruning does not move the marker"
    );
    assert_eq!(transparent_index_entry_count(&state.db), 0);
}

/// An existing indexed database keeps its chain state but stops optional index writes in place.
#[test]
fn existing_indexed_database_stops_index_writes_in_place() {
    let _init_guard = zakura_test::init();
    let chain = test_chain();
    let dir = TempDir::new().expect("temp dir");
    let initial_config = config_at(dir.path(), NodeConfig::PrunedFullVerification);
    let indexes_before;
    let path_before;
    {
        let mut state = open(&initial_config, &chain.network);
        commit_checkpoint(&mut state, &chain.blocks[..3]);
        path_before = state.db.path().to_owned();
        indexes_before = TRANSPARENT_INDEX_COLUMN_FAMILIES
            .map(|cf| state.db.raw_column_family_entries_for_test(cf));
        assert!(transparent_index_entry_count(&state.db) > 0);
        assert_eq!(state.db.transparent_indexes_skipped_from(), None);
    }

    let config = config_at(dir.path(), NodeConfig::PrunedCheckpoint);
    {
        let mut state = open(&config, &chain.network);
        assert_eq!(state.db.path(), path_before);
        assert_eq!(state.db.finalized_tip_height(), Some(Height(2)));
        commit_checkpoint(&mut state, &chain.blocks[3..]);
        assert!(state.db.skips_transparent_indexes());
        assert_eq!(state.db.transparent_indexes_skipped_from(), Some(Height(3)));
        assert_eq!(
            TRANSPARENT_INDEX_COLUMN_FAMILIES
                .map(|cf| { state.db.raw_column_family_entries_for_test(cf) }),
            indexes_before,
            "existing index rows are retained without further writes"
        );
        assert!(state.db.address_balance(&chain.other).is_err());
        assert!(state.db.address_utxos(&chain.other).is_err());
        assert!(state
            .db
            .address_tx_ids(&chain.other, Height(0)..=Height(3))
            .is_err());
        assert!(state.db.utxo(&chain.unspent_output).is_some());
        assert_eq!(state.db.finalized_tip_height(), Some(Height(3)));
    }

    // Disabling checkpoint sync must not resume updates to incomplete indexes.
    let state = open(&initial_config, &chain.network);
    assert!(state.db.skips_transparent_indexes());
    assert_eq!(state.db.transparent_indexes_skipped_from(), Some(Height(3)));
    assert_eq!(
        TRANSPARENT_INDEX_COLUMN_FAMILIES
            .map(|cf| { state.db.raw_column_family_entries_for_test(cf) }),
        indexes_before
    );
    drop(state);
    rollback_finalized_state(
        initial_config.clone(),
        &chain.network,
        RollbackFinalizedStateOptions {
            target_height: Height(1),
            keep_rolled_back_blocks: false,
            max_checkpoint_height: None,
        },
    )
    .expect("retained block bodies permit rollback");
    let mut state = open(&initial_config, &chain.network);
    assert_eq!(state.db.finalized_tip_height(), Some(Height(1)));
    assert_eq!(
        state.db.transparent_indexes_skipped_from(),
        Some(Height(3)),
        "rolling back before the first skipped height preserves the marker"
    );
    assert_eq!(
        TRANSPARENT_INDEX_COLUMN_FAMILIES.map(|cf| state.db.raw_column_family_entries_for_test(cf)),
        indexes_before
    );
    assert!(
        state.db.utxo(&chain.spent_coinbase).is_some(),
        "rollback restores the spent canonical output"
    );
    commit_checkpoint(&mut state, &chain.blocks[2..3]);
    assert_eq!(state.db.finalized_tip_height(), Some(Height(2)));
    assert_eq!(
        TRANSPARENT_INDEX_COLUMN_FAMILIES.map(|cf| state.db.raw_column_family_entries_for_test(cf)),
        indexes_before
    );
    assert!(state.db.address_balance(&chain.other).is_err());
}

/// A rollback before any skipped commit must still reverse complete index rows.
#[test]
fn indexed_rollback_before_first_skipped_commit_keeps_indexes_complete() {
    let _init_guard = zakura_test::init();
    let chain = test_chain();
    let dir = TempDir::new().expect("temp dir");
    let indexed_config = config_at(dir.path(), NodeConfig::PrunedFullVerification);
    {
        let mut state = open(&indexed_config, &chain.network);
        commit_checkpoint(&mut state, &chain.blocks);
        assert_eq!(state.db.transparent_indexes_skipped_from(), None);
    }
    rollback_finalized_state(
        config_at(dir.path(), NodeConfig::PrunedCheckpoint),
        &chain.network,
        RollbackFinalizedStateOptions {
            target_height: Height(1),
            keep_rolled_back_blocks: false,
            max_checkpoint_height: None,
        },
    )
    .expect("complete indexes can be rolled back");
    let state = open(&indexed_config, &chain.network);
    assert_eq!(state.db.transparent_indexes_skipped_from(), None);
    assert!(!state.db.skips_transparent_indexes());
    assert_eq!(
        state
            .db
            .address_balance(&chain.address)
            .expect("indexes remain complete")
            .map(|(balance, _)| balance),
        Some(amount(50_000))
    );
    assert!(state
        .db
        .address_utxos(&chain.other)
        .expect("indexes remain complete")
        .is_empty());
    assert!(state.db.utxo(&chain.spent_coinbase).is_some());
}

/// Every transparent address and spender query fails on a database without transparent indexes,
/// even when stale index entries exist, while outpoint and shielded queries keep working.
#[tokio::test(flavor = "multi_thread")]
async fn transparent_queries_fail_even_with_stale_index_entries() {
    let _init_guard = zakura_test::init();
    let chain = test_chain();
    let network = chain.network.clone();
    let dir = TempDir::new().expect("temp dir");
    let config = config_at(dir.path(), NodeConfig::PrunedCheckpoint);

    let (ironwood_tx, ironwood_nullifier) = ironwood_v6_tx(Height(5));
    let ironwood_block = child_block(
        chain.blocks.last().expect("the chain has blocks"),
        vec![coinbase_tx(Height(4), amount(1), &chain.other), ironwood_tx],
    );

    {
        let mut state = open(&config, &network);
        commit_checkpoint(&mut state, &chain.blocks);
        commit_checkpoint(&mut state, &[ironwood_block]);

        // Plant stale entries that look like a complete index: a balance for the address, and a
        // spender for the spent coinbase output.
        let indexed_dir = TempDir::new().expect("temp dir");
        let mut indexed = open(
            &config_at(indexed_dir.path(), NodeConfig::PrunedFullVerification),
            &network,
        );
        commit_checkpoint(&mut indexed, &chain.blocks);
        for cf in TRANSPARENT_INDEX_COLUMN_FAMILIES {
            for (key, value) in indexed.db.raw_column_family_entries_for_test(cf) {
                state.db.insert_raw_entry_for_test(cf, &key, &value);
            }
        }
        let spent_location = state
            .db
            .output_location(&chain.spent_coinbase)
            .expect("the spent output has a location");
        state.db.insert_raw_entry_for_test(
            TX_LOC_BY_SPENT_OUT_LOC,
            &crate::IntoDisk::as_bytes(&spent_location),
            &crate::IntoDisk::as_bytes(
                &state
                    .db
                    .transaction_location(chain.blocks[2].transactions[1].hash())
                    .expect("the spender has a location"),
            ),
        );
        assert!(state.db.address_balance_location(&chain.address).is_some());
        assert!(state
            .db
            .tx_location_by_spent_output_location(&spent_location)
            .is_some());

        // Direct database queries.
        let addresses = HashSet::from([chain.address]);
        assert_eq!(
            state.db.address_balance(&chain.address),
            Err(TransparentIndexesUnavailable)
        );
        assert_eq!(
            state.db.address_utxos(&chain.address),
            Err(TransparentIndexesUnavailable)
        );
        assert_eq!(
            state
                .db
                .address_tx_ids(&chain.address, Height(0)..=Height(4)),
            Err(TransparentIndexesUnavailable)
        );
        assert_eq!(
            state.db.partial_finalized_transparent_balance(&addresses),
            Err(TransparentIndexesUnavailable)
        );
        assert_eq!(
            state.db.partial_finalized_address_utxos(&addresses),
            Err(TransparentIndexesUnavailable)
        );
        assert_eq!(
            state
                .db
                .partial_finalized_transparent_tx_ids(&addresses, Height(0)..=Height(4)),
            Err(TransparentIndexesUnavailable)
        );
        assert_eq!(
            state.db.spending_tx_loc(&chain.spent_coinbase),
            Err(TransparentIndexesUnavailable),
            "a stale spender entry is not served"
        );

        // An empty address set is not answered with an empty result either.
        let no_addresses = HashSet::new();
        assert_eq!(
            state
                .db
                .partial_finalized_transparent_balance(&no_addresses),
            Err(TransparentIndexesUnavailable)
        );
        assert_eq!(
            state.db.partial_finalized_address_utxos(&no_addresses),
            Err(TransparentIndexesUnavailable)
        );
        assert_eq!(
            state
                .db
                .partial_finalized_transparent_tx_ids(&no_addresses, Height(0)..=Height(4)),
            Err(TransparentIndexesUnavailable)
        );

        // Indexed databases still answer an empty address set with empty results.
        assert_eq!(
            indexed
                .db
                .partial_finalized_transparent_balance(&no_addresses),
            Ok((Amount::zero(), 0))
        );
        assert_eq!(
            indexed.db.partial_finalized_address_utxos(&no_addresses),
            Ok(BTreeMap::new())
        );
        assert_eq!(
            indexed
                .db
                .partial_finalized_transparent_tx_ids(&no_addresses, Height(0)..=Height(4)),
            Ok(BTreeMap::new())
        );

        // Outpoint UTXO and Ironwood nullifier queries still work.
        assert!(state.db.utxo(&chain.unspent_output).is_some());
        assert!(state.db.contains_ironwood_nullifier(&ironwood_nullifier));
    }

    // The read service, which backs the RPC methods, returns the typed error.
    let (mut read_state, _db, _sender) =
        init_read_only(config.clone(), &network).expect("read-only state opens");

    let is_unavailable = |error: crate::BoxError| {
        error.downcast_ref::<TransparentIndexesUnavailable>()
            == Some(&TransparentIndexesUnavailable)
    };

    let addresses = HashSet::from([chain.address, chain.other]);
    for request in [
        ReadRequest::AddressBalance(addresses.clone()),
        ReadRequest::UtxosByAddresses(addresses.clone()),
        ReadRequest::TransactionIdsByAddresses {
            addresses: addresses.clone(),
            height_range: Height(0)..=Height(4),
        },
        #[cfg(feature = "indexer")]
        ReadRequest::SpendingTransactionId(crate::Spend::OutPoint(chain.spent_coinbase)),
    ] {
        let error = read_state
            .ready()
            .await
            .expect("read service is ready")
            .call(request.clone())
            .await
            .expect_err("transparent index queries fail");
        assert!(is_unavailable(error), "{request:?} returns the typed error");
    }

    let response = read_state
        .ready()
        .await
        .expect("read service is ready")
        .call(ReadRequest::UnspentBestChainUtxo(chain.unspent_output))
        .await
        .expect("outpoint UTXO queries are answered");
    assert!(matches!(
        response,
        ReadResponse::UnspentBestChainUtxo(Some(_))
    ));

    #[cfg(feature = "indexer")]
    {
        let response = read_state
            .ready()
            .await
            .expect("read service is ready")
            .call(ReadRequest::SpendingTransactionId(crate::Spend::Ironwood(
                ironwood_nullifier,
            )))
            .await
            .expect("shielded spender queries are answered");
        assert!(
            matches!(response, ReadResponse::TransactionId(Some(_))),
            "the Ironwood spender is indexed: {response:?}"
        );
    }
}

/// A database with incomplete transparent indexes can't be reopened in archive mode.
#[test]
fn skipped_database_is_refused_in_archive_mode() {
    let _init_guard = zakura_test::init();
    let chain = test_chain();
    let network = chain.network.clone();

    let dir = TempDir::new().expect("temp dir");
    {
        let mut state = open(
            &config_at(dir.path(), NodeConfig::PrunedCheckpoint),
            &network,
        );
        commit_checkpoint(&mut state, &chain.blocks);
    }
    assert!(matches!(
        FinalizedState::new(&config_at(dir.path(), NodeConfig::Archive), &network),
        Err(StateInitError::TransparentIndexesMissingInArchiveMode { .. })
    ));

    // The durable marker alone also refuses archive mode.
    let marked = TempDir::new().expect("temp dir");
    {
        let state = open(
            &config_at(marked.path(), NodeConfig::PrunedFullVerification),
            &network,
        );
        let mut batch = crate::DiskWriteBatch::new();
        batch.prepare_transparent_indexes_skipped_marker_batch(&state.db, Height(0));
        state.db.write_batch(batch).expect("marker write succeeds");
        assert!(state.db.skips_transparent_indexes());
    }
    assert!(matches!(
        FinalizedState::new(&config_at(marked.path(), NodeConfig::Archive), &network),
        Err(StateInitError::TransparentIndexesMissingInArchiveMode { .. })
    ));

    // The refused database is still usable as a pruned database.
    let state = open(
        &config_at(dir.path(), NodeConfig::PrunedCheckpoint),
        &network,
    );
    assert_eq!(state.db.finalized_tip_height(), Some(Height(3)));
}

/// The existing major-format upgrade preserves indexed chain state before writes stop.
#[test]
fn indexed_database_upgrades_in_place_before_index_updates_stop() {
    let _init_guard = zakura_test::init();
    let chain = test_chain();
    let dir = TempDir::new().expect("temp dir");
    let initial_config = config_at(dir.path(), NodeConfig::PrunedFullVerification);
    let current_version = state_database_format_version_in_code();
    let previous_version = semver::Version::new(29, 1, 0);
    let current_path =
        initial_config.db_path(STATE_DATABASE_KIND, current_version.major, &chain.network);
    let previous_path =
        initial_config.db_path(STATE_DATABASE_KIND, previous_version.major, &chain.network);
    let consensus_before;
    let indexes_before;
    {
        let mut state = open(&initial_config, &chain.network);
        commit_checkpoint(&mut state, &chain.blocks[..3]);
        consensus_before = consensus_column_families(&state.db);
        indexes_before = TRANSPARENT_INDEX_COLUMN_FAMILIES
            .map(|cf| state.db.raw_column_family_entries_for_test(cf));
        // These column families have unchanged encodings in the prior format. Recreate its
        // version metadata and directory to exercise the real cache-reuse and upgrade path.
        crate::write_database_format_version_to_disk(
            &initial_config,
            STATE_DATABASE_KIND,
            current_version.major,
            &previous_version,
            &chain.network,
        )
        .expect("fixture version is written");
    }
    std::fs::create_dir_all(
        previous_path
            .parent()
            .expect("version directory has a parent"),
    )
    .expect("previous version directory exists");
    std::fs::rename(&current_path, &previous_path)
        .expect("fixture moves to the previous version path");
    let config = config_at(dir.path(), NodeConfig::PrunedCheckpoint);
    {
        let mut state = open(&config, &chain.network);
        assert!(
            !previous_path.exists(),
            "the existing database was moved, not copied or resynced"
        );
        assert_eq!(state.db.path(), current_path);
        assert_eq!(
            state
                .db
                .format_version_on_disk()
                .expect("version is readable"),
            Some(current_version.clone())
        );
        assert_eq!(consensus_column_families(&state.db), consensus_before);
        assert_eq!(
            TRANSPARENT_INDEX_COLUMN_FAMILIES
                .map(|cf| state.db.raw_column_family_entries_for_test(cf)),
            indexes_before
        );
        assert_eq!(state.db.transparent_indexes_skipped_from(), None);
        commit_checkpoint(&mut state, &chain.blocks[3..]);
        assert_eq!(state.db.transparent_indexes_skipped_from(), Some(Height(3)));
        assert_eq!(
            TRANSPARENT_INDEX_COLUMN_FAMILIES
                .map(|cf| state.db.raw_column_family_entries_for_test(cf)),
            indexes_before
        );
        assert_eq!(state.db.finalized_tip_height(), Some(Height(3)));
    }
    // A binary opening through the released format's normal path cannot see stale rows.
    let released = ZakuraDb::new(
        &initial_config,
        STATE_DATABASE_KIND,
        &previous_version,
        &chain.network,
        true,
        STATE_COLUMN_FAMILIES_IN_CODE
            .iter()
            .map(ToString::to_string),
        true,
    );
    assert!(matches!(
        released,
        Err(StateInitError::ReadOnlyDatabaseNotFound { .. })
    ));
    let state = open(&config, &chain.network);
    assert_eq!(state.db.finalized_tip_height(), Some(Height(3)));
    assert!(state.db.skips_transparent_indexes());
}

/// Skipping a release can reuse a cache only through explicitly reusable major upgrades.
#[test]
fn cache_reuse_requires_every_intermediate_major_upgrade() {
    use crate::service::finalized_state::disk_db::DiskDb;

    let _init_guard = zakura_test::init();
    let chain = test_chain();
    let dir = TempDir::new().expect("temp dir");
    let config = config_at(dir.path(), NodeConfig::PrunedFullVerification);
    let current = state_database_format_version_in_code();
    let previous = semver::Version::new(28, 2, 0);
    let current_path = config.db_path(STATE_DATABASE_KIND, current.major, &chain.network);
    let previous_path = config.db_path(STATE_DATABASE_KIND, previous.major, &chain.network);
    {
        let mut state = open(&config, &chain.network);
        commit_checkpoint(&mut state, &chain.blocks[..3]);
        crate::write_database_format_version_to_disk(
            &config,
            STATE_DATABASE_KIND,
            current.major,
            &previous,
            &chain.network,
        )
        .expect("fixture version is written");
    }
    std::fs::create_dir_all(
        previous_path
            .parent()
            .expect("version directory has a parent"),
    )
    .expect("older version parent exists");
    std::fs::rename(&current_path, &previous_path)
        .expect("fixture moves to the older version directory");
    assert_eq!(
        DiskDb::try_reusing_previous_db_after_major_upgrade(
            &[30],
            &current,
            &config,
            STATE_DATABASE_KIND,
            &chain.network
        ),
        None,
        "an unsupported intermediate major upgrade prevents reuse"
    );
    assert!(previous_path.exists());
    assert!(!current_path.exists());
    assert_eq!(
        DiskDb::try_reusing_previous_db_after_major_upgrade(
            &[29, 30],
            &current,
            &config,
            STATE_DATABASE_KIND,
            &chain.network
        ),
        Some(previous),
        "all intermediate upgrades explicitly permit cache reuse"
    );
    assert!(!previous_path.exists());
    assert!(current_path.exists());
}

/// Offline rollback reverses the canonical UTXO set of a database without transparent indexes,
/// keeps its marker, and decides that from durable state rather than the checkpoint setting.
#[test]
fn rollback_without_transparent_indexes_reverses_consensus_state() {
    let _init_guard = zakura_test::init();
    let chain = test_chain();
    let network = chain.network.clone();

    let skipped_dir = TempDir::new().expect("temp dir");
    let fresh_dir = TempDir::new().expect("temp dir");
    let indexed_dir = TempDir::new().expect("temp dir");
    {
        let mut skipped = open(
            &config_at(skipped_dir.path(), NodeConfig::PrunedCheckpoint),
            &network,
        );
        commit_checkpoint(&mut skipped, &chain.blocks);
        let mut fresh = open(
            &config_at(fresh_dir.path(), NodeConfig::PrunedCheckpoint),
            &network,
        );
        commit_checkpoint(&mut fresh, &chain.blocks[..2]);
        let mut indexed = open(
            &config_at(indexed_dir.path(), NodeConfig::PrunedFullVerification),
            &network,
        );
        commit_checkpoint(&mut indexed, &chain.blocks);
    }

    let options = RollbackFinalizedStateOptions {
        target_height: Height(1),
        keep_rolled_back_blocks: false,
        max_checkpoint_height: None,
    };

    // The rollback config disables checkpoint sync, like an operator config that differs from
    // the one the database was synced with.
    rollback_finalized_state(
        config_at(skipped_dir.path(), NodeConfig::PrunedFullVerification),
        &network,
        options.clone(),
    )
    .expect("rollback succeeds when every rolled-back block is available");
    rollback_finalized_state(
        config_at(indexed_dir.path(), NodeConfig::PrunedFullVerification),
        &network,
        options,
    )
    .expect("indexed rollback succeeds");

    let skipped = open(
        &config_at(skipped_dir.path(), NodeConfig::PrunedFullVerification),
        &network,
    );
    let fresh = open(
        &config_at(fresh_dir.path(), NodeConfig::PrunedCheckpoint),
        &network,
    );
    let indexed = open(
        &config_at(indexed_dir.path(), NodeConfig::PrunedFullVerification),
        &network,
    );

    assert_eq!(skipped.db.finalized_tip_height(), Some(Height(1)));
    assert_eq!(
        skipped
            .db
            .raw_column_family_entries_for_test("utxo_by_out_loc"),
        fresh
            .db
            .raw_column_family_entries_for_test("utxo_by_out_loc"),
        "the UTXO set matches a database synced only to the target"
    );
    assert_eq!(
        skipped
            .db
            .raw_column_family_entries_for_test("utxo_by_out_loc"),
        indexed
            .db
            .raw_column_family_entries_for_test("utxo_by_out_loc"),
        "the UTXO set matches an indexed database rolled back to the target"
    );
    assert_eq!(
        skipped.db.finalized_value_pool(),
        indexed.db.finalized_value_pool()
    );
    assert!(skipped.db.utxo(&chain.spent_coinbase).is_some());
    assert_eq!(transparent_index_entry_count(&skipped.db), 0);
    assert_eq!(
        skipped.db.transparent_indexes_skipped_from(),
        Some(Height(0)),
        "rollback keeps the marker"
    );
    assert_eq!(
        indexed
            .db
            .address_balance(&chain.address)
            .expect("indexed databases answer balance queries")
            .map(|(balance, _)| balance),
        Some(amount(50_000)),
        "indexed rollback restores the address balance"
    );
}

/// Rollback fails without writing anything when a rolled-back block body was pruned.
#[test]
fn rollback_without_transparent_indexes_does_not_mutate_when_bodies_are_missing() {
    let _init_guard = zakura_test::init();
    let chain = test_chain();
    let network = chain.network.clone();
    let dir = TempDir::new().expect("temp dir");
    let config = config_at(dir.path(), NodeConfig::PrunedCheckpoint);

    let before = {
        let mut state = open(&config, &network);
        commit_checkpoint(&mut state, &chain.blocks);

        let mut batch = crate::DiskWriteBatch::new();
        batch.prepare_prune_batch(&state.db, Height(2), Height(3));
        state.db.write_batch(batch).expect("pruning succeeds");

        consensus_column_families(&state.db)
    };

    let error = rollback_finalized_state(
        config.clone(),
        &network,
        RollbackFinalizedStateOptions {
            target_height: Height(1),
            keep_rolled_back_blocks: false,
            max_checkpoint_height: None,
        },
    )
    .expect_err("rollback needs every rolled-back block body");
    // Reversing height 3 needs the output it spent from the pruned height 2 body, and reversing
    // height 2 needs that body itself. Either way rollback reports the existing missing-data error.
    assert!(
        matches!(
            error,
            RollbackFinalizedStateError::MissingBlock { height } if height == Height(2)
        ) || matches!(
            error,
            RollbackFinalizedStateError::MissingTransaction { hash }
                if chain.blocks[2].transactions.iter().any(|tx| tx.hash() == hash)
        ),
        "{error:?}"
    );

    let state = open(&config, &network);
    assert_eq!(
        consensus_column_families(&state.db),
        before,
        "nothing was written"
    );
    assert_eq!(state.db.transparent_indexes_skipped_from(), Some(Height(0)));
}

/// Startup, shutdown, and periodic format checks pass on a database without transparent indexes,
/// and never write the omitted indexes.
#[test]
fn format_checks_never_rebuild_omitted_transparent_indexes() {
    let _init_guard = zakura_test::init();
    let chain = test_chain();
    let network = chain.network.clone();
    let dir = TempDir::new().expect("temp dir");
    let config = config_at(dir.path(), NodeConfig::PrunedCheckpoint);

    let (ironwood_tx, ironwood_nullifier) = ironwood_v6_tx(Height(5));
    let ironwood_block = child_block(
        chain.blocks.last().expect("the chain has blocks"),
        vec![coinbase_tx(Height(4), amount(1), &chain.other), ironwood_tx],
    );

    {
        let mut state = open(&config, &network);
        commit_checkpoint(&mut state, &chain.blocks);
        commit_checkpoint(&mut state, &[ironwood_block]);
    }

    // Reopening runs the startup format check, which includes the indexer spender rebuild when
    // that feature is enabled.
    let state = open(&config, &network);
    let (_cancel_sender, cancel_receiver) = cancel_channel();
    assert_eq!(
        DbFormatChange::format_validity_checks_detailed(&state.db, &cancel_receiver)
            .expect("checks are not cancelled"),
        Ok(()),
        "the address received check does not demand balances that were never built"
    );

    assert_eq!(transparent_index_entry_count(&state.db), 0);
    assert!(state.db.contains_ironwood_nullifier(&ironwood_nullifier));
    #[cfg(feature = "indexer")]
    assert!(
        state
            .db
            .spending_transaction_hash(&crate::Spend::Ironwood(ironwood_nullifier))
            .expect("shielded spender queries are answered")
            .is_some(),
        "the Ironwood spender is still indexed"
    );
}

/// The marker key can't be mistaken for the raw transaction pruning cursor.
#[test]
fn transparent_index_marker_does_not_mark_a_database_pruned() {
    let _init_guard = zakura_test::init();
    let chain = test_chain();
    let network = chain.network.clone();
    let dir = TempDir::new().expect("temp dir");

    let mut state = open(
        &config_at(dir.path(), NodeConfig::PrunedCheckpoint),
        &network,
    );
    commit_checkpoint(&mut state, &chain.blocks);

    assert!(state.db.transparent_indexes_skipped_from().is_some());
    assert_eq!(state.db.lowest_retained_height(), None);
    assert!(!state.db.is_pruned());
    assert!(state.db.block(Height(1).into()).is_some());
    assert_eq!(
        state
            .db
            .raw_column_family_entries_for_test(PRUNING_METADATA)
            .len(),
        1,
        "only the marker is stored"
    );
}