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
//! Receipt lifecycle tests using the real migrated schema, independent of a network service.
use super::*;
use crate::{
    WalletDb,
    testing::db::{test_clock, test_rng},
    wallet::init::WalletMigrator,
};
use zcash_client_backend::data_api::{
    TransactionStatus,
    status::{TransactionStatusMode, TransactionStatusWork},
    transparent_ledger::TransparentLedgerMode,
};
use zcash_protocol::consensus::Network;

fn database(
    path: &std::path::Path,
) -> WalletDb<
    rusqlite::Connection,
    Network,
    crate::util::testing::FixedClock,
    zcash_client_backend::data_api::testing::TestRng,
> {
    let mut db = WalletDb::for_path(path, Network::TestNetwork, test_clock(), test_rng()).unwrap();
    WalletMigrator::new().init_or_migrate(&mut db).unwrap();
    db
}
fn seed(conn: &Connection) -> TxId {
    let txid = TxId::from_bytes([9; 32]);
    conn.execute(
        "INSERT INTO blocks (height, hash, time, sapling_tree) VALUES (100, ?1, 0, X'00')",
        [[7u8; 32]],
    )
    .unwrap();
    conn.execute("INSERT INTO transactions (txid, block, mined_height, tx_index, min_observed_height, expiry_height) VALUES (?1, 100, 100, 3, 100, 103)", [txid.as_ref()]).unwrap();
    conn.execute_batch(
        "INSERT INTO scan_queue (block_range_start, block_range_end, priority) VALUES (1, 501, 10)",
    )
    .unwrap();
    txid
}
fn rewind(conn: &mut Connection) {
    let tx = conn.transaction().unwrap();
    super::super::queue_status_for_unobservable_transactions(&tx, Some(BlockHeight::from_u32(99)))
        .unwrap();
    capture_before_rewind(&tx, BlockHeight::from_u32(99), BlockHeight::from_u32(99)).unwrap();
    tx.execute_batch("UPDATE transactions SET block = NULL, mined_height = NULL, tx_index = NULL; DELETE FROM blocks WHERE height = 100;").unwrap();
    tx.commit().unwrap();
}
fn accept(conn: &mut Connection, hash: [u8; 32]) {
    let tx = conn.transaction().unwrap();
    tx.execute("INSERT INTO blocks (height, hash, time, sapling_tree) VALUES (100, ?1, 0, X'00') ON CONFLICT (height) DO UPDATE SET hash=excluded.hash", [hash]).unwrap();
    reconcile_scanned_blocks(
        &tx,
        &Network::TestNetwork,
        #[cfg(feature = "transparent-inputs")]
        &GapLimits::default(),
        &[(BlockHeight::from_u32(100), BlockHash(hash))],
    )
    .unwrap();
    tx.commit().unwrap();
}
fn receipts(conn: &Connection) -> i64 {
    conn.query_row("SELECT COUNT(*) FROM tx_reconfirmation_receipts", [], |r| {
        r.get(0)
    })
    .unwrap()
}
fn work(conn: &Connection) -> Vec<TransactionStatusWork> {
    super::super::transaction_status_work(
        conn,
        TransactionStatusMode::Public,
        Some(TransparentLedgerMode::Public),
    )
    .unwrap()
}
fn observed(conn: &mut Connection, txid: TxId, status: TransactionStatus) {
    let tx = conn.transaction().unwrap();
    super::super::set_transaction_status(
        &tx,
        &Network::TestNetwork,
        #[cfg(feature = "transparent-inputs")]
        &GapLimits::default(),
        txid,
        status,
    )
    .unwrap();
    tx.commit().unwrap();
}

