zakura-client-sqlite 0.1.0-rc0

An SQLite-based Zcash light client
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
//! Migrations for the `zcash_client_sqlite` wallet database.
//!
//! The constants in this module cover all states of the migration DAG that have been
//! exposed in a public crate release, in the order that crate users would have
//! encountered them.
//!
//! Omitted versions had the same migration state as the first prior version that is
//! included.

mod account_delete_cascade;
mod add_account_birthdays;
mod add_account_uuids;
mod add_transaction_trust_marker;
mod add_transaction_views;
mod add_transparent_receiver_address_index;
mod add_transparent_value_index;
mod add_utxo_account;
mod addresses_table;
mod ensure_default_transparent_address;
mod ensure_orchard_ua_receiver;
mod ephemeral_addresses;
mod fix_bad_change_flagging;
mod fix_bad_ironwood_change_flagging;
mod fix_broken_commitment_trees;
mod fix_transparent_received_outputs;
mod fix_v_transactions_expired_unmined;
mod full_account_ids;
mod initial_setup;
mod ironwood_pool_code_views;
mod ironwood_received_notes;
mod ironwood_shardtree;
mod ivk_item_cache;
mod note_locking;
mod nullifier_map;
mod orchard_ironwood_migration_anchor_interval;
mod orchard_ironwood_migration_tables;
mod orchard_ironwood_migration_unsatisfiability;
mod orchard_note_version;
mod orchard_received_notes;
mod orchard_shardtree;
mod received_notes_nullable_nf;
mod receiving_key_scopes;
mod sapling_memo_consistency;
mod sent_notes_to_internal;
mod shardtree_support;
mod spend_key_available;
mod standalone_p2sh;
mod support_legacy_sqlite;
mod support_zcashd_wallet_import;
mod transparent_gap_limit_handling;
mod tree_retained_checkpoints;
mod tx_observation_height;
mod tx_retrieval_queue;
mod tx_retrieval_queue_expiry;
mod tx_status_observation_intent;
mod ufvk_support;
mod utxos_table;
mod utxos_to_txos;
mod v_address_uses_ironwood;
mod v_received_output_spends_account;
mod v_sapling_shard_unscanned_ranges;
mod v_transactions_additional_totals;
mod v_transactions_net;
mod v_transactions_note_uniqueness;
mod v_transactions_pool_crossing;
mod v_transactions_shielding_balance;
mod v_transactions_transparent_history;
mod v_transactions_zip318_kind;
mod v_tx_outputs_key_scopes;
mod v_tx_outputs_return_addrs;
mod v_tx_outputs_transparent_addresses;
mod v_tx_outputs_use_legacy_false;
mod wallet_summaries;
mod witness_stabilized_notes;
mod zip318_classification;

use std::{rc::Rc, sync::Mutex};

use rand_core::RngCore;
use rusqlite::{OptionalExtension, named_params};
use schemerz_rusqlite::RusqliteMigration;
use secrecy::SecretVec;
use uuid::Uuid;
use zcash_address::unified::{Encoding as _, Ufvk};
use zcash_protocol::consensus;

use crate::util::Clock;

use super::WalletMigrationError;

