Skip to content

Commit 995f6d8

Browse files
committed
tapdb+tapfreighter: set and retrieve fields from new columns
Set and retrieve the `proof_delivery_complete` and `position` columns from the `asset_transfer_outputs` table.
1 parent 2c3b072 commit 995f6d8

File tree

3 files changed

+90
-22
lines changed

3 files changed

+90
-22
lines changed

tapdb/assets_store.go

Lines changed: 66 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2401,20 +2401,50 @@ func insertAssetTransferOutput(ctx context.Context, q ActiveAssetsStore,
24012401
return fmt.Errorf("unable to insert script key: %w", err)
24022402
}
24032403

2404+
// Now we will mark the output proof as undelivered if it is intended
2405+
// for a counterpart.
2406+
//
2407+
// If the transfer output proof is not intended for a counterpart the
2408+
// `proofDeliveryComplete` field will be left as NULL. Otherwise, we set
2409+
// it to false to indicate that the proof has not been delivered yet.
2410+
shouldDeliverProof, err := output.ShouldDeliverProof()
2411+
if err != nil {
2412+
return fmt.Errorf("unable to determine if proof should be "+
2413+
"delivery for given transfer output: %w", err)
2414+
}
2415+
2416+
var proofDeliveryComplete sql.NullBool
2417+
if shouldDeliverProof {
2418+
proofDeliveryComplete = sql.NullBool{
2419+
Bool: false,
2420+
Valid: true,
2421+
}
2422+
}
2423+
2424+
// Check if position value can be stored in a 32-bit integer. Type cast
2425+
// if possible, otherwise return an error.
2426+
if output.Position > math.MaxInt32 {
2427+
return fmt.Errorf("position value %d is too large for db "+
2428+
"storage", output.Position)
2429+
}
2430+
position := int32(output.Position)
2431+
24042432
dbOutput := NewTransferOutput{
2405-
TransferID: transferID,
2406-
AnchorUtxo: newUtxoID,
2407-
ScriptKey: scriptKeyID,
2408-
ScriptKeyLocal: output.ScriptKeyLocal,
2409-
Amount: int64(output.Amount),
2410-
LockTime: sqlInt32(output.LockTime),
2411-
RelativeLockTime: sqlInt32(output.RelativeLockTime),
2412-
AssetVersion: int32(output.AssetVersion),
2413-
SerializedWitnesses: witnessBuf.Bytes(),
2414-
ProofSuffix: output.ProofSuffix,
2415-
NumPassiveAssets: int32(output.Anchor.NumPassiveAssets),
2416-
OutputType: int16(output.Type),
2417-
ProofCourierAddr: output.ProofCourierAddr,
2433+
TransferID: transferID,
2434+
AnchorUtxo: newUtxoID,
2435+
ScriptKey: scriptKeyID,
2436+
ScriptKeyLocal: output.ScriptKeyLocal,
2437+
Amount: int64(output.Amount),
2438+
LockTime: sqlInt32(output.LockTime),
2439+
RelativeLockTime: sqlInt32(output.RelativeLockTime),
2440+
AssetVersion: int32(output.AssetVersion),
2441+
SerializedWitnesses: witnessBuf.Bytes(),
2442+
ProofSuffix: output.ProofSuffix,
2443+
NumPassiveAssets: int32(output.Anchor.NumPassiveAssets),
2444+
OutputType: int16(output.Type),
2445+
ProofCourierAddr: output.ProofCourierAddr,
2446+
ProofDeliveryComplete: proofDeliveryComplete,
2447+
Position: position,
24182448
}
24192449

24202450
// There might not have been a split, so we can't rely on the split root
@@ -2526,6 +2556,22 @@ func fetchAssetTransferOutputs(ctx context.Context, q ActiveAssetsStore,
25262556
outputAnchor.CommitmentVersion = fn.Ptr(dbRootVersion)
25272557
}
25282558

