Skip to content

Commit d81bd74

Browse files
author
ffranr
authored
Merge pull request #1176 from lightninglabs/hashmail-deliver-logging
Hashmail logging cleanup
2 parents 55c55c9 + f9581d5 commit d81bd74

File tree

2 files changed

+105
-28
lines changed

2 files changed

+105
-28
lines changed

proof/courier.go

Lines changed: 33 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -439,16 +439,19 @@ func (h *HashMailBox) RecvAck(ctx context.Context, sid streamID) error {
439439
return fmt.Errorf("unable to create read stream: %w", err)
440440
}
441441

442+
log.Debugf("Exec stream Recv for receiver ACK (sid=%x)", sid[:])
442443
msg, err := readStream.Recv()
443444
if err != nil {
444-
return err
445+
return fmt.Errorf("failed on stream Recv (sid=%x): %w", sid[:],
446+
err)
445447
}
446448

447449
if bytes.Equal(msg.Msg, ackMsg) {
450+
log.Debugf("Received ACK from sender (sid=%x)", sid[:])
448451
return nil
449452
}
450453

451-
return fmt.Errorf("expected ack, got %x", msg.Msg)
454+
return fmt.Errorf("expected ACK from hashmail service, got %x", msg.Msg)
452455
}
453456

454457
// CleanUp attempts to tear down the mailbox as specified by the passed sid.
@@ -867,13 +870,20 @@ func (h *HashMailCourier) ensureConnect(ctx context.Context) error {
867870
func (h *HashMailCourier) DeliverProof(ctx context.Context,
868871
recipient Recipient, proof *AnnotatedProof) error {
869872

870-
log.Infof("Attempting to deliver receiver proof for send of "+
871-
"asset_id=%v, amt=%v", recipient.AssetID, recipient.Amount)
872-
873-
// Compute the stream IDs for the sender and receiver.
873+
// Compute the stream IDs for the sender and receiver. Note that these
874+
// stream IDs are derived from the recipient's script key only. Which
875+
// means that stream IDs will be identical for multiple proofs sent to
876+
// the same recipient.
874877
senderStreamID := deriveSenderStreamID(recipient)
875878
receiverStreamID := deriveReceiverStreamID(recipient)
876879

880+
log.Infof("Delivering proof to asset transfer receiver "+
881+
"(amt=%v, asset_id=%v, script_pub_key=%x, "+
882+
"sender_sid=%x, receiver_sid=%x)",
883+
recipient.Amount, recipient.AssetID,
884+
recipient.ScriptKey.SerializeCompressed(), senderStreamID,
885+
receiverStreamID)
886+
877887
// Interact with the hashmail service using a backoff procedure to
878888
// ensure that we don't overwhelm the service with delivery attempts.
879889
deliveryExec := func() error {
@@ -901,8 +911,7 @@ func (h *HashMailCourier) DeliverProof(ctx context.Context,
901911
// TODO(roasbeef): do ecies here
902912
// (this ^ TODO relates to encrypting proofs for the receiver
903913
// before uploading to the courier)
904-
log.Infof("Sending receiver proof via sid=%x",
905-
senderStreamID)
914+
log.Infof("Writing proof to mailbox (sid=%x)", senderStreamID)
906915
err = h.mailbox.WriteProof(
907916
ctx, senderStreamID, proof.Blob,
908917
)
@@ -911,21 +920,27 @@ func (h *HashMailCourier) DeliverProof(ctx context.Context,
911920
"transfer receiver: %w", err)
912921
}
913922

914-
// Wait to receive the ACK from the remote party over
915-
// their stream.
916-
log.Infof("Waiting (%v) for receiver ACK via sid=%x",
917-
h.cfg.ReceiverAckTimeout, receiverStreamID)
923+
// Wait to receive ACK from proof transfer receiving peer over
924+
// hashmail service.
925+
log.Infof("Waiting for receiver ACK from hashmail service "+
926+
"(timeout=%v, sid=%x)", h.cfg.ReceiverAckTimeout,
927+
receiverStreamID)
918928

919929
ctxTimeout, cancel := context.WithTimeout(
920930
ctx, h.cfg.ReceiverAckTimeout,
921931
)
922932
defer cancel()
923933
err = h.mailbox.RecvAck(ctxTimeout, receiverStreamID)
924934
if err != nil {
925-
return fmt.Errorf("failed to receive ACK from "+
926-
"receiver within timeout: %w", err)
935+
return fmt.Errorf("failed to retrieve proof transfer "+
936+
"receiver ACK within timeout (sid=%x): %w",
937+
receiverStreamID, err)
927938
}
928939

940+
log.Infof("Retrieved proof transfer receiver ACK from "+
941+
"hashmail service (timeout=%v, sid=%x)",
942+
h.cfg.ReceiverAckTimeout, receiverStreamID)
943+
929944
return nil
930945
}
931946
err := h.backoffHandle.Exec(
@@ -937,12 +952,12 @@ func (h *HashMailCourier) DeliverProof(ctx context.Context,
937952
"failed: %w", err)
938953
}
939954

940-
log.Infof("Received ACK from receiver! Cleaning up mailboxes...")
941-
942955
defer h.Close()
943956

944-
// Once we receive this ACK, we can clean up our mailbox and also the
945-
// receiver's mailbox.
957+
// If the backoff handler's exec routine completes successfully, we can
958+
// remove our mailbox and the receiver's mailbox.
959+
log.Infof("Removing sender and recipient mailboxes from hashmail " +
960+
"service")
946961
if err := h.mailbox.CleanUp(ctx, senderStreamID); err != nil {
947962
return fmt.Errorf("failed to cleanup sender mailbox: %w", err)
948963
}

tapfreighter/chain_porter.go

Lines changed: 72 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -763,15 +763,72 @@ func (p *ChainPorter) updateAssetProofFile(ctx context.Context,
763763
}, nil
764764
}
765765

766+
// reportProofTransfers logs a summary of the transfer outputs that require
767+
// proof delivery and those that do not.
768+
func reportProofTransfers(notDeliveringOutputs []TransferOutput,
769+
pendingDeliveryOutputs []TransferOutput) {
770+
771+
log.Debugf("Count of transfer output(s) by proof delivery status: "+
772+
"(count_delivery_not_applicable=%d, count_pending_delivery=%d)",
773+
len(notDeliveringOutputs), len(pendingDeliveryOutputs))
774+
775+
// Report the transfer outputs that do not require proof delivery.
776+
if len(notDeliveringOutputs) > 0 {
777+
logEntries := make([]string, 0, len(notDeliveringOutputs))
778+
for idx := range notDeliveringOutputs {
779+
out := notDeliveringOutputs[idx]
780+
key := out.ScriptKey.PubKey
781+
782+
entry := fmt.Sprintf("transfer_output_position=%d, "+
783+
"proof_delivery_status=%v, "+
784+
"script_key=%x", out.Position,
785+
out.ProofDeliveryComplete,
786+
key.SerializeCompressed())
787+
logEntries = append(logEntries, entry)
788+
}
789+
790+
entriesJoin := strings.Join(logEntries, "\n")
791+
log.Debugf("Transfer outputs that do not require proof "+
792+
"delivery:\n%v", entriesJoin)
793+
}
794+
795+
// Report the transfer outputs that require proof delivery.
796+
if len(pendingDeliveryOutputs) > 0 {
797+
logEntries := make([]string, 0, len(pendingDeliveryOutputs))
798+
for idx := range pendingDeliveryOutputs {
799+
out := pendingDeliveryOutputs[idx]
800+
key := out.ScriptKey.PubKey
801+
802+
entry := fmt.Sprintf("transfer_output_position=%d, "+
803+
"proof_delivery_status=%v, "+
804+
"proof_courier_addr=%s, "+
805+
"script_key=%x", out.Position,
806+
out.ProofDeliveryComplete, out.ProofCourierAddr,
807+
key.SerializeCompressed())
808+
logEntries = append(logEntries, entry)
809+
}
810+
811+
entriesJoin := strings.Join(logEntries, "\n")
812+
log.Debugf("Transfer outputs that require proof delivery:\n%v",
813+
entriesJoin)
814+
}
815+
}
816+
766817
// transferReceiverProof retrieves the sender and receiver proofs from the
767818
// archive and then transfers the receiver's proof to the receiver. Upon
768819
// successful transfer, the asset parcel delivery is marked as complete.
769820
func (p *ChainPorter) transferReceiverProof(pkg *sendPackage) error {
770821
ctx, cancel := p.WithCtxQuitNoTimeout()
771822
defer cancel()
772823

773-
deliver := func(ctx context.Context, out TransferOutput) error {
774-
key := out.ScriptKey.PubKey
824+
// Classify transfer outputs into those that require proof delivery and
825+
// those that do not.
826+
var (
827+
notDeliveringOutputs []TransferOutput
828+
pendingDeliveryOutputs []TransferOutput
829+
)
830+
for idx := range pkg.OutboundPkg.Outputs {
831+
out := pkg.OutboundPkg.Outputs[idx]
775832

776833
// We'll first check to see if the proof should be delivered.
777834
shouldDeliverProof, err := out.ShouldDeliverProof()
@@ -781,15 +838,20 @@ func (p *ChainPorter) transferReceiverProof(pkg *sendPackage) error {
781838
}
782839

783840
if !shouldDeliverProof {
784-
log.Debugf("Transfer ouput proof does not require "+
785-
"delivery (transfer_output_position=%d, "+
786-
"proof_delivery_status=%v, "+
787-
"script_key=%x)", out.Position,
788-
out.ProofDeliveryComplete,
789-
key.SerializeCompressed())
790-
return nil
841+
notDeliveringOutputs = append(notDeliveringOutputs, out)
842+
continue
791843
}
792844

845+
pendingDeliveryOutputs = append(pendingDeliveryOutputs, out)
846+
}
847+
848+
// Log a summary of the transfer outputs that require proof delivery and
849+
// those that do not.
850+
reportProofTransfers(notDeliveringOutputs, pendingDeliveryOutputs)
851+
852+
deliver := func(ctx context.Context, out TransferOutput) error {
853+
key := out.ScriptKey.PubKey
854+
793855
// We just look for the full proof in the list of final proofs
794856
// by matching the content of the proof suffix.
795857
var receiverProof *proof.AnnotatedProof
@@ -871,7 +933,7 @@ func (p *ChainPorter) transferReceiverProof(pkg *sendPackage) error {
871933
// If we have a non-interactive proof, then we'll launch several
872934
// goroutines to deliver the proof(s) to the receiver(s).
873935
instanceErrors, err := fn.ParSliceErrCollect(
874-
ctx, pkg.OutboundPkg.Outputs, deliver,
936+
ctx, pendingDeliveryOutputs, deliver,
875937
)
876938
if err != nil {
877939
return fmt.Errorf("error delivering proof(s): %w", err)

0 commit comments

Comments
 (0)