routing: restore in memory selfNode within the ChannelRouter

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.
This commit is contained in:
Olaoluwa Osuntokun 2017-10-22 17:29:55 -07:00
parent 76ec41103e
commit 25614f67f8
No known key found for this signature in database
GPG Key ID: 964EA263DD637C21
2 changed files with 16 additions and 12 deletions

@ -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)

@ -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)