zakura-client-sqlite 0.1.0-rc9.1

An SQLite-based Zcash light client
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
use super::*;
use zakura_one_time_ivk::Purpose;
use zcash_client_backend::data_api::one_time_ivk::restore::{DiscoveryStatus, LookupWork};

/// Receive key 8, which the fixtures pay before registering it.
pub(in crate::wallet::one_time_ivk) fn key8() -> KeyId {
    KeyId::new(Purpose::Receive, 8)
}

/// A filter check that marks `key` seen and paid.
pub(in crate::wallet::one_time_ivk) fn paid(key: KeyId) -> FilterCheck {
    FilterCheck {
        key,
        seen: true,
        paid: true,
        recent: false,
    }
}

/// The publication ending at cached block `height`, as the directory declares it, whether
/// or not the wallet stored that block.
pub(in crate::wallet::one_time_ivk) fn publication_at(
    st: &State,
    height: BlockHeight,
) -> PublicationEnd {
    let data: Vec<u8> = value(
        &st.cache().0,
        "SELECT data FROM compactblocks WHERE height = ?1",
        [u32::from(height)],
    );
    let block = CompactBlock::decode(data.as_slice()).unwrap();
    PublicationEnd {
        height,
        hash: block.hash().0,
        tree_size: block
            .chain_metadata
            .unwrap()
            .ironwood_commitment_tree_size
            .into(),
    }
}

/// Runs a whole discovery pass of `account` against `publication`: incoming `checks`, and
/// no refund key used. Returns whether the store took the publication.
pub(in crate::wallet::one_time_ivk) fn run_pass(
    db: &mut Db,
    account: AccountUuid,
    publication: PublicationEnd,
    checks: &[FilterCheck],
) -> bool {
    let done = EnumerationCursor {
        next: 100,
        reach: 100,
    };
    let now = clock_now();
    let took = db
        .record_one_time_filter_checks(
            account,
            publication,
            Purpose::Receive,
            checks,
            done,
            true,
            now,
        )
        .unwrap();
    took && db
        .record_one_time_filter_checks(account, publication, Purpose::Refund, &[], done, true, now)
        .unwrap()
}

/// `account`'s discovery status.
pub(in crate::wallet::one_time_ivk) fn status(db: &Db, account: AccountUuid) -> DiscoveryStatus {
    db.one_time_discovery(account, clock_now())
        .unwrap()
        .unwrap()
}

/// Records `payments` as `key`'s whole history read from `revision`, from page zero.
pub(in crate::wallet::one_time_ivk) fn look_up(
    db: &mut Db,
    account: AccountUuid,
    key: KeyId,
    revision: PublicationEnd,
    payments: &[PendingPayment],
) -> bool {
    let records: Vec<_> = payments.iter().map(directory_answer).collect();
    db.record_one_time_lookup(account, key, revision, 0, &records, true, clock_now())
        .unwrap()
}

/// The path of the chain's Ironwood leaf at `position` at the cached tip.
pub(in crate::wallet::one_time_ivk) fn witness_at(st: &State, position: u32) -> MerklePath {
    use incrementalmerkletree::{frontier::CommitmentTree, witness::IncrementalWitness};
    let mut tree = CommitmentTree::<MerkleHashOrchard, 32>::empty();
    let mut witness: Option<IncrementalWitness<_, 32>> = None;
    let blocks: Vec<Vec<u8>> = column(
        &st.cache().0,
        "SELECT data FROM compactblocks ORDER BY height",
        [],
    );
    let actions = blocks
        .iter()
        .flat_map(|data| CompactBlock::decode(data.as_slice()).unwrap().vtx)
        .flat_map(|tx| tx.ironwood_actions);
    for (leaf, action) in actions.enumerate() {
        let cmx = MerkleHashOrchard::from_cmx(&action.cmx().unwrap());
        match &mut witness {
            Some(witness) => witness.append(cmx).unwrap(),
            None => {
                tree.append(cmx).unwrap();
                if leaf == position as usize {
                    witness = IncrementalWitness::from_tree(tree.clone());
                }
            }
        }
    }
    witness.unwrap().path().unwrap().into()
}

/// `path`'s sibling hashes, as the directory serves them.
pub(in crate::wallet::one_time_ivk) fn siblings(path: &MerklePath) -> [[u8; 32]; 32] {
    std::array::from_fn(|level| path.auth_path()[level].to_bytes())
}

/// Records the path of `key`'s late payment `candidate` at the scanned tip.
pub(in crate::wallet::one_time_ivk) fn send_witness(
    st: &mut State,
    key: KeyId,
    candidate: &PendingPayment,
) {
    let account = st.test_account().unwrap().id();
    let (anchor, path) = (tip(st), witness_at(st, candidate.position));
    let db = st.wallet_mut().db_mut();
    db.record_one_time_witness(
        account,
        key,
        candidate.position,
        anchor,
        siblings(&path),
        clock_now(),
    )
    .unwrap();
}

/// Pays [`key8`] in a [`restored_wallet`] before the wallet knows the key and scans past
/// the payment, then runs a pass that finds the key paid and records its lookup: the
/// payment is a late candidate. Returns the payment and the publication, which ends at the
/// scanned tip.
pub(in crate::wallet::one_time_ivk) fn late_fixture() -> (State, PendingPayment, PublicationEnd) {
    let mut st = restored_wallet();
    let account = st.test_account().unwrap().id();
    let candidate = pay_candidate(&mut st, key8());
    st.generate_and_scan_empty_blocks_with_one_time_ivks(2);
    let publication = discover(&mut st, account, &[paid(key8())]);
    let db = st.wallet_mut().db_mut();
    assert!(look_up(
        db,
        account,
        key8(),
        publication,
        &[candidate.clone()]
    ));
    (st, candidate, publication)
}

