From c96f36961231b2f730c65d34ce0c9c837ca1162a Mon Sep 17 00:00:00 2001 From: Oliver Gugger Date: Wed, 27 Nov 2024 16:00:27 +0100 Subject: [PATCH] tapdb: allow tweak to be upserted This fixes a bug where if we ever inserted a script key before knowing its tweak, we could never update the tweak later on when we learn it. This meant that such keys would be seen as BIP-086 keys, even though we later learn they aren't. --- tapdb/sqlc/assets.sql.go | 12 +++++++++--- tapdb/sqlc/queries/assets.sql | 12 +++++++++--- 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/tapdb/sqlc/assets.sql.go b/tapdb/sqlc/assets.sql.go index fa1ca8f87..a518ebbc3 100644 --- a/tapdb/sqlc/assets.sql.go +++ b/tapdb/sqlc/assets.sql.go @@ -2878,8 +2878,8 @@ INSERT INTO script_keys ( ) VALUES ( $1, $2, $3, $4 ) ON CONFLICT (tweaked_script_key) - -- As a NOP, we just set the script key to the one that triggered the - -- conflict. + -- Overwrite the declared_known and tweak fields if they were previously + -- unknown. DO UPDATE SET tweaked_script_key = EXCLUDED.tweaked_script_key, -- If the script key was previously unknown, we'll update to the new @@ -2888,7 +2888,13 @@ INSERT INTO script_keys ( WHEN script_keys.declared_known IS NULL OR script_keys.declared_known = FALSE THEN COALESCE(EXCLUDED.declared_known, script_keys.declared_known) ELSE script_keys.declared_known - END + END, + -- If the tweak was previously unknown, we'll update to the new value. + tweak = CASE + WHEN script_keys.tweak IS NULL + THEN COALESCE(EXCLUDED.tweak, script_keys.tweak) + ELSE script_keys.tweak + END RETURNING script_key_id ` diff --git a/tapdb/sqlc/queries/assets.sql b/tapdb/sqlc/queries/assets.sql index a16302d27..78bfa313b 100644 --- a/tapdb/sqlc/queries/assets.sql +++ b/tapdb/sqlc/queries/assets.sql @@ -852,8 +852,8 @@ INSERT INTO script_keys ( ) VALUES ( $1, $2, $3, $4 ) ON CONFLICT (tweaked_script_key) - -- As a NOP, we just set the script key to the one that triggered the - -- conflict. + -- Overwrite the declared_known and tweak fields if they were previously + -- unknown. DO UPDATE SET tweaked_script_key = EXCLUDED.tweaked_script_key, -- If the script key was previously unknown, we'll update to the new @@ -862,7 +862,13 @@ INSERT INTO script_keys ( WHEN script_keys.declared_known IS NULL OR script_keys.declared_known = FALSE THEN COALESCE(EXCLUDED.declared_known, script_keys.declared_known) ELSE script_keys.declared_known - END + END, + -- If the tweak was previously unknown, we'll update to the new value. + tweak = CASE + WHEN script_keys.tweak IS NULL + THEN COALESCE(EXCLUDED.tweak, script_keys.tweak) + ELSE script_keys.tweak + END RETURNING script_key_id; -- name: FetchScriptKeyIDByTweakedKey :one