zakura-client-sqlite 0.1.0-rc0

An SQLite-based Zcash light client
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
//! Adds tables for tracking transactions to be downloaded for transparent output and/or memo retrieval.

use rusqlite::named_params;
use schemerz_rusqlite::RusqliteMigration;
use std::collections::HashSet;
use uuid::Uuid;

use zcash_primitives::transaction::builder::DEFAULT_TX_EXPIRY_DELTA;
use zcash_protocol::consensus;

use crate::wallet::init::WalletMigrationError;

use super::{
    ensure_orchard_ua_receiver, ephemeral_addresses, nullifier_map, orchard_shardtree,
    spend_key_available,
};

/// Adds tables for tracking transactions to be downloaded for transparent output and/or memo
/// retrieval.
pub const MIGRATION_ID: Uuid = Uuid::from_u128(0xfec02b61_3988_4b4f_9699_98977fac9e7f);

#[cfg(feature = "transparent-inputs")]
use {
    crate::{
        AccountRef, TxRef,
        error::SqliteClientError,
        wallet::{TxQueryType, transparent::uivk_legacy_transparent_address},
    },
    rusqlite::OptionalExtension as _,
    std::convert::Infallible,
    transparent::address::TransparentAddress,
    zcash_client_backend::data_api::DecryptedTransaction,
    zcash_keys::encoding::AddressCodec,
    zcash_primitives::transaction::Transaction,
    zcash_protocol::{
        TxId,
        consensus::{BlockHeight, BranchId},
    },
};

const DEPENDENCIES: &[Uuid] = &[
    orchard_shardtree::MIGRATION_ID,
    ensure_orchard_ua_receiver::MIGRATION_ID,
    ephemeral_addresses::MIGRATION_ID,
    spend_key_available::MIGRATION_ID,
    nullifier_map::MIGRATION_ID,
];

pub(super) struct Migration<P> {
    pub(super) _params: P,
}

impl<P> schemerz::Migration<Uuid> for Migration<P> {
    fn id(&self) -> Uuid {
        MIGRATION_ID
    }

    fn dependencies(&self) -> HashSet<Uuid> {
        DEPENDENCIES.iter().copied().collect()
    }

    fn description(&self) -> &'static str {
        "Adds tables for tracking transactions to be downloaded for transparent output and/or memo retrieval."
    }
}

impl<P: consensus::Parameters> RusqliteMigration for Migration<P> {
    type Error = WalletMigrationError;

    fn up(&self, conn: &rusqlite::Transaction) -> Result<(), WalletMigrationError> {
        conn.execute_batch(
            "CREATE TABLE tx_retrieval_queue (
                txid BLOB NOT NULL UNIQUE,
                query_type INTEGER NOT NULL,
                dependent_transaction_id INTEGER,
                FOREIGN KEY (dependent_transaction_id) REFERENCES transactions(id_tx)
            );

            ALTER TABLE transactions ADD COLUMN target_height INTEGER;

            CREATE TABLE transparent_spend_search_queue (
                address TEXT NOT NULL,
                transaction_id INTEGER NOT NULL,
                output_index INTEGER NOT NULL,
                FOREIGN KEY (transaction_id) REFERENCES transactions(id_tx),
                CONSTRAINT value_received_height UNIQUE (transaction_id, output_index)
            );

            CREATE TABLE transparent_spend_map (
                spending_transaction_id INTEGER NOT NULL,
                prevout_txid BLOB NOT NULL,
                prevout_output_index INTEGER NOT NULL,
                FOREIGN KEY (spending_transaction_id) REFERENCES transactions(id_tx)
                -- NOTE: We can't create a unique constraint on just (prevout_txid, prevout_output_index)
                -- because the same output may be attempted to be spent in multiple transactions, even
                -- though only one will ever be mined.
                CONSTRAINT transparent_spend_map_unique UNIQUE (
                    spending_transaction_id, prevout_txid, prevout_output_index
                )
            );",
        )?;

        // Add estimated target height information for each transaction we know to
        // have been created by the wallet; transactions that were discovered via
        // chain scanning will have their `created` field set to `NULL`.
        conn.execute(
            "UPDATE transactions
             SET target_height = expiry_height - :default_expiry_delta
             WHERE expiry_height > :default_expiry_delta
             AND created IS NOT NULL",
            named_params![":default_expiry_delta": DEFAULT_TX_EXPIRY_DELTA],
        )?;