#[test]
fn receipt_survives_reopen_and_restores_index_without_network_or_payload_completion() {
    let file = tempfile::NamedTempFile::new().unwrap();
    let mut db = database(file.path());
    let txid = seed(&db.conn);
    db.conn
        .execute(
            "INSERT INTO tx_retrieval_queue (txid, query_type) VALUES (?1, 1)",
            [txid.as_ref()],
        )
        .unwrap();
    rewind(&mut db.conn);
    assert!(work(&db.conn).is_empty());
    drop(db);
    let mut db = database(file.path());
    assert_eq!(receipts(&db.conn), 1);
    // Neither a high contiguous scan frontier nor an empty batch is inclusion evidence.
    let tx = db.conn.transaction().unwrap();
    reconcile_scanned_blocks(
        &tx,
        &Network::TestNetwork,
        #[cfg(feature = "transparent-inputs")]
        &GapLimits::default(),
        &[],
    )
    .unwrap();
    tx.commit().unwrap();
    assert!(work(&db.conn).is_empty());
    accept(&mut db.conn, [7; 32]);
    assert_eq!(receipts(&db.conn), 0);
    let state: (u32, u32, u16, u32, Option<u32>) = db.conn.query_row(
        "SELECT mined_height, block, tx_index, min_observed_height, confirmed_unmined_at_height FROM transactions", [],
        |r| Ok((r.get(0)?, r.get(1)?, r.get(2)?, r.get(3)?, r.get(4)?))).unwrap();
    assert_eq!(state, (100, 100, 3, 100, None));
    assert!(work(&db.conn).is_empty());
    assert_eq!(
        db.conn
            .query_row(
                "SELECT COUNT(*) FROM tx_retrieval_queue WHERE query_type = 1",
                [],
                |r| r.get::<_, i64>(0)
            )
            .unwrap(),
        1
    );
    assert_eq!(
        db.conn
            .query_row(
                "SELECT reconfirm_mined FROM tx_retrieval_queue WHERE query_type = 0",
                [],
                |r| r.get::<_, i64>(0)
            )
            .unwrap(),
        0
    );
}

#[test]
fn changed_block_exposes_private_fallback_without_manufacturing_inclusion_bounds() {
    let file = tempfile::NamedTempFile::new().unwrap();
    let mut db = database(file.path());
    let txid = seed(&db.conn);
    rewind(&mut db.conn);
    accept(&mut db.conn, [8; 32]);
    crate::wallet::transparent_ledger::apply_transparent_policy(
        &db.conn,
        Some(TransparentLedgerMode::Public),
        TransparentLedgerMode::PrivateRequired,
    )
    .unwrap();
    let pending = super::super::transaction_status_work(
        &db.conn,
        TransactionStatusMode::Public,
        Some(TransparentLedgerMode::PrivateRequired),
    )
    .unwrap();
    assert!(
        matches!(pending.as_slice(), [TransactionStatusWork::Private(r)] if r.txid() == txid && r.earliest_possible_inclusion().is_none())
    );
    // Errors (including incomplete coverage) do not invoke the completed-observation write.
    drop(db);
    let mut db = database(file.path());
    assert_eq!(receipts(&db.conn), 1);
    assert_eq!(
        db.conn
            .query_row("SELECT reconfirm_mined FROM tx_retrieval_queue", [], |r| {
                r.get::<_, i64>(0)
            })
            .unwrap(),
        1
    );
    assert!(
        super::super::transaction_status_work(
            &db.conn,
            TransactionStatusMode::Public,
            Some(TransparentLedgerMode::PrivateRequired)
        )
        .unwrap()
        .iter()
        .all(|r| matches!(r, TransactionStatusWork::Private(_)))
    );
    observed(
        &mut db.conn,
        txid,
        TransactionStatus::Mined(BlockHeight::from_u32(101)),
    );
    assert_eq!(receipts(&db.conn), 0);
    assert_eq!(
        db.conn
            .query_row("SELECT mined_height FROM transactions", [], |r| r
                .get::<_, u32>(0))
            .unwrap(),
        101
    );
}

#[test]
fn completed_negative_supersedes_receipt_and_terminal_obligation() {
    let file = tempfile::NamedTempFile::new().unwrap();
    let mut db = database(file.path());
    let txid = seed(&db.conn);
    rewind(&mut db.conn);
    accept(&mut db.conn, [8; 32]);
    assert_eq!(work(&db.conn).len(), 1);
    observed(&mut db.conn, txid, TransactionStatus::TxidNotRecognized);
    assert_eq!(receipts(&db.conn), 0);
    assert!(work(&db.conn).is_empty());
    accept(&mut db.conn, [7; 32]);
    assert!(
        db.conn
            .query_row("SELECT mined_height IS NULL FROM transactions", [], |r| {
                r.get::<_, bool>(0)
            })
            .unwrap()
    );
}