/// The note values the wallet holds for one-time keys, with whether each is spent.
pub(in crate::wallet::one_time_ivk) fn one_time_notes(st: &State) -> Vec<(i64, bool)> {
    rows(
        st.wallet().conn(),
        "SELECT n.value, EXISTS(SELECT 1 FROM ironwood_received_note_spends s
             WHERE s.ironwood_received_note_id = n.id)
         FROM ironwood_received_notes n WHERE n.one_time_key_id IS NOT NULL ORDER BY n.id",
        [],
    )
}

/// A payment found after the scan passed its block waits for its inclusion path, then is
/// admitted unspent, once, and discovery completes.
#[test]
fn a_late_payment_is_admitted_once_its_path_arrives() {
    let (mut st, candidate, publication) = late_fixture();
    let account = st.test_account().unwrap().id();
    let db = st.wallet_mut().db_mut();
    let found = status(db, account);
    assert_eq!(found.publication, Some(publication));
    // The waiting payment keeps discovery incomplete, so the app keeps running it.
    assert!(!found.complete);
    assert!(found.lookups.is_empty());
    assert_eq!(found.witnesses.len(), 1);
    assert_eq!(found.witnesses[0].position, candidate.position);
    assert!(one_time_notes(&st).is_empty());

    send_witness(&mut st, key8(), &candidate);
    assert_eq!(one_time_notes(&st), [(100_000, false)]);
    let db = st.wallet_mut().db_mut();
    assert!(pending(db, account, key8()).is_empty());
    let found = status(db, account);
    assert!(found.witnesses.is_empty());
    assert!(found.complete);
    // The same answer again changes nothing.
    assert!(!look_up(db, account, key8(), publication, &[candidate]));
    assert_eq!(one_time_notes(&st), [(100_000, false)]);
}

/// A late payment whose note was spent before discovery found it is admitted spent: the
/// spending transaction carries an output the scan recognized, so the journal holds the
/// note's nullifier.
#[test]
fn a_spent_late_payment_is_admitted_spent_through_the_journal() {
    let mut st = ironwood_wallet();
    let account = st.test_account().unwrap().id();
    let candidate = pay_candidate(&mut st, key8());
    let note = candidate
        .encrypted_note
        .decrypt(&parent(&st), key8())
        .unwrap();
    let spender = IronwoodFvk(parent(&st));
    let (height, _) = st.generate_next_block_spending(
        &spender,
        (*note.nullifier(), Zatoshis::const_from_u64(100_000)),
        unified(foreign_receiver()),
        Zatoshis::const_from_u64(20_000),
    );
    st.scan_cached_blocks_with_one_time_ivks(height, 1);
    st.generate_and_scan_empty_blocks_with_one_time_ivks(2);
    let publication = discover(&mut st, account, &[paid(key8())]);
    let db = st.wallet_mut().db_mut();
    assert!(look_up(
        db,
        account,
        key8(),
        publication,
        &[candidate.clone()]
    ));
    send_witness(&mut st, key8(), &candidate);
    assert_eq!(one_time_notes(&st), [(100_000, true)]);
}

/// A late payment whose sibling leaf the wallet already holds unpruned is admitted with
/// that sibling intact. Here the sibling is the funder's change after it, its block's last
/// leaf, which the wallet keeps as a reference once a later scan starts after that block.
/// Inserting the sibling's hash before the marked payment let the pair prune, and
/// inserting the payment under the pruned node then lost the sibling.
#[test]
fn a_late_payment_keeps_a_sibling_the_wallet_already_holds() {
    let foreign = orchard::keys::FullViewingKey::from(
        &orchard::keys::SpendingKey::from_bytes([3; 32]).unwrap(),
    );
    let receiver = derive(&ironwood_wallet(), key8()).address_at(0u32, Scope::External);
    // The payment is the first of its bundle's two actions in some attempts.
    for _ in 0..40 {
        let tx = external_payment(receiver, &zcash_protocol::memo::MemoBytes::empty());
        let mut st = ironwood_wallet();
        let account = st.test_account().unwrap().id();
        let (height, _) = st.generate_next_block_from_tx(1, &tx);
        st.scan_cached_blocks_with_one_time_ivks(height, 1);
        let candidate = candidate_at(&st, height, &derive(&st, key8()));
        if candidate.position != 0 {
            continue;
        }
        // A later scan starts after the payment's block.
        pay(&mut st, &foreign, 10_000);
        let publication = discover(&mut st, account, &[paid(key8())]);
        let db = st.wallet_mut().db_mut();
        assert!(look_up(
            db,
            account,
            key8(),
            publication,
            &[candidate.clone()]
        ));
        send_witness(&mut st, key8(), &candidate);
        assert_eq!(one_time_notes(&st), [(50_000, false)]);
        assert_eq!(notes(&st, tip(&st).height).len(), 1);
        return;
    }
    panic!("the payment was never its bundle's first action");
}

/// A publication above the wallet's chain tip, or older than the current pass's, is
/// refused.
#[test]
fn stale_and_future_publications_are_refused() {
    let mut st = restored_wallet();
    let account = st.test_account().unwrap().id();
    let current = publication_at(&st, tip(&st).height);
    let mut future = current;
    future.height = current.height + 10;
    let db = st.wallet_mut().db_mut();
    assert!(!run_pass(db, account, future, &[]));
    assert!(run_pass(db, account, current, &[]));
    st.generate_and_scan_empty_blocks_with_one_time_ivks(1);
    discover(&mut st, account, &[]);
    let db = st.wallet_mut().db_mut();
    assert!(!run_pass(db, account, current, &[]));
}

/// A rewind below a late payment returns it to a hint, which the rescan settles.
#[test]
fn a_rewind_returns_late_payments_to_hints() {
    let (mut st, candidate, _) = late_fixture();
    st.truncate_to_height_retaining_cache(candidate.height - 1);
    assert!(scan_hint(&st));
    let tip = st.latest_cached_block().unwrap().height();
    st.scan_cached_blocks_with_one_time_ivks(
        candidate.height,
        u32::from(tip - candidate.height) as usize + 1,
    );
    assert_eq!(one_time_notes(&st), [(100_000, false)]);
}

