Skip to content

Commit 74c44e2

Browse files
committed
multi: add script key type filter to queries
1 parent 41499fe commit 74c44e2

File tree

15 files changed

+1753
-1379
lines changed

15 files changed

+1753
-1379
lines changed

rpcserver.go

Lines changed: 47 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1010,7 +1010,7 @@ func (r *rpcServer) checkBalanceOverflow(ctx context.Context,
10101010
case assetID != nil:
10111011
// Retrieve the current asset balance.
10121012
balances, err := r.cfg.AssetStore.QueryBalancesByAsset(
1013-
ctx, assetID, true,
1013+
ctx, assetID, true, fn.None[asset.ScriptKeyType](),
10141014
)
10151015
if err != nil {
10161016
return fmt.Errorf("unable to query asset balance: %w",
@@ -1026,7 +1026,7 @@ func (r *rpcServer) checkBalanceOverflow(ctx context.Context,
10261026
case groupPubKey != nil:
10271027
// Retrieve the current balance of the group.
10281028
balances, err := r.cfg.AssetStore.QueryAssetBalancesByGroup(
1029-
ctx, groupPubKey, true,
1029+
ctx, groupPubKey, true, fn.None[asset.ScriptKeyType](),
10301030
)
10311031
if err != nil {
10321032
return fmt.Errorf("unable to query group balance: %w",
@@ -1105,9 +1105,18 @@ func (r *rpcServer) ListAssets(ctx context.Context,
11051105
filters.AnchorPoint = outPoint
11061106
}
11071107

1108+
scriptKeyType, includeSpent, err := taprpc.ParseScriptKeyTypeQuery(
1109+
req.ScriptKeyType,
1110+
)
1111+
if err != nil {
1112+
return nil, fmt.Errorf("unable to parse script key type "+
1113+
"query: %w", err)
1114+
}
1115+
filters.ScriptKeyType = scriptKeyType
1116+
11081117
rpcAssets, err := r.fetchRpcAssets(
1109-
ctx, req.WithWitness, req.IncludeSpent, req.IncludeLeased,
1110-
filters,
1118+
ctx, req.WithWitness, req.IncludeSpent || includeSpent,
1119+
req.IncludeLeased, filters,
11111120
)
11121121

11131122
if err != nil {
@@ -1218,11 +1227,12 @@ func (r *rpcServer) MarshalChainAsset(ctx context.Context, a asset.ChainAsset,
12181227
}
12191228

12201229
func (r *rpcServer) listBalancesByAsset(ctx context.Context,
1221-
assetID *asset.ID, includeLeased bool) (*taprpc.ListBalancesResponse,
1230+
assetID *asset.ID, includeLeased bool,
1231+
skt fn.Option[asset.ScriptKeyType]) (*taprpc.ListBalancesResponse,
12221232
error) {
12231233

12241234
balances, err := r.cfg.AssetStore.QueryBalancesByAsset(
1225-
ctx, assetID, includeLeased,
1235+
ctx, assetID, includeLeased, skt,
12261236
)
12271237
if err != nil {
12281238
return nil, fmt.Errorf("unable to list balances: %w", err)
@@ -1253,11 +1263,12 @@ func (r *rpcServer) listBalancesByAsset(ctx context.Context,
12531263
}
12541264

12551265
func (r *rpcServer) listBalancesByGroupKey(ctx context.Context,
1256-
groupKey *btcec.PublicKey,
1257-
includeLeased bool) (*taprpc.ListBalancesResponse, error) {
1266+
groupKey *btcec.PublicKey, includeLeased bool,
1267+
skt fn.Option[asset.ScriptKeyType]) (*taprpc.ListBalancesResponse,
1268+
error) {
12581269

12591270
balances, err := r.cfg.AssetStore.QueryAssetBalancesByGroup(
1260-
ctx, groupKey, includeLeased,
1271+
ctx, groupKey, includeLeased, skt,
12611272
)
12621273
if err != nil {
12631274
return nil, fmt.Errorf("unable to list balances: %w", err)
@@ -1292,8 +1303,22 @@ func (r *rpcServer) listBalancesByGroupKey(ctx context.Context,
12921303
func (r *rpcServer) ListUtxos(ctx context.Context,
12931304
req *taprpc.ListUtxosRequest) (*taprpc.ListUtxosResponse, error) {
12941305

1306+
scriptKeyType, includeSpent, err := taprpc.ParseScriptKeyTypeQuery(
1307+
req.ScriptKeyType,
1308+
)
1309+
if err != nil {
1310+
return nil, fmt.Errorf("unable to parse script key type "+
1311+
"query: %w", err)
1312+
}
1313+
1314+
filters := &tapdb.AssetQueryFilters{
1315+
CommitmentConstraints: tapfreighter.CommitmentConstraints{
1316+
ScriptKeyType: scriptKeyType,
1317+
},
1318+
}
1319+
12951320
rpcAssets, err := r.fetchRpcAssets(
1296-
ctx, false, false, req.IncludeLeased, nil,
1321+
ctx, false, includeSpent, req.IncludeLeased, filters,
12971322
)
12981323
if err != nil {
12991324
return nil, err
@@ -1395,6 +1420,14 @@ func (r *rpcServer) ListGroups(ctx context.Context,
13951420
func (r *rpcServer) ListBalances(ctx context.Context,
13961421
req *taprpc.ListBalancesRequest) (*taprpc.ListBalancesResponse, error) {
13971422

1423+
scriptKeyType, _, err := taprpc.ParseScriptKeyTypeQuery(
1424+
req.ScriptKeyType,
1425+
)
1426+
if err != nil {
1427+
return nil, fmt.Errorf("unable to parse script key type "+
1428+
"query: %w", err)
1429+
}
1430+
13981431
switch groupBy := req.GroupBy.(type) {
13991432
case *taprpc.ListBalancesRequest_AssetId:
14001433
if !groupBy.AssetId {
@@ -1411,7 +1444,9 @@ func (r *rpcServer) ListBalances(ctx context.Context,
14111444
copy(assetID[:], req.AssetFilter)
14121445
}
14131446

1414-
return r.listBalancesByAsset(ctx, assetID, req.IncludeLeased)
1447+
return r.listBalancesByAsset(
1448+
ctx, assetID, req.IncludeLeased, scriptKeyType,
1449+
)
14151450

14161451
case *taprpc.ListBalancesRequest_GroupKey:
14171452
if !groupBy.GroupKey {
@@ -1429,7 +1464,7 @@ func (r *rpcServer) ListBalances(ctx context.Context,
14291464
}
14301465

14311466
return r.listBalancesByGroupKey(
1432-
ctx, groupKey, req.IncludeLeased,
1467+
ctx, groupKey, req.IncludeLeased, scriptKeyType,
14331468
)
14341469

14351470
default:

tapchannel/aux_funding_controller.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -797,7 +797,7 @@ func (f *FundingController) fundVirtualPacket(ctx context.Context,
797797
fundDesc := &tapsend.FundingDescriptor{
798798
AssetSpecifier: specifier,
799799
Amount: amt,
800-
CoinSelectType: tapsend.Bip86Only,
800+
ScriptKeyType: fn.Some(asset.ScriptKeyBip86),
801801
DistinctSpecifier: true,
802802
}
803803

tapdb/asset_minting_test.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1143,7 +1143,9 @@ func TestCommitBatchChainActions(t *testing.T) {
11431143

11441144
// We'll now query for the set of balances to ensure they all line up
11451145
// with the assets we just created, including the group genesis asset.
1146-
assetBalances, err := confAssets.QueryBalancesByAsset(ctx, nil, false)
1146+
assetBalances, err := confAssets.QueryBalancesByAsset(
1147+
ctx, nil, false, fn.None[asset.ScriptKeyType](),
1148+
)
11471149
require.NoError(t, err)
11481150
require.Equal(t, numSeedlings+1, len(assetBalances))
11491151

@@ -1165,7 +1167,7 @@ func TestCommitBatchChainActions(t *testing.T) {
11651167
}
11661168
numKeyGroups := fn.Reduce(mintedAssets, keyGroupSumReducer)
11671169
assetBalancesByGroup, err := confAssets.QueryAssetBalancesByGroup(
1168-
ctx, nil, false,
1170+
ctx, nil, false, fn.None[asset.ScriptKeyType](),
11691171
)
11701172
require.NoError(t, err)
11711173
require.Equal(t, numKeyGroups, len(assetBalancesByGroup))

tapdb/assets_store.go

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ import (
2424
"github.com/lightninglabs/taproot-assets/tapfreighter"
2525
"github.com/lightninglabs/taproot-assets/tappsbt"
2626
"github.com/lightninglabs/taproot-assets/tapscript"
27-
"github.com/lightninglabs/taproot-assets/tapsend"
2827
"github.com/lightningnetwork/lnd/clock"
2928
"github.com/lightningnetwork/lnd/keychain"
3029
)
@@ -897,13 +896,11 @@ func (a *AssetStore) constraintsToDbFilter(
897896
assetFilter.AssetIDFilter = nil
898897
}
899898

900-
switch query.CoinSelectType {
901-
case tapsend.ScriptTreesAllowed:
902-
assetFilter.Bip86ScriptKeysOnly = false
903-
904-
default:
905-
assetFilter.Bip86ScriptKeysOnly = true
906-
}
899+
// The fn.None option means we don't restrict on script key type
900+
// at all.
901+
query.ScriptKeyType.WhenSome(func(t asset.ScriptKeyType) {
902+
assetFilter.ScriptKeyType = sqlInt16(t)
903+
})
907904
}
908905

909906
return assetFilter, nil
@@ -990,8 +987,8 @@ type AssetQueryFilters struct {
990987
// QueryBalancesByAsset queries the balances for assets or alternatively
991988
// for a selected one that matches the passed asset ID filter.
992989
func (a *AssetStore) QueryBalancesByAsset(ctx context.Context,
993-
assetID *asset.ID,
994-
includeLeased bool) (map[asset.ID]AssetBalance, error) {
990+
assetID *asset.ID, includeLeased bool,
991+
skt fn.Option[asset.ScriptKeyType]) (map[asset.ID]AssetBalance, error) {
995992

996993
// We'll now map the application level filtering to the type of
997994
// filtering our database query understands.
@@ -1002,6 +999,11 @@ func (a *AssetStore) QueryBalancesByAsset(ctx context.Context,
1002999
},
10031000
}
10041001

1002+
// The fn.None option means we don't restrict on script key type at all.
1003+
skt.WhenSome(func(t asset.ScriptKeyType) {
1004+
assetBalancesFilter.ScriptKeyType = sqlInt16(t)
1005+
})
1006+
10051007
// We exclude the assets that are specifically used for funding custom
10061008
// channels. The balance of those assets is reported through lnd channel
10071009
// balance. Those assets are identified by the funding script tree for a
@@ -1070,9 +1072,9 @@ func (a *AssetStore) QueryBalancesByAsset(ctx context.Context,
10701072
// QueryAssetBalancesByGroup queries the asset balances for asset groups or
10711073
// alternatively for a selected one that matches the passed filter.
10721074
func (a *AssetStore) QueryAssetBalancesByGroup(ctx context.Context,
1073-
groupKey *btcec.PublicKey,
1074-
includeLeased bool) (map[asset.SerializedKey]AssetGroupBalance,
1075-
error) {
1075+
groupKey *btcec.PublicKey, includeLeased bool,
1076+
skt fn.Option[asset.ScriptKeyType]) (
1077+
map[asset.SerializedKey]AssetGroupBalance, error) {
10761078

10771079
// We'll now map the application level filtering to the type of
10781080
// filtering our database query understands.
@@ -1083,6 +1085,11 @@ func (a *AssetStore) QueryAssetBalancesByGroup(ctx context.Context,
10831085
},
10841086
}
10851087

1088+
// The fn.None option means we don't restrict on script key type at all.
1089+
skt.WhenSome(func(t asset.ScriptKeyType) {
1090+
assetBalancesFilter.ScriptKeyType = sqlInt16(t)
1091+
})
1092+
10861093
// We exclude the assets that are specifically used for funding custom
10871094
// channels. The balance of those assets is reported through lnd channel
10881095
// balance. Those assets are identified by the funding script tree for a

tapdb/assets_store_test.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2937,11 +2937,11 @@ func TestQueryAssetBalances(t *testing.T) {
29372937
// At first, none of the assets should be leased.
29382938
includeLeased := false
29392939
balances, err := assetsStore.QueryBalancesByAsset(
2940-
ctx, nil, includeLeased,
2940+
ctx, nil, includeLeased, fn.None[asset.ScriptKeyType](),
29412941
)
29422942
require.NoError(t, err)
29432943
balancesByGroup, err := assetsStore.QueryAssetBalancesByGroup(
2944-
ctx, nil, includeLeased,
2944+
ctx, nil, includeLeased, fn.None[asset.ScriptKeyType](),
29452945
)
29462946
require.NoError(t, err)
29472947
require.Len(t, balances, numAssets)
@@ -2977,14 +2977,14 @@ func TestQueryAssetBalances(t *testing.T) {
29772977

29782978
// Only two assets should be returned that is not leased.
29792979
unleasedBalances, err := assetsStore.QueryBalancesByAsset(
2980-
ctx, nil, includeLeased,
2980+
ctx, nil, includeLeased, fn.None[asset.ScriptKeyType](),
29812981
)
29822982
require.NoError(t, err)
29832983
require.Len(t, unleasedBalances, numAssets-2)
29842984

29852985
// Only one group should be returned that is not leased.
29862986
unleasedBalancesByGroup, err := assetsStore.QueryAssetBalancesByGroup(
2987-
ctx, nil, includeLeased,
2987+
ctx, nil, includeLeased, fn.None[asset.ScriptKeyType](),
29882988
)
29892989
require.NoError(t, err)
29902990
require.Len(t, unleasedBalancesByGroup, numGroups-1)
@@ -3007,12 +3007,12 @@ func TestQueryAssetBalances(t *testing.T) {
30073007
// the same results as when the assets where unleased.
30083008
includeLeased = true
30093009
includeLeasedBalances, err := assetsStore.QueryBalancesByAsset(
3010-
ctx, nil, includeLeased,
3010+
ctx, nil, includeLeased, fn.None[asset.ScriptKeyType](),
30113011
)
30123012
require.NoError(t, err)
30133013
require.Len(t, includeLeasedBalances, numAssets)
30143014
includeLeasedBalByGroup, err := assetsStore.QueryAssetBalancesByGroup(
3015-
ctx, nil, includeLeased,
3015+
ctx, nil, includeLeased, fn.None[asset.ScriptKeyType](),
30163016
)
30173017
require.NoError(t, err)
30183018
require.Len(t, includeLeasedBalByGroup, len(assetGen.groupKeys))
@@ -3066,11 +3066,11 @@ func TestQueryAssetBalancesCustomChannelFunding(t *testing.T) {
30663066
// Hit both balance queries, they should return the same result.
30673067
includeLeased := false
30683068
balances, err := assetsStore.QueryBalancesByAsset(
3069-
ctx, nil, includeLeased,
3069+
ctx, nil, includeLeased, fn.None[asset.ScriptKeyType](),
30703070
)
30713071
require.NoError(t, err)
30723072
balancesByGroup, err := assetsStore.QueryAssetBalancesByGroup(
3073-
ctx, nil, includeLeased,
3073+
ctx, nil, includeLeased, fn.None[asset.ScriptKeyType](),
30743074
)
30753075
require.NoError(t, err)
30763076
require.Len(t, balances, numAssets-1)

0 commit comments

Comments
 (0)