Skip to content

Commit e94fb87

Browse files
committed
tapdb: Update AssetQueryFilters to handle additional filter options
1 parent db28a03 commit e94fb87

File tree

4 files changed

+55
-15
lines changed

4 files changed

+55
-15
lines changed

tapdb/assets_store.go

Lines changed: 40 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -858,7 +858,7 @@ func (a *AssetStore) dbAssetsToChainAssets(dbAssets []ConfirmedAsset,
858858
// constraintsToDbFilter maps application level constraints to the set of
859859
// filters we use in the SQL queries.
860860
func (a *AssetStore) constraintsToDbFilter(
861-
query *AssetQueryFilters) QueryAssetFilters {
861+
query *AssetQueryFilters) (QueryAssetFilters, error) {
862862

863863
assetFilter := QueryAssetFilters{
864864
Now: sql.NullTime{
@@ -868,17 +868,36 @@ func (a *AssetStore) constraintsToDbFilter(
868868
}
869869
if query != nil {
870870
if query.MinAmt != 0 {
871-
assetFilter.MinAmt = sql.NullInt64{
872-
Int64: int64(query.MinAmt),
873-
Valid: true,
874-
}
871+
assetFilter.MinAmt = sqlInt64(query.MinAmt)
872+
}
873+
874+
if query.MaxAmt != 0 {
875+
assetFilter.MaxAmt = sqlInt64(query.MaxAmt)
875876
}
877+
876878
if query.MinAnchorHeight != 0 {
877879
assetFilter.MinAnchorHeight = sqlInt32(
878880
query.MinAnchorHeight,
879881
)
880882
}
881883

884+
if query.ScriptKey != nil {
885+
key := query.ScriptKey.PubKey
886+
assetFilter.TweakedScriptKey = key.SerializeCompressed()
887+
}
888+
889+
if query.AnchorPoint != nil {
890+
anchorPointBytes, err := encodeOutpoint(
891+
*query.AnchorPoint,
892+
)
893+
if err != nil {
894+
return QueryAssetFilters{}, fmt.Errorf(
895+
"unable to encode outpoint: %w", err)
896+
}
897+
898+
assetFilter.AnchorPoint = anchorPointBytes
899+
}
900+
882901
// Add asset ID bytes and group key bytes to the filter. These
883902
// byte arrays are empty if the asset ID or group key is not
884903
// specified in the query.
@@ -903,7 +922,7 @@ func (a *AssetStore) constraintsToDbFilter(
903922
}
904923
}
905924

906-
return assetFilter
925+
return assetFilter, nil
907926
}
908927

909928
// specificAssetFilter maps the given asset parameters to the set of filters
@@ -975,6 +994,13 @@ type AssetQueryFilters struct {
975994
// MinAnchorHeight is the minimum block height the asset's anchor tx
976995
// must have been confirmed at.
977996
MinAnchorHeight int32
997+
998+
// ScriptKey allows filtering by asset script key.
999+
ScriptKey *asset.ScriptKey
1000+
1001+
// AnchorPoint allows filtering by the outpoint the asset is anchored
1002+
// to.
1003+
AnchorPoint *wire.OutPoint
9781004
}
9791005

9801006
// QueryBalancesByAsset queries the balances for assets or alternatively
@@ -1194,7 +1220,10 @@ func (a *AssetStore) FetchAllAssets(ctx context.Context, includeSpent,
11941220

11951221
// We'll now map the application level filtering to the type of
11961222
// filtering our database query understands.
1197-
assetFilter := a.constraintsToDbFilter(query)
1223+
assetFilter, err := a.constraintsToDbFilter(query)
1224+
if err != nil {
1225+
return nil, err
1226+
}
11981227

11991228
// By default, the spent boolean is null, which means we'll fetch all
12001229
// assets. Only if we should exclude spent assets, we'll set the spent
@@ -1995,9 +2024,12 @@ func (a *AssetStore) ListEligibleCoins(ctx context.Context,
19952024

19962025
// First, we'll map the commitment constraints to our database query
19972026
// filters.
1998-
assetFilter := a.constraintsToDbFilter(&AssetQueryFilters{
2027+
assetFilter, err := a.constraintsToDbFilter(&AssetQueryFilters{
19992028
CommitmentConstraints: constraints,
20002029
})
2030+
if err != nil {
2031+
return nil, err
2032+
}
20012033

20022034
// We only want to select unspent and non-leased commitments.
20032035
assetFilter.Spent = sqlBool(false)

tapdb/sqlc/assets.sql.go

Lines changed: 10 additions & 7 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: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -498,6 +498,7 @@ JOIN chain_txns txns
498498
-- specified.
499499
WHERE (
500500
assets.amount >= COALESCE(sqlc.narg('min_amt'), assets.amount) AND
501+
assets.amount <= COALESCE(sqlc.narg('max_amt'), assets.amount) AND
501502
assets.spent = COALESCE(sqlc.narg('spent'), assets.spent) AND
502503
(key_group_info_view.tweaked_group_key = sqlc.narg('key_group_filter') OR
503504
sqlc.narg('key_group_filter') IS NULL) AND

tapfreighter/interface.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ type CommitmentConstraints struct {
3939
// to satisfy the constraints.
4040
MinAmt uint64
4141

42+
// MaxAmt specifies the maximum amount that an asset commitment needs to
43+
// hold to satisfy the constraints.
44+
MaxAmt uint64
45+
4246
// PrevIDs are the set of inputs allowed to be used.
4347
PrevIDs []asset.PrevID
4448

0 commit comments

Comments
 (0)