/// A publication ending before the birthday completes discovery without lookups, still
/// protecting its paid keys from issuance. Its keys close, so a recheck checks them for a
/// payment after the birthday.
#[test]
fn a_publication_before_the_birthday_needs_no_lookups() {
    let mut st = restored_wallet();
    let account = st.test_account().unwrap().id();
    let birthday = st.test_account().unwrap().birthday().height();
    let publication = PublicationEnd {
        height: birthday - 1,
        hash: [3; 32],
        tree_size: 0,
    };
    let db = st.wallet_mut().db_mut();
    assert!(run_pass(db, account, publication, &[paid(key8())]));
    let found = status(db, account);
    assert!(found.complete);
    assert!(found.lookups.is_empty());
    assert!(key_state(db, key8()).1);
}

/// Whether the wallet's one queued candidate is a scan hint rather than a late candidate.
fn scan_hint(st: &State) -> bool {
    value(
        st.wallet().conn(),
        "SELECT scan_hint FROM ironwood_one_time_payment_recovery",
        [],
    )
}

/// Mines, without scanning, a block paying [`key8`] above a scanned tip, and runs a pass
/// whose publication ends there, recording the payment: a scan hint. Returns the payment.
fn hint_fixture() -> (State, PendingPayment) {
    let mut st = restored_wallet();
    let account = st.test_account().unwrap().id();
    let fvk = derive(&st, key8());
    let amount = Zatoshis::const_from_u64(100_000);
    let paid_to = IronwoodFvk(fvk.clone());
    let (height, _, _) = st.generate_next_block(&paid_to, AddressType::DefaultExternal, amount);
    let candidate = candidate_at(&st, height, &fvk);
    st.wallet_mut().update_chain_tip(height).unwrap();
    let publication = publication_at(&st, height);
    let db = st.wallet_mut().db_mut();
    assert!(run_pass(db, account, publication, &[paid(key8())]));
    assert!(look_up(
        db,
        account,
        key8(),
        publication,
        &[candidate.clone()]
    ));
    assert!(scan_hint(&st));
    (st, candidate)
}

/// A payment whose block the scan has not stored is a hint: the batch holding it tries the
/// key, finds the note itself, judges the publication, and leaves nothing pending.
#[test]
fn a_hint_is_found_by_the_scan() {
    let (mut st, candidate) = hint_fixture();
    let account = st.test_account().unwrap().id();
    assert!(!status(st.wallet_mut().db_mut(), account).complete);
    st.scan_cached_blocks_with_one_time_ivks(candidate.height, 1);
    assert_eq!(one_time_notes(&st), [(100_000, false)]);
    let db = st.wallet_mut().db_mut();
    assert!(pending(db, account, key8()).is_empty());
    assert!(status(db, account).complete);
}

/// A scan that did not try a hint's key, here a plain scan, leaves its payment late; the
/// late path then admits it.
#[test]
fn a_hint_a_scan_skips_becomes_late() {
    let (mut st, candidate) = hint_fixture();
    st.scan_cached_blocks(candidate.height, 1);
    assert!(one_time_notes(&st).is_empty());
    assert!(!scan_hint(&st));
    send_witness(&mut st, key8(), &candidate);
    assert_eq!(one_time_notes(&st), [(100_000, false)]);
}

/// A hint the scan tried and missed is the directory's error: its lookup restarts from page
/// zero, backed off so a directory that keeps giving it does not loop.
#[test]
fn a_missed_hint_restarts_its_lookup_backed_off() {
    let (mut st, candidate) = hint_fixture();
    let account = st.test_account().unwrap().id();
    // The record claims the next position, which holds no note for the key.
    exec(
        st.wallet().conn(),
        "UPDATE ironwood_one_time_payment_recovery SET position = position + 1",
        [],
    );
    st.scan_cached_blocks_with_one_time_ivks(candidate.height, 1);
    let db = st.wallet_mut().db_mut();
    assert!(pending(db, account, key8()).is_empty());
    let (cutoff, next_attempt): (Option<u32>, i64) = row(
        &db.conn,
        "SELECT cutoff_height, next_attempt_at FROM ironwood_one_time_lookups",
        [],
    );
    assert!(cutoff.is_some());
    assert!(next_attempt > clock_now());
    assert!(status(db, account).lookups.is_empty());
}

/// A fresh account's key registered for a lookup after discovery finished, as a recovered
/// refund key is, keeps discovery incomplete until a pass looks it up.
#[test]
fn a_fresh_accounts_waiting_lookup_keeps_discovery_incomplete() {
    let mut st = scanned_wallet();
    let account = st.test_account().unwrap().id();
    let from = tip(&st).height + 1;
    let db = st.wallet_mut().db_mut();
    let key = reserve_key(db, account, Purpose::Receive, from).key_id();
    exec(
        &db.conn,
        "INSERT INTO ironwood_one_time_lookups (one_time_key_id)
         SELECT id FROM ironwood_one_time_keys WHERE key_index = ?1",
        [key.index().to_be_bytes()],
    );
    let found = status(db, account);
    assert!(!found.complete);
    assert!(found.publication.is_none());
    discover(&mut st, account, &[]);
    let due = status(st.wallet().db(), account).lookups;
    assert_eq!(due.len(), 1);
    assert_eq!(due[0].work.key, key);
}

