Skip to content

Commit c27ea4d

Browse files
committed
tapdb: update InsertScriptKey to allow flipping known to true
In this commit, we update `InsertScriptKey` to allow flipping known to true, if it was false before. This is useful as at times a script key from a smart contract might have been inserting on disk, but with declared known as false. Then if we tried to insert it again, with known as true, the upsert logic would end up making no change on disk.
1 parent 9c9439c commit c27ea4d

File tree

4 files changed

+67
-3
lines changed

4 files changed

+67
-3
lines changed

tapdb/addrs.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -681,14 +681,15 @@ func (t *TapAddressBook) InsertScriptKey(ctx context.Context,
681681
scriptKey asset.ScriptKey, declaredKnown bool) error {
682682

683683
var writeTxOpts AddrBookTxOptions
684-
return t.db.ExecTx(ctx, &writeTxOpts, func(q AddrBook) error {
684+
err := t.db.ExecTx(ctx, &writeTxOpts, func(q AddrBook) error {
685685
internalKeyID, err := insertInternalKey(
686686
ctx, q, scriptKey.RawKey,
687687
)
688688
if err != nil {
689689
return fmt.Errorf("error inserting internal key: %w",
690690
err)
691691
}
692+
692693
_, err = q.UpsertScriptKey(ctx, NewScriptKey{
693694
InternalKeyID: internalKeyID,
694695
TweakedScriptKey: scriptKey.PubKey.SerializeCompressed(),
@@ -697,6 +698,11 @@ func (t *TapAddressBook) InsertScriptKey(ctx context.Context,
697698
})
698699
return err
699700
})
701+
if err != nil {
702+
return fmt.Errorf("error inserting script key: %w", err)
703+
}
704+
705+
return nil
700706
}
701707

702708
// GetOrCreateEvent creates a new address event for the given status, address

tapdb/addrs_test.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,11 @@ import (
1111
"github.com/btcsuite/btcd/wire"
1212
"github.com/lightninglabs/lndclient"
1313
"github.com/lightninglabs/taproot-assets/address"
14+
"github.com/lightninglabs/taproot-assets/asset"
1415
"github.com/lightninglabs/taproot-assets/internal/test"
1516
"github.com/lightninglabs/taproot-assets/tapdb/sqlc"
1617
"github.com/lightningnetwork/lnd/clock"
18+
"github.com/lightningnetwork/lnd/keychain"
1719
"github.com/lightningnetwork/lnd/lnrpc"
1820
"github.com/stretchr/testify/require"
1921
)
@@ -644,3 +646,45 @@ func TestAddressEventQuery(t *testing.T) {
644646
})
645647
}
646648
}
649+
650+
// TestScriptKeyKnownUpsert tests that we can insert a script key, then insert
651+
// it again declared as known.
652+
func TestScriptKeyKnownUpsert(t *testing.T) {
653+
t.Parallel()
654+
655+
// First, make a new addr book instance we'll use in the test below.
656+
testClock := clock.NewTestClock(time.Now())
657+
addrBook, _ := newAddrBook(t, testClock)
658+
659+
ctx := context.Background()
660+
661+
// Make a random script key that we'll use to insert into the database.
662+
scriptKey := asset.RandScriptKey(t)
663+
scriptKey.TweakedScriptKey = &asset.TweakedScriptKey{
664+
RawKey: keychain.KeyDescriptor{
665+
PubKey: asset.RandScriptKey(t).PubKey,
666+
},
667+
}
668+
669+
// We'll insert a random script key into the database. We won't declare
670+
// it as known though.
671+
known := false
672+
err := addrBook.InsertScriptKey(ctx, scriptKey, known)
673+
require.NoError(t, err)
674+
675+
// We'll fetch the script key and confirm that it's not known.
676+
dbScriptKey, err := addrBook.FetchScriptKey(ctx, scriptKey.PubKey)
677+
require.NoError(t, err)
678+
require.False(t, dbScriptKey.DeclaredKnown)
679+
680+
known = true
681+
682+
// We'll now insert it again, but this time declare it as known.
683+
err = addrBook.InsertScriptKey(ctx, scriptKey, known)
684+
require.NoError(t, err)
685+
686+
// We'll fetch the script key and confirm that it's known.
687+
dbScriptKey, err = addrBook.FetchScriptKey(ctx, scriptKey.PubKey)
688+
require.NoError(t, err)
689+
require.True(t, dbScriptKey.DeclaredKnown)
690+
}

tapdb/sqlc/assets.sql.go

Lines changed: 8 additions & 1 deletion
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: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -854,7 +854,14 @@ INSERT INTO script_keys (
854854
) ON CONFLICT (tweaked_script_key)
855855
-- As a NOP, we just set the script key to the one that triggered the
856856
-- conflict.
857-
DO UPDATE SET tweaked_script_key = EXCLUDED.tweaked_script_key
857+
DO UPDATE SET
858+
tweaked_script_key = EXCLUDED.tweaked_script_key,
859+
-- If the script key was previously unknown, we'll update to the new
860+
-- value.
861+
declared_known = CASE
862+
WHEN script_keys.declared_known = FALSE THEN EXCLUDED.declared_known
863+
ELSE script_keys.declared_known
864+
END
858865
RETURNING script_key_id;
859866

860867
-- name: FetchScriptKeyIDByTweakedKey :one

0 commit comments

Comments
 (0)