        // Populate the enhancement queues with any transparent history information that we don't
        // already have.
        #[cfg(feature = "transparent-inputs")]
        {
            let mut stmt_transactions =
                conn.prepare("SELECT id_tx, raw, mined_height FROM transactions")?;
            let mut rows = stmt_transactions.query([])?;
            while let Some(row) = rows.next()? {
                let tx_ref = row.get(0).map(TxRef)?;
                let tx_data = row.get::<_, Option<Vec<u8>>>(1)?;
                let mined_height = row.get::<_, Option<u32>>(2)?.map(BlockHeight::from);

                if let Some(tx_data) = tx_data {
                    let tx = Transaction::read(
                        &tx_data[..],
                        // We assume unmined transactions are created with the current consensus branch ID.
                        mined_height.map_or(BranchId::Sapling, |h| {
                            BranchId::for_height(&self._params, h)
                        }),
                    )
                    .map_err(|_| {
                        WalletMigrationError::CorruptedData(
                            "Could not read serialized transaction data.".to_owned(),
                        )
                    })?;

                    for (txout, output_index) in tx
                        .transparent_bundle()
                        .iter()
                        .flat_map(|b| b.vout.iter())
                        .zip(0u32..)
                    {
                        if let Some(address) = txout.recipient_address() {
                            let find_address_account = || {
                                conn.query_row(
                                    "SELECT account_id FROM addresses
                                     WHERE cached_transparent_receiver_address = :address
                                     UNION
                                     SELECT account_id from ephemeral_addresses
                                     WHERE address = :address",
                                    named_params![":address": address.encode(&self._params)],
                                    |row| row.get(0).map(AccountRef),
                                )
                                .optional()
                            };
                            let find_legacy_address_account =
                                || -> Result<Option<AccountRef>, SqliteClientError> {
                                    let mut stmt = conn.prepare("SELECT id, uivk FROM accounts")?;
                                    let mut rows = stmt.query([])?;
                                    while let Some(row) = rows.next()? {
                                        let account_id = row.get(0).map(AccountRef)?;
                                        let uivk_str = row.get::<_, String>(1)?;

                                        if let Some((legacy_taddr, _)) =
                                            uivk_legacy_transparent_address(
                                                &self._params,
                                                &uivk_str,
                                            )?
                                            && legacy_taddr == address
                                        {
                                            return Ok(Some(account_id));
                                        }
                                    }

                                    Ok(None)
                                };

                            if find_address_account()?.is_some()
                                || find_legacy_address_account()?.is_some()
                            {
                                queue_transparent_spend_detection(
                                    conn,
                                    &self._params,
                                    address,
                                    tx_ref,
                                    output_index,
                                )?
                            }
                        }
                    }

                    let d_tx = DecryptedTransaction::<Transaction, Infallible>::new(
                        mined_height,
                        &tx,
                        vec![],
                        #[cfg(feature = "orchard")]
                        vec![],
                        #[cfg(feature = "orchard")]
                        vec![],
                    );

                    queue_transparent_input_retrieval(conn, tx_ref, &d_tx)?;
                    queue_unmined_tx_retrieval(conn, &d_tx)?;
                }
            }
        }

        Ok(())
    }

    fn down(&self, conn: &rusqlite::Transaction) -> Result<(), WalletMigrationError> {
        conn.execute_batch(
            "DROP TABLE transparent_spend_map;
             DROP TABLE transparent_spend_search_queue;
             ALTER TABLE transactions DROP COLUMN target_height;
             DROP TABLE tx_retrieval_queue;",
        )?;

        Ok(())
    }
}

#[cfg(feature = "transparent-inputs")]
fn queue_transparent_spend_detection<P: consensus::Parameters>(
    conn: &rusqlite::Transaction<'_>,
    params: &P,
    receiving_address: TransparentAddress,
    tx_ref: TxRef,
    output_index: u32,
) -> Result<(), SqliteClientError> {
    let mut stmt = conn.prepare_cached(
        "INSERT INTO transparent_spend_search_queue
         (address, transaction_id, output_index)
         VALUES
         (:address, :transaction_id, :output_index)
         ON CONFLICT (transaction_id, output_index) DO NOTHING",
    )?;

    let addr_str = receiving_address.encode(params);
    stmt.execute(named_params! {
        ":address": addr_str,
        ":transaction_id": tx_ref.0,
        ":output_index": output_index
    })?;

    Ok(())
}

#[cfg(feature = "transparent-inputs")]
fn queue_transparent_input_retrieval<AccountId>(
    conn: &rusqlite::Transaction<'_>,
    tx_ref: TxRef,
    d_tx: &DecryptedTransaction<Transaction, AccountId>,
) -> Result<(), SqliteClientError> {
    if let Some(b) = d_tx.tx().transparent_bundle()
        && !b.is_coinbase()
    {
        // queue the transparent inputs for enhancement
        queue_tx_retrieval(
            conn,
            b.vin.iter().map(|txin| *txin.prevout().txid()),
            Some(tx_ref),
        )?;
    }

    Ok(())
}

