From 8558534417add16c4496c8ea759f7984cfa3dfc0 Mon Sep 17 00:00:00 2001 From: Joost Jager Date: Thu, 5 Mar 2020 13:54:03 +0100 Subject: [PATCH] routing: add clock to router config --- routing/router.go | 8 ++++++-- routing/router_test.go | 3 +++ server.go | 1 + 3 files changed, 10 insertions(+), 2 deletions(-) diff --git a/routing/router.go b/routing/router.go index 8dbb45de5..a4b264e1b 100644 --- a/routing/router.go +++ b/routing/router.go @@ -17,6 +17,7 @@ import ( sphinx "github.com/lightningnetwork/lightning-onion" "github.com/lightningnetwork/lnd/channeldb" + "github.com/lightningnetwork/lnd/clock" "github.com/lightningnetwork/lnd/htlcswitch" "github.com/lightningnetwork/lnd/input" "github.com/lightningnetwork/lnd/lntypes" @@ -302,6 +303,9 @@ type Config struct { // PathFindingConfig defines global path finding parameters. PathFindingConfig PathFindingConfig + + // Clock is mockable time provider. + Clock clock.Clock } // EdgeLocator is a struct used to identify a specific edge. @@ -1680,7 +1684,7 @@ func (r *ChannelRouter) preparePayment(payment *LightningPayment) ( info := &channeldb.PaymentCreationInfo{ PaymentHash: payment.PaymentHash, Value: payment.Amount, - CreationTime: time.Now(), + CreationTime: r.cfg.Clock.Now(), PaymentRequest: payment.PaymentRequest, } @@ -1709,7 +1713,7 @@ func (r *ChannelRouter) SendToRoute(hash lntypes.Hash, route *route.Route) ( info := &channeldb.PaymentCreationInfo{ PaymentHash: hash, Value: amt, - CreationTime: time.Now(), + CreationTime: r.cfg.Clock.Now(), PaymentRequest: nil, } diff --git a/routing/router_test.go b/routing/router_test.go index b3470d1c3..dd8908c4d 100644 --- a/routing/router_test.go +++ b/routing/router_test.go @@ -19,6 +19,7 @@ import ( "github.com/davecgh/go-spew/spew" "github.com/lightningnetwork/lnd/channeldb" + "github.com/lightningnetwork/lnd/clock" "github.com/lightningnetwork/lnd/htlcswitch" "github.com/lightningnetwork/lnd/lntypes" "github.com/lightningnetwork/lnd/lnwire" @@ -131,6 +132,7 @@ func createTestCtxFromGraphInstance(startingHeight uint32, graphInstance *testGr return next, nil }, PathFindingConfig: pathFindingConfig, + Clock: clock.NewTestClock(time.Unix(1, 0)), }) if err != nil { return nil, nil, fmt.Errorf("unable to create router %v", err) @@ -2967,6 +2969,7 @@ func TestRouterPaymentStateMachine(t *testing.T) { next := atomic.AddUint64(&uniquePaymentID, 1) return next, nil }, + Clock: clock.NewTestClock(time.Unix(1, 0)), }) if err != nil { t.Fatalf("unable to create router %v", err) diff --git a/server.go b/server.go index bde65671a..fd0de2666 100644 --- a/server.go +++ b/server.go @@ -740,6 +740,7 @@ func newServer(listenAddrs []net.Addr, chanDB *channeldb.DB, AssumeChannelValid: cfg.Routing.UseAssumeChannelValid(), NextPaymentID: sequencer.NextID, PathFindingConfig: pathFindingConfig, + Clock: clock.NewDefaultClock(), }) if err != nil { return nil, fmt.Errorf("can't create router: %v", err)