@@ -1207,11 +1207,22 @@ func TestGraphTraversalCacheable(t *testing.T) {
1207
1207
// Iterate through all the known channels within the graph DB by
1208
1208
// iterating over each node, once again if the map is empty that
1209
1209
// indicates that all edges have properly been reached.
1210
+ var nodes []GraphCacheNode
1210
1211
err = graph .ForEachNodeCacheable (
1211
1212
func (tx kvdb.RTx , node GraphCacheNode ) error {
1212
1213
delete (nodeMap , node .PubKey ())
1213
1214
1214
- return node .ForEachChannel (
1215
+ nodes = append (nodes , node )
1216
+
1217
+ return nil
1218
+ },
1219
+ )
1220
+ require .NoError (t , err )
1221
+ require .Len (t , nodeMap , 0 )
1222
+
1223
+ err = graph .db .View (func (tx kvdb.RTx ) error {
1224
+ for _ , node := range nodes {
1225
+ err := node .ForEachChannel (
1215
1226
tx , func (tx kvdb.RTx , info * ChannelEdgeInfo ,
1216
1227
policy * ChannelEdgePolicy ,
1217
1228
policy2 * ChannelEdgePolicy ) error {
@@ -1220,10 +1231,15 @@ func TestGraphTraversalCacheable(t *testing.T) {
1220
1231
return nil
1221
1232
},
1222
1233
)
1223
- },
1224
- )
1234
+ if err != nil {
1235
+ return err
1236
+ }
1237
+ }
1238
+
1239
+ return nil
1240
+ }, func () {})
1241
+
1225
1242
require .NoError (t , err )
1226
- require .Len (t , nodeMap , 0 )
1227
1243
require .Len (t , chanIndex , 0 )
1228
1244
}
1229
1245
@@ -3695,9 +3711,20 @@ func BenchmarkForEachChannel(b *testing.B) {
3695
3711
totalCapacity btcutil.Amount
3696
3712
maxHTLCs lnwire.MilliSatoshi
3697
3713
)
3698
- err := graph .ForEachNodeCacheable (
3699
- func (tx kvdb.RTx , n GraphCacheNode ) error {
3700
- return n .ForEachChannel (
3714
+
3715
+ var nodes []GraphCacheNode
3716
+ err = graph .ForEachNodeCacheable (
3717
+ func (tx kvdb.RTx , node GraphCacheNode ) error {
3718
+ nodes = append (nodes , node )
3719
+
3720
+ return nil
3721
+ },
3722
+ )
3723
+ require .NoError (b , err )
3724
+
3725
+ err = graph .db .View (func (tx kvdb.RTx ) error {
3726
+ for _ , n := range nodes {
3727
+ err := n .ForEachChannel (
3701
3728
tx , func (tx kvdb.RTx ,
3702
3729
info * ChannelEdgeInfo ,
3703
3730
policy * ChannelEdgePolicy ,
@@ -3715,8 +3742,13 @@ func BenchmarkForEachChannel(b *testing.B) {
3715
3742
return nil
3716
3743
},
3717
3744
)
3718
- },
3719
- )
3745
+ if err != nil {
3746
+ return err
3747
+ }
3748
+ }
3749
+
3750
+ return nil
3751
+ }, func () {})
3720
3752
require .NoError (b , err )
3721
3753
}
3722
3754
}
@@ -3760,3 +3792,52 @@ func TestGraphCacheForEachNodeChannel(t *testing.T) {
3760
3792
3761
3793
require .Equal (t , numChans , 1 )
3762
3794
}
3795
+
3796
+ // TestGraphLoading asserts that the cache is properly reconstructed after a
3797
+ // restart.
3798
+ func TestGraphLoading (t * testing.T ) {
3799
+ // First, create a temporary directory to be used for the duration of
3800
+ // this test.
3801
+ tempDirName , err := ioutil .TempDir ("" , "channelgraph" )
3802
+ require .NoError (t , err )
3803
+ defer os .RemoveAll (tempDirName )
3804
+
3805
+ // Next, create the graph for the first time.
3806
+ backend , backendCleanup , err := kvdb .GetTestBackend (tempDirName , "cgr" )
3807
+ require .NoError (t , err )
3808
+ defer backend .Close ()
3809
+ defer backendCleanup ()
3810
+
3811
+ opts := DefaultOptions ()
3812
+ graph , err := NewChannelGraph (
3813
+ backend , opts .RejectCacheSize , opts .ChannelCacheSize ,
3814
+ opts .BatchCommitInterval , opts .PreAllocCacheNumNodes ,
3815
+ true , false ,
3816
+ )
3817
+ require .NoError (t , err )
3818
+
3819
+ // Populate the graph with test data.
3820
+ const numNodes = 100
3821
+ const numChannels = 4
3822
+ _ , _ = fillTestGraph (t , graph , numNodes , numChannels )
3823
+
3824
+ // Recreate the graph. This should cause the graph cache to be
3825
+ // populated.
3826
+ graphReloaded , err := NewChannelGraph (
3827
+ backend , opts .RejectCacheSize , opts .ChannelCacheSize ,
3828
+ opts .BatchCommitInterval , opts .PreAllocCacheNumNodes ,
3829
+ true , false ,
3830
+ )
3831
+ require .NoError (t , err )
3832
+
3833
+ // Assert that the cache content is identical.
3834
+ require .Equal (
3835
+ t , graph .graphCache .nodeChannels ,
3836
+ graphReloaded .graphCache .nodeChannels ,
3837
+ )
3838
+
3839
+ require .Equal (
3840
+ t , graph .graphCache .nodeFeatures ,
3841
+ graphReloaded .graphCache .nodeFeatures ,
3842
+ )
3843
+ }
0 commit comments