Skip to content

Commit a635090

Browse files
authored
Merge pull request #466 from bhandras/routing-plugin-force-mc-import
routing: integrate with the forced MC import to eliminate the "1 sec hack" when importing pair history
2 parents c05ee21 + eb1371e commit a635090

File tree

7 files changed

+53
-57
lines changed

7 files changed

+53
-57
lines changed

go.mod

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ require (
1111
github.com/grpc-ecosystem/grpc-gateway/v2 v2.5.0
1212
github.com/jessevdk/go-flags v1.4.0
1313
github.com/lightninglabs/aperture v0.1.6-beta
14-
github.com/lightninglabs/lndclient v0.14.0-8
14+
github.com/lightninglabs/lndclient v0.14.2-3
1515
github.com/lightninglabs/loop/swapserverrpc v1.0.0
1616
github.com/lightninglabs/protobuf-hex-display v1.4.3-hex-display
17-
github.com/lightningnetwork/lnd v0.14.2-beta.rc1
17+
github.com/lightningnetwork/lnd v0.14.2-beta
1818
github.com/lightningnetwork/lnd/cert v1.1.0
1919
github.com/lightningnetwork/lnd/clock v1.1.0
2020
github.com/lightningnetwork/lnd/queue v1.1.0
@@ -32,5 +32,5 @@ go 1.15
3232

3333
replace github.com/lightninglabs/loop/swapserverrpc => ./swapserverrpc
3434

35-
// TODO(bhandras): remove once v0.14.2-beta is tagged.
35+
// TODO(bhandras): remove once v0.14.3-beta or v0.15.0-beta is tagged.
3636
replace github.com/lightningnetwork/lnd => github.com/lightningnetwork/lnd v0.14.1-beta.0.20220131141659-60625b6c1a0b

go.sum

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -548,8 +548,8 @@ github.com/lightninglabs/aperture v0.1.6-beta/go.mod h1:9xl4mx778ZAzrB87nLHMqk+X
548548
github.com/lightninglabs/gozmq v0.0.0-20191113021534-d20a764486bf h1:HZKvJUHlcXI/f/O0Avg7t8sqkPo78HFzjmeYFl6DPnc=
549549
github.com/lightninglabs/gozmq v0.0.0-20191113021534-d20a764486bf/go.mod h1:vxmQPeIQxPf6Jf9rM8R+B4rKBqLA2AjttNxkFBL2Plk=
550550
github.com/lightninglabs/lndclient v0.11.0-4/go.mod h1:8/cTKNwgL87NX123gmlv3Xh6p1a7pvzu+40Un3PhHiI=
551-
github.com/lightninglabs/lndclient v0.14.0-8 h1:vdwV6yFU4A7BjG2V8cpI8Kqdl2M0NSfsA+RWR+JGTko=
552-
github.com/lightninglabs/lndclient v0.14.0-8/go.mod h1:YIE/Yac69hIMiq9cm/ZC2sP4F0Llv3tC4hZGfgOhdeY=
551+
github.com/lightninglabs/lndclient v0.14.2-3 h1:19HWiGCUNejCAm9Ffl5Y56A8u6ZDhhfyjPSDbMDmjuw=
552+
github.com/lightninglabs/lndclient v0.14.2-3/go.mod h1:8fu/+OAK1PqUx9oglz5p0WPOSAwKBUwrC3q3D1yi6Oo=
553553
github.com/lightninglabs/neutrino v0.12.1/go.mod h1:GlKninWpRBbL7b8G0oQ36/8downfnFwKsr0hbRA6E/E=
554554
github.com/lightninglabs/neutrino v0.13.0 h1:j3PKWEJCwqwMn/qLASz2j0IuCF6AumS9DaM0i0pM/nY=
555555
github.com/lightninglabs/neutrino v0.13.0/go.mod h1:GlKninWpRBbL7b8G0oQ36/8downfnFwKsr0hbRA6E/E=
@@ -568,7 +568,6 @@ github.com/lightningnetwork/lnd/clock v1.1.0/go.mod h1:KnQudQ6w0IAMZi1SgvecLZQZ4
568568
github.com/lightningnetwork/lnd/healthcheck v1.0.0/go.mod h1:u92p1JGFJNMSkMvztKEwmt1P3TRnLeJBXZ3M85xkU1E=
569569
github.com/lightningnetwork/lnd/healthcheck v1.2.0 h1:00bkNn+hGJM9j3Ht+ulf8YNcSx1HEYSDv5pf4HB1/uQ=
570570
github.com/lightningnetwork/lnd/healthcheck v1.2.0/go.mod h1:WSz3lsUjErJQZ3gb+zW32nM3WIHNiZ3A40GVoaQY5wU=
571-
github.com/lightningnetwork/lnd/kvdb v1.2.0/go.mod h1:4DW7zc9AWqqJBvwhSE251VasYikTELItN70hESIt9RU=
572571
github.com/lightningnetwork/lnd/kvdb v1.3.0 h1:0sadTsmfIoorGvd2zf09q/H7sxa/OU48NCpCQUPNN48=
573572
github.com/lightningnetwork/lnd/kvdb v1.3.0/go.mod h1:x+IpsuDynubjokUofavLXroeGfS/WrqUXXTK6vN/gp4=
574573
github.com/lightningnetwork/lnd/queue v1.0.1/go.mod h1:vaQwexir73flPW43Mrm7JOgJHmcEFBWWSl9HlyASoms=

loopd/run.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ var (
2626
// listed build tags/subservers need to be enabled.
2727
LoopMinRequiredLndVersion = &verrpc.Version{
2828
AppMajor: 0,
29-
AppMinor: 11,
30-
AppPatch: 1,
29+
AppMinor: 14,
30+
AppPatch: 2,
3131
BuildTags: []string{
3232
"signrpc", "walletrpc", "chainrpc", "invoicesrpc",
3333
},

release_notes.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ This file tracks release notes for the loop client.
1919
* Loop client now supports optional routing plugins to improve off-chain payment
2020
reliability. One such plugin that the client implemenets will gradually prefer
2121
increasingly more expensive routes in case payments using cheap routes time out.
22+
Note that with this addition the minimum required LND version is LND 0.14.2-beta.
2223

2324
#### Breaking Changes
2425

routing_plugin.go

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"fmt"
66
"sort"
77
"sync"
8-
"time"
98

109
"github.com/btcsuite/btclog"
1110
"github.com/btcsuite/btcutil"
@@ -520,13 +519,10 @@ func (r *lowToHighRoutingPlugin) BeforePayment(ctx context.Context,
520519
limit := minFee +
521520
((maxFee-minFee)/int64(maxAttempts))*int64(currAttempt)
522521

523-
// Create a timestamp just slightly in the future as Mission Control
524-
// stores timestamps with sub second precision where as we send a unix
525-
// timestamp it may occur that we can't override the last entries as
526-
// they have the same unix timestamp.
527-
// TODO(bhandras): not very reliable, ideally we'd need a force import
528-
// for MC.
529-
now := r.clock.Now().Add(time.Second)
522+
// With the forced MC import we can safely set the pair history
523+
// timestamps to the current time as import will always just override
524+
// current MC state.
525+
now := r.clock.Now()
530526

531527
allowed := 0
532528
entries := make(
@@ -566,7 +562,7 @@ func (r *lowToHighRoutingPlugin) BeforePayment(ctx context.Context,
566562
return ErrRoutingPluginNoMoreRetries
567563
}
568564

569-
err := r.lnd.Router.ImportMissionControl(ctx, entries)
565+
err := r.lnd.Router.ImportMissionControl(ctx, entries, true)
570566
if err != nil {
571567
return err
572568
}
@@ -606,10 +602,10 @@ func (r *lowToHighRoutingPlugin) Done(ctx context.Context) error {
606602
return nil
607603
}
608604

609-
// Roll the entry times forward (to be able to override recent updates).
610-
// Use the "time travel" trick which is required to make overrides
611-
// succeed.
612-
now := r.clock.Now().Add(time.Second)
605+
// With the forced import we're safe to just set the pair history
606+
// timestamps to the current time as import will always succeed and
607+
// override current MC state.
608+
now := r.clock.Now()
613609
entries := make(
614610
[]lndclient.MissionControlEntry, 0, len(r.nodesByMaxFee),
615611
)
@@ -647,7 +643,7 @@ func (r *lowToHighRoutingPlugin) Done(ctx context.Context) error {
647643
}
648644
}
649645

650-
err := r.lnd.Router.ImportMissionControl(ctx, entries)
646+
err := r.lnd.Router.ImportMissionControl(ctx, entries, true)
651647
if err != nil {
652648
return err
653649
}

routing_plugin_test.go

Lines changed: 29 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,6 @@ func TestLowHighRoutingPlugin(t *testing.T) {
8181
target := loopNode
8282
amt := btcutil.Amount(50)
8383
testTime := time.Now().UTC()
84-
// We expect Mission Control entries to be set to now + 1 sec.
85-
testTimeMc := testTime.Add(time.Second)
8684

8785
tests := []struct {
8886
name string
@@ -168,7 +166,7 @@ func TestLowHighRoutingPlugin(t *testing.T) {
168166
NodeTo: dave,
169167
FailTime: time.Time{},
170168
FailAmt: 0,
171-
SuccessTime: testTimeMc,
169+
SuccessTime: testTime,
172170
SuccessAmt: 10000,
173171
},
174172
},
@@ -210,14 +208,14 @@ func TestLowHighRoutingPlugin(t *testing.T) {
210208
{
211209
NodeFrom: bob,
212210
NodeTo: dave,
213-
FailTime: testTimeMc,
211+
FailTime: testTime,
214212
FailAmt: 1,
215213
},
216214
// Encourage Charlie - Dave
217215
{
218216
NodeFrom: charlie,
219217
NodeTo: dave,
220-
SuccessTime: testTimeMc,
218+
SuccessTime: testTime,
221219
SuccessAmt: 1000000,
222220
},
223221
},
@@ -228,15 +226,15 @@ func TestLowHighRoutingPlugin(t *testing.T) {
228226
NodeTo: dave,
229227
FailTime: time.Time{},
230228
FailAmt: 0,
231-
SuccessTime: testTimeMc,
229+
SuccessTime: testTime,
232230
SuccessAmt: 10000,
233231
},
234232
{
235233
NodeFrom: charlie,
236234
NodeTo: dave,
237-
FailTime: testTimeMc,
235+
FailTime: testTime,
238236
FailAmt: 1000001,
239-
SuccessTime: testTimeMc,
237+
SuccessTime: testTime,
240238
SuccessAmt: 1000000,
241239
},
242240
},
@@ -283,13 +281,13 @@ func TestLowHighRoutingPlugin(t *testing.T) {
283281
{
284282
NodeFrom: bob,
285283
NodeTo: dave,
286-
SuccessTime: testTimeMc,
284+
SuccessTime: testTime,
287285
SuccessAmt: 999000,
288286
},
289287
{
290288
NodeFrom: charlie,
291289
NodeTo: dave,
292-
SuccessTime: testTimeMc,
290+
SuccessTime: testTime,
293291
SuccessAmt: 999000,
294292
},
295293
},
@@ -304,17 +302,17 @@ func TestLowHighRoutingPlugin(t *testing.T) {
304302
{
305303
NodeFrom: bob,
306304
NodeTo: dave,
307-
SuccessTime: testTimeMc,
305+
SuccessTime: testTime,
308306
SuccessAmt: 999000,
309-
FailTime: testTimeMc,
307+
FailTime: testTime,
310308
FailAmt: 999001,
311309
},
312310
{
313311
NodeFrom: charlie,
314312
NodeTo: dave,
315-
SuccessTime: testTimeMc,
313+
SuccessTime: testTime,
316314
SuccessAmt: 999000,
317-
FailTime: testTimeMc,
315+
FailTime: testTime,
318316
FailAmt: 999001,
319317
},
320318
},
@@ -375,21 +373,21 @@ func TestLowHighRoutingPlugin(t *testing.T) {
375373
{
376374
NodeFrom: bob,
377375
NodeTo: eugene,
378-
FailTime: testTimeMc,
376+
FailTime: testTime,
379377
FailAmt: 1,
380378
},
381379
// Encourage Charlie - Eugene
382380
{
383381
NodeFrom: charlie,
384382
NodeTo: eugene,
385-
SuccessTime: testTimeMc,
383+
SuccessTime: testTime,
386384
SuccessAmt: 1000000,
387385
},
388386
// Encourage Dave - Eugene
389387
{
390388
NodeFrom: dave,
391389
NodeTo: eugene,
392-
SuccessTime: testTimeMc,
390+
SuccessTime: testTime,
393391
SuccessAmt: 1000000,
394392
},
395393
},
@@ -399,21 +397,21 @@ func TestLowHighRoutingPlugin(t *testing.T) {
399397
{
400398
NodeFrom: bob,
401399
NodeTo: eugene,
402-
FailTime: testTimeMc,
400+
FailTime: testTime,
403401
FailAmt: 1,
404402
},
405403
// Discourage Charlie - Eugene
406404
{
407405
NodeFrom: charlie,
408406
NodeTo: eugene,
409-
FailTime: testTimeMc,
407+
FailTime: testTime,
410408
FailAmt: 1,
411409
},
412410
// Encourage Dave - Eugene
413411
{
414412
NodeFrom: dave,
415413
NodeTo: eugene,
416-
SuccessTime: testTimeMc,
414+
SuccessTime: testTime,
417415
SuccessAmt: 1000000,
418416
},
419417
},
@@ -422,25 +420,25 @@ func TestLowHighRoutingPlugin(t *testing.T) {
422420
{
423421
NodeFrom: bob,
424422
NodeTo: eugene,
425-
FailTime: testTimeMc,
423+
FailTime: testTime,
426424
FailAmt: 1000001,
427-
SuccessTime: testTimeMc,
425+
SuccessTime: testTime,
428426
SuccessAmt: 1000000,
429427
},
430428
{
431429
NodeFrom: charlie,
432430
NodeTo: eugene,
433431
FailTime: time.Time{},
434432
FailAmt: 0,
435-
SuccessTime: testTimeMc,
433+
SuccessTime: testTime,
436434
SuccessAmt: 10000,
437435
},
438436
{
439437
NodeFrom: dave,
440438
NodeTo: eugene,
441-
FailTime: testTimeMc,
439+
FailTime: testTime,
442440
FailAmt: 1000001,
443-
SuccessTime: testTimeMc,
441+
SuccessTime: testTime,
444442
SuccessAmt: 1000000,
445443
},
446444
},
@@ -500,14 +498,14 @@ func TestLowHighRoutingPlugin(t *testing.T) {
500498
{
501499
NodeFrom: frank,
502500
NodeTo: george,
503-
FailTime: testTimeMc,
501+
FailTime: testTime,
504502
FailAmt: 1,
505503
},
506504
// Encourage Dave - George
507505
{
508506
NodeFrom: dave,
509507
NodeTo: george,
510-
SuccessTime: testTimeMc,
508+
SuccessTime: testTime,
511509
SuccessAmt: 1000000,
512510
},
513511
},
@@ -522,17 +520,17 @@ func TestLowHighRoutingPlugin(t *testing.T) {
522520
{
523521
NodeFrom: frank,
524522
NodeTo: george,
525-
FailTime: testTimeMc,
523+
FailTime: testTime,
526524
FailAmt: 1000001,
527-
SuccessTime: testTimeMc,
525+
SuccessTime: testTime,
528526
SuccessAmt: 1000000,
529527
},
530528
{
531529
NodeFrom: dave,
532530
NodeTo: george,
533-
FailTime: testTimeMc,
531+
FailTime: testTime,
534532
FailAmt: 1000001,
535-
SuccessTime: testTimeMc,
533+
SuccessTime: testTime,
536534
SuccessAmt: 1000000,
537535
},
538536
},

test/router_mock.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ func (r *mockRouter) QueryMissionControl(ctx context.Context) (
5252
// ImpotMissionControl is a mocked reimplementation of the pair import.
5353
// Reference: lnd/router/missioncontrol_state.go:importSnapshot().
5454
func (r *mockRouter) ImportMissionControl(ctx context.Context,
55-
entries []lndclient.MissionControlEntry) error {
55+
entries []lndclient.MissionControlEntry, force bool) error {
5656

5757
for _, entry := range entries {
5858
found := false
@@ -79,12 +79,14 @@ func (r *mockRouter) ImportMissionControl(ctx context.Context,
7979

8080
// Import success result second.
8181
current.SuccessTime = entry.SuccessTime
82-
if entry.SuccessAmt > current.SuccessAmt {
82+
if force ||
83+
entry.SuccessAmt > current.SuccessAmt {
84+
8385
current.SuccessAmt = entry.SuccessAmt
8486
}
8587

86-
if !current.FailTime.IsZero() &&
87-
entry.SuccessAmt >= current.FailAmt {
88+
if !force && (!current.FailTime.IsZero() &&
89+
entry.SuccessAmt >= current.FailAmt) {
8890

8991
current.FailAmt = entry.SuccessAmt + 1
9092
}

0 commit comments

Comments
 (0)