Skip to content

Commit 1ac345d

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 3f65627 commit 1ac345d

File tree

4 files changed

+92
-43
lines changed

4 files changed

+92
-43
lines changed

tapdb/assets_store.go

Lines changed: 30 additions & 19 deletions
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
)
@@ -999,19 +998,25 @@ func (a *AssetStore) QueryBalancesByAsset(ctx context.Context,
999998
},
1000999
}
10011000

1001+
// We exclude the assets that are specifically used for funding custom
1002+
// channels. The balance of those assets is reported through lnd channel
1003+
// balance. Those assets are identified by the specific script key type
1004+
// for channel keys. We exclude them unless explicitly queried for.
1005+
assetBalancesFilter.ExcludeScriptKeyType = sqlInt16(
1006+
asset.ScriptKeyScriptPathChannel,
1007+
)
1008+
10021009
// The fn.None option means we don't restrict on script key type at all.
10031010
skt.WhenSome(func(t asset.ScriptKeyType) {
10041011
assetBalancesFilter.ScriptKeyType = sqlInt16(t)
1005-
})
10061012

1007-
// We exclude the assets that are specifically used for funding custom
1008-
// channels. The balance of those assets is reported through lnd channel
1009-
// balance. Those assets are identified by the funding script tree for a
1010-
// custom channel asset-level script key.
1011-
excludeKey := asset.NewScriptKey(
1012-
tapscript.NewChannelFundingScriptTree().TaprootKey,
1013-
)
1014-
assetBalancesFilter.ExcludeKey = excludeKey.PubKey.SerializeCompressed()
1013+
// If the user explicitly wants to see the channel related asset
1014+
// balances, we need to set the exclude type to NULL.
1015+
if t == asset.ScriptKeyScriptPathChannel {
1016+
nullValue := sql.NullInt16{}
1017+
assetBalancesFilter.ExcludeScriptKeyType = nullValue
1018+
}
1019+
})
10151020

10161021
// By default, we only show assets that are not leased.
10171022
if !includeLeased {
@@ -1085,19 +1090,25 @@ func (a *AssetStore) QueryAssetBalancesByGroup(ctx context.Context,
10851090
},
10861091
}
10871092

1093+
// We exclude the assets that are specifically used for funding custom
1094+
// channels. The balance of those assets is reported through lnd channel
1095+
// balance. Those assets are identified by the specific script key type
1096+
// for channel keys. We exclude them unless explicitly queried for.
1097+
assetBalancesFilter.ExcludeScriptKeyType = sqlInt16(
1098+
asset.ScriptKeyScriptPathChannel,
1099+
)
1100+
10881101
// The fn.None option means we don't restrict on script key type at all.
10891102
skt.WhenSome(func(t asset.ScriptKeyType) {
10901103
assetBalancesFilter.ScriptKeyType = sqlInt16(t)
1091-
})
10921104

1093-
// We exclude the assets that are specifically used for funding custom
1094-
// channels. The balance of those assets is reported through lnd channel
1095-
// balance. Those assets are identified by the funding script tree for a
1096-
// custom channel asset-level script key.
1097-
excludeKey := asset.NewScriptKey(
1098-
tapscript.NewChannelFundingScriptTree().TaprootKey,
1099-
)
1100-
assetBalancesFilter.ExcludeKey = excludeKey.PubKey.SerializeCompressed()
1105+
// If the user explicitly wants to see the channel related asset
1106+
// balances, we need to set the exclude type to NULL.
1107+
if t == asset.ScriptKeyScriptPathChannel {
1108+
nullValue := sql.NullInt16{}
1109+
assetBalancesFilter.ExcludeScriptKeyType = nullValue
1110+
}
1111+
})
11011112

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

tapdb/assets_store_test.go

Lines changed: 42 additions & 4 deletions
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"
@@ -3027,23 +3028,33 @@ func TestQueryAssetBalancesCustomChannelFunding(t *testing.T) {
30273028
const numGroups = 1
30283029
assetGen := newAssetGenerator(t, numAssets, numGroups)
30293030

3030-
fundingScriptKey := asset.NewScriptKey(
3031-
tapscript.NewChannelFundingScriptTree().TaprootKey,
3031+
fundingKey := tapscript.NewChannelFundingScriptTree()
3032+
xOnlyKey, _ := schnorr.ParsePubKey(
3033+
schnorr.SerializePubKey(fundingKey.TaprootKey),
30323034
)
3035+
fundingScriptKey := asset.ScriptKey{
3036+
PubKey: xOnlyKey,
3037+
TweakedScriptKey: &asset.TweakedScriptKey{
3038+
RawKey: keychain.KeyDescriptor{
3039+
PubKey: fundingKey.InternalKey,
3040+
},
3041+
Type: asset.ScriptKeyScriptPathChannel,
3042+
},
3043+
}
30333044

30343045
assetDesc := []assetDesc{
30353046
{
30363047
assetGen: assetGen.assetGens[0],
30373048
anchorPoint: assetGen.anchorPoints[0],
30383049
keyGroup: assetGen.groupKeys[0],
3039-
amt: 4,
3050+
amt: 8,
30403051
scriptKey: &fundingScriptKey,
30413052
},
30423053
{
30433054
assetGen: assetGen.assetGens[1],
30443055
anchorPoint: assetGen.anchorPoints[1],
30453056
keyGroup: assetGen.groupKeys[0],
3046-
amt: 4,
3057+
amt: 12,
30473058
},
30483059
}
30493060
assetGen.genAssets(t, assetsStore, assetDesc)
@@ -3074,4 +3085,31 @@ func TestQueryAssetBalancesCustomChannelFunding(t *testing.T) {
30743085
balanceByGroupSum += balance.Balance
30753086
}
30763087
require.Equal(t, assetDesc[1].amt, balanceByGroupSum)
3088+
3089+
// If we explicitly query for channel related script keys, we should get
3090+
// just those assets.
3091+
balances, err = assetsStore.QueryBalancesByAsset(
3092+
ctx, nil, includeLeased,
3093+
fn.Some(asset.ScriptKeyScriptPathChannel),
3094+
)
3095+
require.NoError(t, err)
3096+
balancesByGroup, err = assetsStore.QueryAssetBalancesByGroup(
3097+
ctx, nil, includeLeased,
3098+
fn.Some(asset.ScriptKeyScriptPathChannel),
3099+
)
3100+
require.NoError(t, err)
3101+
require.Len(t, balances, numAssets-1)
3102+
require.Len(t, balancesByGroup, numAssets-1)
3103+
3104+
balanceSum = uint64(0)
3105+
for _, balance := range balances {
3106+
balanceSum += balance.Balance
3107+
}
3108+
require.Equal(t, assetDesc[0].amt, balanceSum)
3109+
3110+
balanceByGroupSum = uint64(0)
3111+
for _, balance := range balancesByGroup {
3112+
balanceByGroupSum += balance.Balance
3113+
}
3114+
require.Equal(t, assetDesc[0].amt, balanceByGroupSum)
30773115
}

tapdb/sqlc/assets.sql.go

Lines changed: 15 additions & 15 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: 5 additions & 5 deletions
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)