graph: migrate tests from Fatal to require helpers

Replace remaining fatal-style assertions in graph package tests with
direct testify/require helpers. This simplifies control flow and makes
test intent clearer by using NoError, True/False, Len, Empty, and
Equal/EqualValues assertions.

Also remove FailNow-style patterns in favor of specific assertion
helpers.
This commit is contained in:
Elle Mouton committed 2026-02-25 11:31:28 +02:00
1 parent f8899dd324
commit 8dfe747c02
2 files changed
+151 -330

No files matched your search

+116 -217
View File
@@ -109,9 +109,10 @@ func TestIgnoreNodeAnnouncement(t *testing.T) {
)
err := ctx.builder.AddNode(t.Context(), node)
if !IsError(err, ErrIgnored) {
t.Fatalf("expected to get ErrIgnore, instead got: %v", err)
}
require.Truef(
t, IsError(err, ErrIgnored),
"expected to get ErrIgnore, instead got: %v", err,
)
}
// TestIgnoreChannelEdgePolicyForUnknownChannel checks that a router will
@@ -179,14 +180,17 @@ func TestIgnoreChannelEdgePolicyForUnknownChannel(t *testing.T) {
// Attempt to update the edge. This should be ignored, since the edge
// is not yet added to the router.
err = ctx.builder.UpdateEdge(ctxb, edgePolicy)
if !IsError(err, ErrIgnored) {
t.Fatalf("expected to get ErrIgnore, instead got: %v", err)
}
require.Truef(
t, IsError(err, ErrIgnored),
"expected to get ErrIgnore, instead got: %v", err,
)
// Add the edge.
require.NoErrorf(t, ctx.builder.AddEdge(ctxb, edge),
require.NoErrorf(
t, ctx.builder.AddEdge(ctxb, edge),
"expected to be able to add edge to the channel graph, even "+
"though the vertexes were unknown: %v.", err)
"though the vertexes were unknown: %v.", err,
)
// Now updating the edge policy should succeed.
require.NoError(t, ctx.builder.UpdateEdge(ctxb, edgePolicy))
@@ -224,9 +228,7 @@ func TestWakeUpOnStaleBranch(t *testing.T) {
bitcoinKey2.SerializeCompressed(),
chanValue, height,
)
if err != nil {
t.Fatalf("unable create channel edge: %v", err)
}
require.NoError(t, err)
block.Transactions = append(block.Transactions,
fundingTx)
chanID1 = chanID.ToUint64()
@@ -256,9 +258,7 @@ func TestWakeUpOnStaleBranch(t *testing.T) {
bitcoinKey1.SerializeCompressed(),
bitcoinKey2.SerializeCompressed(),
chanValue, height)
if err != nil {
t.Fatalf("unable create channel edge: %v", err)
}
require.NoError(t, err)
block.Transactions = append(block.Transactions,
fundingTx)
chanID2 = chanID.ToUint64()
@@ -298,9 +298,7 @@ func TestWakeUpOnStaleBranch(t *testing.T) {
)
require.NoError(t, err)
if err := ctx.builder.AddEdge(ctxb, edge1); err != nil {
t.Fatalf("unable to add edge: %v", err)
}
require.NoError(t, ctx.builder.AddEdge(ctxb, edge1))
edge2, err := models.NewV1Channel(
chanID2, *chaincfg.SimNetParams.GenesisHash, node1.PubKeyBytes,
@@ -314,37 +312,21 @@ func TestWakeUpOnStaleBranch(t *testing.T) {
)
require.NoError(t, err)
if err := ctx.builder.AddEdge(ctxb, edge2); err != nil {
t.Fatalf("unable to add edge: %v", err)
}
require.NoError(t, ctx.builder.AddEdge(ctxb, edge2))
// Check that the fundingTxs are in the graph db.
has, isZombie, err := ctx.graph.HasChannelEdge(chanID1)
if err != nil {
t.Fatalf("error looking for edge: %v", chanID1)
}
if !has {
t.Fatalf("could not find edge in graph")
}
if isZombie {
t.Fatal("edge was marked as zombie")
}
require.NoError(t, err)
require.True(t, has)
require.False(t, isZombie)
has, isZombie, err = ctx.graph.HasChannelEdge(chanID2)
if err != nil {
t.Fatalf("error looking for edge: %v", chanID2)
}
if !has {
t.Fatalf("could not find edge in graph")
}
if isZombie {
t.Fatal("edge was marked as zombie")
}
require.NoError(t, err)
require.True(t, has)
require.False(t, isZombie)
// Stop the router, so we can reorg the chain while its offline.
if err := ctx.builder.Stop(); err != nil {
t.Fatalf("unable to stop router: %v", err)
}
require.NoError(t, ctx.builder.Stop())
// Create a 15 block fork.
for i := uint32(1); i <= 15; i++ {
@@ -380,33 +362,20 @@ func TestWakeUpOnStaleBranch(t *testing.T) {
require.NoError(t, err)
// It should resync to the longer chain on startup.
if err := router.Start(); err != nil {
t.Fatalf("unable to start router: %v", err)
}
require.NoError(t, router.Start())
// The channel with chanID2 should not be in the database anymore,
// since it is not confirmed on the longest chain. chanID1 should
// still be.
has, isZombie, err = ctx.graph.HasChannelEdge(chanID1)
require.NoError(t, err)
if !has {
t.Fatalf("did not find edge in graph")
}
if isZombie {
t.Fatal("edge was marked as zombie")
}
require.True(t, has)
require.False(t, isZombie)
has, isZombie, err = ctx.graph.HasChannelEdge(chanID2)
if err != nil {
t.Fatalf("error looking for edge: %v", chanID2)
}
if has {
t.Fatalf("found edge in graph")
}
if isZombie {
t.Fatal("reorged edge should not be marked as zombie")
}
require.NoError(t, err)
require.False(t, has)
require.False(t, isZombie)
}
// TestDisconnectedBlocks checks that the router handles a reorg happening when
@@ -435,9 +404,7 @@ func TestDisconnectedBlocks(t *testing.T) {
bitcoinKey2.SerializeCompressed(),
chanValue, height,
)
if err != nil {
t.Fatalf("unable create channel edge: %v", err)
}
require.NoError(t, err)
block.Transactions = append(block.Transactions,
fundingTx)
chanID1 = chanID.ToUint64()
@@ -467,9 +434,7 @@ func TestDisconnectedBlocks(t *testing.T) {
bitcoinKey2.SerializeCompressed(),
chanValue, height,
)
if err != nil {
t.Fatalf("unable create channel edge: %v", err)
}
require.NoError(t, err)
block.Transactions = append(block.Transactions,
fundingTx)
chanID2 = chanID.ToUint64()
@@ -508,9 +473,7 @@ func TestDisconnectedBlocks(t *testing.T) {
)
require.NoError(t, err)
if err := ctx.builder.AddEdge(ctxb, edge1); err != nil {
t.Fatalf("unable to add edge: %v", err)
}
require.NoError(t, ctx.builder.AddEdge(ctxb, edge1))
edge2, err := models.NewV1Channel(
chanID2, *chaincfg.SimNetParams.GenesisHash, node1.PubKeyBytes,
@@ -522,32 +485,18 @@ func TestDisconnectedBlocks(t *testing.T) {
)
require.NoError(t, err)
if err := ctx.builder.AddEdge(ctxb, edge2); err != nil {
t.Fatalf("unable to add edge: %v", err)
}
require.NoError(t, ctx.builder.AddEdge(ctxb, edge2))
// Check that the fundingTxs are in the graph db.
has, isZombie, err := ctx.graph.HasChannelEdge(chanID1)
if err != nil {
t.Fatalf("error looking for edge: %v", chanID1)
}
if !has {
t.Fatalf("could not find edge in graph")
}
if isZombie {
t.Fatal("edge was marked as zombie")
}
require.NoError(t, err)
require.True(t, has)
require.False(t, isZombie)
has, isZombie, err = ctx.graph.HasChannelEdge(chanID2)
if err != nil {
t.Fatalf("error looking for edge: %v", chanID2)
}
if !has {
t.Fatalf("could not find edge in graph")
}
if isZombie {
t.Fatal("edge was marked as zombie")
}
require.NoError(t, err)
require.True(t, has)
require.False(t, isZombie)
// Create a 15 block fork. We first let the chainView notify the router
// about stale blocks, before sending the now connected blocks. We do
@@ -581,26 +530,14 @@ func TestDisconnectedBlocks(t *testing.T) {
// chanID2 should not be in the database anymore, since it is not
// confirmed on the longest chain. chanID1 should still be.
has, isZombie, err = ctx.graph.HasChannelEdge(chanID1)
if err != nil {
t.Fatalf("error looking for edge: %v", chanID1)
}
if !has {
t.Fatalf("did not find edge in graph")
}
if isZombie {
t.Fatal("edge was marked as zombie")
}
require.NoError(t, err)
require.True(t, has)
require.False(t, isZombie)
has, isZombie, err = ctx.graph.HasChannelEdge(chanID2)
if err != nil {
t.Fatalf("error looking for edge: %v", chanID2)
}
if has {
t.Fatalf("found edge in graph")
}
if isZombie {
t.Fatal("reorged edge should not be marked as zombie")
}
require.NoError(t, err)
require.False(t, has)
require.False(t, isZombie)
}
// TestChansClosedOfflinePruneGraph tests that if channels we know of are
@@ -660,23 +597,15 @@ func TestChansClosedOfflinePruneGraph(t *testing.T) {
)
require.NoError(t, err)
if err := ctx.builder.AddEdge(ctxb, edge1); err != nil {
t.Fatalf("unable to add edge: %v", err)
}
require.NoError(t, ctx.builder.AddEdge(ctxb, edge1))
// The router should now be aware of the channel we created above.
hasChan, isZombie, err := ctx.graph.HasChannelEdge(
chanID1.ToUint64(),
)
if err != nil {
t.Fatalf("error looking for edge: %v", chanID1)
}
if !hasChan {
t.Fatalf("could not find edge in graph")
}
if isZombie {
t.Fatal("edge was marked as zombie")
}
require.NoError(t, err)
require.True(t, hasChan)
require.False(t, isZombie)
// With the transaction included, and the router's database state
// updated, we'll now mine 5 additional blocks on top of it.
@@ -695,15 +624,10 @@ func TestChansClosedOfflinePruneGraph(t *testing.T) {
// At this point, our starting height should be 107.
_, chainHeight, err := ctx.chain.GetBestBlock()
require.NoError(t, err, "unable to get best block")
if chainHeight != 107 {
t.Fatalf("incorrect chain height: expected %v, got %v",
107, chainHeight)
}
require.EqualValues(t, 107, chainHeight)
// Next, we'll "shut down" the router in order to simulate downtime.
if err := ctx.builder.Stop(); err != nil {
t.Fatalf("unable to shutdown router: %v", err)
}
require.NoError(t, ctx.builder.Stop())
// While the router is "offline" we'll mine 5 additional blocks, with
// the second block closing the channel we created above.
@@ -735,10 +659,7 @@ func TestChansClosedOfflinePruneGraph(t *testing.T) {
// At this point, our starting height should be 112.
_, chainHeight, err = ctx.chain.GetBestBlock()
require.NoError(t, err, "unable to get best block")
if chainHeight != 112 {
t.Fatalf("incorrect chain height: expected %v, got %v",
112, chainHeight)
}
require.EqualValues(t, 112, chainHeight)
// Now we'll re-start the ChannelRouter. It should recognize that it's
// behind the main chain and prune all the blocks that it missed while
@@ -750,15 +671,9 @@ func TestChansClosedOfflinePruneGraph(t *testing.T) {
hasChan, isZombie, err = ctx.graph.HasChannelEdge(
chanID1.ToUint64(),
)
if err != nil {
t.Fatalf("error looking for edge: %v", chanID1)
}
if hasChan {
t.Fatalf("channel was found in graph but shouldn't have been")
}
if isZombie {
t.Fatal("closed channel should not be marked as zombie")
}
require.NoError(t, err)
require.False(t, hasChan)
require.False(t, isZombie)
}
// TestPruneChannelGraphStaleEdges ensures that we properly prune stale edges
@@ -857,9 +772,7 @@ func TestPruneChannelGraphStaleEdges(t *testing.T) {
testGraph, err := createTestGraphFromChannels(
t, true, testChannels, "a",
)
if err != nil {
t.Fatalf("unable to create test graph: %v", err)
}
require.NoError(t, err)
const startingHeight = 100
ctx := createTestCtxFromGraphInstance(
@@ -871,9 +784,7 @@ func TestPruneChannelGraphStaleEdges(t *testing.T) {
// Proceed to prune the channels - only the last one should be
// pruned.
if err := ctx.builder.pruneZombieChans(); err != nil {
t.Fatalf("unable to prune zombie channels: %v", err)
}
require.NoError(t, ctx.builder.pruneZombieChans())
// We expect channels that have either both edges stale, or one
// edge stale with both known.
@@ -1022,9 +933,7 @@ func testPruneChannelGraphDoubleDisabled(t *testing.T, assumeValid bool) {
assertChannelsPruned(t, ctx.graph, testChannels, prunedChannel)
}
if err := ctx.builder.pruneZombieChans(); err != nil {
t.Fatalf("unable to prune zombie channels: %v", err)
}
require.NoError(t, ctx.builder.pruneZombieChans())
// If we attempted to prune them without AssumeChannelValid being set,
// none should be pruned. Otherwise the last channel should still be
@@ -1075,16 +984,12 @@ func TestIsStaleNode(t *testing.T) {
)
require.NoError(t, err)
if err := ctx.builder.AddEdge(ctxb, edge); err != nil {
t.Fatalf("unable to add edge: %v", err)
}
require.NoError(t, ctx.builder.AddEdge(ctxb, edge))
// Before we add the node, if we query for staleness, we should get
// false, as we haven't added the full node.
updateTimeStamp := time.Unix(123, 0)
if ctx.builder.IsStaleNode(ctxb, pub1, updateTimeStamp) {
t.Fatalf("incorrectly detected node as stale")
}
require.False(t, ctx.builder.IsStaleNode(ctxb, pub1, updateTimeStamp))
// With the node stub in the database, we'll add the fully node
// announcement to the database.
@@ -1098,22 +1003,16 @@ func TestIsStaleNode(t *testing.T) {
Features: testFeatures.RawFeatureVector,
},
)
if err := ctx.builder.AddNode(t.Context(), n1); err != nil {
t.Fatalf("could not add node: %v", err)
}
require.NoError(t, ctx.builder.AddNode(t.Context(), n1))
// If we use the same timestamp and query for staleness, we should get
// true.
if !ctx.builder.IsStaleNode(ctxb, pub1, updateTimeStamp) {
t.Fatalf("failure to detect stale node update")
}
require.True(t, ctx.builder.IsStaleNode(ctxb, pub1, updateTimeStamp))
// If we update the timestamp and once again query for staleness, it
// should report false.
newTimeStamp := time.Unix(1234, 0)
if ctx.builder.IsStaleNode(ctxb, pub1, newTimeStamp) {
t.Fatalf("incorrectly detected node as stale")
}
require.False(t, ctx.builder.IsStaleNode(ctxb, pub1, newTimeStamp))
}
// TestIsKnownEdge tests that the IsKnownEdge method properly detects stale
@@ -1155,15 +1054,11 @@ func TestIsKnownEdge(t *testing.T) {
)
require.NoError(t, err)
if err := ctx.builder.AddEdge(ctxb, edge); err != nil {
t.Fatalf("unable to add edge: %v", err)
}
require.NoError(t, ctx.builder.AddEdge(ctxb, edge))
// Now that the edge has been inserted, query is the router already
// knows of the edge should return true.
if !ctx.builder.IsKnownEdge(*chanID) {
t.Fatalf("router should detect edge as known")
}
require.True(t, ctx.builder.IsKnownEdge(*chanID))
}
// TestIsStaleEdgePolicy tests that the IsStaleEdgePolicy properly detects
@@ -1198,12 +1093,16 @@ func TestIsStaleEdgePolicy(t *testing.T) {
// If we query for staleness before adding the edge, we should get
// false.
updateTimeStamp := time.Unix(123, 0)
if ctx.builder.IsStaleEdgePolicy(*chanID, updateTimeStamp, 0) {
t.Fatalf("router failed to detect fresh edge policy")
}
if ctx.builder.IsStaleEdgePolicy(*chanID, updateTimeStamp, 1) {
t.Fatalf("router failed to detect fresh edge policy")
}
require.False(
t, ctx.builder.IsStaleEdgePolicy(
*chanID, updateTimeStamp, 0,
),
)
require.False(
t, ctx.builder.IsStaleEdgePolicy(
*chanID, updateTimeStamp, 1,
),
)
edge, err := models.NewV1Channel(
chanID.ToUint64(), *chaincfg.SimNetParams.GenesisHash, pub1,
@@ -1214,9 +1113,7 @@ func TestIsStaleEdgePolicy(t *testing.T) {
)
require.NoError(t, err)
if err := ctx.builder.AddEdge(ctxb, edge); err != nil {
t.Fatalf("unable to add edge: %v", err)
}
require.NoError(t, ctx.builder.AddEdge(ctxb, edge))
// We'll also add two edge policies, one for each direction.
edgePolicy := &models.ChannelEdgePolicy{
@@ -1230,9 +1127,7 @@ func TestIsStaleEdgePolicy(t *testing.T) {
FeeProportionalMillionths: 10000,
}
edgePolicy.ChannelFlags = 0
if err := ctx.builder.UpdateEdge(ctxb, edgePolicy); err != nil {
t.Fatalf("unable to update edge policy: %v", err)
}
require.NoError(t, ctx.builder.UpdateEdge(ctxb, edgePolicy))
edgePolicy = &models.ChannelEdgePolicy{
Version: lnwire.GossipVersion1,
@@ -1245,28 +1140,34 @@ func TestIsStaleEdgePolicy(t *testing.T) {
FeeProportionalMillionths: 10000,
}
edgePolicy.ChannelFlags = 1
if err := ctx.builder.UpdateEdge(ctxb, edgePolicy); err != nil {
t.Fatalf("unable to update edge policy: %v", err)
}
require.NoError(t, ctx.builder.UpdateEdge(ctxb, edgePolicy))
// Now that the edges have been added, an identical (chanID, flag,
// timestamp) tuple for each edge should be detected as a stale edge.
if !ctx.builder.IsStaleEdgePolicy(*chanID, updateTimeStamp, 0) {
t.Fatalf("router failed to detect stale edge policy")
}
if !ctx.builder.IsStaleEdgePolicy(*chanID, updateTimeStamp, 1) {
t.Fatalf("router failed to detect stale edge policy")
}
require.True(
t, ctx.builder.IsStaleEdgePolicy(
*chanID, updateTimeStamp, 0,
),
)
require.True(
t, ctx.builder.IsStaleEdgePolicy(
*chanID, updateTimeStamp, 1,
),
)
// If we now update the timestamp for both edges, the router should
// detect that this tuple represents a fresh edge.
updateTimeStamp = time.Unix(9999, 0)
if ctx.builder.IsStaleEdgePolicy(*chanID, updateTimeStamp, 0) {
t.Fatalf("router failed to detect fresh edge policy")
}
if ctx.builder.IsStaleEdgePolicy(*chanID, updateTimeStamp, 1) {
t.Fatalf("router failed to detect fresh edge policy")
}
require.False(
t, ctx.builder.IsStaleEdgePolicy(
*chanID, updateTimeStamp, 0,
),
)
require.False(
t, ctx.builder.IsStaleEdgePolicy(
*chanID, updateTimeStamp, 1,
),
)
}
// TestBlockDifferenceFix tests if when the router is behind on blocks, the
@@ -1722,27 +1623,25 @@ func assertChannelsPruned(t *testing.T, graph *graphdb.VersionedGraph,
exists, isZombie, err := graph.HasChannelEdge(
channel.ChannelID,
)
if err != nil {
t.Fatalf("unable to determine existence of "+
"channel=%v in the graph: %v",
channel.ChannelID, err)
}
if !shouldPrune && !exists {
t.Fatalf("expected channel=%v to exist within "+
"the graph", channel.ChannelID)
}
if shouldPrune && exists {
t.Fatalf("expected channel=%v to not exist "+
"within the graph", channel.ChannelID)
}
if !shouldPrune && isZombie {
t.Fatalf("expected channel=%v to not be marked "+
"as zombie", channel.ChannelID)
}
if shouldPrune && !isZombie {
t.Fatalf("expected channel=%v to be marked as "+
"zombie", channel.ChannelID)
require.NoError(t, err)
if shouldPrune {
require.Falsef(t, exists,
"expected channel=%v to not exist within "+
"the graph",
channel.ChannelID)
require.Truef(t, isZombie,
"expected channel=%v to be marked as zombie",
channel.ChannelID)
continue
}
require.Truef(t, exists,
"expected channel=%v to exist within the graph",
channel.ChannelID)
require.Falsef(t, isZombie,
"expected channel=%v to not be marked as zombie",
channel.ChannelID)
}
}