Skip to content

Commit 8b5a01f

Browse files
committed
accounts: start logging useful info from payment request handlers
This commit adds a prefixed logger that is then used to log trace information about a payment. This will make it easy to see from the logs which account ID is making the request, what the request ID is and what the relavant payment hash is.
1 parent 9ffd05a commit 8b5a01f

File tree

4 files changed

+39
-13
lines changed

4 files changed

+39
-13
lines changed

accounts/checkers.go

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -522,12 +522,7 @@ func checkSend(ctx context.Context, chainParams *chaincfg.Params,
522522
service Service, amt, amtMsat int64, invoice string,
523523
paymentHash []byte, feeLimit *lnrpc.FeeLimit) error {
524524

525-
acct, err := AccountFromContext(ctx)
526-
if err != nil {
527-
return err
528-
}
529-
530-
reqID, err := RequestIDFromContext(ctx)
525+
log, acct, reqID, err := requestScopedValuesFromCtx(ctx)
531526
if err != nil {
532527
return err
533528
}
@@ -584,6 +579,9 @@ func checkSend(ctx context.Context, chainParams *chaincfg.Params,
584579
return fmt.Errorf("a payment hash is required")
585580
}
586581

582+
log.Tracef("Handling send request for payment with hash: %s and "+
583+
"amount: %d", pHash, sendAmt)
584+
587585
// We also add the max fee to the amount to check. This might mean that
588586
// not every single satoshi of an account can be used up. But it
589587
// prevents an account from going into a negative balance if we only
@@ -642,7 +640,7 @@ func checkSendResponse(ctx context.Context, service Service,
642640
func checkSendToRoute(ctx context.Context, service Service, paymentHash []byte,
643641
route *lnrpc.Route) error {
644642

645-
acct, err := AccountFromContext(ctx)
643+
log, acct, reqID, err := requestScopedValuesFromCtx(ctx)
646644
if err != nil {
647645
return err
648646
}
@@ -652,11 +650,6 @@ func checkSendToRoute(ctx context.Context, service Service, paymentHash []byte,
652650
return err
653651
}
654652

655-
reqID, err := RequestIDFromContext(ctx)
656-
if err != nil {
657-
return err
658-
}
659-
660653
if route == nil {
661654
return fmt.Errorf("invalid route")
662655
}
@@ -666,6 +659,9 @@ func checkSendToRoute(ctx context.Context, service Service, paymentHash []byte,
666659
sendAmt = lnwire.MilliSatoshi(route.TotalAmtMsat)
667660
}
668661

662+
log.Tracef("Handling send request for payment with hash: %s and "+
663+
"amount: %d", hash, sendAmt)
664+
669665
// We also add the max fee to the amount to check. This might mean that
670666
// not every single satoshi of an account can be used up. But it
671667
// prevents an account from going into a negative balance if we only

accounts/context.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ package accounts
33
import (
44
"context"
55
"fmt"
6+
7+
"github.com/btcsuite/btclog"
8+
"github.com/lightningnetwork/lnd/build"
69
)
710

811
// ContextKey is the type that we use to identify account specific values in the
@@ -72,3 +75,25 @@ func RequestIDFromContext(ctx context.Context) (uint64, error) {
7275

7376
return reqID, nil
7477
}
78+
79+
// requestScopedValuesFromCtx is a helper function that can be used to extract
80+
// an account and requestID from the given context. It also creates a new
81+
// prefixed logger that can be used by account request and response handlers.
82+
// Each log line will be prefixed by the account ID and the request ID.
83+
func requestScopedValuesFromCtx(ctx context.Context) (btclog.Logger,
84+
*OffChainBalanceAccount, uint64, error) {
85+
86+
acc, err := AccountFromContext(ctx)
87+
if err != nil {
88+
return nil, nil, 0, err
89+
}
90+
91+
reqID, err := RequestIDFromContext(ctx)
92+
if err != nil {
93+
return nil, nil, 0, err
94+
}
95+
96+
prefix := fmt.Sprintf("[account: %s, request: %d]", acc.ID, reqID)
97+
98+
return build.NewPrefixLog(prefix, log), acc, reqID, nil
99+
}

accounts/interface.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,11 @@ func ParseAccountID(idStr string) (*AccountID, error) {
5353
return &id, nil
5454
}
5555

56+
// String returns the string representation of the AccountID.
57+
func (a AccountID) String() string {
58+
return hex.EncodeToString(a[:])
59+
}
60+
5661
// PaymentEntry is the data we track per payment that is associated with an
5762
// account. This basically includes all information required to make sure
5863
// in-flight payments don't exceed the total available account balance.

accounts/log.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import (
55
"github.com/lightningnetwork/lnd/build"
66
)
77

8-
const Subsystem = "ACCT"
8+
const Subsystem = "ACNT"
99

1010
// log is a logger that is initialized with no output filters. This
1111
// means the package will not perform any logging by default until the caller

0 commit comments

Comments
 (0)