/// A publication whose last block differs from the wallet's is rejected: nothing is due
/// against it, and the account waits for a newer one. Its paid key's lookup stays due in
/// the next pass.
#[test]
fn a_publication_off_the_wallets_chain_is_rejected() {
    let mut st = restored_wallet();
    let account = st.test_account().unwrap().id();
    let mut publication = publication_at(&st, tip(&st).height);
    publication.hash = [9; 32];
    let db = st.wallet_mut().db_mut();
    assert!(run_pass(db, account, publication, &[paid(key8())]));
    let found = status(db, account);
    assert!(!found.complete);
    assert!(found.lookups.is_empty());
    st.generate_and_scan_empty_blocks_with_one_time_ivks(1);
    let publication = discover(&mut st, account, &[]);
    let db = st.wallet_mut().db_mut();
    let found = status(db, account);
    assert!(!found.complete);
    assert_eq!(found.lookups.len(), 1);
    assert!(look_up(db, account, key8(), publication, &[]));
    assert!(status(db, account).complete);
}

/// A publication rejected for its tree size can be corrected at the same block, and a claim
/// that contradicts an accepted one is refused. Another account's pass over the rejected
/// claim starts over.
#[test]
fn a_rejected_tree_size_can_be_corrected() {
    let mut st = ironwood_wallet();
    let account = st.test_account().unwrap().id();
    let seed = SecretVec::new(vec![3; 32]);
    let birthday = st.test_account().unwrap().birthday().clone();
    let (other, _) = st
        .wallet_mut()
        .db_mut()
        .create_account("other", &seed, &birthday, None)
        .unwrap();
    st.generate_and_scan_empty_blocks_with_one_time_ivks(1);
    let publication = publication_at(&st, tip(&st).height);
    let wrong = PublicationEnd {
        tree_size: publication.tree_size + 1,
        ..publication
    };
    let db = st.wallet_mut().db_mut();
    assert!(run_pass(db, other, wrong, &[]));
    assert!(run_pass(db, account, wrong, &[paid(key8())]));
    let found = status(db, account);
    assert!(found.publication.is_none());
    assert!(found.lookups.is_empty());
    assert!(run_pass(db, account, publication, &[paid(key8())]));
    let found = status(db, account);
    assert_eq!(found.publication, Some(publication));
    assert_eq!(found.lookups.len(), 1);
    assert!(!run_pass(db, account, wrong, &[]));
    assert!(!look_up(db, account, key8(), wrong, &[]));
    assert!(look_up(db, account, key8(), publication, &[]));
    assert!(status(db, account).complete);
    let found = status(db, other);
    assert!(found.publication.is_none());
    assert!(!found.complete);
}

/// A recheck reads again the history of a key with a known payment, including one before
/// the birthday, but not of a key the filters only marked seen, which waits for newer
/// filters to mark it paid.
#[test]
fn a_recheck_looks_up_only_keys_with_payments() {
    let (mut st, candidate, _) = late_fixture();
    let account = st.test_account().unwrap().id();
    send_witness(&mut st, key8(), &candidate);
    let seen = |index| FilterCheck {
        key: KeyId::new(Purpose::Receive, index),
        paid: false,
        ..paid(key8())
    };
    st.generate_and_scan_empty_blocks_with_one_time_ivks(1);
    discover(&mut st, account, &[seen(9), seen(10)]);
    let db = st.wallet_mut().db_mut();
    exec(
        &db.conn,
        "UPDATE ironwood_one_time_keys SET paid_before_birthday = 1 WHERE key_index = ?1",
        [10u64.to_be_bytes()],
    );
    db.recheck_one_time_key_history(account).unwrap();
    st.generate_and_scan_empty_blocks_with_one_time_ivks(1);
    discover(&mut st, account, &[]);
    let db = st.wallet_mut().db_mut();
    let due: Vec<KeyId> = status(db, account)
        .lookups
        .iter()
        .map(|l| l.work.key)
        .collect();
    assert_eq!(due, [key8(), KeyId::new(Purpose::Receive, 10)]);
}

/// A filter hit on a closed key past the searched range, maybe a false positive at a sparse
/// index, does not move refund issuance past it.
#[test]
fn a_hit_past_the_search_does_not_advance_allocation() {
    let mut st = scanned_wallet();
    let account = st.test_account().unwrap().id();
    let publication = publication_at(&st, tip(&st).height);
    let sparse = FilterCheck {
        key: KeyId::new(Purpose::Refund, 1_000_000),
        paid: false,
        ..paid(key8())
    };
    let searched = EnumerationCursor {
        next: 100,
        reach: 100,
    };
    let db = st.wallet_mut().db_mut();
    for (purpose, checks) in [(Purpose::Receive, vec![]), (Purpose::Refund, vec![sparse])] {
        assert!(
            db.record_one_time_filter_checks(
                account,
                publication,
                purpose,
                &checks,
                searched,
                true,
                clock_now()
            )
            .unwrap()
        );
    }
    let next = reserve_key(db, account, Purpose::Refund, start());
    assert_eq!(next.key_id().index(), 0);
}

/// An unknown account is an error, not an account with no discovery to run.
#[test]
fn an_unknown_accounts_discovery_is_an_error() {
    let st = restored_wallet();
    let unknown = AccountUuid::from_uuid(uuid::Uuid::nil());
    assert!(matches!(
        st.wallet().db().one_time_discovery(unknown, clock_now()),
        Err(SqliteClientError::AccountUnknown)
    ));
}

/// A key the filters mark seen but not paid closes even when the publication was accepted
/// before the pass registered it, so a newer publication rechecks it.
#[test]
fn a_seen_key_closes_against_an_accepted_publication() {
    let mut st = restored_wallet();
    let account = st.test_account().unwrap().id();
    let seen = FilterCheck {
        paid: false,
        ..paid(key8())
    };
    discover(&mut st, account, &[seen]);
    let db = st.wallet_mut().db_mut();
    let (closed,): (bool,) = row(
        &db.conn,
        "SELECT closed_at IS NOT NULL FROM ironwood_one_time_keys WHERE key_index = ?1",
        [key8().index().to_be_bytes()],
    );
    assert!(closed);
    assert!(status(db, account).complete);
}

