Skip to content

Commit a63b30d

Browse files
authored
Merge pull request #1871 from lightninglabs/wip/proof-transfer-logging
Improve proof transfer logging and adjust log levels for clearer diagnostics
2 parents 632294a + e4044ff commit a63b30d

File tree

6 files changed

+61
-29
lines changed

6 files changed

+61
-29
lines changed

proof/archive.go

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,10 +119,34 @@ func (l *Locator) Hash() ([32]byte, error) {
119119
}
120120
}
121121

122-
// Hash the buffer.
123122
return sha256.Sum256(buf.Bytes()), nil
124123
}
125124

125+
// LogString returns a human-readable representation of the locator. This is
126+
// primarily used for logging purposes.
127+
func (l *Locator) LogString() (string, error) {
128+
var zero string
129+
130+
assetSpec, err := asset.NewSpecifier(l.AssetID, l.GroupKey, nil, true)
131+
if err != nil {
132+
return zero, fmt.Errorf("unable to create asset specifier: %w",
133+
err)
134+
}
135+
136+
var outpointStr string
137+
if l.OutPoint != nil {
138+
outpointStr = l.OutPoint.String()
139+
}
140+
141+
locatorHash, err := l.Hash()
142+
if err != nil {
143+
return zero, fmt.Errorf("hashing locator: %w", err)
144+
}
145+
146+
return fmt.Sprintf("Locator(asset_spec=%s, outpoint=%s, hash=%x)",
147+
assetSpec.String(), outpointStr, locatorHash), nil
148+
}
149+
126150
// AnnotatedProof an annotated proof contains the raw proof blob along with a
127151
// locator that may convey additional information related to the proof.
128152
type AnnotatedProof struct {

proof/courier.go

Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"time"
1212

1313
"github.com/btcsuite/btcd/btcec/v2"
14+
"github.com/btcsuite/btclog/v2"
1415
"github.com/lightninglabs/lightning-node-connect/hashmailrpc"
1516
"github.com/lightninglabs/taproot-assets/asset"
1617
"github.com/lightninglabs/taproot-assets/fn"
@@ -613,12 +614,12 @@ func (b *BackoffHandler) initialDelay(ctx context.Context,
613614
return nil
614615
}
615616

616-
locatorHash, err := proofLocator.Hash()
617+
locatorStr, err := proofLocator.LogString()
617618
if err != nil {
618619
return err
619620
}
620-
log.Debugf("Handling initial proof transfer delay (locator_hash=%x)",
621-
locatorHash[:])
621+
log.DebugS(ctx, "Handling initial proof transfer delay", "locator",
622+
locatorStr)
622623

623624
// Query delivery log to ensure a sensible rate of delivery attempts.
624625
timestamps, err := b.transferLog.QueryProofTransferLog(
@@ -630,8 +631,8 @@ func (b *BackoffHandler) initialDelay(ctx context.Context,
630631
}
631632

632633
if len(timestamps) == 0 {
633-
log.Debugf("No previous transfer attempts found for proof "+
634-
"(locator_hash=%x)", locatorHash[:])
634+
log.DebugS(ctx, "No previous transfer attempts found for proof",
635+
"locator", locatorStr)
635636
return nil
636637
}
637638

@@ -648,9 +649,9 @@ func (b *BackoffHandler) initialDelay(ctx context.Context,
648649
backoffResetWait := b.cfg.BackoffResetWait
649650
if timeSinceLastAttempt < backoffResetWait {
650651
waitDuration := backoffResetWait - timeSinceLastAttempt
651-
log.Debugf("Waiting %v before attempting to transfer proof "+
652-
"(locator_hash=%x) using backoff procedure",
653-
waitDuration, locatorHash[:])
652+
log.DebugS(ctx, "Backoff: waiting before attempting to "+
653+
"transfer proof", "wait_duration", waitDuration,
654+
"locator", locatorStr)
654655

655656
err := b.wait(ctx, waitDuration)
656657
if err != nil {
@@ -672,13 +673,13 @@ func (b *BackoffHandler) Exec(ctx context.Context, proofLocator Locator,
672673
return fmt.Errorf("backoff config not specified")
673674
}
674675

675-
locatorHash, err := proofLocator.Hash()
676+
locatorStr, err := proofLocator.LogString()
676677
if err != nil {
677-
return err
678+
return fmt.Errorf("generate locator log string: %w", err)
678679
}
679-
log.Infof("Starting proof transfer backoff procedure "+
680-
"(transfer_type=%s, locator_hash=%x)", transferType,
681-
locatorHash[:])
680+
681+
log.InfoS(ctx, "Starting proof transfer backoff procedure",
682+
"transfer_type", transferType, "locator", locatorStr)
682683

683684
// Conditionally perform an initial delay based on the transfer log to
684685
// ensure that we don't spam the courier service with proof transfer
@@ -708,7 +709,7 @@ func (b *BackoffHandler) Exec(ctx context.Context, proofLocator Locator,
708709
"attempt: %w", err)
709710
}
710711

711-
// Execute target proof transfer function.
712+
// Execute the target proof transfer function.
712713
errExec = transferFunc()
713714
if errExec == nil {
714715
// The target function executed successfully, we can
@@ -732,10 +733,10 @@ func (b *BackoffHandler) Exec(ctx context.Context, proofLocator Locator,
732733
)
733734
subscriberEvent(waitEvent)
734735

735-
log.Debugf("Proof transfer failed with error. Backing off. "+
736-
"(transfer_type=%s, locator_hash=%x, backoff=%s, "+
737-
"attempt=%d): %v",
738-
transferType, locatorHash[:], backoff, i, errExec)
736+
log.DebugS(ctx, "Backing off: proof transfer failed",
737+
"transfer_type", transferType, "locator",
738+
locatorStr, "backoff", backoff, "attempt", i, "err",
739+
errExec)
739740

740741
// Wait before reattempting execution.
741742
err := b.wait(ctx, backoff)
@@ -976,8 +977,10 @@ func (h *HashMailCourier) DeliverProof(ctx context.Context, recipient Recipient,
976977

977978
return nil
978979
}
980+
981+
backoffCtx := btclog.WithCtx(ctx, "addr", h.addr.String())
979982
err := h.backoffHandle.Exec(
980-
ctx, proof.Locator, SendTransferType, deliveryExec,
983+
backoffCtx, proof.Locator, SendTransferType, deliveryExec,
981984
h.publishSubscriberEvent,
982985
)
983986
if err != nil {
@@ -1550,8 +1553,10 @@ func (c *UniverseRpcCourier) DeliverProof(ctx context.Context,
15501553

15511554
return nil
15521555
}
1556+
1557+
backoffCtx := btclog.WithCtx(ctx, "addr", c.addr.String())
15531558
err = c.backoffHandle.Exec(
1554-
ctx, loc, SendTransferType, deliverFunc,
1559+
backoffCtx, loc, SendTransferType, deliverFunc,
15551560
c.publishSubscriberEvent,
15561561
)
15571562
if err != nil {
@@ -1626,8 +1631,10 @@ func (c *UniverseRpcCourier) ReceiveProof(ctx context.Context,
16261631

16271632
return nil
16281633
}
1634+
1635+
backoffCtx := btclog.WithCtx(ctx, "addr", c.addr.String())
16291636
err := c.backoffHandle.Exec(
1630-
ctx, loc, ReceiveTransferType, receiveFunc,
1637+
backoffCtx, loc, ReceiveTransferType, receiveFunc,
16311638
c.publishSubscriberEvent,
16321639
)
16331640
if err != nil {

proof/taproot.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -423,7 +423,7 @@ func (p TaprootProof) DeriveByAssetExclusion(assetCommitmentKey,
423423
// asset ID, but we want to verify that the particular asset we care
424424
// about isn't included.
425425
default:
426-
log.Debugf("Deriving commitment by asset exclusion")
426+
log.Tracef("Deriving commitment by asset exclusion")
427427
tapCommitment, err = p.CommitmentProof.
428428
DeriveByAssetExclusion(assetCommitmentKey)
429429
}
@@ -485,7 +485,7 @@ func deriveCommitmentKeys(commitment *commitment.TapCommitment,
485485
commitmentKeys[downgradePubKey] = downgradedCommitment
486486
}
487487

488-
log.Debugf("Derived Taproot Asset commitment by %s "+
488+
log.Tracef("Derived Taproot Asset commitment by %s "+
489489
"taproot_asset_root=%x, internal_key=%x, Commitment V2 "+
490490
"taproot_key=%x, NonV2 taproot_key=%x",
491491
proofType, fn.ByteSlice(commitment.TapscriptRoot(nil)),

universe/archive.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,8 @@ func withUni[T any](fetcher uniFetcher, id Identifier,
129129
func (a *Archive) RootNode(ctx context.Context,
130130
id Identifier) (Root, error) {
131131

132-
log.Debugf("Looking up root node for base Universe %v", spew.Sdump(id))
132+
log.TraceS(ctx, "Looking up universe root node", "universe",
133+
id.StringForLog())
133134

134135
return a.cfg.Multiverse.UniverseRootNode(ctx, id)
135136
}

universe/federation.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -751,8 +751,8 @@ func (f *FederationEnvoy) SyncServers(serverAddrs []ServerAddr) error {
751751
syncServer := func(ctx context.Context, serverAddr ServerAddr) error {
752752
err := f.syncServerState(ctx, serverAddr, *syncConfigs)
753753
if err != nil {
754-
log.Warnf("encountered an error whilst syncing with "+
755-
"server=%v: %v", spew.Sdump(serverAddr), err)
754+
log.WarnS(ctx, "Error syncing with universe", err,
755+
"server", serverAddr.HostStr())
756756
}
757757
return nil
758758
}

universe/syncer.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -301,8 +301,8 @@ func (s *SimpleSyncer) syncRoot(ctx context.Context, remoteRoot Root,
301301

302302
// If the local root matches the remote root, then we're done here.
303303
case err == nil && mssmt.IsEqualNode(localRoot, remoteRoot):
304-
log.Debugf("Root for %v matches, no sync needed",
305-
uniID.String())
304+
log.DebugS(ctx, "Universe roots match, no sync needed",
305+
"universe", uniID.StringForLog())
306306

307307
return nil
308308

0 commit comments

Comments
 (0)