/// Identifiers of the individual migrations that make up this crate's internal migration graph.
///
/// External migrations registered via
/// [`WalletMigrator::with_external_migrations`] are applied as part of a single graph alongside
/// the internal ones, so an external migration that reads or extends internal schema must
/// declare a dependency that orders it after the migration which creates that schema.
///
/// These identifiers are unstable by nature, which is why they sit behind the `unstable`
/// feature: each names an individual migration rather than a state of the graph that a
/// published release exposed, so the set of them, and which of them are reachable, changes
/// between releases. A release constant such as [`V_0_19_0`] carries no such risk. The
/// intended shape is therefore to depend on an identifier here while developing against
/// unreleased schema, and to move the anchor to the release constant that covers it once
/// that release exists.
///
/// Depending on an identifier here only orders your migration *after* the named one; it does
/// not prevent later internal migrations from being applied before yours.
///
/// [`WalletMigrator::with_external_migrations`]: super::WalletMigrator::with_external_migrations
#[cfg(feature = "unstable")]
pub mod ids {
    pub use super::{
        account_delete_cascade::MIGRATION_ID as ACCOUNT_DELETE_CASCADE,
        add_account_birthdays::MIGRATION_ID as ADD_ACCOUNT_BIRTHDAYS,
        add_account_uuids::MIGRATION_ID as ADD_ACCOUNT_UUIDS,
        add_transaction_trust_marker::MIGRATION_ID as ADD_TRANSACTION_TRUST_MARKER,
        add_transaction_views::MIGRATION_ID as ADD_TRANSACTION_VIEWS,
        add_transparent_receiver_address_index::MIGRATION_ID as ADD_TRANSPARENT_RECEIVER_ADDRESS_INDEX,
        add_transparent_value_index::MIGRATION_ID as ADD_TRANSPARENT_VALUE_INDEX,
        add_utxo_account::MIGRATION_ID as ADD_UTXO_ACCOUNT,
        addresses_table::MIGRATION_ID as ADDRESSES_TABLE,
        ensure_default_transparent_address::MIGRATION_ID as ENSURE_DEFAULT_TRANSPARENT_ADDRESS,
        ensure_orchard_ua_receiver::MIGRATION_ID as ENSURE_ORCHARD_UA_RECEIVER,
        ephemeral_addresses::MIGRATION_ID as EPHEMERAL_ADDRESSES,
        fix_bad_change_flagging::MIGRATION_ID as FIX_BAD_CHANGE_FLAGGING,
        fix_bad_ironwood_change_flagging::MIGRATION_ID as FIX_BAD_IRONWOOD_CHANGE_FLAGGING,
        fix_broken_commitment_trees::MIGRATION_ID as FIX_BROKEN_COMMITMENT_TREES,
        fix_transparent_received_outputs::MIGRATION_ID as FIX_TRANSPARENT_RECEIVED_OUTPUTS,
        fix_v_transactions_expired_unmined::MIGRATION_ID as FIX_V_TRANSACTIONS_EXPIRED_UNMINED,
        full_account_ids::MIGRATION_ID as FULL_ACCOUNT_IDS,
        initial_setup::MIGRATION_ID as INITIAL_SETUP,
        ironwood_pool_code_views::MIGRATION_ID as IRONWOOD_POOL_CODE_VIEWS,
        ironwood_received_notes::MIGRATION_ID as IRONWOOD_RECEIVED_NOTES,
        ironwood_shardtree::MIGRATION_ID as IRONWOOD_SHARDTREE,
        ivk_item_cache::MIGRATION_ID as IVK_ITEM_CACHE, note_locking::MIGRATION_ID as NOTE_LOCKING,
        nullifier_map::MIGRATION_ID as NULLIFIER_MAP,
        orchard_ironwood_migration_anchor_interval::MIGRATION_ID as ORCHARD_IRONWOOD_MIGRATION_ANCHOR_INTERVAL,
        orchard_ironwood_migration_tables::MIGRATION_ID as ORCHARD_IRONWOOD_MIGRATION_TABLES,
        orchard_ironwood_migration_unsatisfiability::MIGRATION_ID as ORCHARD_IRONWOOD_MIGRATION_UNSATISFIABILITY,
        orchard_note_version::MIGRATION_ID as ORCHARD_NOTE_VERSION,
        orchard_received_notes::MIGRATION_ID as ORCHARD_RECEIVED_NOTES,
        orchard_shardtree::MIGRATION_ID as ORCHARD_SHARDTREE,
        received_notes_nullable_nf::MIGRATION_ID as RECEIVED_NOTES_NULLABLE_NF,
        receiving_key_scopes::MIGRATION_ID as RECEIVING_KEY_SCOPES,
        sapling_memo_consistency::MIGRATION_ID as SAPLING_MEMO_CONSISTENCY,
        sent_notes_to_internal::MIGRATION_ID as SENT_NOTES_TO_INTERNAL,
        shardtree_support::MIGRATION_ID as SHARDTREE_SUPPORT,
        spend_key_available::MIGRATION_ID as SPEND_KEY_AVAILABLE,
        standalone_p2sh::MIGRATION_ID as STANDALONE_P2SH,
        support_legacy_sqlite::MIGRATION_ID as SUPPORT_LEGACY_SQLITE,
        support_zcashd_wallet_import::MIGRATION_ID as SUPPORT_ZCASHD_WALLET_IMPORT,
        transparent_gap_limit_handling::MIGRATION_ID as TRANSPARENT_GAP_LIMIT_HANDLING,
        tree_retained_checkpoints::MIGRATION_ID as TREE_RETAINED_CHECKPOINTS,
        tx_observation_height::MIGRATION_ID as TX_OBSERVATION_HEIGHT,
        tx_retrieval_queue::MIGRATION_ID as TX_RETRIEVAL_QUEUE,
        tx_retrieval_queue_expiry::MIGRATION_ID as TX_RETRIEVAL_QUEUE_EXPIRY,
        tx_status_observation_intent::MIGRATION_ID as TX_STATUS_OBSERVATION_INTENT,
        ufvk_support::MIGRATION_ID as UFVK_SUPPORT, utxos_table::MIGRATION_ID as UTXOS_TABLE,
        utxos_to_txos::MIGRATION_ID as UTXOS_TO_TXOS,
        v_address_uses_ironwood::MIGRATION_ID as V_ADDRESS_USES_IRONWOOD,
        v_received_output_spends_account::MIGRATION_ID as V_RECEIVED_OUTPUT_SPENDS_ACCOUNT,
        v_sapling_shard_unscanned_ranges::MIGRATION_ID as V_SAPLING_SHARD_UNSCANNED_RANGES,
        v_transactions_additional_totals::MIGRATION_ID as V_TRANSACTIONS_ADDITIONAL_TOTALS,
        v_transactions_net::MIGRATION_ID as V_TRANSACTIONS_NET,
        v_transactions_note_uniqueness::MIGRATION_ID as V_TRANSACTIONS_NOTE_UNIQUENESS,
        v_transactions_pool_crossing::MIGRATION_ID as V_TRANSACTIONS_POOL_CROSSING,
        v_transactions_shielding_balance::MIGRATION_ID as V_TRANSACTIONS_SHIELDING_BALANCE,
        v_transactions_transparent_history::MIGRATION_ID as V_TRANSACTIONS_TRANSPARENT_HISTORY,
        v_transactions_zip318_kind::MIGRATION_ID as V_TRANSACTIONS_ZIP318_KIND,
        v_tx_outputs_key_scopes::MIGRATION_ID as V_TX_OUTPUTS_KEY_SCOPES,
        v_tx_outputs_return_addrs::MIGRATION_ID as V_TX_OUTPUTS_RETURN_ADDRS,
        v_tx_outputs_transparent_addresses::MIGRATION_ID as V_TX_OUTPUTS_TRANSPARENT_ADDRESSES,
        v_tx_outputs_use_legacy_false::MIGRATION_ID as V_TX_OUTPUTS_USE_LEGACY_FALSE,
        wallet_summaries::MIGRATION_ID as WALLET_SUMMARIES,
        witness_stabilized_notes::MIGRATION_ID as WITNESS_STABILIZED_NOTES,
        zip318_classification::MIGRATION_ID as ZIP318_CLASSIFICATION,
    };
}