/// A page refused for a bad record keeps nothing it queued before that record, so the
/// corrected page is taken.
#[test]
fn a_refused_page_queues_nothing() {
    let mut st = ironwood_wallet();
    let account = st.test_account().unwrap().id();
    let candidate = pay_candidate(&mut st, key8());
    st.generate_and_scan_empty_blocks_with_one_time_ivks(1);
    let publication = discover(&mut st, account, &[paid(key8())]);
    let db = st.wallet_mut().db_mut();
    let beyond = DirectoryPayment {
        height: u32::from(publication.height) + 1,
        ..directory_answer(&candidate)
    };
    let page = [directory_answer(&candidate), beyond];
    assert!(
        !db.record_one_time_lookup(account, key8(), publication, 0, &page, true, clock_now())
            .unwrap()
    );
    assert!(pending(db, account, key8()).is_empty());
    exec(
        &db.conn,
        "UPDATE ironwood_one_time_lookups SET next_attempt_at = 0",
        [],
    );
    assert!(look_up(db, account, key8(), publication, &[candidate]));
    assert_eq!(pending(db, account, key8()).len(), 1);
}

/// Paging resumes from the cursor and refuses a page that does not continue it.
#[test]
fn paging_resumes_from_its_cursor() {
    let mut st = ironwood_wallet();
    let account = st.test_account().unwrap().id();
    let first = pay_candidate(&mut st, key8());
    let fvk = derive(&st, key8());
    let height = pay(&mut st, &fvk, 50_000);
    let second = candidate_at(&st, height, &fvk);
    st.generate_and_scan_empty_blocks_with_one_time_ivks(1);
    // The scan did not know the key, so both payments are late.
    assert!(unspent_keys(&st, tip(&st).height).is_empty());
    let publication = discover(&mut st, account, &[paid(key8())]);
    let db = st.wallet_mut().db_mut();
    let now = clock_now();
    let page = |p: &PendingPayment| [directory_answer(p)];
    let record = |db: &mut Db, from: u32, p: &PendingPayment, done: bool| {
        db.record_one_time_lookup(account, key8(), publication, from, &page(p), done, now)
            .unwrap()
    };
    assert!(record(db, 0, &first, false));
    let LookupWork { applied, last, .. } = status(db, account).lookups[0].clone();
    assert_eq!(applied, 1);
    assert_eq!(
        last.map(|(position, _)| position),
        Some(u64::from(first.position))
    );
    assert!(!record(db, 2, &second, true));
    assert!(record(db, 1, &second, true));
    let found = status(db, account);
    assert!(found.lookups.is_empty());
    assert_eq!(found.witnesses.len(), 2);
}

/// A fresh account needs no discovery, even once a key it issued closes without a payment,
/// until a recheck asks for it. The recheck's pass looks up the closed key the filters mark
/// paid, and its discovery stays visible while that lookup is unfinished, even once a
/// payment it found leaves no key closed unpaid. Issuance waits only for the pass's
/// registration.
#[test]
fn a_fresh_account_rechecks_on_request() {
    let mut st = scanned_wallet();
    let account = st.test_account().unwrap().id();
    // The key starts after its payment, so the scan misses it and discovery finds it late.
    let after = tip(&st).height + 10;
    let db = st.wallet_mut().db_mut();
    assert!(
        db.one_time_discovery(account, clock_now())
            .unwrap()
            .is_none()
    );
    let key = reserve_key(db, account, Purpose::Receive, after).key_id();
    exec(
        &db.conn,
        "UPDATE ironwood_one_time_keys SET closed_at = 1 WHERE key_index = ?1",
        [key.index().to_be_bytes()],
    );
    assert!(
        db.one_time_discovery(account, clock_now())
            .unwrap()
            .is_none()
    );
    db.recheck_one_time_key_history(account).unwrap();
    assert!(status(db, account).publication.is_none());
    let candidate = pay_candidate(&mut st, key);
    st.generate_and_scan_empty_blocks_with_one_time_ivks(1);
    let publication = discover(&mut st, account, &[paid(key)]);
    let db = st.wallet_mut().db_mut();
    let page = [directory_answer(&candidate)];
    assert!(
        db.record_one_time_lookup(account, key, publication, 0, &page, false, clock_now())
            .unwrap()
    );
    send_witness(&mut st, key, &candidate);
    assert_eq!(one_time_notes(&st), [(100_000, false)]);
    let db = st.wallet_mut().db_mut();
    let found = status(db, account);
    assert!(!found.complete);
    assert_eq!(found.lookups.len(), 1);
    assert!(
        prepare_from(
            db,
            account,
            clock_now(),
            after,
            Some(&empty_seen(clock_now()))
        )
        .unwrap()
        .is_ok()
    );
    assert!(
        db.record_one_time_lookup(account, key, publication, 1, &[], true, clock_now())
            .unwrap()
    );
    assert!(status(db, account).complete);
}

/// A late payment whose blocks the scan finished without journal coverage, as before the
/// journal existed, has those blocks scanned again, then is admitted.
#[test]
fn a_late_payment_over_uncovered_blocks_is_rescanned() {
    let (mut st, candidate, _) = late_fixture();
    exec(
        st.wallet().conn(),
        "DELETE FROM ironwood_journal_coverage",
        [],
    );
    send_witness(&mut st, key8(), &candidate);
    assert!(one_time_notes(&st).is_empty());
    let queued: Vec<(u32, u32)> = rows(
        st.wallet().conn(),
        "SELECT block_range_start, block_range_end FROM scan_queue WHERE priority != ?1",
        [crate::wallet::scanning::priority_code(
            &ScanPriority::Scanned,
        )],
    );
    assert_eq!(
        queued.first().map(|r| r.0),
        Some(u32::from(candidate.height))
    );
    let tip = st.latest_cached_block().unwrap().height();
    let blocks = u32::from(tip) - u32::from(candidate.height) + 1;
    st.scan_cached_blocks_with_one_time_ivks(candidate.height, blocks as usize);
    assert_eq!(one_time_notes(&st), [(100_000, false)]);
}

