@@ -26,6 +26,7 @@ import (
26
26
"github.com/lightningnetwork/lnd/fn/v2"
27
27
"github.com/lightningnetwork/lnd/graph/db/models"
28
28
"github.com/lightningnetwork/lnd/kvdb"
29
+ "github.com/lightningnetwork/lnd/lntest/wait"
29
30
"github.com/lightningnetwork/lnd/lnwire"
30
31
"github.com/lightningnetwork/lnd/routing/route"
31
32
"github.com/stretchr/testify/require"
@@ -102,7 +103,7 @@ func TestNodeInsertionAndDeletion(t *testing.T) {
102
103
t .Parallel ()
103
104
ctx := context .Background ()
104
105
105
- graph := MakeTestGraph (t )
106
+ graph := MakeTestGraph (t , WithSyncGraphCachePopulation () )
106
107
107
108
// We'd like to test basic insertion/deletion for vertexes from the
108
109
// graph, so we'll create a test vertex to start with.
@@ -260,7 +261,7 @@ func TestPartialNode(t *testing.T) {
260
261
t .Parallel ()
261
262
ctx := context .Background ()
262
263
263
- graph := MakeTestGraph (t )
264
+ graph := MakeTestGraph (t , WithSyncGraphCachePopulation () )
264
265
265
266
// To insert a partial node, we need to add a channel edge that has
266
267
// node keys for nodes we are not yet aware
@@ -386,7 +387,7 @@ func TestEdgeInsertionDeletion(t *testing.T) {
386
387
t .Parallel ()
387
388
ctx := context .Background ()
388
389
389
- graph := MakeTestGraph (t )
390
+ graph := MakeTestGraph (t , WithSyncGraphCachePopulation () )
390
391
391
392
// We'd like to test the insertion/deletion of edges, so we create two
392
393
// vertexes to connect.
@@ -511,7 +512,7 @@ func TestDisconnectBlockAtHeight(t *testing.T) {
511
512
t .Parallel ()
512
513
ctx := context .Background ()
513
514
514
- graph := MakeTestGraph (t )
515
+ graph := MakeTestGraph (t , WithSyncGraphCachePopulation () )
515
516
516
517
sourceNode := createTestVertex (t )
517
518
if err := graph .SetSourceNode (ctx , sourceNode ); err != nil {
@@ -802,7 +803,7 @@ func TestEdgeInfoUpdates(t *testing.T) {
802
803
t .Parallel ()
803
804
ctx := context .Background ()
804
805
805
- graph := MakeTestGraph (t )
806
+ graph := MakeTestGraph (t , WithSyncGraphCachePopulation () )
806
807
807
808
// We'd like to test the update of edges inserted into the database, so
808
809
// we create two vertexes to connect.
@@ -4350,7 +4351,9 @@ func TestGraphLoading(t *testing.T) {
4350
4351
// Next, create the graph for the first time.
4351
4352
graphStore := NewTestDB (t )
4352
4353
4353
- graph , err := NewChannelGraph (graphStore )
4354
+ graph , err := NewChannelGraph (
4355
+ graphStore , WithSyncGraphCachePopulation (),
4356
+ )
4354
4357
require .NoError (t , err )
4355
4358
require .NoError (t , graph .Start ())
4356
4359
t .Cleanup (func () {
@@ -4364,7 +4367,9 @@ func TestGraphLoading(t *testing.T) {
4364
4367
4365
4368
// Recreate the graph. This should cause the graph cache to be
4366
4369
// populated.
4367
- graphReloaded , err := NewChannelGraph (graphStore )
4370
+ graphReloaded , err := NewChannelGraph (
4371
+ graphStore , WithSyncGraphCachePopulation (),
4372
+ )
4368
4373
require .NoError (t , err )
4369
4374
require .NoError (t , graphReloaded .Start ())
4370
4375
t .Cleanup (func () {
@@ -4383,6 +4388,100 @@ func TestGraphLoading(t *testing.T) {
4383
4388
)
4384
4389
}
4385
4390
4391
+ // TestAsyncGraphCache tests the behaviour of the ChannelGraph when the graph
4392
+ // cache is populated asynchronously.
4393
+ func TestAsyncGraphCache (t * testing.T ) {
4394
+ t .Parallel ()
4395
+ ctx := context .Background ()
4396
+
4397
+ const (
4398
+ numNodes = 100
4399
+ numChannels = 3
4400
+ )
4401
+
4402
+ // Next, create the graph for the first time.
4403
+ graphStore := NewTestDB (t )
4404
+
4405
+ // The first time we spin up the graph, we Start is as normal and fill
4406
+ // it with test data. This will ensure that the graph cache has
4407
+ // something to load on the next Start.
4408
+ graph , err := NewChannelGraph (graphStore )
4409
+ require .NoError (t , err )
4410
+ require .NoError (t , graph .Start ())
4411
+ channels , nodes := fillTestGraph (t , graph , numNodes , numChannels )
4412
+
4413
+ assertGraphState := func () {
4414
+ var (
4415
+ numNodes int
4416
+ chanIndex = make (map [uint64 ]struct {}, numChannels )
4417
+ )
4418
+ // We query the graph for all nodes and channels, and
4419
+ // assert that we get the expected number of nodes and
4420
+ // channels.
4421
+ err := graph .ForEachNodeCached (
4422
+ ctx , func (node route.Vertex ,
4423
+ chans map [uint64 ]* DirectedChannel ) error {
4424
+
4425
+ numNodes ++
4426
+ for chanID := range chans {
4427
+ chanIndex [chanID ] = struct {}{}
4428
+ }
4429
+
4430
+ return nil
4431
+ },
4432
+ )
4433
+ require .NoError (t , err )
4434
+
4435
+ require .Equal (t , len (nodes ), numNodes )
4436
+ require .Equal (t , len (channels ), len (chanIndex ))
4437
+ }
4438
+
4439
+ assertGraphState ()
4440
+
4441
+ // Now we stop the graph.
4442
+ require .NoError (t , graph .Stop ())
4443
+
4444
+ // Recreate it but don't start it yet.
4445
+ graph , err = NewChannelGraph (graphStore )
4446
+ require .NoError (t , err )
4447
+
4448
+ // Spin off a goroutine that starts to make queries to the ChannelGraph.
4449
+ // We start this before we start the graph, so that we can ensure that
4450
+ // the queries are made while the graph cache is being populated.
4451
+ var (
4452
+ wg sync.WaitGroup
4453
+ numRuns = 10
4454
+ )
4455
+ for i := 0 ; i < numRuns ; i ++ {
4456
+ wg .Add (1 )
4457
+ go func () {
4458
+ defer wg .Done ()
4459
+
4460
+ assertGraphState ()
4461
+ }()
4462
+ }
4463
+
4464
+ require .NoError (t , graph .Start ())
4465
+ t .Cleanup (func () {
4466
+ require .NoError (t , graph .Stop ())
4467
+ })
4468
+
4469
+ wg .Wait ()
4470
+
4471
+ // Wait for the cache to be fully populated.
4472
+ err = wait .Predicate (func () bool {
4473
+ return graph .cacheLoaded .Load ()
4474
+ }, wait .DefaultTimeout )
4475
+ require .NoError (t , err )
4476
+
4477
+ // And then assert that all the expected nodes and channels are
4478
+ // present in the graph cache.
4479
+ for _ , node := range nodes {
4480
+ _ , ok := graph .graphCache .nodeChannels [node .PubKeyBytes ]
4481
+ require .True (t , ok )
4482
+ }
4483
+ }
4484
+
4386
4485
// TestClosedScid tests that we can correctly insert a SCID into the index of
4387
4486
// closed short channel ids.
4388
4487
func TestClosedScid (t * testing.T ) {
0 commit comments