Skip to content

Commit e1d3fe3

Browse files
committed
multi: add script key type filter to queries
1 parent e3903d0 commit e1d3fe3

File tree

15 files changed

+1735
-1360
lines changed

15 files changed

+1735
-1360
lines changed

rpcserver.go

Lines changed: 50 additions & 13 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",
@@ -1059,8 +1059,17 @@ func (r *rpcServer) ListAssets(ctx context.Context,
10591059
"and include_leased")
10601060
}
10611061

1062+
scriptKeyType, includeSpent, err := taprpc.ParseScriptKeyTypeQuery(
1063+
req.ScriptKeyType,
1064+
)
1065+
if err != nil {
1066+
return nil, fmt.Errorf("unable to parse script key type "+
1067+
"query: %w", err)
1068+
}
1069+
10621070
rpcAssets, err := r.fetchRpcAssets(
1063-
ctx, req.WithWitness, req.IncludeSpent, req.IncludeLeased,
1071+
ctx, req.WithWitness, req.IncludeSpent || includeSpent,
1072+
req.IncludeLeased, scriptKeyType,
10641073
)
10651074
if err != nil {
10661075
return nil, err
@@ -1114,10 +1123,16 @@ func (r *rpcServer) ListAssets(ctx context.Context,
11141123
}
11151124

11161125
func (r *rpcServer) fetchRpcAssets(ctx context.Context, withWitness,
1117-
includeSpent, includeLeased bool) ([]*taprpc.Asset, error) {
1126+
includeSpent, includeLeased bool,
1127+
scriptKeyType fn.Option[asset.ScriptKeyType]) ([]*taprpc.Asset, error) {
11181128

1129+
filter := &tapdb.AssetQueryFilters{
1130+
CommitmentConstraints: tapfreighter.CommitmentConstraints{
1131+
ScriptKeyType: scriptKeyType,
1132+
},
1133+
}
11191134
assets, err := r.cfg.AssetStore.FetchAllAssets(
1120-
ctx, includeSpent, includeLeased, nil,
1135+
ctx, includeSpent, includeLeased, filter,
11211136
)
11221137
if err != nil {
11231138
return nil, fmt.Errorf("unable to read chain assets: %w", err)
@@ -1169,11 +1184,12 @@ func (r *rpcServer) MarshalChainAsset(ctx context.Context, a asset.ChainAsset,
11691184
}
11701185

11711186
func (r *rpcServer) listBalancesByAsset(ctx context.Context,
1172-
assetID *asset.ID, includeLeased bool) (*taprpc.ListBalancesResponse,
1187+
assetID *asset.ID, includeLeased bool,
1188+
skt fn.Option[asset.ScriptKeyType]) (*taprpc.ListBalancesResponse,
11731189
error) {
11741190

11751191
balances, err := r.cfg.AssetStore.QueryBalancesByAsset(
1176-
ctx, assetID, includeLeased,
1192+
ctx, assetID, includeLeased, skt,
11771193
)
11781194
if err != nil {
11791195
return nil, fmt.Errorf("unable to list balances: %w", err)
@@ -1204,11 +1220,12 @@ func (r *rpcServer) listBalancesByAsset(ctx context.Context,
12041220
}
12051221

12061222
func (r *rpcServer) listBalancesByGroupKey(ctx context.Context,
1207-
groupKey *btcec.PublicKey,
1208-
includeLeased bool) (*taprpc.ListBalancesResponse, error) {
1223+
groupKey *btcec.PublicKey, includeLeased bool,
1224+
skt fn.Option[asset.ScriptKeyType]) (*taprpc.ListBalancesResponse,
1225+
error) {
12091226

12101227
balances, err := r.cfg.AssetStore.QueryAssetBalancesByGroup(
1211-
ctx, groupKey, includeLeased,
1228+
ctx, groupKey, includeLeased, skt,
12121229
)
12131230
if err != nil {
12141231
return nil, fmt.Errorf("unable to list balances: %w", err)
@@ -1243,7 +1260,17 @@ func (r *rpcServer) listBalancesByGroupKey(ctx context.Context,
12431260
func (r *rpcServer) ListUtxos(ctx context.Context,
12441261
req *taprpc.ListUtxosRequest) (*taprpc.ListUtxosResponse, error) {
12451262

1246-
rpcAssets, err := r.fetchRpcAssets(ctx, false, false, req.IncludeLeased)
1263+
scriptKeyType, includeSpent, err := taprpc.ParseScriptKeyTypeQuery(
1264+
req.ScriptKeyType,
1265+
)
1266+
if err != nil {
1267+
return nil, fmt.Errorf("unable to parse script key type "+
1268+
"query: %w", err)
1269+
}
1270+
1271+
rpcAssets, err := r.fetchRpcAssets(
1272+
ctx, false, includeSpent, req.IncludeLeased, scriptKeyType,
1273+
)
12471274
if err != nil {
12481275
return nil, err
12491276
}
@@ -1344,6 +1371,14 @@ func (r *rpcServer) ListGroups(ctx context.Context,
13441371
func (r *rpcServer) ListBalances(ctx context.Context,
13451372
req *taprpc.ListBalancesRequest) (*taprpc.ListBalancesResponse, error) {
13461373

1374+
scriptKeyType, _, err := taprpc.ParseScriptKeyTypeQuery(
1375+
req.ScriptKeyType,
1376+
)
1377+
if err != nil {
1378+
return nil, fmt.Errorf("unable to parse script key type "+
1379+
"query: %w", err)
1380+
}
1381+
13471382
switch groupBy := req.GroupBy.(type) {
13481383
case *taprpc.ListBalancesRequest_AssetId:
13491384
if !groupBy.AssetId {
@@ -1360,7 +1395,9 @@ func (r *rpcServer) ListBalances(ctx context.Context,
13601395
copy(assetID[:], req.AssetFilter)
13611396
}
13621397

1363-
return r.listBalancesByAsset(ctx, assetID, req.IncludeLeased)
1398+
return r.listBalancesByAsset(
1399+
ctx, assetID, req.IncludeLeased, scriptKeyType,
1400+
)
13641401

13651402
case *taprpc.ListBalancesRequest_GroupKey:
13661403
if !groupBy.GroupKey {
@@ -1378,7 +1415,7 @@ func (r *rpcServer) ListBalances(ctx context.Context,
13781415
}
13791416

13801417
return r.listBalancesByGroupKey(
1381-
ctx, groupKey, req.IncludeLeased,
1418+
ctx, groupKey, req.IncludeLeased, scriptKeyType,
13821419
)
13831420

13841421
default:

tapchannel/aux_funding_controller.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -806,7 +806,7 @@ func (f *FundingController) fundVirtualPacket(ctx context.Context,
806806
fundDesc := &tapsend.FundingDescriptor{
807807
AssetSpecifier: specifier,
808808
Amount: amt,
809-
CoinSelectType: tapsend.Bip86Only,
809+
ScriptKeyType: fn.Some(asset.ScriptKeyBip86),
810810
}
811811

812812
// Fund the packet. This will derive an anchor internal key for us, but

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
)
@@ -873,13 +872,11 @@ func (a *AssetStore) constraintsToDbFilter(
873872
// TODO(roasbeef): only want to allow asset ID or other and not
874873
// both?
875874

876-
switch query.CoinSelectType {
877-
case tapsend.ScriptTreesAllowed:
878-
assetFilter.Bip86ScriptKeysOnly = false
879-
880-
default:
881-
assetFilter.Bip86ScriptKeysOnly = true
882-
}
875+
// The fn.None option means we don't restrict on script key type
876+
// at all.
877+
query.ScriptKeyType.WhenSome(func(t asset.ScriptKeyType) {
878+
assetFilter.ScriptKeyType = sqlInt16(t)
879+
})
883880
}
884881

885882
return assetFilter
@@ -959,8 +956,8 @@ type AssetQueryFilters struct {
959956
// QueryBalancesByAsset queries the balances for assets or alternatively
960957
// for a selected one that matches the passed asset ID filter.
961958
func (a *AssetStore) QueryBalancesByAsset(ctx context.Context,
962-
assetID *asset.ID,
963-
includeLeased bool) (map[asset.ID]AssetBalance, error) {
959+
assetID *asset.ID, includeLeased bool,
960+
skt fn.Option[asset.ScriptKeyType]) (map[asset.ID]AssetBalance, error) {
964961

965962
// We'll now map the application level filtering to the type of
966963
// filtering our database query understands.
@@ -971,6 +968,11 @@ func (a *AssetStore) QueryBalancesByAsset(ctx context.Context,
971968
},
972969
}
973970

971+
// The fn.None option means we don't restrict on script key type at all.
972+
skt.WhenSome(func(t asset.ScriptKeyType) {
973+
assetBalancesFilter.ScriptKeyType = sqlInt16(t)
974+
})
975+
974976
// We exclude the assets that are specifically used for funding custom
975977
// channels. The balance of those assets is reported through lnd channel
976978
// balance. Those assets are identified by the funding script tree for a
@@ -1039,9 +1041,9 @@ func (a *AssetStore) QueryBalancesByAsset(ctx context.Context,
10391041
// QueryAssetBalancesByGroup queries the asset balances for asset groups or
10401042
// alternatively for a selected one that matches the passed filter.
10411043
func (a *AssetStore) QueryAssetBalancesByGroup(ctx context.Context,
1042-
groupKey *btcec.PublicKey,
1043-
includeLeased bool) (map[asset.SerializedKey]AssetGroupBalance,
1044-
error) {
1044+
groupKey *btcec.PublicKey, includeLeased bool,
1045+
skt fn.Option[asset.ScriptKeyType]) (
1046+
map[asset.SerializedKey]AssetGroupBalance, error) {
10451047

10461048
// We'll now map the application level filtering to the type of
10471049
// filtering our database query understands.
@@ -1052,6 +1054,11 @@ func (a *AssetStore) QueryAssetBalancesByGroup(ctx context.Context,
10521054
},
10531055
}
10541056

1057+
// The fn.None option means we don't restrict on script key type at all.
1058+
skt.WhenSome(func(t asset.ScriptKeyType) {
1059+
assetBalancesFilter.ScriptKeyType = sqlInt16(t)
1060+
})
1061+
10551062
// We exclude the assets that are specifically used for funding custom
10561063
// channels. The balance of those assets is reported through lnd channel
10571064
// 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
@@ -2628,11 +2628,11 @@ func TestQueryAssetBalances(t *testing.T) {
26282628
// At first, none of the assets should be leased.
26292629
includeLeased := false
26302630
balances, err := assetsStore.QueryBalancesByAsset(
2631-
ctx, nil, includeLeased,
2631+
ctx, nil, includeLeased, fn.None[asset.ScriptKeyType](),
26322632
)
26332633
require.NoError(t, err)
26342634
balancesByGroup, err := assetsStore.QueryAssetBalancesByGroup(
2635-
ctx, nil, includeLeased,
2635+
ctx, nil, includeLeased, fn.None[asset.ScriptKeyType](),
26362636
)
26372637
require.NoError(t, err)
26382638
require.Len(t, balances, numAssets)
@@ -2668,14 +2668,14 @@ func TestQueryAssetBalances(t *testing.T) {
26682668

26692669
// Only two assets should be returned that is not leased.
26702670
unleasedBalances, err := assetsStore.QueryBalancesByAsset(
2671-
ctx, nil, includeLeased,
2671+
ctx, nil, includeLeased, fn.None[asset.ScriptKeyType](),
26722672
)
26732673
require.NoError(t, err)
26742674
require.Len(t, unleasedBalances, numAssets-2)
26752675

26762676
// Only one group should be returned that is not leased.
26772677
unleasedBalancesByGroup, err := assetsStore.QueryAssetBalancesByGroup(
2678-
ctx, nil, includeLeased,
2678+
ctx, nil, includeLeased, fn.None[asset.ScriptKeyType](),
26792679
)
26802680
require.NoError(t, err)
26812681
require.Len(t, unleasedBalancesByGroup, numGroups-1)
@@ -2698,12 +2698,12 @@ func TestQueryAssetBalances(t *testing.T) {
26982698
// the same results as when the assets where unleased.
26992699
includeLeased = true
27002700
includeLeasedBalances, err := assetsStore.QueryBalancesByAsset(
2701-
ctx, nil, includeLeased,
2701+
ctx, nil, includeLeased, fn.None[asset.ScriptKeyType](),
27022702
)
27032703
require.NoError(t, err)
27042704
require.Len(t, includeLeasedBalances, numAssets)
27052705
includeLeasedBalByGroup, err := assetsStore.QueryAssetBalancesByGroup(
2706-
ctx, nil, includeLeased,
2706+
ctx, nil, includeLeased, fn.None[asset.ScriptKeyType](),
27072707
)
27082708
require.NoError(t, err)
27092709
require.Len(t, includeLeasedBalByGroup, len(assetGen.groupKeys))
@@ -2757,11 +2757,11 @@ func TestQueryAssetBalancesCustomChannelFunding(t *testing.T) {
27572757
// Hit both balance queries, they should return the same result.
27582758
includeLeased := false
27592759
balances, err := assetsStore.QueryBalancesByAsset(
2760-
ctx, nil, includeLeased,
2760+
ctx, nil, includeLeased, fn.None[asset.ScriptKeyType](),
27612761
)
27622762
require.NoError(t, err)
27632763
balancesByGroup, err := assetsStore.QueryAssetBalancesByGroup(
2764-
ctx, nil, includeLeased,
2764+
ctx, nil, includeLeased, fn.None[asset.ScriptKeyType](),
27652765
)
27662766
require.NoError(t, err)
27672767
require.Len(t, balances, numAssets-1)

0 commit comments

Comments
 (0)