Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
tapdb: refactor method QueryParcels for clarity
  • Loading branch information
ffranr committed Jul 31, 2024
commit 165ed77c54565d223fd1e97ab12c79e7ce0bf8a0
36 changes: 22 additions & 14 deletions tapdb/assets_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -3151,24 +3151,30 @@ func (a *AssetStore) PendingParcels(
// QueryParcels returns the set of confirmed or unconfirmed parcels.
func (a *AssetStore) QueryParcels(ctx context.Context,
anchorTxHash *chainhash.Hash,
pending bool) ([]*tapfreighter.OutboundParcel, error) {
unconfirmedTxOnly bool) ([]*tapfreighter.OutboundParcel, error) {

var transfers []*tapfreighter.OutboundParcel
var (
outboundParcels []*tapfreighter.OutboundParcel
readOpts = NewAssetStoreReadTx()
)

readOpts := NewAssetStoreReadTx()
dbErr := a.db.ExecTx(ctx, &readOpts, func(q ActiveAssetsStore) error {
// Construct transfer query.
transferQuery := TransferQuery{
UnconfOnly: pending,
//
// Serialise anchor tx hash as bytes if specified.
var anchorTxHashBytes []byte
if anchorTxHash != nil {
anchorTxHashBytes = anchorTxHash[:]
}

// Include anchor tx hash if specified.
if anchorTxHash != nil {
transferQuery.AnchorTxHash = anchorTxHash[:]
transferQuery := TransferQuery{
// If we want unconfirmed transfers only, we set the
// UnconfOnly field to true.
UnconfOnly: unconfirmedTxOnly,
AnchorTxHash: anchorTxHashBytes,
}

// If we want every unconfirmed transfer, then we only pass in
// the UnconfOnly field.
// Query for asset transfers.
dbTransfers, err := q.QueryAssetTransfers(ctx, transferQuery)
if err != nil {
return err
Expand All @@ -3177,6 +3183,7 @@ func (a *AssetStore) QueryParcels(ctx context.Context,
for idx := range dbTransfers {
dbT := dbTransfers[idx]

// Fetch the inputs and outputs for the transfer.
inputs, err := fetchAssetTransferInputs(ctx, q, dbT.ID)
if err != nil {
return fmt.Errorf("unable to fetch transfer "+
Expand All @@ -3192,7 +3199,8 @@ func (a *AssetStore) QueryParcels(ctx context.Context,
}

// We know that the anchor transaction is the same for
// each output, we can just fetch the first.
// each output. Therefore, we use the first output to
// fetch the transfer's anchor transaction.
if len(outputs) == 0 {
return fmt.Errorf("no outputs for transfer")
}
Expand All @@ -3213,15 +3221,15 @@ func (a *AssetStore) QueryParcels(ctx context.Context,
"anchor tx: %w", err)
}

transfer := &tapfreighter.OutboundParcel{
parcel := &tapfreighter.OutboundParcel{
AnchorTx: anchorTx,
AnchorTxHeightHint: uint32(dbT.HeightHint),
TransferTime: dbT.TransferTimeUnix.UTC(),
ChainFees: dbAnchorTx.ChainFees,
Inputs: inputs,
Outputs: outputs,
}
transfers = append(transfers, transfer)
outboundParcels = append(outboundParcels, parcel)
}

return nil
Expand All @@ -3230,7 +3238,7 @@ func (a *AssetStore) QueryParcels(ctx context.Context,
return nil, dbErr
}

return transfers, nil
return outboundParcels, nil
}

// ErrAssetMetaNotFound is returned when an asset meta is not found in the
Expand Down