Skip to content

Commit 5922abe

Browse files
committed
tapdb: change script key exclusion mechanism
With an explicit type for a script key now being used, we can turn the key based matching into a type based matching, which will also be more generic for unique funding script keys that are required for grouped asset channel funding.
1 parent d6905ed commit 5922abe

File tree

4 files changed

+92
-43
lines changed

4 files changed

+92
-43
lines changed

tapdb/assets_store.go

+30-19
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ import (
2323
"github.com/lightninglabs/taproot-assets/tapdb/sqlc"
2424
"github.com/lightninglabs/taproot-assets/tapfreighter"
2525
"github.com/lightninglabs/taproot-assets/tappsbt"
26-
"github.com/lightninglabs/taproot-assets/tapscript"
2726
"github.com/lightningnetwork/lnd/clock"
2827
"github.com/lightningnetwork/lnd/keychain"
2928
)
@@ -968,19 +967,25 @@ func (a *AssetStore) QueryBalancesByAsset(ctx context.Context,
968967
},
969968
}
970969

970+
// We exclude the assets that are specifically used for funding custom
971+
// channels. The balance of those assets is reported through lnd channel
972+
// balance. Those assets are identified by the specific script key type
973+
// for channel keys. We exclude them unless explicitly queried for.
974+
assetBalancesFilter.ExcludeScriptKeyType = sqlInt16(
975+
asset.ScriptKeyScriptPathChannel,
976+
)
977+
971978
// The fn.None option means we don't restrict on script key type at all.
972979
skt.WhenSome(func(t asset.ScriptKeyType) {
973980
assetBalancesFilter.ScriptKeyType = sqlInt16(t)
974-
})
975981

976-
// We exclude the assets that are specifically used for funding custom
977-
// channels. The balance of those assets is reported through lnd channel
978-
// balance. Those assets are identified by the funding script tree for a
979-
// custom channel asset-level script key.
980-
excludeKey := asset.NewScriptKey(
981-
tapscript.NewChannelFundingScriptTree().TaprootKey,
982-
)
983-
assetBalancesFilter.ExcludeKey = excludeKey.PubKey.SerializeCompressed()
982+
// If the user explicitly wants to see the channel related asset
983+
// balances, we need to set the exclude type to NULL.
984+
if t == asset.ScriptKeyScriptPathChannel {
985+
nullValue := sql.NullInt16{}
986+
assetBalancesFilter.ExcludeScriptKeyType = nullValue
987+
}
988+
})
984989

985990
// By default, we only show assets that are not leased.
986991
if !includeLeased {
@@ -1054,19 +1059,25 @@ func (a *AssetStore) QueryAssetBalancesByGroup(ctx context.Context,
10541059
},
10551060
}
10561061

1062+
// We exclude the assets that are specifically used for funding custom
1063+
// channels. The balance of those assets is reported through lnd channel
1064+
// balance. Those assets are identified by the specific script key type
1065+
// for channel keys. We exclude them unless explicitly queried for.
1066+
assetBalancesFilter.ExcludeScriptKeyType = sqlInt16(
1067+
asset.ScriptKeyScriptPathChannel,
1068+
)
1069+
10571070
// The fn.None option means we don't restrict on script key type at all.
10581071
skt.WhenSome(func(t asset.ScriptKeyType) {
10591072
assetBalancesFilter.ScriptKeyType = sqlInt16(t)
1060-
})
10611073

1062-
// We exclude the assets that are specifically used for funding custom
1063-
// channels. The balance of those assets is reported through lnd channel
1064-
// balance. Those assets are identified by the funding script tree for a
1065-
// custom channel asset-level script key.
1066-
excludeKey := asset.NewScriptKey(
1067-
tapscript.NewChannelFundingScriptTree().TaprootKey,
1068-
)
1069-
assetBalancesFilter.ExcludeKey = excludeKey.PubKey.SerializeCompressed()
1074+
// If the user explicitly wants to see the channel related asset
1075+
// balances, we need to set the exclude type to NULL.
1076+
if t == asset.ScriptKeyScriptPathChannel {
1077+
nullValue := sql.NullInt16{}
1078+
assetBalancesFilter.ExcludeScriptKeyType = nullValue
1079+
}
1080+
})
10701081

