Skip to content

Commit 5282f0e

Browse files
committed
htlcswitch+routing: continue threading context through
In this commit, we remove a `context.TODO()` added in the `Switch.SendHTLC` method & instead pass a context through to the method. The rest of the commit is all about threading contexts through to SendHTLC.
1 parent b9ebb54 commit 5282f0e

File tree

10 files changed

+101
-64
lines changed

10 files changed

+101
-64
lines changed

htlcswitch/link_test.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1585,6 +1585,7 @@ func TestChannelLinkMultiHopInsufficientPayment(t *testing.T) {
15851585
// from Alice if she received not suitable payment hash for htlc.
15861586
func TestChannelLinkMultiHopUnknownPaymentHash(t *testing.T) {
15871587
t.Parallel()
1588+
ctx := context.Background()
15881589

15891590
channels, _, err := createClusterChannels(
15901591
t, btcutil.SatoshiPerBitcoin*5, btcutil.SatoshiPerBitcoin*5,
@@ -1624,7 +1625,7 @@ func TestChannelLinkMultiHopUnknownPaymentHash(t *testing.T) {
16241625

16251626
// Send payment and expose err channel.
16261627
err = n.aliceServer.htlcSwitch.SendHTLC(
1627-
n.firstBobChannelLink.ShortChanID(), pid, htlc,
1628+
ctx, n.firstBobChannelLink.ShortChanID(), pid, htlc,
16281629
)
16291630
require.NoError(t, err, "unable to get send payment")
16301631

@@ -4518,6 +4519,7 @@ func TestChannelLinkUpdateCommitFee(t *testing.T) {
45184519
// failures, reducing ambiguity when a batch is only partially processed.
45194520
func TestChannelLinkAcceptDuplicatePayment(t *testing.T) {
45204521
t.Parallel()
4522+
ctx := context.Background()
45214523

45224524
// First, we'll create our traditional three hop network. We'll only be
45234525
// interacting with and asserting the state of two of the end points
@@ -4559,7 +4561,7 @@ func TestChannelLinkAcceptDuplicatePayment(t *testing.T) {
45594561
// With the invoice now added to Carol's registry, we'll send the
45604562
// payment.
45614563
err = n.aliceServer.htlcSwitch.SendHTLC(
4562-
n.firstBobChannelLink.ShortChanID(), pid, htlc,
4564+
ctx, n.firstBobChannelLink.ShortChanID(), pid, htlc,
45634565
)
45644566
require.NoError(t, err, "unable to send payment to carol")
45654567

@@ -4571,7 +4573,7 @@ func TestChannelLinkAcceptDuplicatePayment(t *testing.T) {
45714573
// Now, if we attempt to send the payment *again* it should be rejected
45724574
// as it's a duplicate request.
45734575
err = n.aliceServer.htlcSwitch.SendHTLC(
4574-
n.firstBobChannelLink.ShortChanID(), pid, htlc,
4576+
ctx, n.firstBobChannelLink.ShortChanID(), pid, htlc,
45754577
)
45764578
if err != ErrDuplicateAdd {
45774579
t.Fatalf("ErrDuplicateAdd should have been "+

htlcswitch/switch.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -546,10 +546,8 @@ func (s *Switch) CleanStore(keepPids map[uint64]struct{}) error {
546546
// package in order to send the htlc update. The attemptID used MUST be unique
547547
// for this HTLC, and MUST be used only once, otherwise the switch might reject
548548
// it.
549-
func (s *Switch) SendHTLC(firstHop lnwire.ShortChannelID, attemptID uint64,
550-
htlc *lnwire.UpdateAddHTLC) error {
551-
552-
ctx := context.TODO()
549+
func (s *Switch) SendHTLC(ctx context.Context, firstHop lnwire.ShortChannelID,
550+
attemptID uint64, htlc *lnwire.UpdateAddHTLC) error {
553551

554552
// Generate and send new update packet, if error will be received on
555553
// this stage it means that packet haven't left boundaries of our

htlcswitch/switch_test.go

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -728,7 +728,7 @@ func testSwitchSendHtlcMapping(t *testing.T, zeroConf, useAlias bool, alias,
728728
Amount: 1,
729729
}
730730

731-
err = s.SendHTLC(outgoingSCID, 0, htlc)
731+
err = s.SendHTLC(context.Background(), outgoingSCID, 0, htlc)
732732
require.NoError(t, err)
733733
}
734734

@@ -2364,7 +2364,9 @@ func testSkipLinkLocalForward(t *testing.T, eligible bool,
23642364
// We'll attempt to send out a new HTLC that has Alice as the first
23652365
// outgoing link. This should fail as Alice isn't yet able to forward
23662366
// any active HTLC's.
2367-
err = s.SendHTLC(aliceChannelLink.ShortChanID(), 0, addMsg)
2367+
err = s.SendHTLC(
2368+
context.Background(), aliceChannelLink.ShortChanID(), 0, addMsg,
2369+
)
23682370
if err == nil {
23692371
t.Fatalf("local forward should fail due to inactive link")
23702372
}
@@ -2693,7 +2695,8 @@ func TestSwitchSendPayment(t *testing.T) {
26932695
errChan := make(chan error)
26942696
go func() {
26952697
err := s.SendHTLC(
2696-
aliceChannelLink.ShortChanID(), paymentID, update,
2698+
context.Background(), aliceChannelLink.ShortChanID(),
2699+
paymentID, update,
26972700
)
26982701
if err != nil {
26992702
errChan <- err
@@ -3200,7 +3203,8 @@ func TestInvalidFailure(t *testing.T) {
32003203

32013204
// Send the request.
32023205
err = s.SendHTLC(
3203-
aliceChannelLink.ShortChanID(), paymentID, update,
3206+
context.Background(), aliceChannelLink.ShortChanID(), paymentID,
3207+
update,
32043208
)
32053209
require.NoError(t, err, "unable to send payment")
32063210

@@ -3552,7 +3556,8 @@ func (n *threeHopNetwork) sendThreeHopPayment(t *testing.T) (*lnwire.UpdateAddHT
35523556
require.NoError(t, err, "unable to add invoice in carol registry")
35533557

35543558
if err := n.aliceServer.htlcSwitch.SendHTLC(
3555-
n.firstBobChannelLink.ShortChanID(), pid, htlc,
3559+
context.Background(), n.firstBobChannelLink.ShortChanID(), pid,
3560+
htlc,
35563561
); err != nil {
35573562
t.Fatalf("could not send htlc")
35583563
}
@@ -4241,6 +4246,7 @@ func TestInterceptableSwitchWatchDog(t *testing.T) {
42414246
// have incoming or outgoing links that breach their fee thresholds.
42424247
func TestSwitchDustForwarding(t *testing.T) {
42434248
t.Parallel()
4249+
ctx := context.Background()
42444250

42454251
// We'll create a three-hop network:
42464252
// - Alice has a dust limit of 200sats with Bob
@@ -4344,7 +4350,7 @@ func TestSwitchDustForwarding(t *testing.T) {
43444350
// anchor channel) we are overexposed in fees (maxFeeExposure) that's
43454351
// why the HTLC is failed back.
43464352
err = n.bobServer.htlcSwitch.SendHTLC(
4347-
aliceBobFirstHop, uint64(bobAttemptID), failingHtlc,
4353+
ctx, aliceBobFirstHop, uint64(bobAttemptID), failingHtlc,
43484354
)
43494355
require.Nil(t, err)
43504356

@@ -4385,7 +4391,7 @@ func TestSwitchDustForwarding(t *testing.T) {
43854391
}
43864392

43874393
err = n.bobServer.htlcSwitch.SendHTLC(
4388-
aliceBobFirstHop, uint64(bobAttemptID), nondustHtlc,
4394+
ctx, aliceBobFirstHop, uint64(bobAttemptID), nondustHtlc,
43894395
)
43904396
require.NoError(t, err)
43914397
assertAlmostDust(n.firstBobChannelLink, bobMbox, lntypes.Local)
@@ -4426,7 +4432,7 @@ func TestSwitchDustForwarding(t *testing.T) {
44264432
carolAttemptID := 0
44274433

44284434
err = n.carolServer.htlcSwitch.SendHTLC(
4429-
n.carolChannelLink.ShortChanID(), uint64(carolAttemptID),
4435+
ctx, n.carolChannelLink.ShortChanID(), uint64(carolAttemptID),
44304436
carolHtlc,
44314437
)
44324438
require.NoError(t, err)
@@ -4469,7 +4475,7 @@ func TestSwitchDustForwarding(t *testing.T) {
44694475
assertAlmostDust(n.aliceChannelLink, aliceMbox, lntypes.Remote)
44704476

44714477
err = n.aliceServer.htlcSwitch.SendHTLC(
4472-
n.aliceChannelLink.ShortChanID(), uint64(aliceAttemptID),
4478+
ctx, n.aliceChannelLink.ShortChanID(), uint64(aliceAttemptID),
44734479
aliceMultihopHtlc,
44744480
)
44754481
require.Nil(t, err)
@@ -4555,7 +4561,9 @@ func sendDustHtlcs(t *testing.T, n *threeHopNetwork, alice bool,
45554561
// before all numHTLCs*2 HTLC's are sent due to double
45564562
// counting. Get around this by continuing to send
45574563
// until successful.
4558-
err = sendingSwitch.SendHTLC(sid, attemptID, htlc)
4564+
err = sendingSwitch.SendHTLC(
4565+
context.Background(), sid, attemptID, htlc,
4566+
)
45594567
if err == nil {
45604568
break
45614569
}
@@ -4660,7 +4668,7 @@ func TestSwitchMailboxDust(t *testing.T) {
46604668

46614669
// Sending one more HTLC to Alice should result in the fee threshold
46624670
// being breached.
4663-
err = s.SendHTLC(aliceChanID, 0, addMsg)
4671+
err = s.SendHTLC(context.Background(), aliceChanID, 0, addMsg)
46644672
require.ErrorIs(t, err, errFeeExposureExceeded)
46654673

46664674
// We'll now call ForwardPackets from Bob to ensure that the mailbox

htlcswitch/test_utils.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -768,6 +768,8 @@ func preparePayment(sendingPeer, receivingPeer lnpeer.Peer,
768768
sender := sendingPeer.(*mockServer)
769769
receiver := receivingPeer.(*mockServer)
770770

771+
ctx := context.Background()
772+
771773
// Generate route convert it to blob, and return next destination for
772774
// htlc add request.
773775
blob, err := generateRoute(hops...)
@@ -785,17 +787,14 @@ func preparePayment(sendingPeer, receivingPeer lnpeer.Peer,
785787

786788
// Check who is last in the route and add invoice to server registry.
787789
hash := invoice.Terms.PaymentPreimage.Hash()
788-
if err := receiver.registry.AddInvoice(
789-
context.Background(), *invoice, hash,
790-
); err != nil {
790+
err = receiver.registry.AddInvoice(ctx, *invoice, hash)
791+
if err != nil {
791792
return nil, nil, err
792793
}
793794

794795
// Send payment and expose err channel.
795796
return invoice, func() error {
796-
err := sender.htlcSwitch.SendHTLC(
797-
firstHop, pid, htlc,
798-
)
797+
err := sender.htlcSwitch.SendHTLC(ctx, firstHop, pid, htlc)
799798
if err != nil {
800799
return err
801800
}
@@ -1321,6 +1320,8 @@ func (n *twoHopNetwork) makeHoldPayment(sendingPeer, receivingPeer lnpeer.Peer,
13211320
invoiceAmt, htlcAmt lnwire.MilliSatoshi,
13221321
timelock uint32, preimage lntypes.Preimage) chan error {
13231322

1323+
ctx := context.Background()
1324+
13241325
paymentErr := make(chan error, 1)
13251326

13261327
sender := sendingPeer.(*mockServer)
@@ -1352,15 +1353,14 @@ func (n *twoHopNetwork) makeHoldPayment(sendingPeer, receivingPeer lnpeer.Peer,
13521353
}
13531354

13541355
// Check who is last in the route and add invoice to server registry.
1355-
if err := receiver.registry.AddInvoice(
1356-
context.Background(), *invoice, rhash,
1357-
); err != nil {
1356+
err = receiver.registry.AddInvoice(ctx, *invoice, rhash)
1357+
if err != nil {
13581358
paymentErr <- err
13591359
return paymentErr
13601360
}
13611361

13621362
// Send payment and expose err channel.
1363-
err = sender.htlcSwitch.SendHTLC(firstHop, pid, htlc)
1363+
err = sender.htlcSwitch.SendHTLC(ctx, firstHop, pid, htlc)
13641364
if err != nil {
13651365
paymentErr <- err
13661366
return paymentErr

lnrpc/routerrpc/router_server.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -923,11 +923,11 @@ func (s *Server) SendToRouteV2(ctx context.Context,
923923
// db.
924924
if req.SkipTempErr {
925925
attempt, err = s.cfg.Router.SendToRouteSkipTempErr(
926-
hash, route, firstHopRecords,
926+
ctx, hash, route, firstHopRecords,
927927
)
928928
} else {
929929
attempt, err = s.cfg.Router.SendToRoute(
930-
hash, route, firstHopRecords,
930+
ctx, hash, route, firstHopRecords,
931931
)
932932
}
933933
if attempt != nil {

routing/mock_test.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package routing
22

33
import (
4+
"context"
45
"fmt"
56
"sync"
67

@@ -29,7 +30,7 @@ type mockPaymentAttemptDispatcherOld struct {
2930

3031
var _ PaymentAttemptDispatcher = (*mockPaymentAttemptDispatcherOld)(nil)
3132

32-
func (m *mockPaymentAttemptDispatcherOld) SendHTLC(
33+
func (m *mockPaymentAttemptDispatcherOld) SendHTLC(_ context.Context,
3334
firstHop lnwire.ShortChannelID, pid uint64,
3435
_ *lnwire.UpdateAddHTLC) error {
3536

@@ -206,7 +207,7 @@ type mockPayerOld struct {
206207

207208
var _ PaymentAttemptDispatcher = (*mockPayerOld)(nil)
208209

209-
func (m *mockPayerOld) SendHTLC(_ lnwire.ShortChannelID,
210+
func (m *mockPayerOld) SendHTLC(_ context.Context, _ lnwire.ShortChannelID,
210211
paymentID uint64,
211212
_ *lnwire.UpdateAddHTLC) error {
212213

@@ -592,7 +593,8 @@ type mockPaymentAttemptDispatcher struct {
592593

593594
var _ PaymentAttemptDispatcher = (*mockPaymentAttemptDispatcher)(nil)
594595

595-
func (m *mockPaymentAttemptDispatcher) SendHTLC(firstHop lnwire.ShortChannelID,
596+
func (m *mockPaymentAttemptDispatcher) SendHTLC(_ context.Context,
597+
firstHop lnwire.ShortChannelID,
596598
pid uint64, htlcAdd *lnwire.UpdateAddHTLC) error {
597599

598600
args := m.Called(firstHop, pid, htlcAdd)

routing/payment_lifecycle.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,7 @@ lifecycle:
304304
}
305305

306306
// Once the attempt is created, send it to the htlcswitch.
307-
result, err := p.sendAttempt(attempt)
307+
result, err := p.sendAttempt(ctx, attempt)
308308
if err != nil {
309309
return exitWithErr(err)
310310
}
@@ -649,7 +649,7 @@ func (p *paymentLifecycle) createNewPaymentAttempt(rt *route.Route,
649649
// sendAttempt attempts to send the current attempt to the switch to complete
650650
// the payment. If this attempt fails, then we'll continue on to the next
651651
// available route.
652-
func (p *paymentLifecycle) sendAttempt(
652+
func (p *paymentLifecycle) sendAttempt(ctx context.Context,
653653
attempt *channeldb.HTLCAttempt) (*attemptResult, error) {
654654

655655
log.Debugf("Sending HTLC attempt(id=%v, total_amt=%v, first_hop_amt=%d"+
@@ -690,7 +690,9 @@ func (p *paymentLifecycle) sendAttempt(
690690
// the Switch successfully has persisted the payment attempt,
691691
// such that we can resume waiting for the result after a
692692
// restart.
693-
err = p.router.cfg.Payer.SendHTLC(firstHop, attempt.AttemptID, htlcAdd)
693+
err = p.router.cfg.Payer.SendHTLC(
694+
ctx, firstHop, attempt.AttemptID, htlcAdd,
695+
)
694696
if err != nil {
695697
log.Errorf("Failed sending attempt %d for payment %v to "+
696698
"switch: %v", attempt.AttemptID, p.identifier, err)

routing/router.go

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ type PaymentAttemptDispatcher interface {
108108
// forward a fully encoded payment to the first hop in the route
109109
// denoted by its public key. A non-nil error is to be returned if the
110110
// payment was unsuccessful.
111-
SendHTLC(firstHop lnwire.ShortChannelID,
111+
SendHTLC(ctx context.Context, firstHop lnwire.ShortChannelID,
112112
attemptID uint64,
113113
htlcAdd *lnwire.UpdateAddHTLC) error
114114

@@ -1020,21 +1020,22 @@ func (r *ChannelRouter) PreparePayment(payment *LightningPayment) (
10201020

10211021
// SendToRoute sends a payment using the provided route and fails the payment
10221022
// when an error is returned from the attempt.
1023-
func (r *ChannelRouter) SendToRoute(htlcHash lntypes.Hash, rt *route.Route,
1023+
func (r *ChannelRouter) SendToRoute(ctx context.Context, htlcHash lntypes.Hash,
1024+
rt *route.Route,
10241025
firstHopCustomRecords lnwire.CustomRecords) (*channeldb.HTLCAttempt,
10251026
error) {
10261027

1027-
return r.sendToRoute(htlcHash, rt, false, firstHopCustomRecords)
1028+
return r.sendToRoute(ctx, htlcHash, rt, false, firstHopCustomRecords)
10281029
}
10291030

10301031
// SendToRouteSkipTempErr sends a payment using the provided route and fails
10311032
// the payment ONLY when a terminal error is returned from the attempt.
1032-
func (r *ChannelRouter) SendToRouteSkipTempErr(htlcHash lntypes.Hash,
1033-
rt *route.Route,
1033+
func (r *ChannelRouter) SendToRouteSkipTempErr(ctx context.Context,
1034+
htlcHash lntypes.Hash, rt *route.Route,
10341035
firstHopCustomRecords lnwire.CustomRecords) (*channeldb.HTLCAttempt,
10351036
error) {
10361037

1037-
return r.sendToRoute(htlcHash, rt, true, firstHopCustomRecords)
1038+
return r.sendToRoute(ctx, htlcHash, rt, true, firstHopCustomRecords)
10381039
}
10391040

10401041
// sendToRoute attempts to send a payment with the given hash through the
@@ -1043,8 +1044,8 @@ func (r *ChannelRouter) SendToRouteSkipTempErr(htlcHash lntypes.Hash,
10431044
// information will contain the preimage. If an error occurs after the attempt
10441045
// was initiated, both return values will be non-nil. If skipTempErr is true,
10451046
// the payment won't be failed unless a terminal error has occurred.
1046-
func (r *ChannelRouter) sendToRoute(htlcHash lntypes.Hash, rt *route.Route,
1047-
skipTempErr bool,
1047+
func (r *ChannelRouter) sendToRoute(ctx context.Context, htlcHash lntypes.Hash,
1048+
rt *route.Route, skipTempErr bool,
10481049
firstHopCustomRecords lnwire.CustomRecords) (*channeldb.HTLCAttempt,
10491050
error) {
10501051

@@ -1166,7 +1167,7 @@ func (r *ChannelRouter) sendToRoute(htlcHash lntypes.Hash, rt *route.Route,
11661167
// the `err` returned here has already been processed by
11671168
// `handleSwitchErr`, which means if there's a terminal failure, the
11681169
// payment has been failed.
1169-
result, err := p.sendAttempt(attempt)
1170+
result, err := p.sendAttempt(ctx, attempt)
11701171
if err != nil {
11711172
return nil, err
11721173
}

0 commit comments

Comments
 (0)