zakura-client-sqlite 0.1.0-rc9.4

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
use super::*;
#[cfg(feature = "orchard")]
use {
    zcash_client_backend::data_api::transparent_ledger::{FeeState, HistoryClassification},
    zcash_protocol::consensus::BlockHeight,
};

mod activity_outgoing;

/// Isolate memo identity from accounting and effects. The integration tests below use the
/// migrated wallet schema and the private enhancement writer instead.
fn memo_fixture() -> rusqlite::Connection {
    let conn = rusqlite::Connection::open_in_memory().unwrap();
    conn.execute_batch(
        "CREATE TABLE v_received_outputs (
            account_id INTEGER, transaction_id INTEGER, pool INTEGER,
            output_index INTEGER, memo BLOB, id_within_pool_table INTEGER);
         CREATE TABLE ironwood_one_time_disputed_receipts (note_id INTEGER PRIMARY KEY);
         CREATE TABLE sent_notes (
            from_account_id INTEGER, transaction_id INTEGER, output_pool INTEGER,
            output_index INTEGER, memo BLOB);
         INSERT INTO sent_notes VALUES (1, 2, 4, 3, NULL);",
    )
    .unwrap();
    conn
}

#[test]
fn recovered_memo_requires_the_same_account_transaction_pool_and_index() {
    for received_identity in [(9, 2, 4, 3), (1, 9, 4, 3), (1, 2, 3, 3), (1, 2, 4, 9)] {
        let conn = memo_fixture();
        conn.execute(
            "INSERT INTO v_received_outputs (account_id, transaction_id, pool, output_index, memo) VALUES (?1, ?2, ?3, ?4, X'F6')",
            rusqlite::params![
                received_identity.0,
                received_identity.1,
                received_identity.2,
                received_identity.3
            ],
        )
        .unwrap();
        assert!(
            has_unretrieved_memo(&conn, 1, 2).unwrap(),
            "{received_identity:?}"
        );
    }
}

#[test]
fn matching_recovered_memos_work_in_every_shielded_pool_but_do_not_hide_other_missing_memos() {
    for pool in [2, 3, 4] {
        let conn = memo_fixture();
        conn.execute("UPDATE sent_notes SET output_pool = ?", [pool])
            .unwrap();
        conn.execute(
            "INSERT INTO v_received_outputs (account_id, transaction_id, pool, output_index, memo) VALUES (1, 2, ?1, 3, X'F6')",
            [pool],
        )
        .unwrap();
        assert!(!has_unretrieved_memo(&conn, 1, 2).unwrap());
        conn.execute(
            "INSERT INTO v_received_outputs (account_id, transaction_id, pool, output_index, memo) VALUES (1, 2, ?1, 4, NULL)",
            [pool],
        )
        .unwrap();
        assert!(has_unretrieved_memo(&conn, 1, 2).unwrap());
    }
}

#[cfg(feature = "orchard")]
mod ironwood {
    use super::*;
    use crate::testing::{
        BlockCache,
        db::{TestDb, TestDbFactory},
    };
    use zcash_client_backend::data_api::{
        Account as _,
        enhance_pir::{
            EnhancePirRead, EnhancePirStoreResult, EnhancePirWork, EnhancePirWrite, EnhanceRecord,
            EnhanceRecordParts, EnhanceTransactionMetadata, EnhancementMode,
            TransactionEnhancementWork,
        },
        testing::{
            AddressType, FakeCompactOutput, IronwoodFvk, TestBuilder, TestState,
            orchard::OrchardPoolTester, pool::ShieldedPoolTester,
        },
        transparent_ledger::TransparentLedgerRead,
    };
    use zcash_primitives::block::BlockHash;
    use zcash_protocol::{local_consensus::LocalNetwork, memo::MemoBytes};

    type State = TestState<BlockCache, TestDb, LocalNetwork>;

