zakura-client-sqlite 0.1.0-rc8

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
//! The block-derived oracle.
//!
//! A fixture chain holds real transactions with transparent bundles at local heights. The
//! oracle walks it block by block with only the account's keys and derives the exact receives,
//! spends, unspent outputs, and balances. A fixture source indexes the same chain by address,
//! as a server would. A driver delivers the source's results through the ledger the way a
//! coordinator might: split into several commits, reordered, with spends before their receives,
//! through pages, and replayed. The candidate diagnostics, the projection, the snapshot, and the
//! selectors must then agree with the oracle exactly.

use std::collections::{BTreeMap, BTreeSet};

use transparent::{
    address::Script,
    bundle::{Bundle, TxIn, TxOut},
};
use zcash_client_backend::data_api::{
    CoinbaseFilter, InputSource as _,
    wallet::{
        TargetHeight,
        input_selection::{LockFilter, LockedInputPolicy},
    },
};
use zcash_primitives::transaction::{Transaction, TransactionData, TxVersion};
use zcash_protocol::consensus::BranchId;

use transparent::keys::{IncomingViewingKey as _, NonHardenedChildIndex};

use super::*;

/// Blocks of real transactions, by height.
#[derive(Default)]
pub(super) struct Chain {
    blocks: BTreeMap<BlockHeight, Vec<Transaction>>,
    nonce: u32,
}

impl Chain {
    fn transaction(&mut self, vin: Vec<OutPoint>, vout: Vec<TxOut>) -> Transaction {
        self.nonce += 1;
        let bundle = Bundle {
            vin: vin
                .into_iter()
                .map(|prevout| TxIn::from_parts(prevout, Script::default(), u32::MAX))
                .collect(),
            vout,
            authorization: transparent::bundle::Authorized,
        };
        // The nonce in the lock time keeps otherwise identical transactions distinct.
        TransactionData::<zcash_primitives::transaction::Authorized>::from_parts(
            TxVersion::V5,
            BranchId::Nu5,
            self.nonce,
            BlockHeight::from_u32(0),
            Some(bundle),
            None,
            None,
            None,
        )
        .freeze()
        .unwrap()
    }

    fn outputs(vout: &[(TransparentAddress, u64)]) -> Vec<TxOut> {
        vout.iter()
            .map(|(address, value)| {
                TxOut::new(Zatoshis::const_from_u64(*value), address.script().into())
            })
            .collect()
    }

    /// Mines a transaction spending `vin` to `vout` at `height`, returning its txid.
    pub(super) fn pay(
        &mut self,
        height: BlockHeight,
        vin: Vec<OutPoint>,
        vout: &[(TransparentAddress, u64)],
    ) -> TxId {
        let tx = self.transaction(vin, Self::outputs(vout));
        let txid = tx.txid();
        self.blocks.entry(height).or_default().push(tx);
        txid
    }

    /// Mines a payment into `vout` from outside the wallet at `height`.
    pub(super) fn fund(&mut self, height: BlockHeight, vout: &[(TransparentAddress, u64)]) -> TxId {
        let foreign = OutPoint::new([0xf0; 32], self.nonce);
        self.pay(height, vec![foreign], vout)
    }

    /// Mines a coinbase transaction paying `vout` at `height`.
    pub(super) fn coinbase(
        &mut self,
        height: BlockHeight,
        vout: &[(TransparentAddress, u64)],
    ) -> TxId {
        let tx = self.transaction(vec![OutPoint::NULL], Self::outputs(vout));
        assert!(tx.transparent_bundle().unwrap().is_coinbase());
        let txid = tx.txid();
        // A coinbase transaction is the first in its block.
        self.blocks.entry(height).or_default().insert(0, tx);
        txid
    }

    /// Removes every block above `floor`, returning their transactions in chain order.
    pub(super) fn truncate(&mut self, floor: BlockHeight) -> Vec<(BlockHeight, Transaction)> {
        let above = self.blocks.split_off(&(floor + 1));
        above
            .into_iter()
            .flat_map(|(height, txs)| txs.into_iter().map(move |tx| (height, tx)))
            .collect()
    }