pub(super) fn all_migrations<
    P: consensus::Parameters + 'static,
    C: Clock + Clone + 'static,
    R: RngCore + Clone + 'static,
>(
    params: &P,
    clock: C,
    rng: R,
    seed: Option<Rc<SecretVec<u8>>>,
) -> Vec<Box<dyn RusqliteMigration<Error = WalletMigrationError>>> {
    //                                   initial_setup
    //                                   /           \
    //                          utxos_table         ufvk_support
    //                             |                 /         \
    //                             |    addresses_table   sent_notes_to_internal
    //                             |          /                /
    //                           add_utxo_account             /
    //                                        \              /
    //                                     add_transaction_views
    //                                               |
    //                                       v_transactions_net
    //                                               |
    //                                            received_notes_nullable_nf---------------------.
    //                                            /           |                                   \
    //                                           /            |                                    \
    //           ,-------------- shardtree_support    sapling_memo_consistency                    nullifier_map
    //          /                     /           \                       \                                  |
    // orchard_shardtree   add_account_birthdays   receiving_key_scopes   v_transactions_transparent_history |
    //   |                    |                 \            |                     |                         |
    //   |   v_sapling_shard_unscanned_ranges    \           |       v_tx_outputs_use_legacy_false           |
    //   |                    |                   \          |                     |                         |
    //   |            wallet_summaries             \         |      v_transactions_shielding_balance         /
    //   \                    \                     \        |                     |                        /
    //    \                    \                     \       |      v_transactions_note_uniqueness         /
    //     \                    \                     \      |        /                                   /
    //      \                    `------------------- full_account_ids                                   /
    //       \                                        /               \                                 /
    //        \                         orchard_received_notes        spend_key_available              /
    //         \                            /          \                      /                       /
    //          \     ensure_orchard_ua_receiver     utxos_to_txos           /                       /
    //           \                          \              |                /                       /
    //            \                          \     ephemeral_addresses     /                       /
    //             \                          \            |              /                       /
    //              `----------------------------- tx_retrieval_queue ---------------------------'
    //                                                  /    \
    //                              support_legacy_sqlite    tx_retrieval_queue_expiry ----------------.
    //                                 /              \                                                 \
    //            fix_broken_commitment_trees         add_account_uuids                                  \
    //                       /                                /        \                                  \
    //    fix_bad_change_flagging      transparent_gap_limit_handling   v_transactions_additional_totals   \
    //                       \                       |                      /                               \
    //                        \      ensure_default_transparent_address    /                                 \
    //                         \                     |                    /                                   \
    //                          `---- fix_transparent_received_outputs --'                                     \
    //                                    /         /           \                                              |
    //                                   /         /             \                                             |
    //                                  /         /               \                                            |
    //           support_zcashd_wallet_import    /             fix_v_transactions_expired_unmined              |
    //                \                         /                    /        |         \                      |
    //                 \      tx_observation_height                 /         |          \                     |
    //                  \                       \                  /          |           \                    /
    //                   \                   add_transaction_trust_marker     |  v_tx_outputs_return_addrs    /
    //                    \                       \                           |                     /        /
    //                     \                       \         v_received_output_spends_account      /        /
    //                      \                       \               /                             /        /
    //                       `------------------- account_delete_cascade ---------------------------------'
    //                              /               /                  |              \
    //     add_transparent_value_index  v_tx_outputs_key_scopes  standalone_p2sh    witness_stabilized_notes
    //                                     /        |             /            \
    //                               ivk_item_cache |            /           orchard_note_version
    //                             .----------------'           /                  \
    //                             |  add_transparent_receiver_address_index        \
    //                             |                                                 \
    //                             |                                        ironwood_received_notes ----------------
    //                             |                                        /         |          \                  \
    //                             |                     ironwood_pool_code_views     |      note_locking  fix_bad_ironwood_change_flagging
    //                             |                             |          \         |
    //                             |                             |           \ v_address_uses_ironwood
    //                             |                             |            \
    //                             |              v_transactions_pool_crossing \
    //                             `------------------------------------------- v_tx_outputs_transparent_addresses
    //
    let rng = Rc::new(Mutex::new(rng));
    vec![
        Box::new(initial_setup::Migration {}),
        Box::new(utxos_table::Migration {}),
        Box::new(ufvk_support::Migration {
            params: params.clone(),
            seed: seed.clone(),
        }),
        Box::new(addresses_table::Migration {
            params: params.clone(),
        }),
        Box::new(add_utxo_account::Migration {
            _params: params.clone(),
        }),
        Box::new(sent_notes_to_internal::Migration {}),
        Box::new(add_transaction_views::Migration),
        Box::new(v_transactions_net::Migration),
        Box::new(received_notes_nullable_nf::Migration),
        Box::new(shardtree_support::Migration {
            params: params.clone(),
        }),
        Box::new(nullifier_map::Migration),
        Box::new(sapling_memo_consistency::Migration {
            params: params.clone(),
        }),
        Box::new(add_account_birthdays::Migration {
            params: params.clone(),
        }),
        Box::new(v_sapling_shard_unscanned_ranges::Migration {
            params: params.clone(),
        }),
        Box::new(wallet_summaries::Migration),
        Box::new(v_transactions_transparent_history::Migration),
        Box::new(v_tx_outputs_use_legacy_false::Migration),
        Box::new(v_transactions_shielding_balance::Migration),
        Box::new(v_transactions_note_uniqueness::Migration),
        Box::new(receiving_key_scopes::Migration {
            params: params.clone(),
        }),
        Box::new(full_account_ids::Migration {
            seed,
            params: params.clone(),
        }),
        Box::new(orchard_shardtree::Migration {
            params: params.clone(),
        }),
        Box::new(ironwood_shardtree::Migration {
            params: params.clone(),
        }),
        Box::new(orchard_received_notes::Migration),
        Box::new(ensure_orchard_ua_receiver::Migration {
            params: params.clone(),
        }),
        Box::new(utxos_to_txos::Migration),
        Box::new(ephemeral_addresses::Migration {
            params: params.clone(),
        }),
        Box::new(spend_key_available::Migration),
        Box::new(tx_retrieval_queue::Migration {
            _params: params.clone(),
        }),
        Box::new(support_legacy_sqlite::Migration),
        Box::new(fix_broken_commitment_trees::Migration {
            params: params.clone(),
        }),
        Box::new(fix_bad_change_flagging::Migration),
        Box::new(add_account_uuids::Migration),
        Box::new(v_transactions_additional_totals::Migration),
        Box::new(transparent_gap_limit_handling::Migration {
            params: params.clone(),
            _clock: clock.clone(),
            _rng: rng.clone(),
        }),
        Box::new(ensure_default_transparent_address::Migration {
            _params: params.clone(),
        }),
        Box::new(tx_retrieval_queue_expiry::Migration),
        Box::new(fix_transparent_received_outputs::Migration),
        Box::new(support_zcashd_wallet_import::Migration),
        Box::new(fix_v_transactions_expired_unmined::Migration),
        Box::new(v_tx_outputs_return_addrs::Migration),
        Box::new(tx_observation_height::Migration {
            params: params.clone(),
        }),
        Box::new(v_received_output_spends_account::Migration),
        Box::new(add_transaction_trust_marker::Migration),
        Box::new(account_delete_cascade::Migration),
        Box::new(v_tx_outputs_key_scopes::Migration),
        Box::new(standalone_p2sh::Migration),
        Box::new(ivk_item_cache::Migration {
            params: params.clone(),
        }),
        Box::new(witness_stabilized_notes::Migration {
            params: params.clone(),
        }),
        Box::new(add_transparent_receiver_address_index::Migration {
            params: params.clone(),
        }),
        Box::new(add_transparent_value_index::Migration),
        Box::new(orchard_note_version::Migration),
        Box::new(ironwood_received_notes::Migration),
        Box::new(ironwood_pool_code_views::Migration),
        Box::new(fix_bad_ironwood_change_flagging::Migration),
        Box::new(v_address_uses_ironwood::Migration),
        Box::new(v_transactions_pool_crossing::Migration),
        Box::new(zip318_classification::Migration),
        Box::new(v_transactions_zip318_kind::Migration),
        Box::new(orchard_ironwood_migration_tables::Migration),
        Box::new(tree_retained_checkpoints::Migration),
        Box::new(note_locking::Migration),
        Box::new(tx_status_observation_intent::Migration),
        Box::new(orchard_ironwood_migration_anchor_interval::Migration),
        Box::new(v_tx_outputs_transparent_addresses::Migration),
        Box::new(orchard_ironwood_migration_unsatisfiability::Migration),
    ]
}

