Skip to content

Commit 6908372

Browse files
committed
tapdb+tapfreighter: log proof delivery as confirmed
Add a `ConfirmProofDelivery` method to the export log. This function is called after a proof has been successfully delivered to a remote peer.
1 parent 2841027 commit 6908372

File tree

3 files changed

+65
-1
lines changed

3 files changed

+65
-1
lines changed

tapdb/assets_store.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,12 @@ type (
126126
// output.
127127
NewTransferOutput = sqlc.InsertAssetTransferOutputParams
128128

129+
// OutputProofDeliveryStatus wraps the params needed to set the delivery
130+
// status of a given output proof.
131+
//
132+
// nolint: lll
133+
OutputProofDeliveryStatus = sqlc.SetTransferOutputProofDeliveryStatusParams
134+
129135
// NewPassiveAsset wraps the params needed to insert a new passive
130136
// asset.
131137
NewPassiveAsset = sqlc.InsertPassiveAssetParams
@@ -278,6 +284,11 @@ type ActiveAssetsStore interface {
278284
InsertAssetTransferOutput(ctx context.Context,
279285
arg NewTransferOutput) error
280286

287+
// SetTransferOutputProofDeliveryStatus sets the delivery status of a
288+
// given transfer output proof.
289+
SetTransferOutputProofDeliveryStatus(ctx context.Context,
290+
arg OutputProofDeliveryStatus) error
291+
281292
// FetchTransferInputs fetches the inputs to a given asset transfer.
282293
FetchTransferInputs(ctx context.Context,
283294
transferID int64) ([]TransferInputRow, error)
@@ -2750,6 +2761,43 @@ func (a *AssetStore) QueryProofTransferLog(ctx context.Context,
27502761
return timestamps, err
27512762
}
27522763

2764+
// ConfirmProofDelivery marks a transfer output proof as successfully
2765+
// delivered to counterparty.
2766+
func (a *AssetStore) ConfirmProofDelivery(ctx context.Context,
2767+
anchorOutpoint wire.OutPoint, outputPosition uint64) error {
2768+
2769+
// Serialize the anchor outpoint to bytes.
2770+
anchorOutpointBytes, err := encodeOutpoint(anchorOutpoint)
2771+
if err != nil {
2772+
return fmt.Errorf("unable to encode anchor outpoint: %w", err)
2773+
}
2774+
2775+
// Ensure that the position value can be stored in a 32-bit integer.
2776+
// Type cast if possible, otherwise return an error.
2777+
if outputPosition > math.MaxInt32 {
2778+
return fmt.Errorf("position value is too large for db: %d",
2779+
outputPosition)
2780+
}
2781+
outPosition := int32(outputPosition)
2782+
2783+
var writeTxOpts AssetStoreTxOptions
2784+
2785+
err = a.db.ExecTx(ctx, &writeTxOpts, func(q ActiveAssetsStore) error {
2786+
params := OutputProofDeliveryStatus{
2787+
DeliveryComplete: sqlBool(true),
2788+
SerializedAnchorOutpoint: anchorOutpointBytes,
2789+
Position: outPosition,
2790+
}
2791+
return q.SetTransferOutputProofDeliveryStatus(ctx, params)
2792+
})
2793+
if err != nil {
2794+
return fmt.Errorf("failed to confirm transfer output proof "+
2795+
"delivery status in db: %w", err)
2796+
}
2797+
2798+
return nil
2799+
}
2800+
27532801
// ConfirmParcelDelivery marks a spend event on disk as confirmed. This updates
27542802
// the on-chain reference information on disk to point to this new spend.
27552803
func (a *AssetStore) ConfirmParcelDelivery(ctx context.Context,

tapfreighter/chain_porter.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -640,6 +640,8 @@ func (p *ChainPorter) transferReceiverProof(pkg *sendPackage) error {
640640
ctx, cancel := p.WithCtxQuitNoTimeout()
641641
defer cancel()
642642

643+
anchorTXID := pkg.OutboundPkg.AnchorTx.TxHash()
644+
643645
deliver := func(ctx context.Context, out TransferOutput) error {
644646
key := out.ScriptKey.PubKey
645647

@@ -720,6 +722,16 @@ func (p *ChainPorter) transferReceiverProof(pkg *sendPackage) error {
720722
"courier service: %w", err)
721723
}
722724

725+
// The proof has been successfully delivered to the receiver.
726+
// Now, we will update our transfer log to reflect this.
727+
err = p.cfg.ExportLog.ConfirmProofDelivery(
728+
ctx, out.Anchor.OutPoint, out.Position,
729+
)
730+
if err != nil {
731+
return fmt.Errorf("unable to log proof delivery "+
732+
"confirmation: %w", err)
733+
}
734+
723735
return nil
724736
}
725737

@@ -764,7 +776,7 @@ func (p *ChainPorter) transferReceiverProof(pkg *sendPackage) error {
764776
// At this point we have the confirmation signal, so we can mark the
765777
// parcel delivery as completed in the database.
766778
err = p.cfg.ExportLog.ConfirmParcelDelivery(ctx, &AssetConfirmEvent{
767-
AnchorTXID: pkg.OutboundPkg.AnchorTx.TxHash(),
779+
AnchorTXID: anchorTXID,
768780
BlockHash: *pkg.TransferTxConfEvent.BlockHash,
769781
BlockHeight: int32(pkg.TransferTxConfEvent.BlockHeight),
770782
TxIndex: int32(pkg.TransferTxConfEvent.TxIndex),

tapfreighter/interface.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -415,6 +415,10 @@ type ExportLog interface {
415415
// transactions for re-broadcast.
416416
PendingParcels(context.Context) ([]*OutboundParcel, error)
417417

418+
// ConfirmProofDelivery marks a transfer output proof as successfully
419+
// transferred.
420+
ConfirmProofDelivery(context.Context, wire.OutPoint, uint64) error
421+
418422
// ConfirmParcelDelivery marks a spend event on disk as confirmed. This
419423
// updates the on-chain reference information on disk to point to this
420424
// new spend.

0 commit comments

Comments
 (0)