    /// Mines `tx` again at `height`.
    pub(super) fn remine(&mut self, height: BlockHeight, tx: Transaction) {
        let block = self.blocks.entry(height).or_default();
        if tx.transparent_bundle().unwrap().is_coinbase() {
            block.insert(0, tx);
        } else {
            block.push(tx);
        }
    }

    fn mined(&self, through: BlockHeight) -> impl Iterator<Item = (BlockHeight, &Transaction)> {
        self.blocks
            .range(..=through)
            .flat_map(|(height, txs)| txs.iter().map(move |tx| (*height, tx)))
    }
}

fn outpoint_key(outpoint: &OutPoint) -> ([u8; 32], u32) {
    (*outpoint.hash(), outpoint.n())
}

/// What the chain holds for an account, derived without the ledger.
#[derive(Debug)]
pub(super) struct Expected {
    pub(super) receives: Vec<ReceiveEvent>,
    pub(super) spends: Vec<SpendEvent>,
    pub(super) unspent: Vec<ReceiveEvent>,
}

impl Expected {
    pub(super) fn total(&self, coinbase: bool) -> Zatoshis {
        self.unspent
            .iter()
            .filter(|r| r.coinbase == coinbase)
            .map(|r| r.value)
            .sum::<Option<Zatoshis>>()
            .unwrap()
    }

    fn unspent_outpoints(&self, coinbase: Option<bool>) -> Vec<OutPoint> {
        self.unspent
            .iter()
            .filter(|r| coinbase.is_none_or(|c| r.coinbase == c))
            .map(|r| r.outpoint.clone())
            .collect()
    }
}

/// Walks `chain` through `through` and derives, for the `owned` addresses, every receive and
/// spend and the outputs left unspent.
pub(super) fn oracle(
    chain: &Chain,
    owned: &BTreeSet<TransparentAddress>,
    through: BlockHeight,
) -> Expected {
    let mut receives = vec![];
    let mut spends = vec![];
    let mut utxos: BTreeMap<_, ReceiveEvent> = BTreeMap::new();
    for (height, tx) in chain.mined(through) {
        let bundle = tx.transparent_bundle().unwrap();
        let coinbase = bundle.is_coinbase();
        if !coinbase {
            for (index, input) in bundle.vin.iter().enumerate() {
                if let Some(spent) = utxos.remove(&outpoint_key(input.prevout())) {
                    spends.push(SpendEvent {
                        metadata: None,
                        spending_txid: tx.txid(),
                        input_index: index as u32,
                        prevout: input.prevout().clone(),
                        prevout_address: spent.address,
                        mined_height: height,
                    });
                }
            }
        }
        for (n, output) in bundle.vout.iter().enumerate() {
            let Some(address) = output.recipient_address().filter(|a| owned.contains(a)) else {
                continue;
            };
            let received = ReceiveEvent {
                metadata: None,
                outpoint: OutPoint::new(*tx.txid().as_ref(), n as u32),
                address,
                value: output.value(),
                coinbase,
                mined_height: height,
            };
            utxos.insert(outpoint_key(&received.outpoint), received.clone());
            receives.push(received);
        }
    }
    receives.sort_by_key(|r| outpoint_key(&r.outpoint));
    spends.sort_by_key(|s| (*s.spending_txid.as_ref(), s.input_index));
    Expected {
        receives,
        spends,
        unspent: utxos.into_values().collect(),
    }
}

