multi: use MakeTestGraphNew for all remaining unit tests
And delete the old MakeTestGraph function. Here we need to update some of our unit tests to ensure that any updates to existing nodes or channel policies have newer timestamps. This is so that we don't violate our SQL DB constraints that prevent updates to a node or channel policy record if the new update is not newer (has a newer LastUpdate time) than the currently presisted record).
This commit is contained in:
6 files changed
+50
-65
No files matched your search
@@ -36,7 +36,7 @@ type testDBGraph struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func newDiskChanGraph(t *testing.T) (testGraph, error) {
|
func newDiskChanGraph(t *testing.T) (testGraph, error) {
|
||||||
graphDB := graphdb.MakeTestGraph(t)
|
graphDB := graphdb.MakeTestGraphNew(t)
|
||||||
require.NoError(t, graphDB.Start())
|
require.NoError(t, graphDB.Start())
|
||||||
t.Cleanup(func() {
|
t.Cleanup(func() {
|
||||||
require.NoError(t, graphDB.Stop())
|
require.NoError(t, graphDB.Stop())
|
||||||
|
|||||||
+44
-28
@@ -892,15 +892,27 @@ func TestPruneChannelGraphDoubleDisabled(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func testPruneChannelGraphDoubleDisabled(t *testing.T, assumeValid bool) {
|
func testPruneChannelGraphDoubleDisabled(t *testing.T, assumeValid bool) {
|
||||||
|
timestamp := time.Now()
|
||||||
|
|
||||||
|
// nextTimeStamp is a helper closure that will return a new
|
||||||
|
// timestamp each time it's called, this helps us create channel updates
|
||||||
|
// with new timestamps so that we don't run into our SQL DB constraint
|
||||||
|
// which only allows an update to a channel edge if the last update
|
||||||
|
// timestamp is greater than the previous one.
|
||||||
|
nextTimeStamp := func() time.Time {
|
||||||
|
timestamp = timestamp.Add(time.Second)
|
||||||
|
|
||||||
|
return timestamp
|
||||||
|
}
|
||||||
|
|
||||||
// We'll create the following test graph so that only the last channel
|
// We'll create the following test graph so that only the last channel
|
||||||
// is pruned. We'll use a fresh timestamp to ensure they're not pruned
|
// is pruned. We'll use a fresh timestamp to ensure they're not pruned
|
||||||
// according to that heuristic.
|
// according to that heuristic.
|
||||||
timestamp := time.Now()
|
|
||||||
testChannels := []*testChannel{
|
testChannels := []*testChannel{
|
||||||
// Channel from self shouldn't be pruned.
|
// Channel from self shouldn't be pruned.
|
||||||
symmetricTestChannel(
|
symmetricTestChannel(
|
||||||
"self", "a", 100000, &testChannelPolicy{
|
"self", "a", 100000, &testChannelPolicy{
|
||||||
LastUpdate: timestamp,
|
LastUpdate: nextTimeStamp(),
|
||||||
Disabled: true,
|
Disabled: true,
|
||||||
}, 99,
|
}, 99,
|
||||||
),
|
),
|
||||||
@@ -918,7 +930,7 @@ func testPruneChannelGraphDoubleDisabled(t *testing.T, assumeValid bool) {
|
|||||||
Node1: &testChannelEnd{
|
Node1: &testChannelEnd{
|
||||||
Alias: "a",
|
Alias: "a",
|
||||||
testChannelPolicy: &testChannelPolicy{
|
testChannelPolicy: &testChannelPolicy{
|
||||||
LastUpdate: timestamp,
|
LastUpdate: nextTimeStamp(),
|
||||||
Disabled: true,
|
Disabled: true,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
@@ -932,7 +944,7 @@ func testPruneChannelGraphDoubleDisabled(t *testing.T, assumeValid bool) {
|
|||||||
Node1: &testChannelEnd{
|
Node1: &testChannelEnd{
|
||||||
Alias: "a",
|
Alias: "a",
|
||||||
testChannelPolicy: &testChannelPolicy{
|
testChannelPolicy: &testChannelPolicy{
|
||||||
LastUpdate: timestamp,
|
LastUpdate: nextTimeStamp(),
|
||||||
Disabled: false,
|
Disabled: false,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
@@ -946,14 +958,14 @@ func testPruneChannelGraphDoubleDisabled(t *testing.T, assumeValid bool) {
|
|||||||
Node1: &testChannelEnd{
|
Node1: &testChannelEnd{
|
||||||
Alias: "a",
|
Alias: "a",
|
||||||
testChannelPolicy: &testChannelPolicy{
|
testChannelPolicy: &testChannelPolicy{
|
||||||
LastUpdate: timestamp,
|
LastUpdate: nextTimeStamp(),
|
||||||
Disabled: true,
|
Disabled: true,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
Node2: &testChannelEnd{
|
Node2: &testChannelEnd{
|
||||||
Alias: "b",
|
Alias: "b",
|
||||||
testChannelPolicy: &testChannelPolicy{
|
testChannelPolicy: &testChannelPolicy{
|
||||||
LastUpdate: timestamp,
|
LastUpdate: nextTimeStamp(),
|
||||||
Disabled: false,
|
Disabled: false,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
@@ -963,13 +975,13 @@ func testPruneChannelGraphDoubleDisabled(t *testing.T, assumeValid bool) {
|
|||||||
|
|
||||||
// Both edges enabled.
|
// Both edges enabled.
|
||||||
symmetricTestChannel("c", "d", 100000, &testChannelPolicy{
|
symmetricTestChannel("c", "d", 100000, &testChannelPolicy{
|
||||||
LastUpdate: timestamp,
|
LastUpdate: nextTimeStamp(),
|
||||||
Disabled: false,
|
Disabled: false,
|
||||||
}, 2),
|
}, 2),
|
||||||
|
|
||||||
// Both edges disabled, only one pruned.
|
// Both edges disabled, only one pruned.
|
||||||
symmetricTestChannel("e", "f", 100000, &testChannelPolicy{
|
symmetricTestChannel("e", "f", 100000, &testChannelPolicy{
|
||||||
LastUpdate: timestamp,
|
LastUpdate: nextTimeStamp(),
|
||||||
Disabled: true,
|
Disabled: true,
|
||||||
}, 3),
|
}, 3),
|
||||||
}
|
}
|
||||||
@@ -1363,7 +1375,9 @@ func parseTestGraph(t *testing.T, useCache bool, path string) (
|
|||||||
testAddrs = append(testAddrs, testAddr)
|
testAddrs = append(testAddrs, testAddr)
|
||||||
|
|
||||||
// Next, create a temporary graph database for usage within the test.
|
// Next, create a temporary graph database for usage within the test.
|
||||||
graph := graphdb.MakeTestGraph(t, graphdb.WithUseGraphCache(useCache))
|
graph := graphdb.MakeTestGraphNew(
|
||||||
|
t, graphdb.WithUseGraphCache(useCache),
|
||||||
|
)
|
||||||
|
|
||||||
aliasMap := make(map[string]route.Vertex)
|
aliasMap := make(map[string]route.Vertex)
|
||||||
privKeyMap := make(map[string]*btcec.PrivateKey)
|
privKeyMap := make(map[string]*btcec.PrivateKey)
|
||||||
@@ -1441,6 +1455,13 @@ func parseTestGraph(t *testing.T, useCache bool, path string) (
|
|||||||
}
|
}
|
||||||
|
|
||||||
source = dbNode
|
source = dbNode
|
||||||
|
|
||||||
|
// Set the selected source node.
|
||||||
|
if err := graph.SetSourceNode(ctx, source); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
// With the node fully parsed, add it as a vertex within the
|
// 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) (
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if source != nil {
|
|
||||||
// Set the selected source node
|
|
||||||
if err := graph.SetSourceNode(ctx, source); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// With all the vertexes inserted, we can now insert the edges into the
|
// With all the vertexes inserted, we can now insert the edges into the
|
||||||
// test graph.
|
// test graph.
|
||||||
for _, edge := range g.Edges {
|
for _, edge := range g.Edges {
|
||||||
@@ -1739,14 +1753,16 @@ func createTestGraphFromChannels(t *testing.T, useCache bool,
|
|||||||
testAddrs = append(testAddrs, testAddr)
|
testAddrs = append(testAddrs, testAddr)
|
||||||
|
|
||||||
// Next, create a temporary graph database for usage within the test.
|
// Next, create a temporary graph database for usage within the test.
|
||||||
graph := graphdb.MakeTestGraph(t, graphdb.WithUseGraphCache(useCache))
|
graph := graphdb.MakeTestGraphNew(
|
||||||
|
t, graphdb.WithUseGraphCache(useCache),
|
||||||
|
)
|
||||||
|
|
||||||
aliasMap := make(map[string]route.Vertex)
|
aliasMap := make(map[string]route.Vertex)
|
||||||
privKeyMap := make(map[string]*btcec.PrivateKey)
|
privKeyMap := make(map[string]*btcec.PrivateKey)
|
||||||
|
|
||||||
nodeIndex := byte(0)
|
nodeIndex := byte(0)
|
||||||
addNodeWithAlias := func(alias string, features *lnwire.FeatureVector) (
|
addNodeWithAlias := func(alias string,
|
||||||
*models.LightningNode, error) {
|
features *lnwire.FeatureVector) error {
|
||||||
|
|
||||||
keyBytes := []byte{
|
keyBytes := []byte{
|
||||||
0, 0, 0, 0, 0, 0, 0, 0,
|
0, 0, 0, 0, 0, 0, 0, 0,
|
||||||
@@ -1776,26 +1792,26 @@ func createTestGraphFromChannels(t *testing.T, useCache bool,
|
|||||||
|
|
||||||
// With the node fully parsed, add it as a vertex within the
|
// With the node fully parsed, add it as a vertex within the
|
||||||
// graph.
|
// graph.
|
||||||
if err := graph.AddLightningNode(ctx, dbNode); err != nil {
|
if alias == source {
|
||||||
return nil, err
|
err = graph.SetSourceNode(ctx, dbNode)
|
||||||
|
require.NoError(t, err)
|
||||||
|
} else {
|
||||||
|
err := graph.AddLightningNode(ctx, dbNode)
|
||||||
|
require.NoError(t, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
aliasMap[alias] = dbNode.PubKeyBytes
|
aliasMap[alias] = dbNode.PubKeyBytes
|
||||||
nodeIndex++
|
nodeIndex++
|
||||||
|
|
||||||
return dbNode, nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add the source node.
|
// Add the source node.
|
||||||
dbNode, err := addNodeWithAlias(source, lnwire.EmptyFeatureVector())
|
err = addNodeWithAlias(source, lnwire.EmptyFeatureVector())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
if err = graph.SetSourceNode(ctx, dbNode); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
// Initialize variable that keeps track of the next channel id to assign
|
// Initialize variable that keeps track of the next channel id to assign
|
||||||
// if none is specified.
|
// if none is specified.
|
||||||
nextUnassignedChannelID := uint64(100000)
|
nextUnassignedChannelID := uint64(100000)
|
||||||
@@ -1813,7 +1829,7 @@ func createTestGraphFromChannels(t *testing.T, useCache bool,
|
|||||||
features =
|
features =
|
||||||
node.testChannelPolicy.Features
|
node.testChannelPolicy.Features
|
||||||
}
|
}
|
||||||
_, err := addNodeWithAlias(
|
err := addNodeWithAlias(
|
||||||
node.Alias, features,
|
node.Alias, features,
|
||||||
)
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -3147,7 +3147,7 @@ func TestChannelEdgePruningUpdateIndexDeletion(t *testing.T) {
|
|||||||
t.Parallel()
|
t.Parallel()
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
|
|
||||||
graph := MakeTestGraph(t)
|
graph := MakeTestGraphNew(t)
|
||||||
|
|
||||||
// The update index only applies to the bbolt graph.
|
// The update index only applies to the bbolt graph.
|
||||||
boltStore, ok := graph.V1Store.(*KVStore)
|
boltStore, ok := graph.V1Store.(*KVStore)
|
||||||
@@ -3681,7 +3681,7 @@ func TestEdgePolicyMissingMaxHtcl(t *testing.T) {
|
|||||||
t.Parallel()
|
t.Parallel()
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
|
|
||||||
graph := MakeTestGraph(t)
|
graph := MakeTestGraphNew(t)
|
||||||
|
|
||||||
// This test currently directly edits the bytes stored in the bbolt DB.
|
// This test currently directly edits the bytes stored in the bbolt DB.
|
||||||
boltStore, ok := graph.V1Store.(*KVStore)
|
boltStore, ok := graph.V1Store.(*KVStore)
|
||||||
|
|||||||
@@ -12,7 +12,6 @@ import (
|
|||||||
"net"
|
"net"
|
||||||
"sort"
|
"sort"
|
||||||
"sync"
|
"sync"
|
||||||
"testing"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/btcsuite/btcd/btcec/v2"
|
"github.com/btcsuite/btcd/btcec/v2"
|
||||||
@@ -28,7 +27,6 @@ import (
|
|||||||
"github.com/lightningnetwork/lnd/kvdb"
|
"github.com/lightningnetwork/lnd/kvdb"
|
||||||
"github.com/lightningnetwork/lnd/lnwire"
|
"github.com/lightningnetwork/lnd/lnwire"
|
||||||
"github.com/lightningnetwork/lnd/routing/route"
|
"github.com/lightningnetwork/lnd/routing/route"
|
||||||
"github.com/stretchr/testify/require"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@@ -4869,32 +4867,3 @@ func (c *chanGraphNodeTx) ForEachChannel(f func(*models.ChannelEdgeInfo,
|
|||||||
},
|
},
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
// MakeTestGraph creates a new instance of the ChannelGraph for testing
|
|
||||||
// purposes.
|
|
||||||
//
|
|
||||||
// NOTE: this helper currently creates a ChannelGraph that is only ever backed
|
|
||||||
// by the `KVStore` of the `V1Store` interface.
|
|
||||||
func MakeTestGraph(t testing.TB, opts ...ChanGraphOption) *ChannelGraph {
|
|
||||||
t.Helper()
|
|
||||||
|
|
||||||
// Next, create KVStore for the first time.
|
|
||||||
backend, backendCleanup, err := kvdb.GetTestBackend(t.TempDir(), "cgr")
|
|
||||||
t.Cleanup(backendCleanup)
|
|
||||||
require.NoError(t, err)
|
|
||||||
t.Cleanup(func() {
|
|
||||||
require.NoError(t, backend.Close())
|
|
||||||
})
|
|
||||||
|
|
||||||
graphStore, err := NewKVStore(backend)
|
|
||||||
require.NoError(t, err)
|
|
||||||
|
|
||||||
graph, err := NewChannelGraph(graphStore, opts...)
|
|
||||||
require.NoError(t, err)
|
|
||||||
require.NoError(t, graph.Start())
|
|
||||||
t.Cleanup(func() {
|
|
||||||
require.NoError(t, graph.Stop())
|
|
||||||
})
|
|
||||||
|
|
||||||
return graph
|
|
||||||
}
|
|
||||||
@@ -763,7 +763,7 @@ func TestNodeUpdateNotification(t *testing.T) {
|
|||||||
// then it should trigger a new notification.
|
// then it should trigger a new notification.
|
||||||
// TODO(roasbeef): assume monotonic time.
|
// TODO(roasbeef): assume monotonic time.
|
||||||
nodeUpdateAnn := *node1
|
nodeUpdateAnn := *node1
|
||||||
nodeUpdateAnn.LastUpdate = node1.LastUpdate.Add(300 * time.Millisecond)
|
nodeUpdateAnn.LastUpdate = node1.LastUpdate.Add(time.Second)
|
||||||
|
|
||||||
// Add new node topology update to the channel router.
|
// Add new node topology update to the channel router.
|
||||||
if err := ctx.builder.AddNode(ctxb, &nodeUpdateAnn); err != nil {
|
if err := ctx.builder.AddNode(ctxb, &nodeUpdateAnn); err != nil {
|
||||||
@@ -1059,7 +1059,7 @@ type testCtx struct {
|
|||||||
func createTestCtxSingleNode(t *testing.T,
|
func createTestCtxSingleNode(t *testing.T,
|
||||||
startingHeight uint32) *testCtx {
|
startingHeight uint32) *testCtx {
|
||||||
|
|
||||||
graph := graphdb.MakeTestGraph(t)
|
graph := graphdb.MakeTestGraphNew(t)
|
||||||
sourceNode := createTestNode(t)
|
sourceNode := createTestNode(t)
|
||||||
|
|
||||||
require.NoError(t,
|
require.NoError(t,
|
||||||
|
|||||||
+1
-1
@@ -602,7 +602,7 @@ func createTestPeer(t *testing.T) *peerTestCtx {
|
|||||||
|
|
||||||
const chanActiveTimeout = time.Minute
|
const chanActiveTimeout = time.Minute
|
||||||
|
|
||||||
dbAliceGraph := graphdb.MakeTestGraph(t)
|
dbAliceGraph := graphdb.MakeTestGraphNew(t)
|
||||||
require.NoError(t, dbAliceGraph.Start())
|
require.NoError(t, dbAliceGraph.Start())
|
||||||
t.Cleanup(func() {
|
t.Cleanup(func() {
|
||||||
require.NoError(t, dbAliceGraph.Stop())
|
require.NoError(t, dbAliceGraph.Stop())
|
||||||
|
|||||||
Reference in new issue
Block a user