Skip to content

Commit

Permalink
[wip] Ensure there are no duplicated script_pubkeys in sqlite
Browse files Browse the repository at this point in the history
Add a `UNIQUE` constraint on the script_pubkeys table so that it doesn't
grow constantly when caching new addresses.

Fixes #801
  • Loading branch information
afilini committed Nov 26, 2022
1 parent 1c95ca3 commit c51d544
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/database/sqlite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,10 @@ static MIGRATIONS: &[&str] = &[
"CREATE TABLE utxos (value INTEGER, keychain TEXT, vout INTEGER, txid BLOB, script BLOB, is_spent BOOLEAN DEFAULT 0);",
"INSERT INTO utxos SELECT value, keychain, vout, txid, script, is_spent FROM utxos_old;",
"DROP TABLE utxos_old;",
"CREATE UNIQUE INDEX idx_utxos_txid_vout ON utxos(txid, vout);"
"CREATE UNIQUE INDEX idx_utxos_txid_vout ON utxos(txid, vout);",
// Fix issue https://github.com/bitcoindevkit/bdk/issues/801: drop duplicated script_pubkeys
// TODO "",
"CREATE UNIQUE INDEX idx_script_pks_unique ON script_pubkeys(keychain, child);",
];

/// Sqlite database stored on filesystem
Expand Down Expand Up @@ -88,7 +91,7 @@ impl SqliteDatabase {
child: u32,
script: &[u8],
) -> Result<i64, Error> {
let mut statement = self.connection.prepare_cached("INSERT INTO script_pubkeys (keychain, child, script) VALUES (:keychain, :child, :script)")?;
let mut statement = self.connection.prepare_cached("INSERT OR REPLACE INTO script_pubkeys (keychain, child, script) VALUES (:keychain, :child, :script)")?;
statement.execute(named_params! {
":keychain": keychain,
":child": child,
Expand Down

0 comments on commit c51d544

Please sign in to comment.