/// What a server indexing `chain` by address returns for `addresses` through `through`: the
/// outputs paying them, and the inputs spending those outputs.
pub(super) fn source(
    chain: &Chain,
    addresses: &BTreeSet<TransparentAddress>,
    through: BlockHeight,
) -> (Vec<ReceiveEvent>, Vec<SpendEvent>) {
    let mut paid = BTreeMap::new();
    let mut receives = vec![];
    let mut spends = vec![];
    for (height, tx) in chain.mined(through) {
        let bundle = tx.transparent_bundle().unwrap();
        for (n, output) in bundle.vout.iter().enumerate() {
            let outpoint = OutPoint::new(*tx.txid().as_ref(), n as u32);
            if let Some(address) = output.recipient_address() {
                paid.insert(outpoint_key(&outpoint), address);
                if addresses.contains(&address) {
                    receives.push(ReceiveEvent {
                        metadata: None,
                        outpoint,
                        address,
                        value: output.value(),
                        coinbase: bundle.is_coinbase(),
                        mined_height: height,
                    });
                }
            }
        }
        if bundle.is_coinbase() {
            continue;
        }
        for (index, input) in bundle.vin.iter().enumerate() {
            if let Some(address) = paid
                .get(&outpoint_key(input.prevout()))
                .filter(|a| addresses.contains(a))
            {
                spends.push(SpendEvent {
                    metadata: None,
                    spending_txid: tx.txid(),
                    input_index: index as u32,
                    prevout: input.prevout().clone(),
                    prevout_address: *address,
                    mined_height: height,
                });
            }
        }
    }
    // Delivered newest first, as a paginated server might.
    receives.reverse();
    spends.reverse();
    (receives, spends)
}

fn script_of(address: &TransparentAddress) -> Vec<u8> {
    Script::from(address.script()).0.0
}

/// Recovers `account` through its target from `chain` as the production adapter does, from
/// public reads alone, and returns the number of passes.
///
/// Each pass reads the watch set, resumes the pages it lists, and covers every watched address
/// from its required start through the target. Recovery ends at the first pass that finds no
/// open page and the same addresses as the pass before. Only that address set is carried
/// between passes; coverage is never read.
///
/// Delivery varies by address position: one commit, then a replay; spends first, then receives
/// with coverage; or one commit. In the third position, an address the window added since the
/// previous pass is instead delivered through a page opened with its receives and left for the
/// next pass to resume. Only such an address gets a page, because a revision cannot reopen
/// retrieval over a range it already covered.
pub(super) fn recover(
    st: &mut State,
    account: AccountUuid,
    chain: &Chain,
    revision: &RecoveryRevision,
) -> usize {
    let mut previous: Option<BTreeMap<TransparentAddress, BlockHeight>> = None;
    for pass in 1..=64 {
        let ws = watch(st, account);
        let watched: BTreeMap<_, _> = ws
            .addresses
            .iter()
            .map(|w| (w.address, w.required_from))
            .collect();
        if previous.as_ref() == Some(&watched) && ws.pending_pages.is_empty() {
            return pass;
        }
        let target = ws.target.unwrap();
        let base = || {
            let mut c = commit(&ws);
            c.revision = revision.clone();
            c
        };

        for page in &ws.pending_pages {
            assert_eq!(page.revision, *revision);
            let addresses: BTreeSet<_> = page.request.addresses.iter().copied().collect();
            let (receives, spends) = source(chain, &addresses, page.request.through);
            let mut c = base();
            c.anchor = page.target;
            c.receives = receives;
            c.spends = spends;
            c.coverage = addresses
                .iter()
                .map(|address| AddressRange {
                    address: *address,
                    from: page.request.from,
                    through: page.request.through,
                })
                .collect();
            c.completed_pages.push(page.request.page.clone());
            apply(st, c).unwrap();
        }

        for (index, (address, required_from)) in watched.iter().enumerate() {
            let range = AddressRange {
                address: *address,
                from: *required_from,
                through: target.height,
            };
            let (receives, spends) = source(chain, &BTreeSet::from([*address]), target.height);
            let fresh = previous
                .as_ref()
                .is_some_and(|previous| !previous.contains_key(address));
            match index % 3 {
                0 => {
                    let mut c = base();
                    c.receives = receives;
                    c.spends = spends;
                    c.coverage = vec![range];
                    apply(st, c.clone()).unwrap();
                    // A replay changes nothing.
                    let before = recovery(st, account);
                    apply(st, c).unwrap();
                    assert_eq!(recovery(st, account), before);
                }
                1 => {
                    let mut c = base();
                    c.spends = spends;
                    apply(st, c).unwrap();
                    let mut c = base();
                    c.receives = receives;
                    c.coverage = vec![range];
                    apply(st, c).unwrap();
                }
                _ if fresh => {
                    let mut c = base();
                    c.receives = receives;
                    c.opened_pages.push(PageRequest {
                        page: format!("pass {pass} address {index}").into_bytes(),
                        addresses: vec![*address],
                        from: range.from,
                        through: range.through,
                    });
                    apply(st, c).unwrap();
                }
                _ => {
                    let mut c = base();
                    c.receives = receives;
                    c.spends = spends;
                    c.coverage = vec![range];
                    apply(st, c).unwrap();
                }
            }
        }
        previous = Some(watched);
    }
    panic!("fixture recovery did not converge within 64 passes")
}

