Skip to content

Commit dc9bc9e

Browse files
committed
routing: integrate forced MC import to the low/high plugin
1 parent 9ace542 commit dc9bc9e

File tree

3 files changed

+45
-49
lines changed

3 files changed

+45
-49
lines changed

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, false)
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, false)
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)