/// A lookup still due when a newer pass starts reads on to the newer publication's end,
/// keeping its cursor, so it finds a payment between the two. It refuses a revision ending
/// below that cutoff, which cannot show the history through it.
#[test]
fn a_new_pass_raises_a_due_lookups_cutoff() {
    let mut st = restored_wallet();
    let account = st.test_account().unwrap().id();
    let first = discover(&mut st, account, &[paid(key8())]);
    let candidate = pay_candidate(&mut st, key8());
    st.generate_and_scan_empty_blocks_with_one_time_ivks(1);
    let second = discover(&mut st, account, &[paid(key8())]);
    let db = st.wallet_mut().db_mut();
    let due = status(db, account).lookups;
    assert_eq!(due.len(), 1);
    assert_eq!(due[0].cutoff, second.height);
    assert!(!look_up(db, account, key8(), first, &[]));
    assert!(look_up(db, account, key8(), second, &[candidate]));
    assert_eq!(status(db, account).witnesses.len(), 1);
}

/// A key registered for a lookup after its account's pass, as a refund key recovered from
/// a funding memo is, is looked up through every block the scan stored without it, not
/// only through the pass's publication.
#[test]
fn a_key_registered_after_the_pass_is_looked_up_through_the_scanned_blocks() {
    let mut st = restored_wallet();
    let account = st.test_account().unwrap().id();
    let pass = discover(&mut st, account, &[]);
    let candidate = pay_candidate(&mut st, key8());
    st.generate_and_scan_empty_blocks_with_one_time_ivks(1);
    let scanned = publication_at(&st, tip(&st).height);
    let db = st.wallet_mut().db_mut();
    recover(db, account, key8(), candidate.height);
    let due = status(db, account).lookups;
    assert_eq!(due.len(), 1);
    assert_eq!(due[0].cutoff, scanned.height);
    assert!(!look_up(db, account, key8(), pass, &[]));
    assert!(look_up(db, account, key8(), scanned, &[candidate]));
    assert_eq!(status(db, account).witnesses.len(), 1);
}

/// Mines an empty block above the scanned tip without scanning it, learns the new chain
/// tip, and returns the publication ending at that block.
fn unscanned_publication(st: &mut State) -> PublicationEnd {
    let (height, _) = st.generate_empty_block();
    st.wallet_mut().update_chain_tip(height).unwrap();
    publication_at(st, height)
}

/// A lookup read from a revision the scan has not judged yet keeps discovery incomplete,
/// even with nothing found, until the scan finds that revision on the wallet's chain.
#[test]
fn a_lookup_from_an_unjudged_revision_keeps_discovery_incomplete() {
    let mut st = restored_wallet();
    let account = st.test_account().unwrap().id();
    discover(&mut st, account, &[paid(key8())]);
    let revision = unscanned_publication(&mut st);
    let db = st.wallet_mut().db_mut();
    assert!(look_up(db, account, key8(), revision, &[]));
    assert!(!status(db, account).complete);
    st.scan_cached_blocks_with_one_time_ivks(revision.height, 1);
    assert!(status(st.wallet().db(), account).complete);
}

/// A lookup read from a revision the scan then finds off the wallet's chain starts over
/// from page zero, dropping that read and the payment it supplied, and the payment comes
/// back from a revision that matches.
#[test]
fn a_lookup_from_a_rejected_revision_starts_over() {
    let mut st = restored_wallet();
    let account = st.test_account().unwrap().id();
    let candidate = pay_candidate(&mut st, key8());
    st.generate_and_scan_empty_blocks_with_one_time_ivks(1);
    discover(&mut st, account, &[paid(key8())]);
    let revision = unscanned_publication(&mut st);
    let wrong = PublicationEnd {
        hash: [9; 32],
        ..revision
    };
    let db = st.wallet_mut().db_mut();
    assert!(look_up(
        db,
        account,
        key8(),
        wrong,
        std::slice::from_ref(&candidate)
    ));
    st.scan_cached_blocks_with_one_time_ivks(revision.height, 1);
    let db = st.wallet_mut().db_mut();
    assert!(pending(db, account, key8()).is_empty());
    let due = status(db, account).lookups;
    assert_eq!(due.len(), 1);
    assert_eq!((due[0].applied, due[0].last), (0, None));
    assert!(look_up(
        db,
        account,
        key8(),
        revision,
        std::slice::from_ref(&candidate)
    ));
    send_witness(&mut st, key8(), &candidate);
    assert_eq!(one_time_notes(&st), [(100_000, false)]);
    assert!(status(st.wallet().db(), account).complete);
}

/// A late payment whose path does not lead to the wallet's root is the directory's error:
/// the payment is dropped and its lookup starts over, backed off.
#[test]
fn a_late_payment_with_a_wrong_path_is_looked_up_again() {
    let (mut st, candidate, _) = late_fixture();
    let account = st.test_account().unwrap().id();
    let (anchor, path) = (tip(&st), witness_at(&st, candidate.position));
    let mut siblings = siblings(&path);
    siblings[0] = siblings[1];
    let db = st.wallet_mut().db_mut();
    db.record_one_time_witness(
        account,
        key8(),
        candidate.position,
        anchor,
        siblings,
        clock_now(),
    )
    .unwrap();
    assert!(pending(db, account, key8()).is_empty());
    let (cutoff, applied, next_attempt): (Option<u32>, u32, i64) = row(
        &db.conn,
        "SELECT cutoff_height, applied, next_attempt_at FROM ironwood_one_time_lookups",
        [],
    );
    assert!(cutoff.is_some());
    assert_eq!(applied, 0);
    assert!(next_attempt > clock_now());
    assert!(one_time_notes(&st).is_empty());
}