/// Every derived external and internal address of the test account within reach of the
/// fixture chains below.
pub(super) fn owned_addresses(st: &State) -> BTreeSet<TransparentAddress> {
    let key = st
        .test_account()
        .unwrap()
        .usk()
        .transparent()
        .to_account_pubkey();
    let external = key.derive_external_ivk().unwrap();
    let internal = key.derive_internal_ivk().unwrap();
    (0..48)
        .map(|i| {
            external
                .derive_address(NonHardenedChildIndex::from_index(i).unwrap())
                .unwrap()
        })
        .chain((0..24).map(|i| {
            internal
                .derive_address(NonHardenedChildIndex::from_index(i).unwrap())
                .unwrap()
        }))
        .collect()
}

pub(super) fn external_at(st: &State, index: u32) -> TransparentAddress {
    st.test_account()
        .unwrap()
        .usk()
        .transparent()
        .to_account_pubkey()
        .derive_external_ivk()
        .unwrap()
        .derive_address(NonHardenedChildIndex::from_index(index).unwrap())
        .unwrap()
}

pub(super) fn internal_at(st: &State, index: u32) -> TransparentAddress {
    st.test_account()
        .unwrap()
        .usk()
        .transparent()
        .to_account_pubkey()
        .derive_internal_ivk()
        .unwrap()
        .derive_address(NonHardenedChildIndex::from_index(index).unwrap())
        .unwrap()
}

fn foreign(tag: u8) -> TransparentAddress {
    TransparentAddress::PublicKeyHash([tag; 20])
}

/// Asserts that `account`'s candidate diagnostics equal the oracle's view of `chain` at the
/// account's target, returning that view.
pub(super) fn assert_diagnostics_agree(
    st: &State,
    account: AccountUuid,
    chain: &Chain,
    owned: &BTreeSet<TransparentAddress>,
) -> Expected {
    let r = recovery(st, account);
    let target = r.target.unwrap().height;
    let expected = oracle(chain, owned, target);
    assert_eq!(r.blockers, vec![]);
    assert_eq!(r.covered_through, Some(target));
    assert_eq!(r.unresolved_spends, 0);
    let mut receives = r.receives.clone();
    receives.sort_by_key(|r| outpoint_key(&r.outpoint));
    assert_eq!(receives, expected.receives);
    let mut spends = r.spends.clone();
    spends.sort_by_key(|s| (*s.spending_txid.as_ref(), s.input_index));
    assert_eq!(spends, expected.spends);
    let mut unspent = r.unspent.clone();
    unspent.sort_by_key(outpoint_key);
    assert_eq!(unspent, expected.unspent_outpoints(None));
    assert_eq!(
        r.recovered_unverified,
        Some((expected.total(false) + expected.total(true)).unwrap())
    );
    expected
}

