Skip to content

Commit

Permalink
Make ephemeral_addresses.address non-null & unique
Browse files Browse the repository at this point in the history
We should never construct an ephemeral address from an invalid index,
and therefore the address column need not be nullable.
  • Loading branch information
nuttycom committed Jul 17, 2024
1 parent 24b6d50 commit ab7f1b9
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 19 deletions.
7 changes: 2 additions & 5 deletions zcash_client_sqlite/src/wallet/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,13 +116,14 @@ pub(super) const TABLE_EPHEMERAL_ADDRESSES: &str = r#"
CREATE TABLE ephemeral_addresses (
account_id INTEGER NOT NULL,
address_index INTEGER NOT NULL,
address TEXT,
address TEXT NOT NULL,
used_in_tx INTEGER,
seen_in_tx INTEGER,
FOREIGN KEY (account_id) REFERENCES accounts(id),
FOREIGN KEY (used_in_tx) REFERENCES transactions(id_tx),
FOREIGN KEY (seen_in_tx) REFERENCES transactions(id_tx),
PRIMARY KEY (account_id, address_index),
CONSTRAINT ephemeral_addr_uniq UNIQUE (address),
CONSTRAINT used_implies_seen CHECK (
used_in_tx IS NULL OR seen_in_tx IS NOT NULL
),
Expand All @@ -135,10 +136,6 @@ CREATE TABLE ephemeral_addresses (
// libsqlite3-sys requires at least version 3.14.0.
// "WITHOUT ROWID" tells SQLite to use a clustered index on the (composite) primary key.
const_assert_eq!(GAP_LIMIT, 20);
pub(super) const INDEX_EPHEMERAL_ADDRESSES_ADDRESS: &str = r#"
CREATE INDEX ephemeral_addresses_address ON ephemeral_addresses (
address ASC
)"#;

/// Stores information about every block that the wallet has scanned.
///
Expand Down
1 change: 0 additions & 1 deletion zcash_client_sqlite/src/wallet/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,6 @@ mod tests {
db::INDEX_ACCOUNTS_UIVK,
db::INDEX_HD_ACCOUNT,
db::INDEX_ADDRESSES_ACCOUNTS,
db::INDEX_EPHEMERAL_ADDRESSES_ADDRESS,
db::INDEX_NF_MAP_LOCATOR_IDX,
db::INDEX_ORCHARD_RECEIVED_NOTES_ACCOUNT,
db::INDEX_ORCHARD_RECEIVED_NOTES_TX,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,24 +45,22 @@ impl<P: consensus::Parameters> RusqliteMigration for Migration<P> {
"CREATE TABLE ephemeral_addresses (
account_id INTEGER NOT NULL,
address_index INTEGER NOT NULL,
address TEXT,
address TEXT NOT NULL,
used_in_tx INTEGER,
seen_in_tx INTEGER,
FOREIGN KEY (account_id) REFERENCES accounts(id),
FOREIGN KEY (used_in_tx) REFERENCES transactions(id_tx),
FOREIGN KEY (seen_in_tx) REFERENCES transactions(id_tx),
PRIMARY KEY (account_id, address_index),
CONSTRAINT ephemeral_addr_uniq UNIQUE (address),
CONSTRAINT used_implies_seen CHECK (
used_in_tx IS NULL OR seen_in_tx IS NOT NULL
),
CONSTRAINT index_range_and_address_nullity CHECK (
(address_index BETWEEN 0 AND 0x7FFFFFFF AND address IS NOT NULL) OR
(address_index BETWEEN 0x80000000 AND 0x7FFFFFFF + 20 AND address IS NULL AND used_in_tx IS NULL AND seen_in_tx IS NULL)
)
) WITHOUT ROWID;
CREATE INDEX ephemeral_addresses_address ON ephemeral_addresses (
address ASC
);",
) WITHOUT ROWID;"
)?;

// Make sure that at least `GAP_LIMIT` ephemeral transparent addresses are
Expand Down
17 changes: 9 additions & 8 deletions zcash_client_sqlite/src/wallet/transparent/ephemeral.rs
Original file line number Diff line number Diff line change
Expand Up @@ -282,18 +282,19 @@ fn reserve_until<P: consensus::Parameters>(
)?;

for raw_index in range_to_store {
let address_str_opt = match NonHardenedChildIndex::from_index(raw_index) {
Some(address_index) => Some(
let address_str = NonHardenedChildIndex::from_index(raw_index)
.map(|address_index| {
ephemeral_ivk
.derive_ephemeral_address(address_index)?
.encode(params),
),
None => None,
};
.derive_ephemeral_address(address_index)
.map(|addr| addr.encode(params))
})
.transpose()?
.expect("we do not attempt to generate addresses outside the non-hardened index range");

stmt_insert_ephemeral_address.execute(named_params![
":account_id": account_id.0,
":address_index": raw_index,
":address": address_str_opt,
":address": address_str,
])?;
}
Ok(())
Expand Down

0 comments on commit ab7f1b9

Please sign in to comment.