/// All states of the migration DAG that have been exposed in a public crate release, in
/// the order that crate users would have encountered them.
///
/// Omitted versions had the same migration state as the first prior version that is
/// included.
#[allow(dead_code)] // marked as dead code so that this appears in docs with --document-private-items
const PUBLIC_MIGRATION_STATES: &[&[Uuid]] = &[
    V_0_4_0,
    V_0_6_0,
    V_0_8_0,
    V_0_9_0,
    V_0_10_0,
    V_0_10_3,
    V_0_11_0,
    V_0_11_1,
    V_0_11_2,
    V_0_12_0,
    V_0_13_0,
    V_0_14_0,
    V_0_15_0,
    V_0_16_0,
    V_0_16_2,
    V_0_16_4,
    V_0_17_2,
    V_0_17_3,
    V_0_18_0,
    V_0_18_5,
    V_0_19_0,
    V_0_20_0,
    V_0_22_0_RC1,
    V_0_22_0_RC2,
];

/// Leaf migrations in the 0.4.0 release.
pub const V_0_4_0: &[Uuid] = &[add_transaction_views::MIGRATION_ID];

/// Leaf migrations in the 0.6.0 release.
pub const V_0_6_0: &[Uuid] = &[v_transactions_net::MIGRATION_ID];

/// Leaf migrations in the 0.8.0 release.
pub const V_0_8_0: &[Uuid] = &[
    nullifier_map::MIGRATION_ID,
    v_transactions_note_uniqueness::MIGRATION_ID,
    wallet_summaries::MIGRATION_ID,
];