/// The ledger-origin outputs and spends of `account` whose transactions are mined, as
/// `(outpoint, script, value, height)` and `(spending txid, prevout)`.
#[allow(clippy::type_complexity)]
fn projection(
    st: &State,
    account: AccountUuid,
) -> (
    BTreeSet<(([u8; 32], u32), Vec<u8>, u64, u32)>,
    BTreeSet<([u8; 32], ([u8; 32], u32))>,
) {
    let outputs = conn(st)
        .prepare(
            "SELECT t.txid, o.output_index, o.script, o.value_zat, t.mined_height
             FROM transparent_received_outputs o
             JOIN transactions t ON t.id_tx = o.transaction_id
             JOIN accounts a ON a.id = o.account_id
             WHERE a.uuid = ?1 AND t.mined_height IS NOT NULL
             AND EXISTS (SELECT 1 FROM tpir_output_origins oo
                         WHERE oo.output_id = o.id AND oo.origin = 2)",
        )
        .unwrap()
        .query_map([account.0], |row| {
            Ok((
                (row.get::<_, [u8; 32]>(0)?, row.get(1)?),
                row.get(2)?,
                row.get::<_, i64>(3)? as u64,
                row.get(4)?,
            ))
        })
        .unwrap()
        .collect::<Result<_, _>>()
        .unwrap();
    let spends = conn(st)
        .prepare(
            "SELECT st.txid, pt.txid, o.output_index
             FROM transparent_received_output_spends s
             JOIN transparent_received_outputs o ON o.id = s.transparent_received_output_id
             JOIN transactions pt ON pt.id_tx = o.transaction_id
             JOIN transactions st ON st.id_tx = s.transaction_id
             JOIN accounts a ON a.id = o.account_id
             WHERE a.uuid = ?1 AND st.mined_height IS NOT NULL
             AND EXISTS (SELECT 1 FROM tpir_spend_origins so
                         WHERE so.spending_transaction_id = s.transaction_id
                         AND so.prevout_txid = pt.txid
                         AND so.prevout_output_index = o.output_index
                         AND so.origin = 2)",
        )
        .unwrap()
        .query_map([account.0], |row| {
            Ok((row.get(0)?, (row.get(1)?, row.get(2)?)))
        })
        .unwrap()
        .collect::<Result<_, _>>()
        .unwrap();
    (outputs, spends)
}

/// Asserts that `account`'s projection, snapshot, and selectors equal `expected`.
pub(super) fn assert_authority_agrees(st: &State, account: AccountUuid, expected: &Expected) {
    let (outputs, spends) = projection(st, account);
    assert_eq!(
        outputs,
        expected
            .receives
            .iter()
            .map(|r| (
                outpoint_key(&r.outpoint),
                script_of(&r.address),
                u64::from(r.value),
                u32::from(r.mined_height)
            ))
            .collect()
    );
    assert_eq!(
        spends,
        expected
            .spends
            .iter()
            .map(|s| (*s.spending_txid.as_ref(), outpoint_key(&s.prevout)))
            .collect()
    );

    let s = snapshot(st, account);
    assert_eq!(s.authority, TransparentAuthority::Private);
    assert_eq!(s.blockers, vec![]);
    let authorized = s.authorized.unwrap();
    assert_eq!(authorized.regular.total(), expected.total(false));
    assert_eq!(authorized.regular.spendable_value(), expected.total(false));
    assert_eq!(authorized.coinbase.total(), expected.total(true));
    // The fixture chain is far shorter than coinbase maturity.
    assert_eq!(authorized.coinbase.spendable_value(), Zatoshis::ZERO);

    let target = TargetHeight::from(st.wallet().chain_height().unwrap().unwrap() + 1);
    let addresses: Vec<_> = watch(st, account)
        .addresses
        .iter()
        .map(|w| w.address)
        .collect();
    let mut selected: Vec<_> = st
        .wallet()
        .db()
        .get_spendable_transparent_outputs_for_addresses(
            &addresses,
            target,
            ConfirmationsPolicy::MIN,
            CoinbaseFilter::AllTransparentOutputs,
            LockFilter::Policy(&LockedInputPolicy::Exclude),
        )
        .unwrap()
        .iter()
        .map(|o| o.outpoint().clone())
        .collect();
    selected.sort_by_key(outpoint_key);
    assert_eq!(selected, expected.unspent_outpoints(Some(false)));
}