#[test]
fn missing_block_hash_uses_status_fallback_and_schema_rejects_fabricated_receipts() {
    let file = tempfile::NamedTempFile::new().unwrap();
    let mut db = database(file.path());
    seed(&db.conn);
    db.conn
        .execute_batch("UPDATE transactions SET block = NULL; DELETE FROM blocks")
        .unwrap();
    rewind(&mut db.conn);
    assert_eq!(receipts(&db.conn), 0);
    assert_eq!(work(&db.conn).len(), 1); // remains active despite expiry + reorg depth
    for (height, hash, index) in [
        (100i64, vec![0u8; 31], Some(3i64)),
        (-1, vec![0; 32], None),
        (4294967296, vec![0; 32], None),
        (100, vec![0; 32], Some(65536)),
    ] {
        assert!(db.conn.execute("INSERT INTO tx_reconfirmation_receipts (transaction_id, mined_height, block_hash, tx_index) SELECT id_tx, ?1, ?2, ?3 FROM transactions", rusqlite::params![height,hash,index]).is_err());
    }
}

#[test]
fn repeated_rewind_revalidates_changed_receipt_and_newer_mined_state_wins() {
    let file = tempfile::NamedTempFile::new().unwrap();
    let mut db = database(file.path());
    let txid = seed(&db.conn);
    rewind(&mut db.conn);
    accept(&mut db.conn, [8; 32]);
    assert_eq!(work(&db.conn).len(), 1);
    rewind(&mut db.conn);
    assert!(work(&db.conn).is_empty());
    accept(&mut db.conn, [7; 32]);
    assert_eq!(
        db.conn
            .query_row("SELECT mined_height FROM transactions", [], |r| r
                .get::<_, u32>(0))
            .unwrap(),
        100
    );
    rewind(&mut db.conn);
    // Model a newer authoritative ingestion before the old receipt's height is scanned.
    db.conn
        .execute_batch("UPDATE transactions SET mined_height = 101, min_observed_height = 100")
        .unwrap();
    accept(&mut db.conn, [7; 32]);
    assert_eq!(receipts(&db.conn), 0);
    assert_eq!(
        db.conn
            .query_row(
                "SELECT mined_height FROM transactions WHERE txid=?1",
                [txid.as_ref()],
                |r| r.get::<_, u32>(0)
            )
            .unwrap(),
        101
    );
}

#[test]
fn capture_and_reconciliation_failure_roll_back_receipts_metadata_and_flags() {
    let file = tempfile::NamedTempFile::new().unwrap();
    let mut db = database(file.path());
    seed(&db.conn);
    db.conn.execute_batch("CREATE TRIGGER reject_receipt BEFORE INSERT ON tx_reconfirmation_receipts BEGIN SELECT RAISE(ABORT, 'receipt failure'); END").unwrap();
    {
        let tx = db.conn.transaction().unwrap();
        super::super::queue_status_for_unobservable_transactions(
            &tx,
            Some(BlockHeight::from_u32(99)),
        )
        .unwrap();
        assert!(
            capture_before_rewind(&tx, BlockHeight::from_u32(99), BlockHeight::from_u32(99))
                .is_err()
        );
    }
    assert_eq!(
        db.conn
            .query_row("SELECT COUNT(*) FROM tx_retrieval_queue", [], |r| r
                .get::<_, i64>(0))
            .unwrap(),
        0
    );
    db.conn
        .execute_batch("DROP TRIGGER reject_receipt")
        .unwrap();
    rewind(&mut db.conn);
    db.conn.execute_batch("CREATE TRIGGER reject_resolution BEFORE DELETE ON tx_reconfirmation_receipts BEGIN SELECT RAISE(ABORT, 'resolution failure'); END").unwrap();
    {
        let tx = db.conn.transaction().unwrap();
        tx.execute(
            "INSERT INTO blocks (height,hash,time,sapling_tree) VALUES (100,?1,0,X'00')",
            [[7u8; 32]],
        )
        .unwrap();
        assert!(
            reconcile_scanned_blocks(
                &tx,
                &Network::TestNetwork,
                #[cfg(feature = "transparent-inputs")]
                &GapLimits::default(),
                &[(BlockHeight::from_u32(100), BlockHash([7; 32]))]
            )
            .is_err()
        );
    }
    assert_eq!(receipts(&db.conn), 1);
    assert!(
        db.conn
            .query_row("SELECT mined_height IS NULL FROM transactions", [], |r| {
                r.get::<_, bool>(0)
            })
            .unwrap()
    );
    assert_eq!(
        db.conn
            .query_row("SELECT reconfirm_mined FROM tx_retrieval_queue", [], |r| {
                r.get::<_, i64>(0)
            })
            .unwrap(),
        1
    );
    assert_eq!(
        db.conn
            .query_row("SELECT COUNT(*) FROM blocks", [], |r| r.get::<_, i64>(0))
            .unwrap(),
        0
    );
}

