Skip to content

Commit

Permalink
zcash_client_sqlite: Move pinned indices to wallet::db module
Browse files Browse the repository at this point in the history
  • Loading branch information
str4d committed Jun 18, 2024
1 parent 9c98a64 commit 3599c6a
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 24 deletions.
35 changes: 35 additions & 0 deletions zcash_client_sqlite/src/wallet/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ CREATE TABLE "accounts" (
)
)
)"#;
pub(super) const INDEX_ACCOUNTS_UFVK: &str =
r#"CREATE UNIQUE INDEX accounts_ufvk ON "accounts" (ufvk)"#;
pub(super) const INDEX_ACCOUNTS_UIVK: &str =
r#"CREATE UNIQUE INDEX accounts_uivk ON "accounts" (uivk)"#;
pub(super) const INDEX_HD_ACCOUNT: &str =
r#"CREATE UNIQUE INDEX hd_account ON "accounts" (hd_seed_fingerprint, hd_account_index)"#;

pub(super) const TABLE_ADDRESSES: &str = r#"
CREATE TABLE "addresses" (
Expand All @@ -44,6 +50,10 @@ CREATE TABLE "addresses" (
FOREIGN KEY (account_id) REFERENCES accounts(id),
CONSTRAINT diversification UNIQUE (account_id, diversifier_index_be)
)"#;
pub(super) const INDEX_ADDRESSES_ACCOUNTS: &str = r#"
CREATE INDEX "addresses_accounts" ON "addresses" (
"account_id" ASC
)"#;

pub(super) const TABLE_BLOCKS: &str = "
CREATE TABLE blocks (
Expand Down Expand Up @@ -87,6 +97,14 @@ CREATE TABLE "sapling_received_notes" (
FOREIGN KEY (account_id) REFERENCES accounts(id),
CONSTRAINT tx_output UNIQUE (tx, output_index)
)"#;
pub(super) const INDEX_SAPLING_RECEIVED_NOTES_ACCOUNT: &str = r#"
CREATE INDEX "sapling_received_notes_account" ON "sapling_received_notes" (
"account_id" ASC
)"#;
pub(super) const INDEX_SAPLING_RECEIVED_NOTES_TX: &str = r#"
CREATE INDEX "sapling_received_notes_tx" ON "sapling_received_notes" (
"tx" ASC
)"#;

pub(super) const TABLE_SAPLING_RECEIVED_NOTE_SPENDS: &str = "
CREATE TABLE sapling_received_note_spends (
Expand Down Expand Up @@ -120,6 +138,14 @@ CREATE TABLE orchard_received_notes (
FOREIGN KEY (account_id) REFERENCES accounts(id),
CONSTRAINT tx_output UNIQUE (tx, action_index)
)";
pub(super) const INDEX_ORCHARD_RECEIVED_NOTES_ACCOUNT: &str = r#"
CREATE INDEX orchard_received_notes_account ON orchard_received_notes (
account_id ASC
)"#;
pub(super) const INDEX_ORCHARD_RECEIVED_NOTES_TX: &str = r#"
CREATE INDEX orchard_received_notes_tx ON orchard_received_notes (
tx ASC
)"#;

pub(super) const TABLE_ORCHARD_RECEIVED_NOTE_SPENDS: &str = "
CREATE TABLE orchard_received_note_spends (
Expand Down Expand Up @@ -147,6 +173,8 @@ CREATE TABLE "utxos" (
FOREIGN KEY (received_by_account_id) REFERENCES accounts(id),
CONSTRAINT tx_outpoint UNIQUE (prevout_txid, prevout_idx)
)"#;
pub(super) const INDEX_UTXOS_RECEIVED_BY_ACCOUNT: &str =
r#"CREATE INDEX utxos_received_by_account ON "utxos" (received_by_account_id)"#;

pub(super) const TABLE_TRANSPARENT_RECEIVED_OUTPUT_SPENDS: &str = "
CREATE TABLE transparent_received_output_spends (
Expand Down Expand Up @@ -180,6 +208,11 @@ CREATE TABLE "sent_notes" (
(to_address IS NOT NULL) OR (to_account_id IS NOT NULL)
)
)"#;
pub(super) const INDEX_SENT_NOTES_FROM_ACCOUNT: &str =
r#"CREATE INDEX sent_notes_from_account ON "sent_notes" (from_account_id)"#;
pub(super) const INDEX_SENT_NOTES_TO_ACCOUNT: &str =
r#"CREATE INDEX sent_notes_to_account ON "sent_notes" (to_account_id)"#;
pub(super) const INDEX_SENT_NOTES_TX: &str = r#"CREATE INDEX sent_notes_tx ON "sent_notes" (tx)"#;

//
// State for shard trees
Expand Down Expand Up @@ -288,6 +321,8 @@ CREATE TABLE nullifier_map (
ON UPDATE RESTRICT,
CONSTRAINT nf_uniq UNIQUE (spend_pool, nf)
)";
pub(super) const INDEX_NF_MAP_LOCATOR_IDX: &str =
r#"CREATE INDEX nf_map_locator_idx ON nullifier_map(block_height, tx_index)"#;

//
// Internal tables
Expand Down
38 changes: 14 additions & 24 deletions zcash_client_sqlite/src/wallet/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -414,29 +414,19 @@ mod tests {
}

let expected_indices = vec![
r#"CREATE UNIQUE INDEX accounts_ufvk ON "accounts" (ufvk)"#,
r#"CREATE UNIQUE INDEX accounts_uivk ON "accounts" (uivk)"#,
r#"CREATE UNIQUE INDEX hd_account ON "accounts" (hd_seed_fingerprint, hd_account_index)"#,
r#"CREATE INDEX "addresses_accounts" ON "addresses" (
"account_id" ASC
)"#,
r#"CREATE INDEX nf_map_locator_idx ON nullifier_map(block_height, tx_index)"#,
r#"CREATE INDEX orchard_received_notes_account ON orchard_received_notes (
account_id ASC
)"#,
r#"CREATE INDEX orchard_received_notes_tx ON orchard_received_notes (
tx ASC
)"#,
r#"CREATE INDEX "sapling_received_notes_account" ON "sapling_received_notes" (
"account_id" ASC
)"#,
r#"CREATE INDEX "sapling_received_notes_tx" ON "sapling_received_notes" (
"tx" ASC
)"#,
r#"CREATE INDEX sent_notes_from_account ON "sent_notes" (from_account_id)"#,
r#"CREATE INDEX sent_notes_to_account ON "sent_notes" (to_account_id)"#,
r#"CREATE INDEX sent_notes_tx ON "sent_notes" (tx)"#,
r#"CREATE INDEX utxos_received_by_account ON "utxos" (received_by_account_id)"#,
db::INDEX_ACCOUNTS_UFVK,
db::INDEX_ACCOUNTS_UIVK,
db::INDEX_HD_ACCOUNT,
db::INDEX_ADDRESSES_ACCOUNTS,
db::INDEX_NF_MAP_LOCATOR_IDX,
db::INDEX_ORCHARD_RECEIVED_NOTES_ACCOUNT,
db::INDEX_ORCHARD_RECEIVED_NOTES_TX,
db::INDEX_SAPLING_RECEIVED_NOTES_ACCOUNT,
db::INDEX_SAPLING_RECEIVED_NOTES_TX,
db::INDEX_SENT_NOTES_FROM_ACCOUNT,
db::INDEX_SENT_NOTES_TO_ACCOUNT,
db::INDEX_SENT_NOTES_TX,
db::INDEX_UTXOS_RECEIVED_BY_ACCOUNT,
];
let mut indices_query = st
.wallet()
Expand All @@ -449,7 +439,7 @@ mod tests {
let sql: String = row.get(0).unwrap();
assert_eq!(
re.replace_all(&sql, " "),
re.replace_all(expected_indices[expected_idx], " ")
re.replace_all(expected_indices[expected_idx], " ").trim(),
);
expected_idx += 1;
}
Expand Down

0 comments on commit 3599c6a

Please sign in to comment.