2559+
// Parse the proof deliver complete flag from the database.
2560+
var proofDeliveryComplete fn.Option[bool]
2561+
if dbOut.ProofDeliveryComplete.Valid {
2562+
proofDeliveryComplete = fn.Some(
2563+
dbOut.ProofDeliveryComplete.Bool,
2564+
)
2565+
}
2566+
2567+
vOutputType := tappsbt.VOutputType(dbOut.OutputType)
2568+
2569+
// Ensure the position value is valid.
2570+
if dbOut.Position < 0 {
2571+
return nil, fmt.Errorf("invalid position value in "+
2572+
"db: %d", dbOut.Position)
2573+
}
2574+
25292575
outputs[idx] = tapfreighter.TransferOutput{
25302576
Anchor: outputAnchor,
25312577
Amount: uint64(dbOut.Amount),
@@ -2549,9 +2595,11 @@ func fetchAssetTransferOutputs(ctx context.Context, q ActiveAssetsStore,
25492595
splitRootHash,
25502596
uint64(dbOut.SplitCommitmentRootValue.Int64),
25512597
),
2552-
ProofSuffix: dbOut.ProofSuffix,
2553-
Type: tappsbt.VOutputType(dbOut.OutputType),
2554-
ProofCourierAddr: dbOut.ProofCourierAddr,
2598+
ProofSuffix: dbOut.ProofSuffix,
2599+
Type: vOutputType,
2600+
ProofCourierAddr: dbOut.ProofCourierAddr,
2601+
ProofDeliveryComplete: proofDeliveryComplete,
2602+
Position: uint64(dbOut.Position),
25552603
}
25562604

25572605
err = readOutPoint(
@@ -2794,10 +2842,10 @@ func (a *AssetStore) ConfirmParcelDelivery(ctx context.Context,
27942842
!out.ScriptKeyLocal && !isKnown
27952843

27962844
log.Tracef("Skip asset creation for "+
2797-
"output %d?: %v, scriptKey=%x, "+
2845+
"output %d?: %v, position=%v, scriptKey=%x, "+
27982846
"isTombstone=%v, isBurn=%v, "+
27992847
"scriptKeyLocal=%v, scriptKeyKnown=%v",
2800-
idx, skipAssetCreation,
2848+
idx, skipAssetCreation, out.Position,
28012849
scriptPubKey.SerializeCompressed(),
28022850
isTombstone, isBurn, out.ScriptKeyLocal,
28032851
isKnown)

tapfreighter/interface.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,18 @@ type TransferOutput struct {
246246
// ProofCourierAddr is the bytes encoded proof courier service address
247247
// associated with this output.
248248
ProofCourierAddr []byte
249+
250+
// ProofDeliveryComplete is a flag that indicates whether the proof
251+
// delivery for this output is complete.
252+
//
253+
// This field can take one of the following values:
254+
// - None: A proof will not be delivered to a counterparty.
255+
// - False: The proof has not yet been delivered successfully.
256+
// - True: The proof has been delivered to the recipient.
257+
ProofDeliveryComplete fn.Option[bool]
258+
259+
// Position is the position of the output in the transfer output list.
260+
Position uint64
249261
}
250262

251263
// ShouldDeliverProof returns true if a proof corresponding to the subject

tapfreighter/parcel.go

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -531,17 +531,24 @@ func ConvertToTransfer(currentHeight uint32, activeTransfers []*tappsbt.VPacket,
531531
}
532532
}
533533

534+
// The outputPosition represents the index of the output within the list
535+
// of output transfers. It is continuously incremented across all
536+
// outputs and virtual packets.
537+
outputPosition := uint64(0)
538+
534539
for pIdx := range activeTransfers {
535540
vPkt := activeTransfers[pIdx]
536541

537-
for idx := range vPkt.Outputs {
542+
for vPktOutputIdx := range vPkt.Outputs {
538543
tOut, err := transferOutput(
539-
vPkt, idx, anchorTx, passiveAssets, isLocalKey,
544+
vPkt, vPktOutputIdx, outputPosition, anchorTx,
545+
passiveAssets, isLocalKey,
540546
)
541547
if err != nil {
542548
return nil, fmt.Errorf("unable to convert "+
543-
"output %d: %w", idx, err)
549+
"output %d: %w", vPktOutputIdx, err)
544550
}
551+
outputPosition += 1
545552

546553
parcel.Outputs = append(parcel.Outputs, *tOut)
547554
}
@@ -566,7 +573,7 @@ func transferInput(vIn *tappsbt.VInput) (*TransferInput, error) {
566573

567574
// transferOutput creates a TransferOutput from a virtual output and the anchor
568575
// packet.
569-
func transferOutput(vPkt *tappsbt.VPacket, vOutIdx int,
576+
func transferOutput(vPkt *tappsbt.VPacket, vOutIdx int, position uint64,
570577
anchorTx *tapsend.AnchorTransaction, passiveAssets []*tappsbt.VPacket,
571578
isLocalKey func(asset.ScriptKey) bool) (*TransferOutput, error) {
572579

@@ -612,6 +619,7 @@ func transferOutput(vPkt *tappsbt.VPacket, vOutIdx int,
612619
ProofSuffix: proofSuffixBuf.Bytes(),
613620
ProofCourierAddr: proofCourierAddrBytes,
614621
ScriptKeyLocal: isLocalKey(vOut.ScriptKey),
622+
Position: position,
615623
}, nil
616624
}
617625

0 commit comments

Comments
 (0)