Skip to content

Commit

Permalink
tapdb+taprpc: allow showing leased assets
Browse files Browse the repository at this point in the history
  • Loading branch information
guggero committed Aug 14, 2023
1 parent a102050 commit 214cb48
Show file tree
Hide file tree
Showing 9 changed files with 758 additions and 632 deletions.
27 changes: 20 additions & 7 deletions rpcserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -536,8 +536,14 @@ func (r *rpcServer) checkBalanceOverflow(ctx context.Context,
func (r *rpcServer) ListAssets(ctx context.Context,
req *taprpc.ListAssetRequest) (*taprpc.ListAssetResponse, error) {

switch {
case req.IncludeSpent && req.IncludeLeased:
return nil, fmt.Errorf("cannot specify both include_spent " +
"and include_leased")
}

rpcAssets, err := r.fetchRpcAssets(
ctx, req.WithWitness, req.IncludeSpent,
ctx, req.WithWitness, req.IncludeSpent, req.IncludeLeased,
)
if err != nil {
return nil, err
Expand All @@ -548,10 +554,12 @@ func (r *rpcServer) ListAssets(ctx context.Context,
}, nil
}

func (r *rpcServer) fetchRpcAssets(ctx context.Context,
withWitness, includeSpent bool) ([]*taprpc.Asset, error) {
func (r *rpcServer) fetchRpcAssets(ctx context.Context, withWitness,
includeSpent, includeLeased bool) ([]*taprpc.Asset, error) {

assets, err := r.cfg.AssetStore.FetchAllAssets(ctx, includeSpent, nil)
assets, err := r.cfg.AssetStore.FetchAllAssets(
ctx, includeSpent, includeLeased, nil,
)
if err != nil {
return nil, fmt.Errorf("unable to read chain assets: %w", err)
}
Expand Down Expand Up @@ -600,6 +608,11 @@ func (r *rpcServer) marshalChainAsset(ctx context.Context, a *tapdb.ChainAsset,
BlockHeight: a.AnchorBlockHeight,
}

if a.AnchorLeaseOwner != [32]byte{} {
rpcAsset.LeaseOwner = a.AnchorLeaseOwner[:]
rpcAsset.LeaseExpiry = a.AnchorLeaseExpiry.UTC().Unix()
}

return rpcAsset, nil
}

Expand Down Expand Up @@ -763,9 +776,9 @@ func (r *rpcServer) listBalancesByGroupKey(ctx context.Context,
// ListUtxos lists the UTXOs managed by the target daemon, and the assets they
// hold.
func (r *rpcServer) ListUtxos(ctx context.Context,
_ *taprpc.ListUtxosRequest) (*taprpc.ListUtxosResponse, error) {
req *taprpc.ListUtxosRequest) (*taprpc.ListUtxosResponse, error) {

rpcAssets, err := r.fetchRpcAssets(ctx, false, false)
rpcAssets, err := r.fetchRpcAssets(ctx, false, false, req.IncludeLeased)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -2794,7 +2807,7 @@ func (r *rpcServer) QueryProof(ctx context.Context,
return r.marshalIssuanceProof(ctx, req, proof)
}

// unmarsalAssetLeaf unmarshals an asset leaf from the RPC form.
// unmarshalAssetLeaf unmarshals an asset leaf from the RPC form.
func unmarshalAssetLeaf(leaf *unirpc.AssetLeaf) (*universe.MintingLeaf, error) {
// We'll just pull the asset details from the serialized issuance proof
// itself.
Expand Down
2 changes: 1 addition & 1 deletion tapdb/asset_minting_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -691,7 +691,7 @@ func TestCommitBatchChainActions(t *testing.T) {
// back the same number of seedlings.
//
// TODO(roasbeef): move into isolated test
assets, err := confAssets.FetchAllAssets(ctx, false, nil)
assets, err := confAssets.FetchAllAssets(ctx, false, false, nil)
require.NoError(t, err)
require.Equal(t, numSeedlings, len(assets))

Expand Down
9 changes: 7 additions & 2 deletions tapdb/assets_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -1012,8 +1012,8 @@ func (a *AssetStore) FetchGroupedAssets(ctx context.Context) (
}

// FetchAllAssets fetches the set of confirmed assets stored on disk.
func (a *AssetStore) FetchAllAssets(ctx context.Context, includeSpent bool,
query *AssetQueryFilters) ([]*ChainAsset, error) {
func (a *AssetStore) FetchAllAssets(ctx context.Context, includeSpent,
includeLeased bool, query *AssetQueryFilters) ([]*ChainAsset, error) {

var (
dbAssets []ConfirmedAsset
Expand All @@ -1032,6 +1032,11 @@ func (a *AssetStore) FetchAllAssets(ctx context.Context, includeSpent bool,
assetFilter.Spent = sqlBool(false)
}

// By default, we only show assets that are not leased.
if !includeLeased {
assetFilter.Leased = sqlBool(false)
}

// With the query constructed, we can now fetch the assets along w/
// their witness information.
readOpts := NewAssetStoreReadTx()
Expand Down
10 changes: 5 additions & 5 deletions tapdb/assets_store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ func TestImportAssetProof(t *testing.T) {

// We should now be able to retrieve the set of all assets inserted on
// disk.
assets, err := assetStore.FetchAllAssets(ctxb, false, nil)
assets, err := assetStore.FetchAllAssets(ctxb, false, false, nil)
require.NoError(t, err)
require.Len(t, assets, 1)

Expand Down Expand Up @@ -382,7 +382,7 @@ func TestImportAssetProof(t *testing.T) {
require.Equal(t, updatedBlob, []byte(currentBlob))

// Make sure the chain TX was updated as well.
assets, err = assetStore.FetchAllAssets(ctxb, false, nil)
assets, err = assetStore.FetchAllAssets(ctxb, false, false, nil)
require.NoError(t, err)
require.Len(t, assets, 1)

Expand Down Expand Up @@ -893,7 +893,7 @@ func TestAssetExportLog(t *testing.T) {

chainFees := int64(100)

allAssets, err := assetsStore.FetchAllAssets(ctx, true, nil)
allAssets, err := assetsStore.FetchAllAssets(ctx, true, false, nil)
require.NoError(t, err)
require.Len(t, allAssets, numAssets)

Expand Down Expand Up @@ -1076,7 +1076,7 @@ func TestAssetExportLog(t *testing.T) {

// We'll now fetch all the assets to verify that they were updated
// properly on disk.
chainAssets, err := assetsStore.FetchAllAssets(ctx, false, nil)
chainAssets, err := assetsStore.FetchAllAssets(ctx, false, true, nil)
require.NoError(t, err)

// We split one asset into two UTXOs, so there's now one more than
Expand Down Expand Up @@ -1260,7 +1260,7 @@ func TestFetchGroupedAssets(t *testing.T) {
)

// Fetch all assets to check the accuracy of other asset fields.
allAssets, err := assetsStore.FetchAllAssets(ctx, false, nil)
allAssets, err := assetsStore.FetchAllAssets(ctx, false, false, nil)
require.NoError(t, err)

// Sort assets to match the order of the asset descriptors.
Expand Down
Loading

0 comments on commit 214cb48

Please sign in to comment.