Skip to content

Commit

Permalink
Rename a few more id columns
Browse files Browse the repository at this point in the history
  • Loading branch information
AArnott committed Mar 6, 2024
1 parent bc8e8f3 commit 6079fe5
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 46 deletions.
4 changes: 3 additions & 1 deletion zcash_client_backend/src/data_api/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ use std::num::NonZeroU32;

use zcash_keys::{
address::UnifiedAddress,
keys::{HdSeedFingerprint, UnifiedAddressRequest, UnifiedFullViewingKey, AddressGenerationError},
keys::{
AddressGenerationError, HdSeedFingerprint, UnifiedAddressRequest, UnifiedFullViewingKey,
},
};
use zcash_primitives::{
consensus::{self, BlockHeight, NetworkUpgrade},
Expand Down
12 changes: 3 additions & 9 deletions zcash_client_sqlite/src/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1386,17 +1386,11 @@ pub(crate) fn truncate_to_height<P: consensus::Parameters>(
tree.truncate_removing_checkpoint(&block_height).map(|_| ())
})?;

// Remove any legacy Sapling witnesses
conn.execute(
"DELETE FROM sapling_witnesses WHERE block > ?",
[u32::from(block_height)],
)?;

// Rewind received notes
conn.execute(
"DELETE FROM sapling_received_notes
WHERE id_note IN (
SELECT rn.id_note
WHERE id IN (
SELECT rn.id
FROM sapling_received_notes rn
LEFT OUTER JOIN transactions tx
ON tx.id_tx = rn.tx
Expand Down Expand Up @@ -1847,7 +1841,7 @@ pub(crate) fn put_legacy_transparent_utxo<P: consensus::Parameters>(
address = :address,
script = :script,
value_zat = :value_zat
RETURNING id_utxo",
RETURNING id",
)?;

let sql_args = named_params![
Expand Down
25 changes: 8 additions & 17 deletions zcash_client_sqlite/src/wallet/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ mod tests {
CONSTRAINT nf_uniq UNIQUE (spend_pool, nf)
)",
r#"CREATE TABLE "sapling_received_notes" (
id_note INTEGER PRIMARY KEY,
id INTEGER PRIMARY KEY,
tx INTEGER NOT NULL,
output_index INTEGER NOT NULL,
account_id INTEGER NOT NULL,
Expand Down Expand Up @@ -300,15 +300,6 @@ mod tests {
contains_marked INTEGER,
CONSTRAINT root_unique UNIQUE (root_hash)
)",
"CREATE TABLE sapling_witnesses (
id_witness INTEGER PRIMARY KEY,
note INTEGER NOT NULL,
block INTEGER NOT NULL,
witness BLOB NOT NULL,
FOREIGN KEY (note) REFERENCES sapling_received_notes(id_note),
FOREIGN KEY (block) REFERENCES blocks(height),
CONSTRAINT witness_height UNIQUE (note, block)
)",
"CREATE TABLE scan_queue (
block_range_start INTEGER NOT NULL,
block_range_end INTEGER NOT NULL,
Expand All @@ -323,7 +314,7 @@ mod tests {
id blob PRIMARY KEY
)",
r#"CREATE TABLE "sent_notes" (
id_note INTEGER PRIMARY KEY,
id INTEGER PRIMARY KEY,
tx INTEGER NOT NULL,
output_pool INTEGER NOT NULL,
output_index INTEGER NOT NULL,
Expand Down Expand Up @@ -359,7 +350,7 @@ mod tests {
PRIMARY KEY (block_height, tx_index)
)",
r#"CREATE TABLE "utxos" (
id_utxo INTEGER PRIMARY KEY,
id INTEGER PRIMARY KEY,
received_by_account_id INTEGER NOT NULL,
address TEXT NOT NULL,
prevout_txid BLOB NOT NULL,
Expand Down Expand Up @@ -459,7 +450,7 @@ mod tests {
"CREATE VIEW v_transactions AS
WITH
notes AS (
SELECT sapling_received_notes.id_note AS id,
SELECT sapling_received_notes.id AS id,
sapling_received_notes.account_id AS account_id,
transactions.block AS block,
transactions.txid AS txid,
Expand All @@ -482,7 +473,7 @@ mod tests {
JOIN transactions
ON transactions.id_tx = sapling_received_notes.tx
UNION
SELECT utxos.id_utxo AS id,
SELECT utxos.id AS id,
utxos.received_by_account_id AS account_id,
utxos.height AS block,
utxos.prevout_txid AS txid,
Expand All @@ -493,7 +484,7 @@ mod tests {
0 AS memo_present
FROM utxos
UNION
SELECT sapling_received_notes.id_note AS id,
SELECT sapling_received_notes.id AS id,
sapling_received_notes.account_id AS account_id,
transactions.block AS block,
transactions.txid AS txid,
Expand All @@ -506,7 +497,7 @@ mod tests {
JOIN transactions
ON transactions.id_tx = sapling_received_notes.spent
UNION
SELECT utxos.id_utxo AS id,
SELECT utxos.id AS id,
utxos.received_by_account_id AS account_id,
transactions.block AS block,
transactions.txid AS txid,
Expand All @@ -522,7 +513,7 @@ mod tests {
sent_note_counts AS (
SELECT sent_notes.from_account_id AS account_id,
transactions.txid AS txid,
COUNT(DISTINCT sent_notes.id_note) as sent_notes,
COUNT(DISTINCT sent_notes.id) as sent_notes,
SUM(
CASE
WHEN (sent_notes.memo IS NULL OR sent_notes.memo = X'F6' OR sapling_received_notes.tx IS NOT NULL)
Expand Down
25 changes: 14 additions & 11 deletions zcash_client_sqlite/src/wallet/init/migrations/full_account_ids.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ impl RusqliteMigration for Migration {
-- Migrate sapling_received_notes table
CREATE TABLE sapling_received_notes_new (
id_note INTEGER PRIMARY KEY,
id INTEGER PRIMARY KEY,
tx INTEGER NOT NULL,
output_index INTEGER NOT NULL,
account_id INTEGER NOT NULL,
Expand All @@ -137,7 +137,7 @@ impl RusqliteMigration for Migration {
CREATE INDEX "sapling_received_notes_spent" ON "sapling_received_notes_new" (
"spent" ASC
);
INSERT INTO sapling_received_notes_new (id_note, tx, output_index, account_id, diversifier, value, rcm, nf, is_change, memo, spent, commitment_tree_position, recipient_key_scope)
INSERT INTO sapling_received_notes_new (id, tx, output_index, account_id, diversifier, value, rcm, nf, is_change, memo, spent, commitment_tree_position, recipient_key_scope)
SELECT id_note, tx, output_index, account, diversifier, value, rcm, nf, is_change, memo, spent, commitment_tree_position, recipient_key_scope
FROM sapling_received_notes;
Expand All @@ -146,7 +146,7 @@ impl RusqliteMigration for Migration {
-- Migrate sent_notes table
CREATE TABLE sent_notes_new (
id_note INTEGER PRIMARY KEY,
id INTEGER PRIMARY KEY,
tx INTEGER NOT NULL,
output_pool INTEGER NOT NULL,
output_index INTEGER NOT NULL,
Expand All @@ -166,16 +166,19 @@ impl RusqliteMigration for Migration {
CREATE INDEX sent_notes_tx ON sent_notes_new (tx);
CREATE INDEX sent_notes_from_account ON sent_notes_new (from_account_id);
CREATE INDEX sent_notes_to_account ON sent_notes_new (to_account_id);
INSERT INTO sent_notes_new (id_note, tx, output_pool, output_index, from_account_id, to_address, to_account_id, value, memo)
INSERT INTO sent_notes_new (id, tx, output_pool, output_index, from_account_id, to_address, to_account_id, value, memo)
SELECT id_note, tx, output_pool, output_index, from_account, to_address, to_account, value, memo
FROM sent_notes;
DROP TABLE sent_notes;
ALTER TABLE sent_notes_new RENAME TO sent_notes;
-- No one uses this table any more, and it contains a reference to columns we renamed.
DROP TABLE sapling_witnesses;
-- Migrate utxos table
CREATE TABLE utxos_new (
id_utxo INTEGER PRIMARY KEY,
id INTEGER PRIMARY KEY,
received_by_account_id INTEGER NOT NULL,
address TEXT NOT NULL,
prevout_txid BLOB NOT NULL,
Expand All @@ -190,7 +193,7 @@ impl RusqliteMigration for Migration {
);
CREATE INDEX utxos_received_by_account ON utxos_new (received_by_account_id);
CREATE INDEX utxos_spent_in_tx ON utxos_new (spent_in_tx);
INSERT INTO utxos_new (id_utxo, received_by_account_id, address, prevout_txid, prevout_idx, script, value_zat, height, spent_in_tx)
INSERT INTO utxos_new (id, received_by_account_id, address, prevout_txid, prevout_idx, script, value_zat, height, spent_in_tx)
SELECT id_utxo, received_by_account, address, prevout_txid, prevout_idx, script, value_zat, height, spent_in_tx
FROM utxos;
Expand All @@ -205,7 +208,7 @@ impl RusqliteMigration for Migration {
CREATE VIEW v_transactions AS
WITH
notes AS (
SELECT sapling_received_notes.id_note AS id,
SELECT sapling_received_notes.id AS id,
sapling_received_notes.account_id AS account_id,
transactions.block AS block,
transactions.txid AS txid,
Expand All @@ -228,7 +231,7 @@ impl RusqliteMigration for Migration {
JOIN transactions
ON transactions.id_tx = sapling_received_notes.tx
UNION
SELECT utxos.id_utxo AS id,
SELECT utxos.id AS id,
utxos.received_by_account_id AS account_id,
utxos.height AS block,
utxos.prevout_txid AS txid,
Expand All @@ -239,7 +242,7 @@ impl RusqliteMigration for Migration {
0 AS memo_present
FROM utxos
UNION
SELECT sapling_received_notes.id_note AS id,
SELECT sapling_received_notes.id AS id,
sapling_received_notes.account_id AS account_id,
transactions.block AS block,
transactions.txid AS txid,
Expand All @@ -252,7 +255,7 @@ impl RusqliteMigration for Migration {
JOIN transactions
ON transactions.id_tx = sapling_received_notes.spent
UNION
SELECT utxos.id_utxo AS id,
SELECT utxos.id AS id,
utxos.received_by_account_id AS account_id,
transactions.block AS block,
transactions.txid AS txid,
Expand All @@ -268,7 +271,7 @@ impl RusqliteMigration for Migration {
sent_note_counts AS (
SELECT sent_notes.from_account_id AS account_id,
transactions.txid AS txid,
COUNT(DISTINCT sent_notes.id_note) as sent_notes,
COUNT(DISTINCT sent_notes.id) as sent_notes,
SUM(
CASE
WHEN (sent_notes.memo IS NULL OR sent_notes.memo = X'F6' OR sapling_received_notes.tx IS NOT NULL)
Expand Down
16 changes: 8 additions & 8 deletions zcash_client_sqlite/src/wallet/sapling.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ pub(crate) fn get_spendable_sapling_note<P: consensus::Parameters>(
index: u32,
) -> Result<Option<ReceivedNote<ReceivedNoteId, Note>>, SqliteClientError> {
let mut stmt_select_note = conn.prepare_cached(
"SELECT id_note, txid, output_index, diversifier, value, rcm, commitment_tree_position,
"SELECT sapling_received_notes.id, txid, output_index, diversifier, value, rcm, commitment_tree_position,
accounts.uvk, recipient_key_scope
FROM sapling_received_notes
INNER JOIN accounts on accounts.id = sapling_received_notes.account_id
Expand Down Expand Up @@ -267,9 +267,9 @@ pub(crate) fn select_spendable_sapling_notes<P: consensus::Parameters>(
let mut stmt_select_notes = conn.prepare_cached(
"WITH eligible AS (
SELECT
id_note, txid, output_index, diversifier, value, rcm, commitment_tree_position,
sapling_received_notes.id AS id, txid, output_index, diversifier, value, rcm, commitment_tree_position,
SUM(value)
OVER (PARTITION BY sapling_received_notes.account_id, spent ORDER BY id_note) AS so_far,
OVER (PARTITION BY sapling_received_notes.account_id, spent ORDER BY sapling_received_notes.id) AS so_far,
accounts.uvk as uvk, recipient_key_scope
FROM sapling_received_notes
INNER JOIN accounts on accounts.id = sapling_received_notes.account_id
Expand All @@ -279,7 +279,7 @@ pub(crate) fn select_spendable_sapling_notes<P: consensus::Parameters>(
AND commitment_tree_position IS NOT NULL
AND spent IS NULL
AND transactions.block <= :anchor_height
AND id_note NOT IN rarray(:exclude)
AND sapling_received_notes.id NOT IN rarray(:exclude)
AND NOT EXISTS (
SELECT 1 FROM v_sapling_shard_unscanned_ranges unscanned
-- select all the unscanned ranges involving the shard containing this note
Expand All @@ -291,10 +291,10 @@ pub(crate) fn select_spendable_sapling_notes<P: consensus::Parameters>(
AND unscanned.block_range_end > :wallet_birthday
)
)
SELECT id_note, txid, output_index, diversifier, value, rcm, commitment_tree_position, uvk, recipient_key_scope
SELECT id, txid, output_index, diversifier, value, rcm, commitment_tree_position, uvk, recipient_key_scope
FROM eligible WHERE so_far < :target_value
UNION
SELECT id_note, txid, output_index, diversifier, value, rcm, commitment_tree_position, uvk, recipient_key_scope
SELECT id, txid, output_index, diversifier, value, rcm, commitment_tree_position, uvk, recipient_key_scope
FROM (SELECT * from eligible WHERE so_far >= :target_value LIMIT 1)",
)?;

Expand Down Expand Up @@ -326,7 +326,7 @@ pub(crate) fn get_sapling_nullifiers(
) -> Result<Vec<(AccountId, Nullifier)>, SqliteClientError> {
// Get the nullifiers for the notes we are tracking
let mut stmt_fetch_nullifiers = conn.prepare(
"SELECT rn.id_note, rn.account_id, rn.nf, tx.block as block
"SELECT rn.id, rn.account_id, rn.nf, tx.block as block
FROM sapling_received_notes rn
LEFT OUTER JOIN transactions tx
ON tx.id_tx = rn.spent
Expand All @@ -349,7 +349,7 @@ pub(crate) fn get_all_sapling_nullifiers(
) -> Result<Vec<(AccountId, Nullifier)>, SqliteClientError> {
// Get the nullifiers for the notes we are tracking
let mut stmt_fetch_nullifiers = conn.prepare(
"SELECT rn.id_note, rn.account_id, rn.nf
"SELECT rn.id, rn.account_id, rn.nf
FROM sapling_received_notes rn
WHERE nf IS NOT NULL",
)?;
Expand Down

0 comments on commit 6079fe5

Please sign in to comment.