diff --git a/routing_plugin.go b/routing_plugin.go index bd77ab2..066a3c2 100644 --- a/routing_plugin.go +++ b/routing_plugin.go @@ -5,7 +5,6 @@ import ( "fmt" "sort" "sync" - "time" "github.com/btcsuite/btclog" "github.com/btcsuite/btcutil" @@ -520,13 +519,10 @@ func (r *lowToHighRoutingPlugin) BeforePayment(ctx context.Context, limit := minFee + ((maxFee-minFee)/int64(maxAttempts))*int64(currAttempt) - // Create a timestamp just slightly in the future as Mission Control - // stores timestamps with sub second precision where as we send a unix - // timestamp it may occur that we can't override the last entries as - // they have the same unix timestamp. - // TODO(bhandras): not very reliable, ideally we'd need a force import - // for MC. - now := r.clock.Now().Add(time.Second) + // With the forced MC import we can safely set the pair history + // timestamps to the current time as import will always just override + // current MC state. + now := r.clock.Now() allowed := 0 entries := make( @@ -566,7 +562,7 @@ func (r *lowToHighRoutingPlugin) BeforePayment(ctx context.Context, return ErrRoutingPluginNoMoreRetries } - err := r.lnd.Router.ImportMissionControl(ctx, entries, false) + err := r.lnd.Router.ImportMissionControl(ctx, entries, true) if err != nil { return err } @@ -606,10 +602,10 @@ func (r *lowToHighRoutingPlugin) Done(ctx context.Context) error { return nil } - // Roll the entry times forward (to be able to override recent updates). - // Use the "time travel" trick which is required to make overrides - // succeed. - now := r.clock.Now().Add(time.Second) + // With the forced import we're safe to just set the pair history + // timestamps to the current time as import will always succeed and + // override current MC state. + now := r.clock.Now() entries := make( []lndclient.MissionControlEntry, 0, len(r.nodesByMaxFee), ) @@ -647,7 +643,7 @@ func (r *lowToHighRoutingPlugin) Done(ctx context.Context) error { } } - err := r.lnd.Router.ImportMissionControl(ctx, entries, false) + err := r.lnd.Router.ImportMissionControl(ctx, entries, true) if err != nil { return err } diff --git a/routing_plugin_test.go b/routing_plugin_test.go index 4c2b69c..50df14c 100644 --- a/routing_plugin_test.go +++ b/routing_plugin_test.go @@ -81,8 +81,6 @@ func TestLowHighRoutingPlugin(t *testing.T) { target := loopNode amt := btcutil.Amount(50) testTime := time.Now().UTC() - // We expect Mission Control entries to be set to now + 1 sec. - testTimeMc := testTime.Add(time.Second) tests := []struct { name string @@ -168,7 +166,7 @@ func TestLowHighRoutingPlugin(t *testing.T) { NodeTo: dave, FailTime: time.Time{}, FailAmt: 0, - SuccessTime: testTimeMc, + SuccessTime: testTime, SuccessAmt: 10000, }, }, @@ -210,14 +208,14 @@ func TestLowHighRoutingPlugin(t *testing.T) { { NodeFrom: bob, NodeTo: dave, - FailTime: testTimeMc, + FailTime: testTime, FailAmt: 1, }, // Encourage Charlie - Dave { NodeFrom: charlie, NodeTo: dave, - SuccessTime: testTimeMc, + SuccessTime: testTime, SuccessAmt: 1000000, }, }, @@ -228,15 +226,15 @@ func TestLowHighRoutingPlugin(t *testing.T) { NodeTo: dave, FailTime: time.Time{}, FailAmt: 0, - SuccessTime: testTimeMc, + SuccessTime: testTime, SuccessAmt: 10000, }, { NodeFrom: charlie, NodeTo: dave, - FailTime: testTimeMc, + FailTime: testTime, FailAmt: 1000001, - SuccessTime: testTimeMc, + SuccessTime: testTime, SuccessAmt: 1000000, }, }, @@ -283,13 +281,13 @@ func TestLowHighRoutingPlugin(t *testing.T) { { NodeFrom: bob, NodeTo: dave, - SuccessTime: testTimeMc, + SuccessTime: testTime, SuccessAmt: 999000, }, { NodeFrom: charlie, NodeTo: dave, - SuccessTime: testTimeMc, + SuccessTime: testTime, SuccessAmt: 999000, }, }, @@ -304,17 +302,17 @@ func TestLowHighRoutingPlugin(t *testing.T) { { NodeFrom: bob, NodeTo: dave, - SuccessTime: testTimeMc, + SuccessTime: testTime, SuccessAmt: 999000, - FailTime: testTimeMc, + FailTime: testTime, FailAmt: 999001, }, { NodeFrom: charlie, NodeTo: dave, - SuccessTime: testTimeMc, + SuccessTime: testTime, SuccessAmt: 999000, - FailTime: testTimeMc, + FailTime: testTime, FailAmt: 999001, }, }, @@ -375,21 +373,21 @@ func TestLowHighRoutingPlugin(t *testing.T) { { NodeFrom: bob, NodeTo: eugene, - FailTime: testTimeMc, + FailTime: testTime, FailAmt: 1, }, // Encourage Charlie - Eugene { NodeFrom: charlie, NodeTo: eugene, - SuccessTime: testTimeMc, + SuccessTime: testTime, SuccessAmt: 1000000, }, // Encourage Dave - Eugene { NodeFrom: dave, NodeTo: eugene, - SuccessTime: testTimeMc, + SuccessTime: testTime, SuccessAmt: 1000000, }, }, @@ -399,21 +397,21 @@ func TestLowHighRoutingPlugin(t *testing.T) { { NodeFrom: bob, NodeTo: eugene, - FailTime: testTimeMc, + FailTime: testTime, FailAmt: 1, }, // Discourage Charlie - Eugene { NodeFrom: charlie, NodeTo: eugene, - FailTime: testTimeMc, + FailTime: testTime, FailAmt: 1, }, // Encourage Dave - Eugene { NodeFrom: dave, NodeTo: eugene, - SuccessTime: testTimeMc, + SuccessTime: testTime, SuccessAmt: 1000000, }, }, @@ -422,9 +420,9 @@ func TestLowHighRoutingPlugin(t *testing.T) { { NodeFrom: bob, NodeTo: eugene, - FailTime: testTimeMc, + FailTime: testTime, FailAmt: 1000001, - SuccessTime: testTimeMc, + SuccessTime: testTime, SuccessAmt: 1000000, }, { @@ -432,15 +430,15 @@ func TestLowHighRoutingPlugin(t *testing.T) { NodeTo: eugene, FailTime: time.Time{}, FailAmt: 0, - SuccessTime: testTimeMc, + SuccessTime: testTime, SuccessAmt: 10000, }, { NodeFrom: dave, NodeTo: eugene, - FailTime: testTimeMc, + FailTime: testTime, FailAmt: 1000001, - SuccessTime: testTimeMc, + SuccessTime: testTime, SuccessAmt: 1000000, }, }, @@ -500,14 +498,14 @@ func TestLowHighRoutingPlugin(t *testing.T) { { NodeFrom: frank, NodeTo: george, - FailTime: testTimeMc, + FailTime: testTime, FailAmt: 1, }, // Encourage Dave - George { NodeFrom: dave, NodeTo: george, - SuccessTime: testTimeMc, + SuccessTime: testTime, SuccessAmt: 1000000, }, }, @@ -522,17 +520,17 @@ func TestLowHighRoutingPlugin(t *testing.T) { { NodeFrom: frank, NodeTo: george, - FailTime: testTimeMc, + FailTime: testTime, FailAmt: 1000001, - SuccessTime: testTimeMc, + SuccessTime: testTime, SuccessAmt: 1000000, }, { NodeFrom: dave, NodeTo: george, - FailTime: testTimeMc, + FailTime: testTime, FailAmt: 1000001, - SuccessTime: testTimeMc, + SuccessTime: testTime, SuccessAmt: 1000000, }, }, diff --git a/test/router_mock.go b/test/router_mock.go index e14411a..e9998d6 100644 --- a/test/router_mock.go +++ b/test/router_mock.go @@ -52,7 +52,7 @@ func (r *mockRouter) QueryMissionControl(ctx context.Context) ( // ImpotMissionControl is a mocked reimplementation of the pair import. // Reference: lnd/router/missioncontrol_state.go:importSnapshot(). func (r *mockRouter) ImportMissionControl(ctx context.Context, - entries []lndclient.MissionControlEntry) error { + entries []lndclient.MissionControlEntry, force bool) error { for _, entry := range entries { found := false @@ -79,12 +79,14 @@ func (r *mockRouter) ImportMissionControl(ctx context.Context, // Import success result second. current.SuccessTime = entry.SuccessTime - if entry.SuccessAmt > current.SuccessAmt { + if force || + entry.SuccessAmt > current.SuccessAmt { + current.SuccessAmt = entry.SuccessAmt } - if !current.FailTime.IsZero() && - entry.SuccessAmt >= current.FailAmt { + if !force && (!current.FailTime.IsZero() && + entry.SuccessAmt >= current.FailAmt) { current.FailAmt = entry.SuccessAmt + 1 }