/// A late payment waits while the scan's coverage ends one block short of its scanned tip,
/// since a spend of the note there may be missing from the journal. Scanning that block
/// again covers it, and the payment is admitted spent.
#[test]
fn a_late_payment_waits_for_coverage_of_the_scanned_tip() {
    let mut st = ironwood_wallet();
    let account = st.test_account().unwrap().id();
    let candidate = pay_candidate(&mut st, key8());
    let note = candidate
        .encrypted_note
        .decrypt(&parent(&st), key8())
        .unwrap();
    let spender = IronwoodFvk(parent(&st));
    let (height, _) = st.generate_next_block_spending(
        &spender,
        (*note.nullifier(), Zatoshis::const_from_u64(100_000)),
        unified(foreign_receiver()),
        Zatoshis::const_from_u64(20_000),
    );
    st.scan_cached_blocks_with_one_time_ivks(height, 1);
    let publication = discover(&mut st, account, &[paid(key8())]);
    let db = st.wallet_mut().db_mut();
    assert!(look_up(
        db,
        account,
        key8(),
        publication,
        std::slice::from_ref(&candidate)
    ));
    // The spending block was stored without coverage, so without its journal entry.
    let tip = u32::from(height);
    for sql in [
        "UPDATE ironwood_journal_coverage SET end_height = ?1 WHERE end_height > ?1",
        "DELETE FROM ironwood_spend_journal_nullifiers WHERE block_height = ?1",
        "DELETE FROM ironwood_spend_journal WHERE block_height = ?1",
    ] {
        exec(&db.conn, sql, [tip]);
    }
    send_witness(&mut st, key8(), &candidate);
    assert!(one_time_notes(&st).is_empty());
    st.scan_cached_blocks_with_one_time_ivks(height, 1);
    assert_eq!(one_time_notes(&st), [(100_000, true)]);
}

/// A late payment over a one-block hole in the scan's coverage has that block scanned
/// again, then is admitted.
#[test]
fn a_late_payment_over_a_one_block_coverage_hole_is_rescanned() {
    let (mut st, candidate, _) = late_fixture();
    let hole = u32::from(candidate.height) + 1;
    let conn = st.wallet().conn();
    exec(
        conn,
        "INSERT INTO ironwood_journal_coverage (account_id, start_height, end_height)
         SELECT account_id, ?1 + 1, end_height FROM ironwood_journal_coverage
         WHERE start_height <= ?1 AND end_height > ?1 + 1",
        [hole],
    );
    exec(
        conn,
        "UPDATE ironwood_journal_coverage SET end_height = ?1
         WHERE start_height <= ?1 AND end_height > ?1 + 1",
        [hole],
    );
    send_witness(&mut st, key8(), &candidate);
    assert!(one_time_notes(&st).is_empty());
    let queued: Vec<(u32, u32)> = rows(
        st.wallet().conn(),
        "SELECT block_range_start, block_range_end FROM scan_queue WHERE priority != ?1",
        [crate::wallet::scanning::priority_code(
            &ScanPriority::Scanned,
        )],
    );
    assert!(queued.contains(&(hole, hole + 1)), "{queued:?}");
    st.scan_cached_blocks_with_one_time_ivks(BlockHeight::from(hole), 1);
    assert_eq!(one_time_notes(&st), [(100_000, false)]);
}

/// A late payment the scan has since stored under another action is redundant: its path
/// dequeues it, and discovery completes.
#[test]
fn a_late_payment_the_scan_stored_elsewhere_is_dequeued() {
    let (mut st, candidate, _) = late_fixture();
    let account = st.test_account().unwrap().id();
    activate_key(st.wallet_mut().db_mut(), account, key8(), candidate.height);
    let tip = st.latest_cached_block().unwrap().height();
    let blocks = (tip - candidate.height) as usize + 1;
    st.scan_cached_blocks_with_one_time_ivks(candidate.height, blocks);
    assert_eq!(one_time_notes(&st), [(100_000, false)]);
    // The directory's claim names another action of the transaction.
    exec(
        st.wallet().conn(),
        "UPDATE ironwood_one_time_payment_recovery SET action_index = action_index + 1",
        [],
    );
    let claimed = PendingPayment {
        action_index: candidate.action_index + 1,
        ..candidate.clone()
    };
    send_witness(&mut st, key8(), &claimed);
    let db = st.wallet_mut().db_mut();
    assert!(pending(db, account, key8()).is_empty());
    assert!(status(db, account).complete);
    assert_eq!(one_time_notes(&st), [(100_000, false)]);
}

/// Issuance waits until the pass checked both purposes against a publication that matched
/// the wallet's chain: not while the scan has yet to judge it, nor before the refund keys
/// are checked.
#[test]
fn issuance_waits_for_both_purposes_against_a_judged_publication() {
    let mut st = restored_wallet();
    let account = st.test_account().unwrap().id();
    let unjudged = unscanned_publication(&mut st);
    let gap = |db: &mut Db, tip| {
        matches!(
            reserve_refund(db, account, tip),
            Ok(Err(ReservationPolicy::Gap))
        )
    };
    let db = st.wallet_mut().db_mut();
    assert!(run_pass(db, account, unjudged, &[]));
    assert!(gap(db, unjudged.height));
    st.scan_cached_blocks_with_one_time_ivks(unjudged.height, 1);
    st.generate_and_scan_empty_blocks_with_one_time_ivks(1);
    let newer = publication_at(&st, tip(&st).height);
    let done = EnumerationCursor {
        next: 100,
        reach: 100,
    };
    let db = st.wallet_mut().db_mut();
    let check = |db: &mut Db, purpose| {
        db.record_one_time_filter_checks(account, newer, purpose, &[], done, true, clock_now())
            .unwrap()
    };
    assert!(check(db, Purpose::Receive));
    assert!(gap(db, newer.height));
    assert!(check(db, Purpose::Refund));
    let key = reserve_refund(db, account, newer.height).unwrap();
    assert_eq!(key.unwrap().key_id(), KeyId::new(Purpose::Refund, 0));
}

