Skip to content

Commit 0fc932c

Browse files
dairanuttycomJack Grigg
committed
Work-in-progress database support for ZIP 320.
Co-authored-by: Kris Nuttycombe <[email protected]> Co-authored-by: Jack Grigg <[email protected]> Signed-off-by: Daira-Emma Hopwood <[email protected]>
1 parent 62d9ae9 commit 0fc932c

File tree

3 files changed

+61
-2
lines changed

3 files changed

+61
-2
lines changed

zcash_client_sqlite/src/wallet/init.rs

+2
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,8 @@ mod tests {
234234
p2pkh_fvk_item_cache BLOB,
235235
birthday_height INTEGER NOT NULL,
236236
recover_until_height INTEGER,
237+
first_unmined_ephemeral_index INTEGER NOT NULL DEFAULT 0,
238+
first_unused_ephemeral_index INTEGER NOT NULL DEFAULT 0 CONSTRAINT unused_gte_unmined CHECK (first_unused_ephemeral_index >= first_unmined_ephemeral_index),
237239
CHECK ( (account_kind = 0 AND hd_seed_fingerprint IS NOT NULL AND hd_account_index IS NOT NULL AND ufvk IS NOT NULL) OR (account_kind = 1 AND hd_seed_fingerprint IS NULL AND hd_account_index IS NULL) )
238240
)"#,
239241
r#"CREATE TABLE "addresses" (

zcash_client_sqlite/src/wallet/init/migrations.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ mod sapling_memo_consistency;
1313
mod sent_notes_to_internal;
1414
mod shardtree_support;
1515
mod ufvk_support;
16+
mod used_addresses;
1617
mod utxos_table;
1718
mod v_sapling_shard_unscanned_ranges;
1819
mod v_transactions_net;
@@ -61,8 +62,8 @@ pub(super) fn all_migrations<P: consensus::Parameters + 'static>(
6162
// \ | v_transactions_note_uniqueness
6263
// \ | /
6364
// full_account_ids
64-
// |
65-
// orchard_received_notes
65+
// / \
66+
// orchard_received_notes used_addresses
6667
vec![
6768
Box::new(initial_setup::Migration {}),
6869
Box::new(utxos_table::Migration {}),
@@ -109,5 +110,6 @@ pub(super) fn all_migrations<P: consensus::Parameters + 'static>(
109110
params: params.clone(),
110111
}),
111112
Box::new(orchard_received_notes::Migration),
113+
Box::new(used_addresses::Migration),
112114
]
113115
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
//! The migration that records ephemeral addresses used beyond the last known mined address, for each account.
2+
use std::collections::HashSet;
3+
4+
use rusqlite;
5+
use schemer;
6+
use schemer_rusqlite::RusqliteMigration;
7+
use uuid::Uuid;
8+
9+
use crate::wallet::init::WalletMigrationError;
10+
11+
use super::full_account_ids;
12+
13+
pub(super) const MIGRATION_ID: Uuid = Uuid::from_u128(0x0e1d4274_1f8e_44e2_909d_689a4bc2967b);
14+
15+
pub(super) struct Migration;
16+
17+
impl schemer::Migration for Migration {
18+
fn id(&self) -> Uuid {
19+
MIGRATION_ID
20+
}
21+
22+
fn dependencies(&self) -> HashSet<Uuid> {
23+
[full_account_ids::MIGRATION_ID].into_iter().collect()
24+
}
25+
26+
fn description(&self) -> &'static str {
27+
"Record ephemeral addresses used beyond the last known mined address, for each account."
28+
}
29+
}
30+
31+
impl RusqliteMigration for Migration {
32+
type Error = WalletMigrationError;
33+
34+
fn up(&self, transaction: &rusqlite::Transaction) -> Result<(), WalletMigrationError> {
35+
transaction.execute_batch(
36+
r#"
37+
ALTER TABLE accounts ADD first_unmined_ephemeral_index INTEGER NOT NULL DEFAULT 0;
38+
ALTER TABLE accounts ADD first_unused_ephemeral_index INTEGER NOT NULL DEFAULT 0
39+
CONSTRAINT unused_gte_unmined CHECK (first_unused_ephemeral_index >= first_unmined_ephemeral_index);
40+
"#,
41+
)?;
42+
Ok(())
43+
}
44+
45+
fn down(&self, transaction: &rusqlite::Transaction) -> Result<(), WalletMigrationError> {
46+
transaction.execute_batch(
47+
r#"
48+
-- Dropping first_unused_ephemeral_index also drops its constraint.
49+
ALTER TABLE accounts DROP COLUMN first_unused_ephemeral_index;
50+
ALTER TABLE accounts DROP COLUMN first_unmined_ephemeral_index;
51+
"#,
52+
)?;
53+
Ok(())
54+
}
55+
}

0 commit comments

Comments
 (0)