    #[test]
    fn batched_history_preserves_requested_order_duplicates_and_missing_transactions() {
        let mut st = TestBuilder::new()
            .with_data_store_factory(TestDbFactory::default())
            .with_block_cache(BlockCache::new())
            .with_account_from_sapling_activation(BlockHash([0; 32]))
            .build();
        let account = st.test_account().unwrap().id();
        let fvk = OrchardPoolTester::test_account_fvk(&st);
        let mut txids = vec![];
        for value in [20_000, 30_000] {
            let (height, _, _) = st.generate_next_block(
                &fvk,
                AddressType::DefaultExternal,
                Zatoshis::const_from_u64(value),
            );
            st.scan_cached_blocks(height, 1);
            let bytes = st
                .wallet()
                .conn()
                .query_row(
                    "SELECT txid FROM transactions WHERE mined_height = ?",
                    [u32::from(height)],
                    |row| row.get::<_, [u8; 32]>(0),
                )
                .unwrap();
            txids.push(TxId::from_bytes(bytes));
        }
        let db = st.wallet().db();
        let first = db
            .transaction_history_details(account, &txids[..1])
            .unwrap()
            .remove(0);
        let second = db
            .transaction_history_details(account, &txids[1..])
            .unwrap()
            .remove(0);
        assert_eq!(first.account_movement.received, 20_000);
        assert_eq!(second.account_movement.received, 30_000);
        let entries = db
            .transaction_history_details(
                account,
                &[txids[1], TxId::from_bytes([0xff; 32]), txids[0], txids[1]],
            )
            .unwrap();
        assert_eq!(entries, vec![second.clone(), first, second]);
        assert!(
            db.transaction_history_details(account, &[])
                .unwrap()
                .is_empty()
        );
    }

    #[test]
    fn batched_history_keeps_spends_and_receipts_scoped_to_each_account() {
        use secrecy::SecretVec;
        use zcash_client_backend::data_api::WalletWrite;

        let mut st = TestBuilder::new()
            .with_data_store_factory(TestDbFactory::default())
            .with_block_cache(BlockCache::new())
            .with_account_from_sapling_activation(BlockHash([0; 32]))
            .build();
        let first = st.test_account().unwrap().id();
        let birthday = st.test_account().unwrap().birthday().clone();
        let first_fvk = OrchardPoolTester::test_account_fvk(&st);
        let (second, usk) = st
            .wallet_mut()
            .create_account("second", &SecretVec::new(vec![7; 32]), &birthday, None)
            .unwrap();
        let second_fvk = OrchardPoolTester::sk_to_fvk(usk.orchard());
        let (start, _, nf) = st.generate_next_block(
            &first_fvk,
            AddressType::DefaultExternal,
            Zatoshis::const_from_u64(20_000),
        );
        st.generate_next_block(
            &second_fvk,
            AddressType::DefaultExternal,
            Zatoshis::const_from_u64(30_000),
        );
        st.generate_next_block_spending(
            &first_fvk,
            (nf, Zatoshis::const_from_u64(20_000)),
            OrchardPoolTester::fvk_default_address(&second_fvk),
            Zatoshis::const_from_u64(5_000),
        );
        st.scan_cached_blocks(start, 3);
        let txids = st
            .wallet()
            .conn()
            .prepare("SELECT txid FROM transactions ORDER BY mined_height")
            .unwrap()
            .query_map([], |r| Ok(TxId::from_bytes(r.get(0)?)))
            .unwrap()
            .collect::<Result<Vec<_>, _>>()
            .unwrap();
        assert_eq!(txids.len(), 3);
        let db = st.wallet().db();
        let a = db.transaction_history_details(first, &txids).unwrap();
        let b = db.transaction_history_details(second, &txids).unwrap();
        assert_eq!(
            a.iter().map(|e| e.txid).collect::<Vec<_>>(),
            [txids[0], txids[2]]
        );
        assert_eq!(
            b.iter().map(|e| e.txid).collect::<Vec<_>>(),
            [txids[1], txids[2]]
        );
        assert_eq!(
            (a[1].account_movement.received, a[1].account_movement.spent),
            (15_000, 20_000)
        );
        assert_eq!(
            (b[1].account_movement.received, b[1].account_movement.spent),
            (5_000, 0)
        );
        for (account, expected) in [(first, a), (second, b)] {
            let singles = txids
                .iter()
                .flat_map(|txid| db.transaction_history_details(account, &[*txid]).unwrap())
                .collect::<Vec<_>>();
            assert_eq!(expected, singles);
        }
    }

