Skip to content

Commit

Permalink
view: add upsert behavior to record_block to respect key uniqueness c…
Browse files Browse the repository at this point in the history
…onstraints for duplicate commitments
  • Loading branch information
aubrika committed May 30, 2024
1 parent 4f40710 commit efa196a
Showing 1 changed file with 25 additions and 5 deletions.
30 changes: 25 additions & 5 deletions crates/view/src/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1222,7 +1222,12 @@ impl Storage {
dbtx.execute(
"INSERT INTO notes (note_commitment, address, amount, asset_id, rseed)
VALUES (?1, ?2, ?3, ?4, ?5)
ON CONFLICT DO NOTHING",
ON CONFLICT (note_commitment)
DO UPDATE SET
address = excluded.address,
amount = excluded.amount,
asset_id = excluded.asset_id,
rseed = excluded.rseed",
(note_commitment, address, amount, asset_id, rseed),
)?;

Expand Down Expand Up @@ -1418,7 +1423,15 @@ impl Storage {
dbtx.execute(
"INSERT INTO spendable_notes
(note_commitment, nullifier, position, height_created, address_index, source, height_spent, tx_hash)
VALUES (?1, ?2, ?3, ?4, ?5, ?6, NULL, ?7)",
VALUES (?1, ?2, ?3, ?4, ?5, ?6, NULL, ?7)
ON CONFLICT (note_commitment)
DO UPDATE SET nullifier = excluded.nullifier,
position = excluded.position,
height_created = excluded.height_created,
address_index = excluded.address_index,
source = excluded.source,
height_spent = excluded.height_spent,
tx_hash = excluded.tx_hash",
(
&note_commitment,
&nullifier,
Expand All @@ -1443,7 +1456,14 @@ impl Storage {

dbtx.execute(
"INSERT INTO swaps (swap_commitment, swap, position, nullifier, output_data, height_claimed, source)
VALUES (?1, ?2, ?3, ?4, ?5, NULL, ?6)",
VALUES (?1, ?2, ?3, ?4, ?5, NULL, ?6)
ON CONFLICT (swap_commitment)
DO UPDATE SET swap = excluded.swap,
position = excluded.position,
nullifier = excluded.nullifier,
output_data = excluded.output_data,
height_claimed = excluded.height_claimed,
source = excluded.source",
(
&swap_commitment,
&swap_bytes,
Expand Down Expand Up @@ -1539,15 +1559,15 @@ impl Storage {
tracing::debug!(tx_hash = ?hex::encode(tx_hash), "recording extended transaction");

dbtx.execute(
"INSERT INTO tx (tx_hash, tx_bytes, block_height, return_address) VALUES (?1, ?2, ?3, ?4)",
"INSERT OR IGNORE INTO tx (tx_hash, tx_bytes, block_height, return_address) VALUES (?1, ?2, ?3, ?4)",
(&tx_hash, &tx_bytes, tx_block_height, return_address),
)?;

// Associate all of the spent nullifiers with the transaction by hash.
for nf in transaction.spent_nullifiers() {
let nf_bytes = nf.0.to_bytes().to_vec();
dbtx.execute(
"INSERT INTO tx_by_nullifier (nullifier, tx_hash) VALUES (?1, ?2)",
"INSERT OR IGNORE INTO tx_by_nullifier (nullifier, tx_hash) VALUES (?1, ?2)",
(&nf_bytes, &tx_hash),
)?;
}
Expand Down

0 comments on commit efa196a

Please sign in to comment.