Skip to content

Commit 24de46f

Browse files
committed
tapgarden: extract receiving proof into method
1 parent 8af3cd3 commit 24de46f

File tree

1 file changed

+79
-82
lines changed

1 file changed

+79
-82
lines changed

tapgarden/custodian.go

Lines changed: 79 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -405,98 +405,95 @@ func (c *Custodian) inspectWalletTx(walletTx *lndclient.Transaction) error {
405405
// goroutine to use the ProofCourier to import the proof into
406406
// our local DB.
407407
c.Wg.Add(1)
408-
go func() {
409-
defer c.Wg.Done()
408+
go c.receiveProof(addr, op)
409+
}
410410

411-
ctx, cancel := c.WithCtxQuitNoTimeout()
412-
defer cancel()
411+
return nil
412+
}
413413

414-
assetID := addr.AssetID
414+
// receiveProof attempts to receive a proof for the given address and outpoint
415+
// via the proof courier service.
416+
//
417+
// NOTE: This must be called as a goroutine.
418+
func (c *Custodian) receiveProof(addr *address.Tap, op wire.OutPoint) {
419+
defer c.Wg.Done()
415420

416-
log.Debugf("Waiting to receive proof for script key %x",
417-
addr.ScriptKey.SerializeCompressed())
421+
ctx, cancel := c.WithCtxQuitNoTimeout()
422+
defer cancel()
418423

419-
// Initiate proof courier service handle from the proof
420-
// courier address found in the Tap address.
421-
recipient := proof.Recipient{
422-
ScriptKey: &addr.ScriptKey,
423-
AssetID: assetID,
424-
Amount: addr.Amount,
425-
}
426-
courier, err := c.cfg.ProofCourierDispatcher.NewCourier(
427-
&addr.ProofCourierAddr, recipient,
428-
)
429-
if err != nil {
430-
log.Errorf("unable to initiate proof courier "+
431-
"service handle: %v", err)
432-
return
433-
}
424+
assetID := addr.AssetID
434425

435-
// Update courier handle events subscribers before
436-
// attempting to retrieve proof.
437-
c.statusEventsSubsMtx.Lock()
438-
courier.SetSubscribers(c.statusEventsSubs)
439-
c.statusEventsSubsMtx.Unlock()
440-
441-
// Sleep to give the sender an opportunity to transfer
442-
// the proof to the proof courier service.
443-
// Without this delay our first attempt at retrieving
444-
// the proof will very likely fail. We should expect
445-
// retrieval success before this delay.
446-
select {
447-
case <-time.After(c.cfg.ProofRetrievalDelay):
448-
case <-ctx.Done():
449-
return
450-
}
426+
scriptKeyBytes := addr.ScriptKey.SerializeCompressed()
427+
log.Debugf("Waiting to receive proof for script key %x", scriptKeyBytes)
451428

452-
// Attempt to receive proof via proof courier service.
453-
loc := proof.Locator{
454-
AssetID: &assetID,
455-
GroupKey: addr.GroupKey,
456-
ScriptKey: addr.ScriptKey,
457-
OutPoint: &op,
458-
}
459-
addrProof, err := courier.ReceiveProof(ctx, loc)
460-
if err != nil {
461-
log.Errorf("unable to recv proof: %v", err)
462-
return
463-
}
429+
// Initiate proof courier service handle from the proof courier address
430+
// found in the Tap address.
431+
recipient := proof.Recipient{
432+
ScriptKey: &addr.ScriptKey,
433+
AssetID: assetID,
434+
Amount: addr.Amount,
435+
}
436+
courier, err := c.cfg.ProofCourierDispatcher.NewCourier(
437+
&addr.ProofCourierAddr, recipient,
438+
)
439+
if err != nil {
440+
log.Errorf("unable to initiate proof courier service handle: "+
441+
"%v", err)
442+
return
443+
}
464444

465-
log.Debugf("Received proof for: script_key=%x, "+
466-
"asset_id=%x",
467-
addr.ScriptKey.SerializeCompressed(),
468-
assetID[:])
469-
470-
ctx, cancel = c.CtxBlocking()
471-
defer cancel()
472-
473-
headerVerifier := GenHeaderVerifier(
474-
ctx, c.cfg.ChainBridge,
475-
)
476-
err = c.cfg.ProofArchive.ImportProofs(
477-
ctx, headerVerifier, c.cfg.GroupVerifier, false,
478-
addrProof,
479-
)
480-
if err != nil {
481-
log.Errorf("unable to import proofs: %v", err)
482-
return
483-
}
445+
// Update courier handle events subscribers before attempting to
446+
// retrieve proof.
447+
c.statusEventsSubsMtx.Lock()
448+
courier.SetSubscribers(c.statusEventsSubs)
449+
c.statusEventsSubsMtx.Unlock()
450+
451+
// Sleep to give the sender an opportunity to transfer the proof to the
452+
// proof courier service. Without this delay our first attempt at
453+
// retrieving the proof will very likely fail. We should expect
454+
// retrieval success before this delay.
455+
select {
456+
case <-time.After(c.cfg.ProofRetrievalDelay):
457+
case <-ctx.Done():
458+
return
459+
}
484460

485-
// At this point the "receive" process is complete. We
486-
// will now notify all status event subscribers.
487-
recvCompleteEvent := NewAssetRecvCompleteEvent(
488-
*addr, op,
489-
)
490-
err = c.publishSubscriberStatusEvent(recvCompleteEvent)
491-
if err != nil {
492-
log.Errorf("unable publish status event: %v",
493-
err)
494-
return
495-
}
496-
}()
461+
// Attempt to receive proof via proof courier service.
462+
loc := proof.Locator{
463+
AssetID: &assetID,
464+
GroupKey: addr.GroupKey,
465+
ScriptKey: addr.ScriptKey,
466+
OutPoint: &op,
467+
}
468+
addrProof, err := courier.ReceiveProof(ctx, loc)
469+
if err != nil {
470+
log.Errorf("Unable to receive proof using courier: %v", err)
471+
return
497472
}
498473

499-
return nil
474+
log.Debugf("Received proof for: script_key=%x, asset_id=%x",
475+
scriptKeyBytes, assetID[:])
476+
477+
ctx, cancel = c.CtxBlocking()
478+
defer cancel()
479+
480+
headerVerifier := GenHeaderVerifier(ctx, c.cfg.ChainBridge)
481+
err = c.cfg.ProofArchive.ImportProofs(
482+
ctx, headerVerifier, c.cfg.GroupVerifier, false, addrProof,
483+
)
484+
if err != nil {
485+
log.Errorf("Unable to import proofs: %v", err)
486+
return
487+
}
488+
489+
// At this point the "receive" process is complete. We will now notify
490+
// all status event subscribers.
491+
receiveCompleteEvent := NewAssetRecvCompleteEvent(*addr, op)
492+
err = c.publishSubscriberStatusEvent(receiveCompleteEvent)
493+
if err != nil {
494+
log.Errorf("Unable publish status event: %v", err)
495+
return
496+
}
500497
}
501498

502499
// mapToTapAddr attempts to match a transaction output to a Taproot Asset

0 commit comments

Comments
 (0)