/// A wallet scanned twenty blocks past the birthday, under `PrivateRequired`, with a fixture
/// chain that exercises window growth in both scopes, a coinbase output, spends in the block of
/// their receive, a multi-input spend mixing foreign and owned inputs, and outputs to foreign
/// scripts. One of its outputs was also found by public discovery.
pub(super) fn recovery_oracle_wallet() -> (
    State,
    AccountUuid,
    Chain,
    BTreeSet<TransparentAddress>,
    ReceiveEvent,
) {
    let (mut st, accounts) = recovery_wallet_with(0);
    let account = accounts[0];
    scan_new_blocks(&mut st, 10);
    let b = st.test_account().unwrap().birthday().height();
    let owned = owned_addresses(&st);
    let (e, i) = (|n| external_at(&st, n), |n| internal_at(&st, n));

    let mut chain = Chain::default();
    let r1 = chain.fund(b + 1, &[(e(0), 50_000), (foreign(1), 9_000)]);
    let r2 = chain.fund(b + 2, &[(e(0), 20_000)]);
    chain.coinbase(b + 3, &[(e(1), 625_000)]);
    // Activity near each window's end extends it, twice for the external scope.
    let r3 = chain.fund(b + 4, &[(e(9), 31_000), (i(3), 7_000)]);
    let r4 = chain.fund(b + 5, &[(e(17), 12_000), (e(17), 13_000)]);
    // An owned output spent in the block that received it.
    chain.pay(
        b + 5,
        vec![OutPoint::new(*r4.as_ref(), 0)],
        &[(foreign(2), 11_000)],
    );
    // A spend mixing owned and foreign inputs, with change to the internal window.
    chain.pay(
        b + 7,
        vec![
            OutPoint::new([0xee; 32], 3),
            OutPoint::new(*r1.as_ref(), 0),
            OutPoint::new(*r3.as_ref(), 1),
        ],
        &[(foreign(3), 40_000), (i(7), 16_000)],
    );
    chain.fund(b + 8, &[(e(25), 8_000)]);
    // Foreign activity only.
    chain.fund(b + 9, &[(foreign(4), 1_000)]);
    chain.pay(
        b + 10,
        vec![OutPoint::new(*r3.as_ref(), 0)],
        &[(e(26), 30_000)],
    );

    // Public discovery already saw the second payment.
    let legacy = ReceiveEvent {
        metadata: None,
        outpoint: OutPoint::new(*r2.as_ref(), 0),
        address: e(0),
        value: Zatoshis::const_from_u64(20_000),
        coinbase: false,
        mined_height: b + 2,
    };
    let output = zcash_client_backend::wallet::WalletTransparentOutput::from_parts(
        legacy.outpoint.clone(),
        TxOut::new(legacy.value, legacy.address.script().into()),
        Some(legacy.mined_height),
        Some(account),
        Some(TransparentKeyScope::EXTERNAL),
        None,
    )
    .unwrap();
    publicly(&mut st, |st| {
        st.wallet_mut()
            .db_mut()
            .put_received_transparent_utxo(&output)
            .unwrap()
    });
    (st, account, chain, owned, legacy)
}

/// A [`recovery_oracle_wallet`] recovered, qualified, and activated.
pub(super) fn promoted_oracle_wallet() -> (
    State,
    AccountUuid,
    Chain,
    BTreeSet<TransparentAddress>,
    RecoveryRevision,
) {
    let (mut st, account, chain, owned, _) = recovery_oracle_wallet();
    let fixture = revision(1, true);
    recover(&mut st, account, &chain, &fixture);
    qualify(&mut st, &fixture);
    set_policy(&mut st, PrivateRequired);
    activate_recovered_ledger(&mut st, account).unwrap();
    (st, account, chain, owned, fixture)
}

