Skip to content

Commit f124681

Browse files
committed
taprpc+tapdb: ListTransfers RPC optionally filters on anchor tx hash
This commit enhances the `ListTransfers` RPC endpoint by adding support for filtering on anchor transaction hash. This commit also extends the asset store `QueryParcels` method such that it also filters on anchor transaction hash.
1 parent 5a25a46 commit f124681

8 files changed

+731
-522
lines changed

rpcserver.go

Lines changed: 16 additions & 2 deletions
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

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2721,22 +2721,31 @@ func (a *AssetStore) reAnchorPassiveAssets(ctx context.Context,
27212721
func (a *AssetStore) PendingParcels(
27222722
ctx context.Context) ([]*tapfreighter.OutboundParcel, error) {
27232723

2724-
return a.QueryParcels(ctx, true)
2724+
return a.QueryParcels(ctx, nil, true)
27252725
}
27262726

27272727
// QueryParcels returns the set of confirmed or unconfirmed parcels.
27282728
func (a *AssetStore) QueryParcels(ctx context.Context,
2729+
anchorTxHash *chainhash.Hash,
27292730
pending bool) ([]*tapfreighter.OutboundParcel, error) {
27302731

27312732
var transfers []*tapfreighter.OutboundParcel
27322733

27332734
readOpts := NewAssetStoreReadTx()
27342735
dbErr := a.db.ExecTx(ctx, &readOpts, func(q ActiveAssetsStore) error {
2736+
// Construct transfer query.
2737+
transferQuery := TransferQuery{
2738+
UnconfOnly: pending,
2739+
}
2740+
2741+
// Include anchor tx hash if specified.
2742+
if anchorTxHash != nil {
2743+
transferQuery.AnchorTxHash = anchorTxHash[:]
2744+
}
2745+
27352746
// If we want every unconfirmed transfer, then we only pass in
27362747
// the UnconfOnly field.
2737-
dbTransfers, err := q.QueryAssetTransfers(ctx, TransferQuery{
2738-
UnconfOnly: pending,
2739-
})
2748+
dbTransfers, err := q.QueryAssetTransfers(ctx, transferQuery)
27402749
if err != nil {
27412750
return err
27422751
}

tapdb/assets_store_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1311,6 +1311,13 @@ func TestAssetExportLog(t *testing.T) {
13111311
require.Equal(t, 1, len(parcels))
13121312
require.Equal(t, spendDelta, parcels[0])
13131313

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)
1319+
require.Equal(t, spendDelta, parcels[0])
1320+
13141321
// With the asset delta committed and verified, we'll now mark the
13151322
// delta as being confirmed on chain.
13161323
fakeBlockHash := chainhash.Hash(sha256.Sum256([]byte("fake")))

taprpc/taprootassets.pb.go

Lines changed: 531 additions & 516 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

taprpc/taprootassets.pb.gw.go

Lines changed: 117 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

taprpc/taprootassets.proto

Lines changed: 4 additions & 0 deletions
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

Lines changed: 41 additions & 0 deletions
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

Lines changed: 2 additions & 0 deletions
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)