Skip to content

Commit 38b28f2

Browse files
committed
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.
1 parent 3451af2 commit 38b28f2

File tree

3 files changed

+68
-6
lines changed

3 files changed

+68
-6
lines changed

tapdb/addrs_test.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -695,6 +695,14 @@ func assertKeyKnowledge(t *testing.T, ctx context.Context,
695695
require.Equal(t, known, dbScriptKey.DeclaredKnown)
696696
}
697697

698+
func assertTweak(t *testing.T, ctx context.Context, addrBook *TapAddressBook,
699+
scriptKey asset.ScriptKey, tweak []byte) {
700+
701+
dbScriptKey, err := addrBook.FetchScriptKey(ctx, scriptKey.PubKey)
702+
require.NoError(t, err)
703+
require.Equal(t, tweak, dbScriptKey.Tweak)
704+
}
705+
698706
// TestScriptKeyKnownUpsert tests that we can insert a script key, then insert
699707
// it again declared as known.
700708
func TestScriptKeyKnownUpsert(t *testing.T) {
@@ -757,3 +765,45 @@ func TestScriptKeyKnownUpsert(t *testing.T) {
757765
assertKeyKnowledge(t, ctx, addrBook, scriptKey, known)
758766
})
759767
}
768+
769+
// TestScriptKeyTweakUpsert tests that we can insert a script key, then insert
770+
// it again when we know the tweak for it.
771+
func TestScriptKeyTweakUpsert(t *testing.T) {
772+
t.Parallel()
773+
774+
// First, make a new addr book instance we'll use in the test below.
775+
testClock := clock.NewTestClock(time.Now())
776+
addrBook, _ := newAddrBook(t, testClock)
777+
778+
ctx := context.Background()
779+
780+
// In this test, we insert the tweak as NULL, and make sure we overwrite
781+
// it with an actual value again later.
782+
t.Run("null_to_value", func(t *testing.T) {
783+
known := false
784+
scriptKey := randScriptKey(t)
785+
scriptKey.Tweak = nil
786+
787+
// We'll insert a random script key into the database. We won't
788+
// declare it as known though, and it doesn't have the tweak.
789+
err := addrBook.InsertScriptKey(ctx, scriptKey, known)
790+
require.NoError(t, err)
791+
792+
// We'll fetch the script key and confirm that it's not known.
793+
assertKeyKnowledge(t, ctx, addrBook, scriptKey, known)
794+
assertTweak(t, ctx, addrBook, scriptKey, nil)
795+
796+
known = true
797+
randTweak := test.RandBytes(32)
798+
scriptKey.Tweak = randTweak
799+
800+
// We'll now insert it again, but this time declare it as known
801+
// and also know the tweak.
802+
err = addrBook.InsertScriptKey(ctx, scriptKey, known)
803+
require.NoError(t, err)
804+
805+
// We'll fetch the script key and confirm that it's known.
806+
assertKeyKnowledge(t, ctx, addrBook, scriptKey, known)
807+
assertTweak(t, ctx, addrBook, scriptKey, randTweak)
808+
})
809+
}

tapdb/sqlc/assets.sql.go

Lines changed: 9 additions & 3 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: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -852,8 +852,8 @@ INSERT INTO script_keys (
852852
) VALUES (
853853
$1, $2, $3, $4
854854
) ON CONFLICT (tweaked_script_key)
855-
-- As a NOP, we just set the script key to the one that triggered the
856-
-- conflict.
855+
-- Overwrite the declared_known and tweak fields if they were previously
856+
-- unknown.
857857
DO UPDATE SET
858858
tweaked_script_key = EXCLUDED.tweaked_script_key,
859859
-- If the script key was previously unknown, we'll update to the new
@@ -862,7 +862,13 @@ INSERT INTO script_keys (
862862
WHEN script_keys.declared_known IS NULL OR script_keys.declared_known = FALSE
863863
THEN COALESCE(EXCLUDED.declared_known, script_keys.declared_known)
864864
ELSE script_keys.declared_known
865-
END
865+
END,
866+
-- If the tweak was previously unknown, we'll update to the new value.
867+
tweak = CASE
868+
WHEN script_keys.tweak IS NULL
869+
THEN COALESCE(EXCLUDED.tweak, script_keys.tweak)
870+
ELSE script_keys.tweak
871+
END
866872
RETURNING script_key_id;
867873

868874
-- name: FetchScriptKeyIDByTweakedKey :one

0 commit comments

Comments
 (0)