/// Leaf migrations in the 0.9.0 release.
pub const V_0_9_0: &[Uuid] = &[
    nullifier_map::MIGRATION_ID,
    receiving_key_scopes::MIGRATION_ID,
    v_transactions_note_uniqueness::MIGRATION_ID,
    wallet_summaries::MIGRATION_ID,
];

/// Leaf migrations in the 0.10.0 release.
pub const V_0_10_0: &[Uuid] = &[
    nullifier_map::MIGRATION_ID,
    orchard_received_notes::MIGRATION_ID,
    orchard_shardtree::MIGRATION_ID,
];

/// Leaf migrations in the 0.10.3 release.
pub const V_0_10_3: &[Uuid] = &[
    ensure_orchard_ua_receiver::MIGRATION_ID,
    nullifier_map::MIGRATION_ID,
    orchard_shardtree::MIGRATION_ID,
];

/// Leaf migrations in the 0.11.0 release.
pub const V_0_11_0: &[Uuid] = &[
    ensure_orchard_ua_receiver::MIGRATION_ID,
    ephemeral_addresses::MIGRATION_ID,
    nullifier_map::MIGRATION_ID,
    orchard_shardtree::MIGRATION_ID,
    spend_key_available::MIGRATION_ID,
    tx_retrieval_queue::MIGRATION_ID,
];

/// Leaf migrations in the 0.11.1 release.
pub const V_0_11_1: &[Uuid] = &[tx_retrieval_queue::MIGRATION_ID];

/// Leaf migrations in the 0.11.2 release.
pub const V_0_11_2: &[Uuid] = &[support_legacy_sqlite::MIGRATION_ID];

/// Leaf migrations in the 0.12.0 release.
pub const V_0_12_0: &[Uuid] = &[fix_broken_commitment_trees::MIGRATION_ID];

/// Leaf migrations in the 0.13.0 release.
pub const V_0_13_0: &[Uuid] = &[fix_bad_change_flagging::MIGRATION_ID];

/// Leaf migrations in the 0.14.0 release.
pub const V_0_14_0: &[Uuid] = &[
    fix_bad_change_flagging::MIGRATION_ID,
    add_account_uuids::MIGRATION_ID,
];

/// Leaf migrations in the 0.15.0 release.
pub const V_0_15_0: &[Uuid] = &[
    fix_bad_change_flagging::MIGRATION_ID,
    v_transactions_additional_totals::MIGRATION_ID,
];

/// Leaf migrations in the 0.16.0 release.
pub const V_0_16_0: &[Uuid] = &[
    fix_bad_change_flagging::MIGRATION_ID,
    v_transactions_additional_totals::MIGRATION_ID,
    transparent_gap_limit_handling::MIGRATION_ID,
];

/// Leaf migrations in the 0.16.2 release.
pub const V_0_16_2: &[Uuid] = &[
    fix_bad_change_flagging::MIGRATION_ID,
    v_transactions_additional_totals::MIGRATION_ID,
    ensure_default_transparent_address::MIGRATION_ID,
];

/// Leaf migrations in the 0.16.4 release.
pub const V_0_16_4: &[Uuid] = &[
    fix_bad_change_flagging::MIGRATION_ID,
    v_transactions_additional_totals::MIGRATION_ID,
    ensure_default_transparent_address::MIGRATION_ID,
    tx_retrieval_queue_expiry::MIGRATION_ID,
];

/// Leaf migrations in the 0.17.0 release.
pub const V_0_17_0: &[Uuid] = &[fix_transparent_received_outputs::MIGRATION_ID];

/// Leaf migrations in the 0.17.2 release.
pub const V_0_17_2: &[Uuid] = &[
    tx_retrieval_queue_expiry::MIGRATION_ID,
    fix_transparent_received_outputs::MIGRATION_ID,
];

/// Leaf migrations in the 0.17.3 release.
pub const V_0_17_3: &[Uuid] = &[
    tx_retrieval_queue_expiry::MIGRATION_ID,
    fix_v_transactions_expired_unmined::MIGRATION_ID,
];

/// Leaf migrations in the 0.18.0 release.
pub const V_0_18_0: &[Uuid] = &[
    tx_retrieval_queue_expiry::MIGRATION_ID,
    support_zcashd_wallet_import::MIGRATION_ID,
    v_tx_outputs_return_addrs::MIGRATION_ID,
];

/// Leaf migrations in the 0.18.5 release.
pub const V_0_18_5: &[Uuid] = &[
    tx_retrieval_queue_expiry::MIGRATION_ID,
    support_zcashd_wallet_import::MIGRATION_ID,
    v_tx_outputs_return_addrs::MIGRATION_ID,
    tx_observation_height::MIGRATION_ID,
];

/// Leaf migrations in the 0.19.0 release.
pub const V_0_19_0: &[Uuid] = &[account_delete_cascade::MIGRATION_ID];

/// Leaf migrations in the 0.20.0 release.
pub const V_0_20_0: &[Uuid] = &[
    v_tx_outputs_key_scopes::MIGRATION_ID,
    ivk_item_cache::MIGRATION_ID,
    witness_stabilized_notes::MIGRATION_ID,
];

