graph/tests: make createChannelEdge assert internally
What changed, and why it matters
This commit is a small cleanup of internal test helper code. It changes how a test-only function reports errors so that callers don't have to check an error value themselves. There is no change to the actual LND node software that users run, and no security relevance.
No security action needed. This is a routine test refactoring.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The commit refactors createChannelEdge in graph/notifications_test.go to accept a testing.TB handle, mark itself as a test helper via t.Helper(), and use require.NoError internally instead of returning an error. All call sites in graph/builder_test.go and graph/notifications_test.go are updated to drop the immediately-asserted error return. This is purely a test-code readability/maintainability change; no production code is modified.
Changed components
graph/builder_test.gograph/notifications_test.goInspect captured patch +34 / −48
diff --git a/graph/builder_test.go b/graph/builder_test.go
index 7c1980e..e561d25 100644
--- a/graph/builder_test.go
+++ b/graph/builder_test.go
@@ -54,11 +54,10 @@ func TestAddProof(t *testing.T) {
// In order to be able to add the edge we should have a valid funding
// UTXO within the blockchain.
- script, fundingTx, _, chanID, err := createChannelEdge(
- bitcoinKey1.SerializeCompressed(),
+ script, fundingTx, _, chanID := createChannelEdge(
+ t, bitcoinKey1.SerializeCompressed(),
bitcoinKey2.SerializeCompressed(), 100, 0,
)
- require.NoError(t, err, "unable create channel edge")
fundingBlock := &wire.MsgBlock{
Transactions: []*wire.MsgTx{fundingTx},
}
@@ -142,11 +141,10 @@ func TestIgnoreChannelEdgePolicyForUnknownChannel(t *testing.T) {
// Add the edge between the two unknown nodes to the graph, and check
// that the nodes are found after the fact.
- script, fundingTx, _, chanID, err := createChannelEdge(
- bitcoinKey1.SerializeCompressed(),
+ script, fundingTx, _, chanID := createChannelEdge(
+ t, bitcoinKey1.SerializeCompressed(),
bitcoinKey2.SerializeCompressed(), 10000, 500,
)
- require.NoError(t, err, "unable to create channel edge")
fundingBlock := &wire.MsgBlock{
Transactions: []*wire.MsgTx{fundingTx},
}
@@ -223,12 +221,11 @@ func TestWakeUpOnStaleBranch(t *testing.T) {
}
height := startingBlockHeight + i
if i == 5 {
- script, fundingTx, _, chanID, err := createChannelEdge(
- bitcoinKey1.SerializeCompressed(),
+ script, fundingTx, _, chanID := createChannelEdge(
+ t, bitcoinKey1.SerializeCompressed(),
bitcoinKey2.SerializeCompressed(),
chanValue, height,
)
- require.NoError(t, err)
block.Transactions = append(block.Transactions,
fundingTx)
chanID1 = chanID.ToUint64()
@@ -254,11 +251,10 @@ func TestWakeUpOnStaleBranch(t *testing.T) {
}
height := uint32(forkHeight) + i
if i == 5 {
- script, fundingTx, _, chanID, err := createChannelEdge(
- bitcoinKey1.SerializeCompressed(),
+ script, fundingTx, _, chanID := createChannelEdge(
+ t, bitcoinKey1.SerializeCompressed(),
bitcoinKey2.SerializeCompressed(),
chanValue, height)
- require.NoError(t, err)
block.Transactions = append(block.Transactions,
fundingTx)
chanID2 = chanID.ToUint64()
@@ -399,12 +395,11 @@ func TestDisconnectedBlocks(t *testing.T) {
}
height := startingBlockHeight + i
if i == 5 {
- _, fundingTx, _, chanID, err := createChannelEdge(
- bitcoinKey1.SerializeCompressed(),
+ _, fundingTx, _, chanID := createChannelEdge(
+ t, bitcoinKey1.SerializeCompressed(),
bitcoinKey2.SerializeCompressed(),
chanValue, height,
)
- require.NoError(t, err)
block.Transactions = append(block.Transactions,
fundingTx)
chanID1 = chanID.ToUint64()
@@ -429,12 +424,11 @@ func TestDisconnectedBlocks(t *testing.T) {
}
height := uint32(forkHeight) + i
if i == 5 {
- _, fundingTx, _, chanID, err := createChannelEdge(
- bitcoinKey1.SerializeCompressed(),
+ _, fundingTx, _, chanID := createChannelEdge(
+ t, bitcoinKey1.SerializeCompressed(),
bitcoinKey2.SerializeCompressed(),
chanValue, height,
)
- require.NoError(t, err)
block.Transactions = append(block.Transactions,
fundingTx)
chanID2 = chanID.ToUint64()
@@ -557,12 +551,11 @@ func TestChansClosedOfflinePruneGraph(t *testing.T) {
Transactions: []*wire.MsgTx{},
}
nextHeight := startingBlockHeight + 1
- script, fundingTx1, chanUTXO, chanID1, err := createChannelEdge(
- bitcoinKey1.SerializeCompressed(),
+ script, fundingTx1, chanUTXO, chanID1 := createChannelEdge(
+ t, bitcoinKey1.SerializeCompressed(),
bitcoinKey2.SerializeCompressed(),
chanValue, uint32(nextHeight),
)
- require.NoError(t, err, "unable create channel edge")
block102.Transactions = append(block102.Transactions, fundingTx1)
ctx.chain.addBlock(block102, uint32(nextHeight), rand.Uint32())
ctx.chain.setBestBlock(int32(nextHeight))
@@ -964,12 +957,11 @@ func TestIsStaleNode(t *testing.T) {
copy(pub1[:], priv1.PubKey().SerializeCompressed())
copy(pub2[:], priv2.PubKey().SerializeCompressed())
- script, fundingTx, _, chanID, err := createChannelEdge(
- bitcoinKey1.SerializeCompressed(),
+ script, fundingTx, _, chanID := createChannelEdge(
+ t, bitcoinKey1.SerializeCompressed(),
bitcoinKey2.SerializeCompressed(),
10000, 500,
)
- require.NoError(t, err, "unable to create channel edge")
fundingBlock := &wire.MsgBlock{
Transactions: []*wire.MsgTx{fundingTx},
}
@@ -1033,12 +1025,11 @@ func TestIsKnownEdge(t *testing.T) {
copy(pub1[:], priv1.PubKey().SerializeCompressed())
copy(pub2[:], priv2.PubKey().SerializeCompressed())
- script, fundingTx, _, chanID, err := createChannelEdge(
- bitcoinKey1.SerializeCompressed(),
+ script, fundingTx, _, chanID := createChannelEdge(
+ t, bitcoinKey1.SerializeCompressed(),
bitcoinKey2.SerializeCompressed(),
10000, 500,
)
- require.NoError(t, err, "unable to create channel edge")
fundingBlock := &wire.MsgBlock{
Transactions: []*wire.MsgTx{fundingTx},
}
@@ -1079,12 +1070,11 @@ func TestIsStaleEdgePolicy(t *testing.T) {
copy(pub1[:], priv1.PubKey().SerializeCompressed())
copy(pub2[:], priv2.PubKey().SerializeCompressed())
- script, fundingTx, _, chanID, err := createChannelEdge(
- bitcoinKey1.SerializeCompressed(),
+ script, fundingTx, _, chanID := createChannelEdge(
+ t, bitcoinKey1.SerializeCompressed(),
bitcoinKey2.SerializeCompressed(),
10000, 500,
)
- require.NoError(t, err, "unable to create channel edge")
fundingBlock := &wire.MsgBlock{
Transactions: []*wire.MsgTx{fundingTx},
}
diff --git a/graph/notifications_test.go b/graph/notifications_test.go
index bef6ebd..f8bc0f2 100644
--- a/graph/notifications_test.go
+++ b/graph/notifications_test.go
@@ -128,9 +128,11 @@ func randEdgePolicy(t testing.TB, chanID *lnwire.ShortChannelID,
}
}
-func createChannelEdge(bitcoinKey1, bitcoinKey2 []byte,
+func createChannelEdge(t testing.TB, bitcoinKey1, bitcoinKey2 []byte,
chanValue btcutil.Amount, fundingHeight uint32) ([]byte, *wire.MsgTx,
- *wire.OutPoint, *lnwire.ShortChannelID, error) {
+ *wire.OutPoint, *lnwire.ShortChannelID) {
+
+ t.Helper()
fundingTx := wire.NewMsgTx(2)
script, tx, err := input.GenFundingPkScript(
@@ -138,9 +140,7 @@ func createChannelEdge(bitcoinKey1, bitcoinKey2 []byte,
bitcoinKey2,
int64(chanValue),
)
- if err != nil {
- return nil, nil, nil, nil, err
- }
+ require.NoError(t, err)
fundingTx.TxOut = append(fundingTx.TxOut, tx)
chanUtxo := wire.OutPoint{
@@ -155,7 +155,7 @@ func createChannelEdge(bitcoinKey1, bitcoinKey2 []byte,
TxPosition: 0,
}
- return script, fundingTx, &chanUtxo, chanID, nil
+ return script, fundingTx, &chanUtxo, chanID
}
type mockChain struct {
@@ -429,11 +429,10 @@ func TestEdgeUpdateNotification(t *testing.T) {
// First we'll create the utxo for the channel to be "closed"
const chanValue = 10000
- script, fundingTx, chanPoint, chanID, err := createChannelEdge(
- bitcoinKey1.SerializeCompressed(),
+ script, fundingTx, chanPoint, chanID := createChannelEdge(
+ t, bitcoinKey1.SerializeCompressed(),
bitcoinKey2.SerializeCompressed(), chanValue, 0,
)
- require.NoError(t, err, "unable create channel edge")
// We'll also add a record for the block that included our funding
// transaction.
@@ -589,12 +588,11 @@ func TestNodeUpdateNotification(t *testing.T) {
// We only accept node announcements from nodes having a known channel,
// so create one now.
const chanValue = 10000
- script, fundingTx, _, chanID, err := createChannelEdge(
- bitcoinKey1.SerializeCompressed(),
+ script, fundingTx, _, chanID := createChannelEdge(
+ t, bitcoinKey1.SerializeCompressed(),
bitcoinKey2.SerializeCompressed(),
chanValue, startingBlockHeight,
)
- require.NoError(t, err, "unable create channel edge")
// We'll also add a record for the block that included our funding
// transaction.
@@ -756,12 +754,11 @@ func TestNotificationCancellation(t *testing.T) {
// We'll create the utxo for a new channel.
const chanValue = 10000
- script, fundingTx, chanPoint, chanID, err := createChannelEdge(
- bitcoinKey1.SerializeCompressed(),
+ script, fundingTx, chanPoint, chanID := createChannelEdge(
+ t, bitcoinKey1.SerializeCompressed(),
bitcoinKey2.SerializeCompressed(),
chanValue, startingBlockHeight,
)
- require.NoError(t, err, "unable create channel edge")
// We'll also add a record for the block that included our funding
// transaction.
@@ -835,12 +832,11 @@ func TestChannelCloseNotification(t *testing.T) {
// First we'll create the utxo for the channel to be "closed"
const chanValue = 10000
- script, fundingTx, chanUtxo, chanID, err := createChannelEdge(
- bitcoinKey1.SerializeCompressed(),
+ script, fundingTx, chanUtxo, chanID := createChannelEdge(
+ t, bitcoinKey1.SerializeCompressed(),
bitcoinKey2.SerializeCompressed(), chanValue,
startingBlockHeight,
)
- require.NoError(t, err, "unable create channel edge")
// We'll also add a record for the block that included our funding
// transaction.
Why this scored 15/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.