#[cfg(feature = "transparent-inputs")]
fn queue_unmined_tx_retrieval<AccountId>(
    conn: &rusqlite::Transaction<'_>,
    d_tx: &DecryptedTransaction<Transaction, AccountId>,
) -> Result<(), SqliteClientError> {
    let detectable_via_scanning = d_tx.tx().sapling_bundle().is_some();
    #[cfg(feature = "orchard")]
    let detectable_via_scanning = detectable_via_scanning | d_tx.tx().orchard_bundle().is_some();

    if d_tx.mined_height().is_none() && !detectable_via_scanning {
        queue_tx_retrieval(conn, std::iter::once(d_tx.tx().txid()), None)?
    }

    Ok(())
}

#[cfg(feature = "transparent-inputs")]
fn queue_tx_retrieval(
    conn: &rusqlite::Transaction<'_>,
    txids: impl Iterator<Item = TxId>,
    dependent_tx_ref: Option<TxRef>,
) -> Result<(), SqliteClientError> {
    // Add an entry to the transaction retrieval queue if it would not be redundant.
    let mut stmt_insert_tx = conn.prepare_cached(
        "INSERT INTO tx_retrieval_queue (txid, query_type, dependent_transaction_id)
            SELECT
            :txid,
            IIF(
                EXISTS (SELECT 1 FROM transactions WHERE txid = :txid AND raw IS NOT NULL),
                :status_type,
                :enhancement_type
            ),
            :dependent_transaction_id
        ON CONFLICT (txid) DO UPDATE
        SET query_type =
            IIF(
                EXISTS (SELECT 1 FROM transactions WHERE txid = :txid AND raw IS NOT NULL),
                :status_type,
                :enhancement_type
            ),
            dependent_transaction_id = IFNULL(:dependent_transaction_id, dependent_transaction_id)",
    )?;

    for txid in txids {
        stmt_insert_tx.execute(named_params! {
            ":txid": txid.as_ref(),
            ":status_type": TxQueryType::Status.code(),
            ":enhancement_type": TxQueryType::Enhancement.code(),
            ":dependent_transaction_id": dependent_tx_ref.map(|r| r.0),
        })?;
    }

    Ok(())
}

#[cfg(test)]
mod tests {
    use rusqlite::named_params;
    use secrecy::Secret;
    use tempfile::NamedTempFile;

    use ::transparent::{
        address::{Script, TransparentAddress},
        bundle::{OutPoint, TxIn, TxOut},
    };
    use zcash_primitives::transaction::{Authorized, TransactionData, TxVersion};
    use zcash_protocol::{
        consensus::{BranchId, Network},
        value::Zatoshis,
    };

    use crate::{
        WalletDb,
        testing::db::{test_clock, test_rng},
        wallet::init::{WalletMigrator, migrations::tests::test_migrate},
    };

    use super::{DEPENDENCIES, MIGRATION_ID};

    #[test]
    fn migrate() {
        test_migrate(&[MIGRATION_ID]);
    }

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

        let seed_bytes = vec![0xab; 32];

        // Migrate to database state just prior to this migration.
        WalletMigrator::new()
            .with_seed(Secret::new(seed_bytes.clone()))
            .ignore_seed_relevance()
            .init_or_migrate_to(&mut db_data, DEPENDENCIES)
            .unwrap();

        // Add transactions to the wallet that exercise the data migration.
        let add_tx_to_wallet = |tx: TransactionData<Authorized>| {
            let tx = tx.freeze().unwrap();
            let txid = tx.txid();
            let mut raw_tx = vec![];
            tx.write(&mut raw_tx).unwrap();
            db_data
                .conn
                .execute(
                    r#"INSERT INTO transactions (txid, raw) VALUES (:txid, :raw);"#,
                    named_params! {":txid": txid.as_ref(), ":raw": raw_tx},
                )
                .unwrap();
        };
        add_tx_to_wallet(TransactionData::from_parts(
            TxVersion::V5,
            BranchId::Nu5,
            0,
            12345678.into(),
            #[cfg(all(zcash_unstable = "nu7", feature = "zip-233"))]
            Zatoshis::ZERO,
            Some(transparent::bundle::Bundle {
                vin: vec![TxIn::from_parts(OutPoint::fake(), Script::default(), 0)],
                vout: vec![TxOut::new(
                    Zatoshis::const_from_u64(10_000),
                    TransparentAddress::PublicKeyHash([7; 20]).script().into(),
                )],
                authorization: transparent::bundle::Authorized,
            }),
            None,
            None,
            None,
        ));

        // Check that we can apply this migration.
        WalletMigrator::new()
            .with_seed(Secret::new(seed_bytes))
            .ignore_seed_relevance()
            .init_or_migrate_to(&mut db_data, &[MIGRATION_ID])
            .unwrap();
    }
}