    /// Set HISTORY_BENCH_TRANSACTIONS=5000 and RUST_TEST_NOCAPTURE=1 for the
    /// 20,000-output benchmark. The default keeps the same comparison cheap in CI.
    #[test]
    fn history_batch_benchmark() {
        use std::time::Instant;

        let count = std::env::var("HISTORY_BENCH_TRANSACTIONS")
            .map(|v| v.parse::<usize>().expect("positive transaction count"))
            .unwrap_or(16);
        assert!(count > 0);
        let mut st = TestBuilder::new()
            .with_data_store_factory(TestDbFactory::default())
            .with_block_cache(BlockCache::new())
            .with_account_from_sapling_activation(BlockHash([0; 32]))
            .build();
        let account = st.test_account().unwrap().id();
        let fvk = OrchardPoolTester::test_account_fvk(&st);
        let outputs = (0..4)
            .map(|_| {
                FakeCompactOutput::new(
                    &fvk,
                    AddressType::DefaultExternal,
                    Zatoshis::const_from_u64(20_000),
                )
            })
            .collect::<Vec<_>>();
        let mut start = None;
        for _ in 0..count {
            let (height, _, _) = st.generate_next_block_multi(&outputs);
            start.get_or_insert(height);
        }
        st.scan_cached_blocks(start.unwrap(), count);
        let txids = st
            .wallet()
            .conn()
            .prepare("SELECT txid FROM transactions ORDER BY mined_height")
            .unwrap()
            .query_map([], |r| Ok(TxId::from_bytes(r.get(0)?)))
            .unwrap()
            .collect::<Result<Vec<_>, _>>()
            .unwrap();
        assert_eq!(txids.len(), count);
        let db = st.wallet().db();
        let started = Instant::now();
        let all = db.transaction_history_details(account, &txids).unwrap();
        let full = started.elapsed();
        assert_eq!(all.len(), count);
        assert!(
            all.iter()
                .all(|entry| entry.account_movement.received == 80_000)
        );
        let started = Instant::now();
        let chunks = txids
            .chunks(256)
            .flat_map(|chunk| db.transaction_history_details(account, chunk).unwrap())
            .collect::<Vec<_>>();
        let chunked = started.elapsed();
        assert_eq!(all, chunks);
        // Single-entry reads exercise the receipt caller as well as providing an
        // independent batching control. Limit this comparison for large runs.
        for (txid, expected) in txids.iter().zip(&all).take(16) {
            assert_eq!(
                db.transaction_history_details(account, &[*txid]).unwrap(),
                std::slice::from_ref(expected)
            );
        }
        eprintln!(
            "history benchmark: SQLite {}, transactions={count}, outputs={}, full={full:?}, batches256={chunked:?}",
            rusqlite::version(),
            count * 4
        );
    }

    struct Fixture {
        st: State,
        account: AccountUuid,
        txid: TxId,
        tx: i64,
    }

