@@ -892,15 +892,27 @@ func TestPruneChannelGraphDoubleDisabled(t *testing.T) {
892892}
893893
894894func testPruneChannelGraphDoubleDisabled (t * testing.T , assumeValid bool ) {
895+ timestamp := time .Now ()
896+
897+ // nextTimeStamp is a helper closure that will return a new
898+ // timestamp each time it's called, this helps us create channel updates
899+ // with new timestamps so that we don't run into our SQL DB constraint
900+ // which only allows an update to a channel edge if the last update
901+ // timestamp is greater than the previous one.
902+ nextTimeStamp := func () time.Time {
903+ timestamp = timestamp .Add (time .Second )
904+
905+ return timestamp
906+ }
907+
895908 // We'll create the following test graph so that only the last channel
896909 // is pruned. We'll use a fresh timestamp to ensure they're not pruned
897910 // according to that heuristic.
898- timestamp := time .Now ()
899911 testChannels := []* testChannel {
900912 // Channel from self shouldn't be pruned.
901913 symmetricTestChannel (
902914 "self" , "a" , 100000 , & testChannelPolicy {
903- LastUpdate : timestamp ,
915+ LastUpdate : nextTimeStamp () ,
904916 Disabled : true ,
905917 }, 99 ,
906918 ),
@@ -918,7 +930,7 @@ func testPruneChannelGraphDoubleDisabled(t *testing.T, assumeValid bool) {
918930 Node1 : & testChannelEnd {
919931 Alias : "a" ,
920932 testChannelPolicy : & testChannelPolicy {
921- LastUpdate : timestamp ,
933+ LastUpdate : nextTimeStamp () ,
922934 Disabled : true ,
923935 },
924936 },
@@ -932,7 +944,7 @@ func testPruneChannelGraphDoubleDisabled(t *testing.T, assumeValid bool) {
932944 Node1 : & testChannelEnd {
933945 Alias : "a" ,
934946 testChannelPolicy : & testChannelPolicy {
935- LastUpdate : timestamp ,
947+ LastUpdate : nextTimeStamp () ,
936948 Disabled : false ,
937949 },
938950 },
@@ -946,14 +958,14 @@ func testPruneChannelGraphDoubleDisabled(t *testing.T, assumeValid bool) {
946958 Node1 : & testChannelEnd {
947959 Alias : "a" ,
948960 testChannelPolicy : & testChannelPolicy {
949- LastUpdate : timestamp ,
961+ LastUpdate : nextTimeStamp () ,
950962 Disabled : true ,
951963 },
952964 },
953965 Node2 : & testChannelEnd {
954966 Alias : "b" ,
955967 testChannelPolicy : & testChannelPolicy {
956- LastUpdate : timestamp ,
968+ LastUpdate : nextTimeStamp () ,
957969 Disabled : false ,
958970 },
959971 },
@@ -963,13 +975,13 @@ func testPruneChannelGraphDoubleDisabled(t *testing.T, assumeValid bool) {
963975
964976 // Both edges enabled.
965977 symmetricTestChannel ("c" , "d" , 100000 , & testChannelPolicy {
966- LastUpdate : timestamp ,
978+ LastUpdate : nextTimeStamp () ,
967979 Disabled : false ,
968980 }, 2 ),
969981
970982 // Both edges disabled, only one pruned.
971983 symmetricTestChannel ("e" , "f" , 100000 , & testChannelPolicy {
972- LastUpdate : timestamp ,
984+ LastUpdate : nextTimeStamp () ,
973985 Disabled : true ,
974986 }, 3 ),
975987 }
@@ -1363,7 +1375,9 @@ func parseTestGraph(t *testing.T, useCache bool, path string) (
13631375 testAddrs = append (testAddrs , testAddr )
13641376
13651377 // Next, create a temporary graph database for usage within the test.
1366- graph := graphdb .MakeTestGraph (t , graphdb .WithUseGraphCache (useCache ))
1378+ graph := graphdb .MakeTestGraph (
1379+ t , graphdb .WithUseGraphCache (useCache ),
1380+ )
13671381
13681382 aliasMap := make (map [string ]route.Vertex )
13691383 privKeyMap := make (map [string ]* btcec.PrivateKey )
@@ -1441,6 +1455,13 @@ func parseTestGraph(t *testing.T, useCache bool, path string) (
14411455 }
14421456
14431457 source = dbNode
1458+
1459+ // Set the selected source node.
1460+ if err := graph .SetSourceNode (ctx , source ); err != nil {
1461+ return nil , err
1462+ }
1463+
1464+ continue
14441465 }
14451466
14461467 // With the node fully parsed, add it as a vertex within the
@@ -1450,13 +1471,6 @@ func parseTestGraph(t *testing.T, useCache bool, path string) (
14501471 }
14511472 }
14521473
1453- if source != nil {
1454- // Set the selected source node
1455- if err := graph .SetSourceNode (ctx , source ); err != nil {
1456- return nil , err
1457- }
1458- }
1459-
14601474 // With all the vertexes inserted, we can now insert the edges into the
14611475 // test graph.
14621476 for _ , edge := range g .Edges {
@@ -1739,14 +1753,16 @@ func createTestGraphFromChannels(t *testing.T, useCache bool,
17391753 testAddrs = append (testAddrs , testAddr )
17401754
17411755 // Next, create a temporary graph database for usage within the test.
1742- graph := graphdb .MakeTestGraph (t , graphdb .WithUseGraphCache (useCache ))
1756+ graph := graphdb .MakeTestGraph (
1757+ t , graphdb .WithUseGraphCache (useCache ),
1758+ )
17431759
17441760 aliasMap := make (map [string ]route.Vertex )
17451761 privKeyMap := make (map [string ]* btcec.PrivateKey )
17461762
17471763 nodeIndex := byte (0 )
1748- addNodeWithAlias := func (alias string , features * lnwire. FeatureVector ) (
1749- * models. LightningNode , error ) {
1764+ addNodeWithAlias := func (alias string ,
1765+ features * lnwire. FeatureVector ) error {
17501766
17511767 keyBytes := []byte {
17521768 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ,
@@ -1776,26 +1792,26 @@ func createTestGraphFromChannels(t *testing.T, useCache bool,
17761792
17771793 // With the node fully parsed, add it as a vertex within the
17781794 // graph.
1779- if err := graph .AddLightningNode (ctx , dbNode ); err != nil {
1780- return nil , err
1795+ if alias == source {
1796+ err = graph .SetSourceNode (ctx , dbNode )
1797+ require .NoError (t , err )
1798+ } else {
1799+ err := graph .AddLightningNode (ctx , dbNode )
1800+ require .NoError (t , err )
17811801 }
17821802
17831803 aliasMap [alias ] = dbNode .PubKeyBytes
17841804 nodeIndex ++
17851805
1786- return dbNode , nil
1806+ return nil
17871807 }
17881808
17891809 // Add the source node.
1790- dbNode , err : = addNodeWithAlias (source , lnwire .EmptyFeatureVector ())
1810+ err = addNodeWithAlias (source , lnwire .EmptyFeatureVector ())
17911811 if err != nil {
17921812 return nil , err
17931813 }
17941814
1795- if err = graph .SetSourceNode (ctx , dbNode ); err != nil {
1796- return nil , err
1797- }
1798-
17991815 // Initialize variable that keeps track of the next channel id to assign
18001816 // if none is specified.
18011817 nextUnassignedChannelID := uint64 (100000 )
@@ -1813,7 +1829,7 @@ func createTestGraphFromChannels(t *testing.T, useCache bool,
18131829 features =
18141830 node .testChannelPolicy .Features
18151831 }
1816- _ , err := addNodeWithAlias (
1832+ err := addNodeWithAlias (
18171833 node .Alias , features ,
18181834 )
18191835 if err != nil {
0 commit comments