Skip to content

Commit 00d7a6b

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 2979885 commit 00d7a6b

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"
@@ -3034,23 +3035,33 @@ func TestQueryAssetBalancesCustomChannelFunding(t *testing.T) {
30343035
const numGroups = 1
30353036
assetGen := newAssetGenerator(t, numAssets, numGroups)
30363037

3037-
fundingScriptKey := asset.NewScriptKey(
3038-
tapscript.NewChannelFundingScriptTree().TaprootKey,
3038+
fundingKey := tapscript.NewChannelFundingScriptTree()
3039+
xOnlyKey, _ := schnorr.ParsePubKey(
3040+
schnorr.SerializePubKey(fundingKey.TaprootKey),
30393041
)
3042+
fundingScriptKey := asset.ScriptKey{
3043+
PubKey: xOnlyKey,
3044+
TweakedScriptKey: &asset.TweakedScriptKey{
3045+
RawKey: keychain.KeyDescriptor{
3046+
PubKey: fundingKey.InternalKey,
3047+
},
3048+
Type: asset.ScriptKeyScriptPathChannel,
3049+
},
3050+
}
30403051

30413052
assetDesc := []assetDesc{
30423053
{
30433054
assetGen: assetGen.assetGens[0],
30443055
anchorPoint: assetGen.anchorPoints[0],
30453056
keyGroup: assetGen.groupKeys[0],
3046-
amt: 4,
3057+
amt: 8,
30473058
scriptKey: &fundingScriptKey,
30483059
},
30493060
{
30503061
assetGen: assetGen.assetGens[1],
30513062
anchorPoint: assetGen.anchorPoints[1],
30523063
keyGroup: assetGen.groupKeys[0],
3053-
amt: 4,
3064+
amt: 12,
30543065
},
30553066
}
30563067
assetGen.genAssets(t, assetsStore, assetDesc)
@@ -3081,4 +3092,31 @@ func TestQueryAssetBalancesCustomChannelFunding(t *testing.T) {
30813092
balanceByGroupSum += balance.Balance
30823093
}
30833094
require.Equal(t, assetDesc[1].amt, balanceByGroupSum)
3095+
3096+
// If we explicitly query for channel related script keys, we should get
3097+
// just those assets.
3098+
balances, err = assetsStore.QueryBalancesByAsset(
3099+
ctx, nil, includeLeased,
3100+
fn.Some(asset.ScriptKeyScriptPathChannel),
3101+
)
3102+
require.NoError(t, err)
3103+
balancesByGroup, err = assetsStore.QueryAssetBalancesByGroup(
3104+
ctx, nil, includeLeased,
3105+
fn.Some(asset.ScriptKeyScriptPathChannel),
3106+
)
3107+
require.NoError(t, err)
3108+
require.Len(t, balances, numAssets-1)
3109+
require.Len(t, balancesByGroup, numAssets-1)
3110+
3111+
balanceSum = uint64(0)
3112+
for _, balance := range balances {
3113+
balanceSum += balance.Balance
3114+
}
3115+
require.Equal(t, assetDesc[0].amt, balanceSum)
3116+
3117+
balanceByGroupSum = uint64(0)
3118+
for _, balance := range balancesByGroup {
3119+
balanceByGroupSum += balance.Balance
3120+
}
3121+
require.Equal(t, assetDesc[0].amt, balanceByGroupSum)
30843122
}

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)