/// Leaf migrations in the 0.22.0-rc.1 release.
pub const V_0_22_0_RC1: &[Uuid] = &[
    v_tx_outputs_key_scopes::MIGRATION_ID,
    ivk_item_cache::MIGRATION_ID,
    add_transparent_receiver_address_index::MIGRATION_ID,
    add_transparent_value_index::MIGRATION_ID,
    ironwood_pool_code_views::MIGRATION_ID,
    tree_retained_checkpoints::MIGRATION_ID,
];

/// Leaf migrations in the 0.22.0-rc.2 release.
pub const V_0_22_0_RC2: &[Uuid] = &[
    v_tx_outputs_key_scopes::MIGRATION_ID,
    ivk_item_cache::MIGRATION_ID,
    add_transparent_receiver_address_index::MIGRATION_ID,
    add_transparent_value_index::MIGRATION_ID,
    ironwood_pool_code_views::MIGRATION_ID,
    orchard_ironwood_migration_tables::MIGRATION_ID,
    tree_retained_checkpoints::MIGRATION_ID,
    note_locking::MIGRATION_ID,
];

/// Leaf migrations as of the current repository state.
pub const CURRENT_LEAF_MIGRATIONS: &[Uuid] = &[
    v_tx_outputs_transparent_addresses::MIGRATION_ID,
    ivk_item_cache::MIGRATION_ID,
    add_transparent_receiver_address_index::MIGRATION_ID,
    add_transparent_value_index::MIGRATION_ID,
    fix_bad_ironwood_change_flagging::MIGRATION_ID,
    v_address_uses_ironwood::MIGRATION_ID,
    orchard_ironwood_migration_unsatisfiability::MIGRATION_ID,
    tree_retained_checkpoints::MIGRATION_ID,
    tx_status_observation_intent::MIGRATION_ID,
    v_transactions_zip318_kind::MIGRATION_ID,
];

pub(super) fn verify_network_compatibility<P: consensus::Parameters>(
    conn: &rusqlite::Connection,
    params: &P,
) -> Result<(), WalletMigrationError> {
    // Ensure that the `ufvk_support` migration has been applied; if it hasn't, we won't be able to
    // validate that the UFVKs in the wallet correspond to the network type that the wallet is
    // being migrated for.
    let has_ufvk = conn
        .query_row(
            &format!(
                "SELECT 1 FROM {} WHERE id = :migration_id",
                super::MIGRATIONS_TABLE
            ),
            named_params![":migration_id": &ufvk_support::MIGRATION_ID.as_bytes()[..]],
            |row| row.get::<_, bool>(0),
        )
        .optional()?
        == Some(true);

    if has_ufvk {
        let mut fvks_stmt = conn.prepare("SELECT ufvk FROM accounts")?;
        let mut rows = fvks_stmt.query([])?;
        while let Some(row) = rows.next()? {
            let ufvk_str = row.get::<_, String>(0)?;
            let (network, _) = Ufvk::decode(&ufvk_str).map_err(|e| {
                WalletMigrationError::CorruptedData(format!("Unable to parse UFVK: {e}"))
            })?;

            if network != params.network_type() {
                let network_name = |n| match n {
                    consensus::NetworkType::Main => "mainnet",
                    consensus::NetworkType::Test => "testnet",
                    consensus::NetworkType::Regtest => "regtest",
                };
                return Err(WalletMigrationError::CorruptedData(format!(
                    "Network type mismatch: account UFVK is for {} but attempting to initialize for {}.",
                    network_name(network),
                    network_name(params.network_type())
                )));
            }
        }
    }

    Ok(())
}

#[cfg(test)]
pub(crate) mod tests {
    use std::collections::HashSet;

    // Used only by the orchard-gated note-generation strategies below.
    #[cfg(feature = "orchard")]
    use proptest::{prelude::any, prop_compose};
    use rusqlite::Connection;
    use secrecy::Secret;
    use tempfile::NamedTempFile;
    use uuid::Uuid;
    use zcash_protocol::consensus::Network;

    #[cfg(feature = "unstable")]
    use super::ids;
    use crate::{
        WalletDb,
        testing::db::{test_clock, test_rng},
        wallet::init::WalletMigrator,
    };
    use schemerz::Migration;

    /// `CURRENT_LEAF_MIGRATIONS` must list exactly the leaves of the migration dependency graph
    /// (the migrations that no other migration depends on), so that migrating to the current
    /// state reaches every migration. This recomputes the leaves from the graph and checks them
    /// against the constant, so a newly added migration that supersedes an existing leaf cannot
    /// silently leave a stale entry behind.
    #[test]
    fn current_leaf_migrations_are_the_dag_leaves() {
        let migrations =
            super::all_migrations(&Network::TestNetwork, test_clock(), test_rng(), None);

        let all_ids: HashSet<Uuid> = migrations.iter().map(|m| m.id()).collect();
        let depended: HashSet<Uuid> = migrations.iter().flat_map(|m| m.dependencies()).collect();
        let computed_leaves: HashSet<Uuid> = all_ids.difference(&depended).copied().collect();

        let listed_leaves: HashSet<Uuid> = super::CURRENT_LEAF_MIGRATIONS.iter().copied().collect();
        assert_eq!(computed_leaves, listed_leaves);
    }

