@@ -246,7 +246,7 @@ func (c channelMapKey) String() string {
246
246
247
247
// getChannelMap loads all channel edge policies from the database and stores
248
248
// them in a map.
249
- func ( c * KVStore ) getChannelMap (edges kvdb.RBucket ) (
249
+ func getChannelMap (edges kvdb.RBucket ) (
250
250
map [channelMapKey ]* models.ChannelEdgePolicy , error ) {
251
251
252
252
// Create a map to store all channel edge policies.
@@ -407,15 +407,30 @@ func (c *KVStore) AddrsForNode(ctx context.Context,
407
407
func (c * KVStore ) ForEachChannel (cb func (* models.ChannelEdgeInfo ,
408
408
* models.ChannelEdgePolicy , * models.ChannelEdgePolicy ) error ) error {
409
409
410
- return c .db .View (func (tx kvdb.RTx ) error {
410
+ return forEachChannel (c .db , cb )
411
+ }
412
+
413
+ // forEachChannel iterates through all the channel edges stored within the
414
+ // graph and invokes the passed callback for each edge. The callback takes two
415
+ // edges as since this is a directed graph, both the in/out edges are visited.
416
+ // If the callback returns an error, then the transaction is aborted and the
417
+ // iteration stops early.
418
+ //
419
+ // NOTE: If an edge can't be found, or wasn't advertised, then a nil pointer
420
+ // for that particular channel edge routing policy will be passed into the
421
+ // callback.
422
+ func forEachChannel (db kvdb.Backend , cb func (* models.ChannelEdgeInfo ,
423
+ * models.ChannelEdgePolicy , * models.ChannelEdgePolicy ) error ) error {
424
+
425
+ return db .View (func (tx kvdb.RTx ) error {
411
426
edges := tx .ReadBucket (edgeBucket )
412
427
if edges == nil {
413
428
return ErrGraphNoEdgesFound
414
429
}
415
430
416
431
// First, load all edges in memory indexed by node and channel
417
432
// id.
418
- channelMap , err := c . getChannelMap (edges )
433
+ channelMap , err := getChannelMap (edges )
419
434
if err != nil {
420
435
return err
421
436
}
@@ -479,7 +494,7 @@ func (c *KVStore) ForEachChannelCacheable(cb func(*models.CachedEdgeInfo,
479
494
480
495
// First, load all edges in memory indexed by node and channel
481
496
// id.
482
- channelMap , err := c . getChannelMap (edges )
497
+ channelMap , err := getChannelMap (edges )
483
498
if err != nil {
484
499
return err
485
500
}
@@ -658,7 +673,7 @@ func (c *KVStore) ForEachNodeCached(cb func(node route.Vertex,
658
673
// We'll iterate over each node, then the set of channels for each
659
674
// node, and construct a similar callback functiopn signature as the
660
675
// main funcotin expects.
661
- return c . forEachNode (func (tx kvdb.RTx ,
676
+ return forEachNode (c . db , func (tx kvdb.RTx ,
662
677
node * models.LightningNode ) error {
663
678
664
679
channels := make (map [uint64 ]* DirectedChannel )
@@ -774,7 +789,7 @@ func (c *KVStore) DisabledChannelIDs() ([]uint64, error) {
774
789
// executed under the same read transaction and so, methods on the NodeTx object
775
790
// _MUST_ only be called from within the call-back.
776
791
func (c * KVStore ) ForEachNode (cb func (tx NodeRTx ) error ) error {
777
- return c . forEachNode (func (tx kvdb.RTx ,
792
+ return forEachNode (c . db , func (tx kvdb.RTx ,
778
793
node * models.LightningNode ) error {
779
794
780
795
return cb (newChanGraphNodeTx (tx , c , node ))
@@ -788,7 +803,7 @@ func (c *KVStore) ForEachNode(cb func(tx NodeRTx) error) error {
788
803
//
789
804
// TODO(roasbeef): add iterator interface to allow for memory efficient graph
790
805
// traversal when graph gets mega.
791
- func ( c * KVStore ) forEachNode (
806
+ func forEachNode (db kvdb. Backend ,
792
807
cb func (kvdb.RTx , * models.LightningNode ) error ) error {
793
808
794
809
traversal := func (tx kvdb.RTx ) error {
@@ -819,7 +834,7 @@ func (c *KVStore) forEachNode(
819
834
})
820
835
}
821
836
822
- return kvdb .View (c . db , traversal , func () {})
837
+ return kvdb .View (db , traversal , func () {})
823
838
}
824
839
825
840
// ForEachNodeCacheable iterates through all the stored vertices/nodes in the
@@ -866,19 +881,23 @@ func (c *KVStore) ForEachNodeCacheable(cb func(route.Vertex,
866
881
// as the center node within a star-graph. This method may be used to kick off
867
882
// a path finding algorithm in order to explore the reachability of another
868
883
// node based off the source node.
869
- func (c * KVStore ) SourceNode (_ context.Context ) (* models.LightningNode ,
870
- error ) {
884
+ func (c * KVStore ) SourceNode (_ context.Context ) (* models.LightningNode , error ) {
885
+ return sourceNode (c .db )
886
+ }
871
887
888
+ // sourceNode fetches the source node of the graph. The source node is treated
889
+ // as the center node within a star-graph.
890
+ func sourceNode (db kvdb.Backend ) (* models.LightningNode , error ) {
872
891
var source * models.LightningNode
873
- err := kvdb .View (c . db , func (tx kvdb.RTx ) error {
892
+ err := kvdb .View (db , func (tx kvdb.RTx ) error {
874
893
// First grab the nodes bucket which stores the mapping from
875
894
// pubKey to node information.
876
895
nodes := tx .ReadBucket (nodeBucket )
877
896
if nodes == nil {
878
897
return ErrGraphNotFound
879
898
}
880
899
881
- node , err := c . sourceNode (nodes )
900
+ node , err := sourceNodeWithTx (nodes )
882
901
if err != nil {
883
902
return err
884
903
}
@@ -895,13 +914,11 @@ func (c *KVStore) SourceNode(_ context.Context) (*models.LightningNode,
895
914
return source , nil
896
915
}
897
916
898
- // sourceNode uses an existing database transaction and returns the source node
899
- // of the graph. The source node is treated as the center node within a
917
+ // sourceNodeWithTx uses an existing database transaction and returns the source
918
+ // node of the graph. The source node is treated as the center node within a
900
919
// star-graph. This method may be used to kick off a path finding algorithm in
901
920
// order to explore the reachability of another node based off the source node.
902
- func (c * KVStore ) sourceNode (nodes kvdb.RBucket ) (* models.LightningNode ,
903
- error ) {
904
-
921
+ func sourceNodeWithTx (nodes kvdb.RBucket ) (* models.LightningNode , error ) {
905
922
selfPub := nodes .Get (sourceKey )
906
923
if selfPub == nil {
907
924
return nil , ErrSourceNodeNotSet
@@ -1554,7 +1571,7 @@ func (c *KVStore) pruneGraphNodes(nodes kvdb.RwBucket,
1554
1571
1555
1572
// We'll retrieve the graph's source node to ensure we don't remove it
1556
1573
// even if it no longer has any open channels.
1557
- sourceNode , err := c . sourceNode (nodes )
1574
+ sourceNode , err := sourceNodeWithTx (nodes )
1558
1575
if err != nil {
1559
1576
return nil , err
1560
1577
}
@@ -3240,7 +3257,7 @@ func (c *KVStore) ForEachSourceNodeChannel(cb func(chanPoint wire.OutPoint,
3240
3257
return ErrGraphNotFound
3241
3258
}
3242
3259
3243
- node , err := c . sourceNode (nodes )
3260
+ node , err := sourceNodeWithTx (nodes )
3244
3261
if err != nil {
3245
3262
return err
3246
3263
}
0 commit comments