Skip to content

Commit c44a9cd

Browse files
authored
Merge pull request #761 from lightninglabs/rpc-listtransfers-filtering
taprpc+tapdb: `ListTransfers` RPC optionally filters on anchor tx hash
2 parents 1741b14 + b2a98a9 commit c44a9cd

9 files changed

+738
-529
lines changed

itest/assertions.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -1568,10 +1568,10 @@ func assertGroups(t *testing.T, client taprpc.TaprootAssetsClient,
15681568
require.NoError(t, err)
15691569

15701570
groupKeys := maps.Keys(assetGroups.Groups)
1571-
require.Equal(t, 2, len(groupKeys))
1571+
require.Len(t, groupKeys, 2)
15721572

15731573
groupedAssets := assetGroups.Groups[groupKeys[0]].Assets
1574-
require.Equal(t, 1, len(groupedAssets))
1574+
require.Len(t, groupedAssets, 1)
15751575
require.Equal(t, 1, len(assetGroups.Groups[groupKeys[1]].Assets))
15761576

15771577
groupedAssets = append(

rpcserver.go

+16-2
Original file line numberDiff line numberDiff line change
@@ -973,10 +973,24 @@ func (r *rpcServer) ListBalances(ctx context.Context,
973973

974974
// ListTransfers lists all asset transfers managed by this daemon.
975975
func (r *rpcServer) ListTransfers(ctx context.Context,
976-
_ *taprpc.ListTransfersRequest) (*taprpc.ListTransfersResponse,
976+
req *taprpc.ListTransfersRequest) (*taprpc.ListTransfersResponse,
977977
error) {
978978

979-
parcels, err := r.cfg.AssetStore.QueryParcels(ctx, false)
979+
// Unmarshal the anchor tx hash if one was provided.
980+
var (
981+
anchorTxHash *chainhash.Hash
982+
err error
983+
)
984+
985+
if len(req.AnchorTxid) != 0 {
986+
anchorTxHash, err = chainhash.NewHashFromStr(req.AnchorTxid)
987+
if err != nil {
988+
return nil, fmt.Errorf("invalid anchor tx hash: %w",
989+
err)
990+
}
991+
}
992+
993+
parcels, err := r.cfg.AssetStore.QueryParcels(ctx, anchorTxHash, false)
980994
if err != nil {
981995
return nil, fmt.Errorf("failed to query parcels: %w", err)
982996
}

tapdb/assets_store.go

+13-4
Original file line numberDiff line numberDiff line change
@@ -2757,22 +2757,31 @@ func (a *AssetStore) reAnchorPassiveAssets(ctx context.Context,
27572757
func (a *AssetStore) PendingParcels(
27582758
ctx context.Context) ([]*tapfreighter.OutboundParcel, error) {
27592759

2760-
return a.QueryParcels(ctx, true)
2760+
return a.QueryParcels(ctx, nil, true)
27612761
}
27622762

27632763
// QueryParcels returns the set of confirmed or unconfirmed parcels.
27642764
func (a *AssetStore) QueryParcels(ctx context.Context,
2765+
anchorTxHash *chainhash.Hash,
27652766
pending bool) ([]*tapfreighter.OutboundParcel, error) {
27662767

27672768
var transfers []*tapfreighter.OutboundParcel
27682769

27692770
readOpts := NewAssetStoreReadTx()
27702771
dbErr := a.db.ExecTx(ctx, &readOpts, func(q ActiveAssetsStore) error {
2772+
// Construct transfer query.
2773+
transferQuery := TransferQuery{
2774+
UnconfOnly: pending,
2775+
}
2776+
2777+
// Include anchor tx hash if specified.
2778+
if anchorTxHash != nil {
2779+
transferQuery.AnchorTxHash = anchorTxHash[:]
2780+
}
2781+
27712782
// If we want every unconfirmed transfer, then we only pass in
27722783
// the UnconfOnly field.
2773-
dbTransfers, err := q.QueryAssetTransfers(ctx, TransferQuery{
2774-
UnconfOnly: pending,
2775-
})
2784+
dbTransfers, err := q.QueryAssetTransfers(ctx, transferQuery)
27762785
if err != nil {
27772786
return err
27782787
}

tapdb/assets_store_test.go

+12-5
Original file line numberDiff line numberDiff line change
@@ -1261,7 +1261,7 @@ func TestAssetExportLog(t *testing.T) {
12611261
// looking for all unconfirmed transfers.
12621262
assetTransfers, err := db.QueryAssetTransfers(ctx, TransferQuery{})
12631263
require.NoError(t, err)
1264-
require.Equal(t, 1, len(assetTransfers))
1264+
require.Len(t, assetTransfers, 1)
12651265

12661266
// We should also be able to find it based on its outpoint.
12671267
firstOutput := spendDelta.Outputs[0]
@@ -1270,7 +1270,7 @@ func TestAssetExportLog(t *testing.T) {
12701270
AnchorTxHash: firstOutputAnchor.OutPoint.Hash[:],
12711271
})
12721272
require.NoError(t, err)
1273-
require.Equal(t, 1, len(assetTransfers))
1273+
require.Len(t, assetTransfers, 1)
12741274

12751275
// Check that the new UTXO is found among our managed UTXOs.
12761276
utxos, err = assetsStore.FetchManagedUTXOs(ctx)
@@ -1302,13 +1302,20 @@ func TestAssetExportLog(t *testing.T) {
13021302
UnconfOnly: true,
13031303
})
13041304
require.NoError(t, err)
1305-
require.Equal(t, 1, len(assetTransfers))
1305+
require.Len(t, assetTransfers, 1)
13061306

13071307
// This should also show up in the set of pending parcels. It should
13081308
// also match exactly the inbound parcel we used to make the delta.
13091309
parcels, err := assetsStore.PendingParcels(ctx)
13101310
require.NoError(t, err)
1311-
require.Equal(t, 1, len(parcels))
1311+
require.Len(t, parcels, 1)
1312+
require.Equal(t, spendDelta, parcels[0])
1313+
1314+
// We should also be able to query for the parcel when filtering on its
1315+
// anchor transaction hash.
1316+
parcels, err = assetsStore.QueryParcels(ctx, &anchorTxHash, true)
1317+
require.NoError(t, err)
1318+
require.Len(t, parcels, 1)
13121319
require.Equal(t, spendDelta, parcels[0])
13131320

13141321
// With the asset delta committed and verified, we'll now mark the
@@ -1421,7 +1428,7 @@ func TestAssetExportLog(t *testing.T) {
14211428
// At this point, there should be no more pending parcels.
14221429
parcels, err = assetsStore.PendingParcels(ctx)
14231430
require.NoError(t, err)
1424-
require.Equal(t, 0, len(parcels))
1431+
require.Len(t, parcels, 0)
14251432
}
14261433

14271434
// TestAssetGroupWitnessUpsert tests that if you try to insert another asset

taprpc/taprootassets.pb.go

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

taprpc/taprootassets.pb.gw.go

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

taprpc/taprootassets.proto

+4
Original file line numberDiff line numberDiff line change
@@ -460,6 +460,10 @@ message ListBalancesResponse {
460460
}
461461

462462
message ListTransfersRequest {
463+
// anchor_txid specifies the hexadecimal encoded txid string of the anchor
464+
// transaction for which to retrieve transfers. An empty value indicates
465+
// that this parameter should be disregarded in transfer selection.
466+
string anchor_txid = 1;
463467
}
464468

465469
message ListTransfersResponse {

taprpc/taprootassets.swagger.json

+41
Original file line numberDiff line numberDiff line change
@@ -419,6 +419,47 @@
419419
}
420420
}
421421
},
422+
"parameters": [
423+
{
424+
"name": "anchor_txid",
425+
"description": "anchor_txid specifies the hexadecimal encoded txid string of the anchor\ntransaction for which to retrieve transfers. An empty value indicates\nthat this parameter should be disregarded in transfer selection.",
426+
"in": "query",
427+
"required": false,
428+
"type": "string"
429+
}
430+
],
431+
"tags": [
432+
"TaprootAssets"
433+
]
434+
}
435+
},
436+
"/v1/taproot-assets/assets/transfers/{anchor_txid}": {
437+
"get": {
438+
"summary": "tapcli: `assets transfers`\nListTransfers lists outbound asset transfers tracked by the target daemon.",
439+
"operationId": "TaprootAssets_ListTransfers2",
440+
"responses": {
441+
"200": {
442+
"description": "A successful response.",
443+
"schema": {
444+
"$ref": "#/definitions/taprpcListTransfersResponse"
445+
}
446+
},
447+
"default": {
448+
"description": "An unexpected error response.",
449+
"schema": {
450+
"$ref": "#/definitions/rpcStatus"
451+
}
452+
}
453+
},
454+
"parameters": [
455+
{
456+
"name": "anchor_txid",
457+
"description": "anchor_txid specifies the hexadecimal encoded txid string of the anchor\ntransaction for which to retrieve transfers. An empty value indicates\nthat this parameter should be disregarded in transfer selection.",
458+
"in": "path",
459+
"required": true,
460+
"type": "string"
461+
}
462+
],
422463
"tags": [
423464
"TaprootAssets"
424465
]

taprpc/taprootassets.yaml

+2
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@ http:
7171

7272
- selector: taprpc.TaprootAssets.ListTransfers
7373
get: "/v1/taproot-assets/assets/transfers"
74+
additional_bindings:
75+
- get: "/v1/taproot-assets/assets/transfers/{anchor_txid}"
7476

7577
- selector: taprpc.TaprootAssets.FetchAssetMeta
7678
get: "/v1/taproot-assets/assets/meta/asset-id/{asset_id_str}"

0 commit comments

Comments
 (0)