    /// Every migration in the graph must have a publicly-exported identifier, so that an
    /// external migration can always anchor itself precisely. Listing them here rather than
    /// deriving them means a migration added without an `ids` entry fails this test.
    #[test]
    #[cfg(feature = "unstable")]
    fn ids_module_covers_every_migration() {
        let exported: HashSet<Uuid> = HashSet::from([
            ids::ACCOUNT_DELETE_CASCADE,
            ids::ADD_ACCOUNT_BIRTHDAYS,
            ids::ADD_ACCOUNT_UUIDS,
            ids::ADD_TRANSACTION_TRUST_MARKER,
            ids::ADD_TRANSACTION_VIEWS,
            ids::ADD_TRANSPARENT_RECEIVER_ADDRESS_INDEX,
            ids::ADD_TRANSPARENT_VALUE_INDEX,
            ids::ADD_UTXO_ACCOUNT,
            ids::ADDRESSES_TABLE,
            ids::ENSURE_DEFAULT_TRANSPARENT_ADDRESS,
            ids::ENSURE_ORCHARD_UA_RECEIVER,
            ids::EPHEMERAL_ADDRESSES,
            ids::FIX_BAD_CHANGE_FLAGGING,
            ids::FIX_BAD_IRONWOOD_CHANGE_FLAGGING,
            ids::FIX_BROKEN_COMMITMENT_TREES,
            ids::FIX_TRANSPARENT_RECEIVED_OUTPUTS,
            ids::FIX_V_TRANSACTIONS_EXPIRED_UNMINED,
            ids::FULL_ACCOUNT_IDS,
            ids::INITIAL_SETUP,
            ids::IRONWOOD_POOL_CODE_VIEWS,
            ids::IRONWOOD_RECEIVED_NOTES,
            ids::IRONWOOD_SHARDTREE,
            ids::IVK_ITEM_CACHE,
            ids::NOTE_LOCKING,
            ids::NULLIFIER_MAP,
            ids::ORCHARD_IRONWOOD_MIGRATION_ANCHOR_INTERVAL,
            ids::ORCHARD_IRONWOOD_MIGRATION_TABLES,
            ids::ORCHARD_IRONWOOD_MIGRATION_UNSATISFIABILITY,
            ids::ORCHARD_NOTE_VERSION,
            ids::ORCHARD_RECEIVED_NOTES,
            ids::ORCHARD_SHARDTREE,
            ids::RECEIVED_NOTES_NULLABLE_NF,
            ids::RECEIVING_KEY_SCOPES,
            ids::SAPLING_MEMO_CONSISTENCY,
            ids::SENT_NOTES_TO_INTERNAL,
            ids::SHARDTREE_SUPPORT,
            ids::SPEND_KEY_AVAILABLE,
            ids::STANDALONE_P2SH,
            ids::SUPPORT_LEGACY_SQLITE,
            ids::SUPPORT_ZCASHD_WALLET_IMPORT,
            ids::TRANSPARENT_GAP_LIMIT_HANDLING,
            ids::TREE_RETAINED_CHECKPOINTS,
            ids::TX_OBSERVATION_HEIGHT,
            ids::TX_RETRIEVAL_QUEUE,
            ids::TX_RETRIEVAL_QUEUE_EXPIRY,
            ids::TX_STATUS_OBSERVATION_INTENT,
            ids::UFVK_SUPPORT,
            ids::UTXOS_TABLE,
            ids::UTXOS_TO_TXOS,
            ids::V_ADDRESS_USES_IRONWOOD,
            ids::V_RECEIVED_OUTPUT_SPENDS_ACCOUNT,
            ids::V_SAPLING_SHARD_UNSCANNED_RANGES,
            ids::V_TRANSACTIONS_ADDITIONAL_TOTALS,
            ids::V_TRANSACTIONS_NET,
            ids::V_TRANSACTIONS_NOTE_UNIQUENESS,
            ids::V_TRANSACTIONS_POOL_CROSSING,
            ids::V_TRANSACTIONS_SHIELDING_BALANCE,
            ids::V_TRANSACTIONS_TRANSPARENT_HISTORY,
            ids::V_TX_OUTPUTS_KEY_SCOPES,
            ids::V_TX_OUTPUTS_RETURN_ADDRS,
            ids::V_TX_OUTPUTS_TRANSPARENT_ADDRESSES,
            ids::V_TX_OUTPUTS_USE_LEGACY_FALSE,
            ids::V_TRANSACTIONS_ZIP318_KIND,
            ids::WALLET_SUMMARIES,
            ids::WITNESS_STABILIZED_NOTES,
            ids::ZIP318_CLASSIFICATION,
        ]);

        let migrations =
            super::all_migrations(&Network::TestNetwork, test_clock(), test_rng(), None);
        let all_ids: HashSet<Uuid> = migrations.iter().map(|m| m.id()).collect();

        assert_eq!(all_ids, exported);
    }

    /// A synthetic set of Orchard note payload values, for exercising migrations that touch the
    /// `orchard_received_notes` table.
    ///
    /// The identity columns (`transaction_id`, `action_index`, `nf`) are assigned by the caller so
    /// that they stay unique; this strategy fuzzes only the note payload. Kept in the shared
    /// migration-test module so later migration tests can reuse it.
    #[cfg(feature = "orchard")]
    #[derive(Clone, Debug)]
    pub(crate) struct ArbOrchardNote {
        pub(crate) value: i64,
        pub(crate) diversifier: [u8; 11],
        pub(crate) rho: [u8; 32],
        pub(crate) rseed: [u8; 32],
        pub(crate) is_change: bool,
        pub(crate) memo: Option<Vec<u8>>,
    }

