Skip to content

Commit 3f011d5

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 c14ccf4 commit 3f011d5

File tree

10 files changed

+135
-38
lines changed

10 files changed

+135
-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 = 32
25+
LatestMigrationVersion = 33
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: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
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 will be inspected by
6+
-- the post-migration step Golang code that determines the type of each script
7+
-- key.
8+
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,
@@ -864,27 +865,37 @@ WHERE txid = $1;
864865

865866
-- name: UpsertScriptKey :one
866867
INSERT INTO script_keys (
867-
internal_key_id, tweaked_script_key, tweak, declared_known
868+
internal_key_id, tweaked_script_key, tweak, declared_known, key_type
868869
) VALUES (
869-
$1, $2, $3, $4
870+
$1, $2, $3, $4, $5
870871
) ON CONFLICT (tweaked_script_key)
871-
-- Overwrite the declared_known and tweak fields if they were previously
872-
-- unknown.
872+
-- Overwrite the declared_known, key_type and tweak fields if they were
873+
-- previously unknown.
873874
DO UPDATE SET
874875
tweaked_script_key = EXCLUDED.tweaked_script_key,
875876
-- If the script key was previously unknown, we'll update to the new
876-
-- value.
877-
declared_known = CASE
878-
WHEN script_keys.declared_known IS NULL OR script_keys.declared_known = FALSE
879-
THEN COALESCE(EXCLUDED.declared_known, script_keys.declared_known)
880-
ELSE script_keys.declared_known
881-
END,
877+
-- value, if that is non-NULL.
878+
declared_known =
879+
CASE
880+
WHEN COALESCE(script_keys.declared_known, FALSE) = FALSE
881+
THEN COALESCE(EXCLUDED.declared_known, script_keys.declared_known)
882+
ELSE script_keys.declared_known
883+
END,
882884
-- If the tweak was previously unknown, we'll update to the new value.
883-
tweak = CASE
884-
WHEN script_keys.tweak IS NULL
885-
THEN COALESCE(EXCLUDED.tweak, script_keys.tweak)
886-
ELSE script_keys.tweak
887-
END
885+
tweak =
886+
CASE
887+
WHEN script_keys.tweak IS NULL
888+
THEN COALESCE(EXCLUDED.tweak, script_keys.tweak)
889+
ELSE script_keys.tweak
890+
END,
891+
-- We only overwrite the key type with a value that does not mean
892+
-- "unknown" (0 or NULL).
893+
key_type =
894+
CASE
895+
WHEN COALESCE(EXCLUDED.key_type, 0) != 0
896+
THEN EXCLUDED.key_type
897+
ELSE script_keys.key_type
898+
END
888899
RETURNING script_key_id;
889900

890901
-- name: FetchScriptKeyIDByTweakedKey :one
@@ -899,6 +910,13 @@ JOIN internal_keys
899910
ON script_keys.internal_key_id = internal_keys.key_id
900911
WHERE script_keys.tweaked_script_key = $1;
901912

913+
-- name: FetchUnknownTypeScriptKeys :many
914+
SELECT sqlc.embed(script_keys), sqlc.embed(internal_keys)
915+
FROM script_keys
916+
JOIN internal_keys
917+
ON script_keys.internal_key_id = internal_keys.key_id
918+
WHERE script_keys.key_type IS NULL;
919+
902920
-- name: FetchInternalKeyLocator :one
903921
SELECT key_family, key_index
904922
FROM internal_keys

tapdb/sqlc/schemas/generated_schema.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -716,7 +716,7 @@ CREATE TABLE script_keys (
716716
-- An optional tweak for the script_key. If NULL, the raw_key may be
717717
-- tweaked BIP-0086 style.
718718
tweak BLOB
719-
, declared_known BOOLEAN);
719+
, declared_known BOOLEAN, key_type SMALLINT);
720720

721721
CREATE INDEX status_idx ON addr_events(status);
722722

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)