10711082
// By default, we only show assets that are not leased.
10721083
if !includeLeased {

tapdb/assets_store_test.go

+42-4
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"time"
1212

1313
"github.com/btcsuite/btcd/btcec/v2"
14+
"github.com/btcsuite/btcd/btcec/v2/schnorr"
1415
"github.com/btcsuite/btcd/chaincfg/chainhash"
1516
"github.com/btcsuite/btcd/txscript"
1617
"github.com/btcsuite/btcd/wire"
@@ -2740,23 +2741,33 @@ func TestQueryAssetBalancesCustomChannelFunding(t *testing.T) {
27402741
const numGroups = 1
27412742
assetGen := newAssetGenerator(t, numAssets, numGroups)
27422743

2743-
fundingScriptKey := asset.NewScriptKey(
2744-
tapscript.NewChannelFundingScriptTree().TaprootKey,
2744+
fundingKey := tapscript.NewChannelFundingScriptTree()
2745+
xOnlyKey, _ := schnorr.ParsePubKey(
2746+
schnorr.SerializePubKey(fundingKey.TaprootKey),
27452747
)
2748+
fundingScriptKey := asset.ScriptKey{
2749+
PubKey: xOnlyKey,
2750+
TweakedScriptKey: &asset.TweakedScriptKey{
2751+
RawKey: keychain.KeyDescriptor{
2752+
PubKey: fundingKey.InternalKey,
2753+
},
2754+
Type: asset.ScriptKeyScriptPathChannel,
2755+
},
2756+
}
27462757

27472758
assetDesc := []assetDesc{
27482759
{
27492760
assetGen: assetGen.assetGens[0],
27502761
anchorPoint: assetGen.anchorPoints[0],
27512762
keyGroup: assetGen.groupKeys[0],
2752-
amt: 4,
2763+
amt: 8,
27532764
scriptKey: &fundingScriptKey,
27542765
},
27552766
{
27562767
assetGen: assetGen.assetGens[1],
27572768
anchorPoint: assetGen.anchorPoints[1],
27582769
keyGroup: assetGen.groupKeys[0],
2759-
amt: 4,
2770+
amt: 12,
27602771
},
27612772
}
27622773
assetGen.genAssets(t, assetsStore, assetDesc)
@@ -2787,4 +2798,31 @@ func TestQueryAssetBalancesCustomChannelFunding(t *testing.T) {
27872798
balanceByGroupSum += balance.Balance
27882799
}
27892800
require.Equal(t, assetDesc[1].amt, balanceByGroupSum)
2801+
2802+
// If we explicitly query for channel related script keys, we should get
2803+
// just those assets.
2804+
balances, err = assetsStore.QueryBalancesByAsset(
2805+
ctx, nil, includeLeased,
2806+
fn.Some(asset.ScriptKeyScriptPathChannel),
2807+
)
2808+
require.NoError(t, err)
2809+
balancesByGroup, err = assetsStore.QueryAssetBalancesByGroup(
2810+
ctx, nil, includeLeased,
2811+
fn.Some(asset.ScriptKeyScriptPathChannel),
2812+
)
2813+
require.NoError(t, err)
2814+
require.Len(t, balances, numAssets-1)
2815+
require.Len(t, balancesByGroup, numAssets-1)
2816+
2817+
balanceSum = uint64(0)
2818+
for _, balance := range balances {
2819+
balanceSum += balance.Balance
2820+
}
2821+
require.Equal(t, assetDesc[0].amt, balanceSum)
2822+
2823+
balanceByGroupSum = uint64(0)
2824+
for _, balance := range balancesByGroup {
2825+
balanceByGroupSum += balance.Balance
2826+
}
2827+
require.Equal(t, assetDesc[0].amt, balanceByGroupSum)
27902828
}

tapdb/sqlc/assets.sql.go

+15-15
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tapdb/sqlc/queries/assets.sql

+5-5
Original file line numberDiff line numberDiff line change
@@ -339,8 +339,8 @@ JOIN managed_utxos utxos
339339
JOIN script_keys
340340
ON assets.script_key_id = script_keys.script_key_id
341341
WHERE spent = FALSE AND
342-
(script_keys.tweaked_script_key != sqlc.narg('exclude_key') OR
343-
sqlc.narg('exclude_key') IS NULL) AND
342+
(script_keys.key_type != sqlc.narg('exclude_script_key_type') OR
343+
sqlc.narg('exclude_script_key_type') IS NULL) AND
344344
(sqlc.narg('script_key_type') = script_keys.key_type OR
345345
sqlc.narg('script_key_type') IS NULL)
346346
GROUP BY assets.genesis_id, genesis_info_view.asset_id,
@@ -369,9 +369,9 @@ JOIN managed_utxos utxos
369369
END
370370
JOIN script_keys
371371
ON assets.script_key_id = script_keys.script_key_id
372-
WHERE spent = FALSE AND
373-
(script_keys.tweaked_script_key != sqlc.narg('exclude_key') OR
374-
sqlc.narg('exclude_key') IS NULL) AND
372+
WHERE spent = FALSE AND
373+
(script_keys.key_type != sqlc.narg('exclude_script_key_type') OR
374+
sqlc.narg('exclude_script_key_type') IS NULL) AND
375375
(sqlc.narg('script_key_type') = script_keys.key_type OR
376376
sqlc.narg('script_key_type') IS NULL)
377377
GROUP BY key_group_info_view.tweaked_group_key;

0 commit comments

Comments
 (0)