#[test]
fn receipts_cascade_with_transaction_deletion() {
    let file = tempfile::NamedTempFile::new().unwrap();
    let mut db = database(file.path());
    seed(&db.conn);
    rewind(&mut db.conn);
    assert_eq!(receipts(&db.conn), 1);
    db.conn.execute_batch("DELETE FROM transactions").unwrap();
    assert_eq!(receipts(&db.conn), 0);
}

#[test]
fn six_wallet_shielded_associations_are_excluded_from_capture_and_queue() {
    // Isolate the eligibility predicate, including pools disabled in this build. Foreign-key
    // note construction is covered by existing real-wallet shielded scan fixtures.
    for table in [
        "sapling_received_notes",
        "sapling_received_note_spends",
        "orchard_received_notes",
        "orchard_received_note_spends",
        "ironwood_received_notes",
        "ironwood_received_note_spends",
    ] {
        let mut conn = Connection::open_in_memory().unwrap();
        conn.execute_batch("PRAGMA foreign_keys=ON; CREATE TABLE transactions(id_tx INTEGER PRIMARY KEY, txid BLOB, mined_height INTEGER, tx_index INTEGER); CREATE TABLE blocks(height INTEGER, hash BLOB); CREATE TABLE tpir_meta(id INTEGER, applied_mode INTEGER, policy_generation INTEGER, min_reader_version INTEGER); INSERT INTO tpir_meta VALUES(0,0,0,1); CREATE TABLE tx_retrieval_queue(txid BLOB, query_type INTEGER, policy_generation INTEGER, reconfirm_mined INTEGER, UNIQUE(txid,query_type));").unwrap();
        for pool in ["sapling", "orchard", "ironwood"] {
            for suffix in ["received_notes", "received_note_spends"] {
                conn.execute_batch(&format!(
                    "CREATE TABLE {pool}_{suffix}(transaction_id INTEGER)"
                ))
                .unwrap();
            }
        }
        conn.execute_batch(RECEIPT_SCHEMA).unwrap();
        conn.execute(
            "INSERT INTO transactions VALUES(1, ?1, 100, 3)",
            [[9u8; 32]],
        )
        .unwrap();
        conn.execute("INSERT INTO blocks VALUES(100, ?1)", [[7u8; 32]])
            .unwrap();
        conn.execute_batch(&format!("INSERT INTO {table} VALUES(1)"))
            .unwrap();
        let tx = conn.transaction().unwrap();
        capture_before_rewind(&tx, BlockHeight::from_u32(99), BlockHeight::from_u32(99)).unwrap();
        super::super::queue_status_for_unobservable_transactions(
            &tx,
            Some(BlockHeight::from_u32(99)),
        )
        .unwrap();
        assert_eq!(receipts(&tx), 0, "{table}");
        assert_eq!(
            tx.query_row("SELECT COUNT(*) FROM tx_retrieval_queue", [], |r| r
                .get::<_, i64>(0))
                .unwrap(),
            0,
            "{table}"
        );
    }
}