    #[cfg(feature = "orchard")]
    prop_compose! {
        /// A strategy generating arbitrary [`ArbOrchardNote`] payload values. Note values are
        /// constrained to the non-negative `i64` range, matching the on-chain 2^63 value bound.
        pub(crate) fn arb_orchard_note()(
            value in 0i64..=i64::MAX,
            diversifier in any::<[u8; 11]>(),
            rho in any::<[u8; 32]>(),
            rseed in any::<[u8; 32]>(),
            is_change in any::<bool>(),
            memo in proptest::option::of(proptest::collection::vec(any::<u8>(), 0..64)),
        ) -> ArbOrchardNote {
            ArbOrchardNote {
                value,
                diversifier,
                rho,
                rseed,
                is_change,
                memo,
            }
        }
    }

    /// A synthetic set of Ironwood note payload values, for exercising migrations that touch the
    /// `ironwood_received_notes` table.
    ///
    /// Ironwood notes ([ZIP 2005], NU6.3) are Orchard-protocol notes obtained from version 3 note
    /// plaintexts, so the payload columns mirror those of [`ArbOrchardNote`]; the type is kept
    /// distinct to follow Ironwood naming and to leave room for the two to diverge. The identity
    /// columns (`transaction_id`, `action_index`, `nf`) are assigned by the caller so that they
    /// stay unique; this strategy fuzzes only the note payload.
    ///
    /// [ZIP 2005]: https://zips.z.cash/zip-2005
    #[cfg(feature = "orchard")]
    #[derive(Clone, Debug)]
    pub(crate) struct ArbIronwoodNote {
        pub(crate) value: i64,
        pub(crate) diversifier: [u8; 11],
        pub(crate) rho: [u8; 32],
        pub(crate) rseed: [u8; 32],
        pub(crate) is_change: bool,
        pub(crate) memo: Option<Vec<u8>>,
    }

    #[cfg(feature = "orchard")]
    prop_compose! {
        /// A strategy generating arbitrary [`ArbIronwoodNote`] payload values. Note values are
        /// constrained to the non-negative `i64` range, matching the on-chain 2^63 value bound.
        pub(crate) fn arb_ironwood_note()(
            value in 0i64..=i64::MAX,
            diversifier in any::<[u8; 11]>(),
            rho in any::<[u8; 32]>(),
            rseed in any::<[u8; 32]>(),
            is_change in any::<bool>(),
            memo in proptest::option::of(proptest::collection::vec(any::<u8>(), 0..64)),
        ) -> ArbIronwoodNote {
            ArbIronwoodNote {
                value,
                diversifier,
                rho,
                rseed,
                is_change,
                memo,
            }
        }
    }

    /// Tests that we can migrate from a completely empty wallet database to the target
    /// migrations.
    pub(crate) fn test_migrate(migrations: &[Uuid]) {
        let data_file = NamedTempFile::new().unwrap();
        let mut db_data = WalletDb::for_path(
            data_file.path(),
            Network::TestNetwork,
            test_clock(),
            test_rng(),
        )
        .unwrap();

        let seed = [0xab; 32];
        assert_matches!(
            WalletMigrator::new()
                .with_seed(Secret::new(seed.to_vec()))
                .ignore_seed_relevance()
                .init_or_migrate_to(&mut db_data, migrations),
            Ok(_)
        );
    }

    #[test]
    fn migrate_between_releases_without_data() {
        let data_file = NamedTempFile::new().unwrap();
        let mut db_data = WalletDb::for_path(
            data_file.path(),
            Network::TestNetwork,
            test_clock(),
            test_rng(),
        )
        .unwrap();

        let seed = [0xab; 32].to_vec();

        let mut prev_state = HashSet::new();
        let mut ensure_migration_state_changed = |conn: &Connection| {
            let new_state = conn
                .prepare_cached("SELECT * FROM schemer_migrations")
                .unwrap()
                .query_map([], |row| row.get::<_, [u8; 16]>(0).map(Uuid::from_bytes))
                .unwrap()
                .collect::<Result<HashSet<Uuid>, _>>()
                .unwrap();
            assert!(prev_state != new_state);
            prev_state = new_state;
        };

        let mut prev_leaves: &[Uuid] = &[];
        for migrations in super::PUBLIC_MIGRATION_STATES {
            assert_matches!(
                WalletMigrator::new()
                    .with_seed(Secret::new(seed.clone()))
                    .ignore_seed_relevance()
                    .init_or_migrate_to(&mut db_data, migrations),
                Ok(_)
            );

            // If we have any new leaves, ensure the migration state changed. This lets us
            // represent releases that changed the graph edges without introducing any new
            // migrations.
            if migrations.iter().any(|m| !prev_leaves.contains(m)) {
                ensure_migration_state_changed(&db_data.conn);
            }

            prev_leaves = *migrations;
        }

        // Now check that we can migrate from the last public release to the current
        // migration state in this branch.
        assert_matches!(
            WalletMigrator::new()
                .with_seed(Secret::new(seed))
                .ignore_seed_relevance()
                .init_or_migrate(&mut db_data),
            Ok(_)
        );
        // We don't ensure that the migration state changed, because it may not have.
    }
}