From 25614f67f8b2db32de6a08017365f4476b5f31ad Mon Sep 17 00:00:00 2001 From: Olaoluwa Osuntokun Date: Sun, 22 Oct 2017 17:29:55 -0700 Subject: [PATCH] routing: restore in memory selfNode within the ChannelRouter MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit In this commit we restore the in memory ChannelRouter as we’ll no dynamically set the ChannelRouter’s pointer within he spec path finding test example. --- routing/pathfind_test.go | 1 + routing/router.go | 27 +++++++++++++++------------ 2 files changed, 16 insertions(+), 12 deletions(-) diff --git a/routing/pathfind_test.go b/routing/pathfind_test.go index a8ddacc38..cab890eb2 100644 --- a/routing/pathfind_test.go +++ b/routing/pathfind_test.go @@ -718,6 +718,7 @@ func TestPathFindSpecExample(t *testing.T) { if err := ctx.graph.SetSourceNode(aliceNode); err != nil { t.Fatalf("unable to set source node: %v", err) } + ctx.router.selfNode = aliceNode source, err := ctx.graph.SourceNode() if err != nil { t.Fatalf("unable to retrieve source node: %v", err) diff --git a/routing/router.go b/routing/router.go index c103417c9..3b5583410 100644 --- a/routing/router.go +++ b/routing/router.go @@ -168,6 +168,11 @@ type ChannelRouter struct { // initialized with. cfg *Config + // selfNode is the center of the star-graph centered around the + // ChannelRouter. The ChannelRouter uses this node as a starting point + // when doing any path finding. + selfNode *channeldb.LightningNode + // routeCache is a map that caches the k-shortest paths from ourselves // to a given target destination for a particular payment amount. This // map is used as an optimization to speed up subsequent payments to a @@ -225,12 +230,18 @@ var _ ChannelGraphSource = (*ChannelRouter)(nil) // to fully sync to the latest state of the UTXO set. func New(cfg Config) (*ChannelRouter, error) { + selfNode, err := cfg.Graph.SourceNode() + if err != nil { + return nil, err + } + return &ChannelRouter{ cfg: &cfg, networkUpdates: make(chan *routingMsg), topologyClients: make(map[uint64]*topologyClient), ntfnClientUpdates: make(chan *topologyClientUpdate), missionControl: newMissionControl(cfg.Graph, selfNode), + selfNode: selfNode, routeCache: make(map[routeTuple][]*Route), quit: make(chan struct{}), }, nil @@ -1009,15 +1020,11 @@ func (r *ChannelRouter) FindRoutes(target *btcec.PublicKey, return nil, err } - selfNode, err := r.cfg.Graph.SourceNode() - if err != nil { - return nil, err - } - // Now that we know the destination is reachable within the graph, // we'll execute our KSP algorithm to find the k-shortest paths from // our source to the destination. - shortestPaths, err := findPaths(tx, r.cfg.Graph, selfNode, target, amt) + shortestPaths, err := findPaths(tx, r.cfg.Graph, r.selfNode, target, + amt) if err != nil { tx.Rollback() return nil, err @@ -1031,7 +1038,7 @@ func (r *ChannelRouter) FindRoutes(target *btcec.PublicKey, // aren't able to support the total satoshis flow once fees have been // factored in. validRoutes := make([]*Route, 0, len(shortestPaths)) - sourceVertex := newVertex(selfNode.PubKey) + sourceVertex := newVertex(r.selfNode.PubKey) for _, path := range shortestPaths { // Attempt to make the path into a route. We snip off the first // hop in the path as it contains a "self-hop" that is inserted @@ -1583,11 +1590,7 @@ func (r *ChannelRouter) ForEachNode(cb func(*channeldb.LightningNode) error) err func (r *ChannelRouter) ForAllOutgoingChannels(cb func(*channeldb.ChannelEdgeInfo, *channeldb.ChannelEdgePolicy) error) error { - selfNode, err := r.cfg.Graph.SourceNode() - if err != nil { - return err - } - return selfNode.ForEachChannel(nil, func(_ *bolt.Tx, c *channeldb.ChannelEdgeInfo, + return r.selfNode.ForEachChannel(nil, func(_ *bolt.Tx, c *channeldb.ChannelEdgeInfo, e, _ *channeldb.ChannelEdgePolicy) error { return cb(c, e)