/// A payment from before the account's birthday is never a scan hint, since the scan never
/// stores its block: it waits for its path, then marks its key paid before the birthday.
#[test]
fn a_payment_before_the_birthday_is_late_not_a_hint() {
    let mut st = ironwood_wallet();
    let seed = SecretVec::new(vec![3; 32]);
    let usk = zcash_keys::keys::UnifiedSpendingKey::from_seed(
        st.network(),
        seed.expose_secret(),
        zip32::AccountId::ZERO,
    )
    .unwrap();
    let fvk = key8().derive(&FullViewingKey::from(usk.orchard())).unwrap();
    let paid_to = IronwoodFvk(fvk.clone());
    let amount = Zatoshis::const_from_u64(100_000);
    let (height, _, _) = st.generate_next_block(&paid_to, AddressType::DefaultExternal, amount);
    let candidate = candidate_at(&st, height, &fvk);
    // Born after the payment, whose block the wallet never stores.
    let prior = st.latest_cached_block().unwrap().chain_state().clone();
    let birthday = zcash_client_backend::data_api::AccountBirthday::from_parts(prior, None);
    let db = st.wallet_mut().db_mut();
    let (other, created) = db.create_account("other", &seed, &birthday, None).unwrap();
    assert_eq!(created.orchard().to_bytes(), usk.orchard().to_bytes());
    let (born, _) = st.generate_empty_block();
    st.wallet_mut().update_chain_tip(born).unwrap();
    st.scan_cached_blocks_with_one_time_ivks(born, 1);
    let publication = publication_at(&st, born);
    let path = witness_at(&st, candidate.position);
    let db = st.wallet_mut().db_mut();
    assert!(run_pass(db, other, publication, &[paid(key8())]));
    assert!(look_up(
        db,
        other,
        key8(),
        publication,
        std::slice::from_ref(&candidate)
    ));
    assert_eq!(status(db, other).witnesses.len(), 1);
    let anchor = ChainPoint {
        height: born,
        hash: BlockHash(publication.hash),
    };
    db.record_one_time_witness(
        other,
        key8(),
        candidate.position,
        anchor,
        siblings(&path),
        clock_now(),
    )
    .unwrap();
    assert!(status(db, other).complete);
    let paid_before: bool = value(
        &db.conn,
        "SELECT k.paid_before_birthday FROM ironwood_one_time_keys k
         JOIN accounts a ON a.id = k.account_id WHERE a.uuid = ?1",
        [other.expose_uuid()],
    );
    assert!(paid_before);
}

/// A rewind below a note a lookup found reopens its key, which closed without scanning,
/// from the note's block, so the rescan finds the note again.
#[test]
fn a_rewind_below_a_looked_up_note_scans_its_key_again() {
    let (mut st, candidate, _) = late_fixture();
    send_witness(&mut st, key8(), &candidate);
    assert_eq!(one_time_notes(&st), [(100_000, false)]);
    st.truncate_to_height_retaining_cache(candidate.height - 1);
    let tip = st.latest_cached_block().unwrap().height();
    let blocks = (tip - candidate.height) as usize + 1;
    st.scan_cached_blocks_with_one_time_ivks(candidate.height, blocks);
    let mined: Vec<Option<u32>> = column(
        st.wallet().conn(),
        "SELECT t.mined_height FROM ironwood_received_notes n
         JOIN transactions t ON t.id_tx = n.transaction_id
         WHERE n.one_time_key_id IS NOT NULL",
        [],
    );
    assert_eq!(mined, [Some(u32::from(candidate.height))]);
}

/// A pass that lists a key as recent watches it from the publication's end, even though
/// issuance registered the key for a lookup from the provider's seen set before the pass.
/// Once the watch ends, a later pass that lists it as recent again, as every key is without
/// a current recent set, does not reopen it.
#[test]
fn a_pass_watches_a_recent_key_once_even_if_issuance_registered_it() {
    let mut st = scanned_wallet();
    let account = st.test_account().unwrap().id();
    let key = KeyId::new(Purpose::Receive, 0);
    let receiver = derive(&st, key)
        .address_at(0u32, Scope::External)
        .to_raw_address_bytes();
    let contains = |receivers: &[[u8; 43]]| receivers.iter().map(|r| *r == receiver).collect();
    let seen = ProviderSeen {
        since: 0,
        until: clock_now(),
        contains: &contains,
    };
    let from = tip(&st).height + 1;
    let db = st.wallet_mut().db_mut();
    let issued = prepare_from(db, account, clock_now(), from, Some(&seen)).unwrap();
    assert!(issued.is_ok_and(|k| k.key_id() != key));
    let watch = |st: &State| -> (Option<u32>, bool) {
        row(
            st.wallet().conn(),
            "SELECT active_from, closed_at IS NOT NULL FROM ironwood_one_time_keys
             WHERE purpose = 1 AND key_index = ?1",
            [key.index().to_be_bytes()],
        )
    };
    assert_eq!(watch(&st), (None, false));
    let recent = FilterCheck {
        key,
        seen: true,
        paid: false,
        recent: true,
    };
    let pass = discover(&mut st, account, &[recent]);
    let start = u32::from(pass.height) + 1;
    assert_eq!(watch(&st), (Some(start), false));
    exec(
        st.wallet().conn(),
        "UPDATE ironwood_one_time_keys SET closed_at = 1 WHERE purpose = 1 AND key_index = ?1",
        [key.index().to_be_bytes()],
    );
    st.generate_and_scan_empty_blocks_with_one_time_ivks(1);
    discover(&mut st, account, &[recent]);
    assert_eq!(watch(&st), (Some(start), true));
}

/// Pays `key` in a new scanned block, before the wallet scans with it, and returns the
/// output as a directory candidate.
fn pay_candidate(st: &mut State, key: KeyId) -> PendingPayment {
    let fvk = derive(st, key);
    let height = pay(st, &fvk, 100_000);
    candidate_at(st, height, &fvk)
}