Skip to content

Commit fbe456e

Browse files
committed
tapdb: add script_keys.key_type column
With this commit we add a new numeric type for the type of a script key. This will be a Golang enum/const that's going to be assigned manually when declaring a key as known. For existing keys, we'll add a new mechanism in the following commits that runs on startup after we detect a SQL migration was applied that will attempt the detection of the type of those keys.
1 parent 45ae387 commit fbe456e

File tree

10 files changed

+134
-38
lines changed

10 files changed

+134
-38
lines changed

tapdb/migrations.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ const (
2222
// daemon.
2323
//
2424
// NOTE: This MUST be updated when a new migration is added.
25-
LatestMigrationVersion = 30
25+
LatestMigrationVersion = 31
2626
)
2727

2828
// MigrationTarget is a functional option that can be passed to applyMigrations

tapdb/sqlc/addrs.sql.go

Lines changed: 4 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tapdb/sqlc/assets.sql.go

Lines changed: 83 additions & 18 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ALTER TABLE script_keys DROP COLUMN key_type;
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
-- The key_type column is used to store the type of key that is stored in the
2+
-- script_keys table. The type is a Golang numeric type that will have values
3+
-- such as BIP-0086, script path with custom (externally defined) script, script
4+
-- path with Taproot Asset Channel related script, etc. The NULL value
5+
-- will mean the type is not known. Existing script keys at the time of this
6+
-- migration will be updated at startup after the migration is applied.
7+
ALTER TABLE script_keys ADD COLUMN key_type SMALLINT;

tapdb/sqlc/models.go

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tapdb/sqlc/querier.go

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tapdb/sqlc/queries/assets.sql

Lines changed: 33 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ SELECT seedling_id, asset_name, asset_type, asset_version, asset_supply,
141141
script_keys.tweak AS script_key_tweak,
142142
script_keys.tweaked_script_key,
143143
script_keys.declared_known AS script_key_declared_known,
144+
script_keys.key_type AS script_key_type,
144145
internal_keys.raw_key AS script_key_raw,
145146
internal_keys.key_family AS script_key_fam,
146147
internal_keys.key_index AS script_key_index,
@@ -855,27 +856,37 @@ WHERE txid = $1;
855856

856857
-- name: UpsertScriptKey :one
857858
INSERT INTO script_keys (
858-
internal_key_id, tweaked_script_key, tweak, declared_known
859+
internal_key_id, tweaked_script_key, tweak, declared_known, key_type
859860
) VALUES (
860-
$1, $2, $3, $4
861+
$1, $2, $3, $4, $5
861862
) ON CONFLICT (tweaked_script_key)
862-
-- Overwrite the declared_known and tweak fields if they were previously
863-
-- unknown.
863+
-- Overwrite the declared_known, key_type and tweak fields if they were
864+
-- previously unknown.
864865
DO UPDATE SET
865866
tweaked_script_key = EXCLUDED.tweaked_script_key,
866867
-- If the script key was previously unknown, we'll update to the new
867-
-- value.
868-
declared_known = CASE
869-
WHEN script_keys.declared_known IS NULL OR script_keys.declared_known = FALSE
870-
THEN COALESCE(EXCLUDED.declared_known, script_keys.declared_known)
871-
ELSE script_keys.declared_known
872-
END,
868+
-- value, if that is non-NULL.
869+
declared_known =
870+
CASE
871+
WHEN COALESCE(script_keys.declared_known, FALSE) = FALSE
872+
THEN COALESCE(EXCLUDED.declared_known, script_keys.declared_known)
873+
ELSE script_keys.declared_known
874+
END,
873875
-- If the tweak was previously unknown, we'll update to the new value.
874-
tweak = CASE
875-
WHEN script_keys.tweak IS NULL
876-
THEN COALESCE(EXCLUDED.tweak, script_keys.tweak)
877-
ELSE script_keys.tweak
878-
END
876+
tweak =
877+
CASE
878+
WHEN script_keys.tweak IS NULL
879+
THEN COALESCE(EXCLUDED.tweak, script_keys.tweak)
880+
ELSE script_keys.tweak
881+
END,
882+
-- We only overwrite the key type with a value that does not mean
883+
-- "unknown" (0 or NULL).
884+
key_type =
885+
CASE
886+
WHEN COALESCE(EXCLUDED.key_type, 0) != 0
887+
THEN EXCLUDED.key_type
888+
ELSE script_keys.key_type
889+
END
879890
RETURNING script_key_id;
880891

881892
-- name: FetchScriptKeyIDByTweakedKey :one
@@ -890,6 +901,13 @@ JOIN internal_keys
890901
ON script_keys.internal_key_id = internal_keys.key_id
891902
WHERE script_keys.tweaked_script_key = $1;
892903

904+
-- name: FetchUnknownTypeScriptKeys :many
905+
SELECT sqlc.embed(script_keys), sqlc.embed(internal_keys)
906+
FROM script_keys
907+
JOIN internal_keys
908+
ON script_keys.internal_key_id = internal_keys.key_id
909+
WHERE script_keys.key_type IS NULL;
910+
893911
-- name: FetchInternalKeyLocator :one
894912
SELECT key_family, key_index
895913
FROM internal_keys

tapdb/sqlc/schemas/generated_schema.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -653,7 +653,7 @@ CREATE TABLE script_keys (
653653
-- An optional tweak for the script_key. If NULL, the raw_key may be
654654
-- tweaked BIP-0086 style.
655655
tweak BLOB
656-
, declared_known BOOLEAN);
656+
, declared_known BOOLEAN, key_type SMALLINT);
657657

658658
CREATE TABLE tapscript_edges (
659659
edge_id INTEGER PRIMARY KEY,

tapdb/sqlc/transfers.sql.go

Lines changed: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)