    impl Fixture {
        fn new(owned_value: u64) -> Self {
            let activation = BlockHeight::from_u32(100_000);
            let network = LocalNetwork {
                nu6: Some(activation),
                nu6_1: Some(activation),
                nu6_2: Some(activation),
                nu6_3: Some(activation),
                ..TestBuilder::<(), ()>::DEFAULT_NETWORK
            };
            let mut st = TestBuilder::new()
                .with_network(network)
                .with_data_store_factory(TestDbFactory::default())
                .with_block_cache(BlockCache::new())
                .with_account_from_sapling_activation(BlockHash([0; 32]))
                .build();
            st.wallet_mut()
                .db_mut()
                .set_enhancement_mode(EnhancementMode::PrivateIronwood);
            let account = st.test_account().unwrap().id();
            let fvk = IronwoodFvk(OrchardPoolTester::test_account_fvk(&st));
            let (height, _, _) = st.generate_next_block(
                &fvk,
                AddressType::DefaultExternal,
                Zatoshis::const_from_u64(30_000),
            );
            st.scan_cached_blocks(height, 1);
            let funding_note: i64 = st
                .wallet()
                .conn()
                .query_row("SELECT id FROM ironwood_received_notes", [], |r| r.get(0))
                .unwrap();
            let (height, _, _) = st.generate_next_block(
                &fvk,
                AddressType::DefaultExternal,
                Zatoshis::from_u64(owned_value).unwrap(),
            );
            st.scan_cached_blocks(height, 1);
            let (tx, txid): (i64, [u8; 32]) = st
                .wallet()
                .conn()
                .query_row(
                    "SELECT id_tx, txid FROM transactions WHERE mined_height = ?",
                    [u32::from(height)],
                    |r| Ok((r.get(0)?, r.get(1)?)),
                )
                .unwrap();
            let conn = st.wallet().conn();
            // Synthetic financial evidence: 30,000 spent = owned receipt + external payment
            // + 5,000 fee. Compact scanning supplies real note identities and settled effects.
            conn.execute(
                "INSERT INTO ironwood_received_note_spends VALUES (?1, ?2)",
                [funding_note, tx],
            )
            .unwrap();
            conn.execute("UPDATE transactions SET fee = 5000 WHERE id_tx = ?", [tx])
                .unwrap();
            conn.execute(
                "INSERT INTO sent_notes (transaction_id, output_pool, output_index,
                    from_account_id, to_account_id, value, memo)
                 SELECT transaction_id, 4, action_index, account_id, account_id, value, NULL
                 FROM ironwood_received_notes WHERE transaction_id = ?",
                [tx],
            )
            .unwrap();
            conn.execute(
                "INSERT INTO sent_notes (transaction_id, output_pool, output_index,
                    from_account_id, to_address, value, memo)
                 SELECT transaction_id, 4, action_index + 1, account_id, 'synthetic external', ?2, X'F6'
                 FROM ironwood_received_notes WHERE transaction_id = ?1",
                rusqlite::params![tx, 25_000 - owned_value]).unwrap();
            Self {
                st,
                account,
                txid: TxId::from_bytes(txid),
                tx,
            }
        }

        fn history(&self) -> TransactionHistoryDetails {
            let mut entries = self
                .st
                .wallet()
                .db()
                .transaction_history_details(self.account, &[self.txid])
                .unwrap();
            assert_eq!(entries.len(), 1);
            entries.remove(0)
        }

        fn recover(&mut self, memo: MemoBytes) {
            let request = self
                .st
                .wallet()
                .db()
                .transaction_enhancement_work()
                .unwrap()
                .into_iter()
                .find_map(|work| match work {
                    TransactionEnhancementWork::Private(EnhancePirWork::Query(request))
                        if request.request_id().txid() == self.txid =>
                    {
                        Some(request)
                    }
                    _ => None,
                })
                .unwrap();
            let pending = crate::wallet::enhance_pir::pending(
                self.st.wallet().conn(),
                self.st.network(),
                request.position(),
            )
            .unwrap()
            .unwrap();
            let encryptor = orchard::note_encryption::IronwoodNoteEncryption::new(
                None,
                pending.note,
                *memo.as_array(),
            );
            let ciphertext = encryptor.encrypt_note_plaintext();
            let record = EnhanceRecord::from_parts(EnhanceRecordParts {
                enc_ciphertext_suffix: ciphertext[52..].try_into().unwrap(),
                cv_net: [0; 32],
                out_ciphertext: [0; 80],
                has_transparent_inputs: false,
                has_transparent_outputs: false,
                metadata: EnhanceTransactionMetadata::new(0, Some(5000)).unwrap(),
            });
            assert_eq!(
                self.st
                    .wallet_mut()
                    .db_mut()
                    .apply_ironwood_enhance_record(request, &record)
                    .unwrap(),
                EnhancePirStoreResult::Stored
            );
            let (received_known, sent_unknown): (bool, bool) = self
                .st
                .wallet()
                .conn()
                .query_row(
                    "SELECT r.memo IS NOT NULL, s.memo IS NULL FROM ironwood_received_notes r
                 JOIN sent_notes s ON s.transaction_id = r.transaction_id
                    AND s.output_pool = 4 AND s.output_index = r.action_index
                 WHERE r.transaction_id = ?",
                    [self.tx],
                    |r| Ok((r.get(0)?, r.get(1)?)),
                )
                .unwrap();
            assert_eq!((received_known, sent_unknown), (true, true));
            assert!(
                crate::wallet::enhance_pir::pending(
                    self.st.wallet().conn(),
                    self.st.network(),
                    request.position()
                )
                .unwrap()
                .is_none()
            );
            assert_eq!(
                self.st
                    .wallet()
                    .conn()
                    .query_row(
                        "SELECT route FROM ironwood_enhance_routing WHERE transaction_id = ?",
                        [self.tx],
                        |r| r.get::<_, i64>(0)
                    )
                    .unwrap(),
                0
            );
        }
    }

    #[test]
    fn private_incoming_recovery_completes_owned_sent_memos() {
        for value in [0, 10_000] {
            for memo in [
                MemoBytes::empty(),
                MemoBytes::from_bytes(b"private memo").unwrap(),
            ] {
                let mut fixture = Fixture::new(value);
                let before = fixture.history();
                assert!(before.effects.iter().all(|e| e.completeness.is_settled()));
                assert_eq!(before.account_movement.spent, 30_000);
                assert_eq!(before.payment_details, DetailCompleteness::Incomplete);
                fixture.recover(memo);
                let after = fixture.history();
                assert_eq!(after.payment_details, DetailCompleteness::Complete);
                assert_eq!(after.classification, HistoryClassification::Reconstructed);
                assert_eq!(after.fee, FeeState::Known(Zatoshis::const_from_u64(5000)));
            }
        }
    }

    #[test]
    fn recovered_owned_memo_does_not_complete_a_missing_external_memo() {
        let mut fixture = Fixture::new(10_000);
        fixture.recover(MemoBytes::empty());
        assert_eq!(
            fixture.history().payment_details,
            DetailCompleteness::Complete
        );
        fixture.st.wallet().conn().execute(
            "UPDATE sent_notes SET memo = NULL WHERE transaction_id = ? AND to_address IS NOT NULL",
            [fixture.tx]).unwrap();
        assert_eq!(
            fixture.history().payment_details,
            DetailCompleteness::Incomplete
        );
    }

    #[test]
    fn recovered_owned_memo_does_not_hide_another_missing_received_memo() {
        let mut fixture = Fixture::new(10_000);
        fixture.recover(MemoBytes::empty());
        assert_eq!(
            fixture.history().payment_details,
            DetailCompleteness::Complete
        );
        // A separate zero-value receipt changes no financial accounting, but still needs a memo.
        fixture
            .st
            .wallet()
            .conn()
            .execute(
                "INSERT INTO ironwood_received_notes (transaction_id, action_index, account_id,
                diversifier, value, rho, rseed, is_change, memo, note_version)
             SELECT transaction_id, action_index + 2, account_id, diversifier, 0, rho, rseed,
                is_change, NULL, note_version
             FROM ironwood_received_notes WHERE transaction_id = ?",
                [fixture.tx],
            )
            .unwrap();
        let history = fixture.history();
        assert!(history.effects.iter().all(|e| e.completeness.is_settled()));
        assert_eq!(history.payment_details, DetailCompleteness::Incomplete);
    }

    #[test]
    fn recovered_owned_memo_does_not_complete_unbalanced_accounting_or_unsettled_effects() {
        let mut fixture = Fixture::new(10_000);
        fixture.recover(MemoBytes::empty());
        assert_eq!(
            fixture.history().payment_details,
            DetailCompleteness::Complete
        );
        fixture
            .st
            .wallet()
            .conn()
            .execute(
                "UPDATE transactions SET fee = 5001 WHERE id_tx = ?",
                [fixture.tx],
            )
            .unwrap();
        assert_eq!(
            fixture.history().payment_details,
            DetailCompleteness::Incomplete
        );
        fixture
            .st
            .wallet()
            .conn()
            .execute(
                "UPDATE transactions SET fee = NULL WHERE id_tx = ?",
                [fixture.tx],
            )
            .unwrap();
        assert_eq!(
            fixture.history().payment_details,
            DetailCompleteness::Incomplete
        );
        fixture
            .st
            .wallet()
            .conn()
            .execute(
                "UPDATE transactions SET fee = 5000, mined_height = NULL WHERE id_tx = ?",
                [fixture.tx],
            )
            .unwrap();
        let history = fixture.history();
        assert!(history.effects.iter().any(|e| !e.completeness.is_settled()));
        assert_eq!(history.payment_details, DetailCompleteness::Incomplete);
    }
}