#[test]
fn candidate_recovery_activation_and_active_commits_match_the_oracle() {
    let (mut st, account, mut chain, owned, legacy) = recovery_oracle_wallet();
    let b = st.test_account().unwrap().birthday().height();
    let fixture = revision(1, true);

    // Candidate recovery reaches the oracle's exact view and leaves the wallet untouched.
    let before = production_dump(conn(&st));
    // Window growth takes several passes.
    assert!(recover(&mut st, account, &chain, &fixture) >= 3);
    let expected = assert_diagnostics_agree(&st, account, &chain, &owned);
    assert_eq!(production_dump(conn(&st)), before);
    // The chain reached external index 26 and internal index 7, far past the initial windows.
    assert!(
        expected
            .receives
            .iter()
            .any(|r| r.address == external_at(&st, 26))
    );
    assert!(
        expected
            .receives
            .iter()
            .any(|r| r.address == internal_at(&st, 7))
    );
    assert!(expected.receives.iter().any(|r| r.coinbase));

    // Activation projects exactly the oracle's view. The legacy output agrees with it, but the
    // oracle, not that agreement, is what the balance is checked against.
    qualify(&mut st, &fixture);
    set_policy(&mut st, PrivateRequired);
    activate_recovered_ledger(&mut st, account).unwrap();
    assert_authority_agrees(&st, account, &expected);
    assert_eq!(
        super::super::super::super::output_origins(conn(&st), &legacy.outpoint),
        vec![0, 2]
    );
    assert!(expected.total(false) > legacy.value);

    // The chain advances: new receives, a spend of a activated output, and more growth.
    scan_new_blocks(&mut st, 3);
    let tip = st.wallet().chain_height().unwrap().unwrap();
    assert!(tip > b + 20);
    let e30 = external_at(&st, 30);
    let r5 = chain.fund(tip - 2, &[(e30, 70_000)]);
    let change = internal_at(&st, 12);
    chain.pay(
        tip,
        vec![
            OutPoint::new(*r5.as_ref(), 0),
            expected
                .unspent
                .iter()
                .find(|r| r.address == external_at(&st, 26))
                .unwrap()
                .outpoint
                .clone(),
        ],
        &[(foreign(5), 60_000), (change, 39_000)],
    );
    recover(&mut st, account, &chain, &fixture);
    let expected = assert_diagnostics_agree(&st, account, &chain, &owned);
    assert_authority_agrees(&st, account, &expected);
    assert!(expected.receives.iter().any(|r| r.address == change));
}

#[test]
fn rewind_and_remining_match_the_oracle() {
    let (mut st, account, mut chain, owned, fixture) = promoted_oracle_wallet();
    let b = st.test_account().unwrap().birthday().height();

    // Blocks above the floor are replaced. Their transactions are mined again on the new chain:
    // the first block's at the same height, the rest three blocks later, except one payment
    // that is never mined again.
    let floor = b + 6;
    st.truncate_to_height(floor);
    let mut orphaned = None;
    for (height, tx) in chain.truncate(floor) {
        let pays_e25 = tx
            .transparent_bundle()
            .unwrap()
            .vout
            .iter()
            .any(|o| o.recipient_address() == Some(external_at(&st, 25)));
        if pays_e25 {
            orphaned = Some(tx.txid());
        } else if height == floor + 1 {
            chain.remine(height, tx);
        } else {
            chain.remine(height + 3, tx);
        }
    }
    let orphaned = orphaned.unwrap();
    scan_new_blocks(&mut st, 8);

    recover(&mut st, account, &chain, &fixture);
    let expected = assert_diagnostics_agree(&st, account, &chain, &owned);
    assert_authority_agrees(&st, account, &expected);
    // The orphaned payment keeps its projection row, unmined, and authorizes nothing.
    assert!(
        !expected
            .receives
            .iter()
            .any(|r| r.outpoint.txid() == &orphaned)
    );
    assert_eq!(st.wallet().get_tx_height(orphaned).unwrap(), None);
    assert_eq!(
        super::super::super::super::output_origins(
            conn(&st),
            &OutPoint::new(*orphaned.as_ref(), 0)
        ),
        vec![2]
    );
}