multi: add and use V1 constructor for models.ChannelEdgeInfo
This makes it clear what fields must/can be set for a V1 channel. Introduce NewV1Channel constructor to create v1 channel edges with proper initialization and validation. The constructor: - Takes required fields (chanID, chainHash, node keys) as parameters - Takes v1-specific fields (bitcoin keys, extra opaque data) via ChannelV1Fields struct - Accepts optional fields (capacity, channel point, features, proof) via functional options (WithCapacity, WithChannelPoint, etc.) - Validates that if an AuthProof is provided, its version matches the channel version This makes it clear which fields are required vs optional for v1 channels and prevents incorrectly initialized channel edges. All tests and production code updated to use the constructor.
This commit is contained in:
18 files changed
+853
-575
No files matched your search
+85
-70
@@ -14,6 +14,7 @@ import (
|
||||
"github.com/btcsuite/btcd/btcec/v2"
|
||||
"github.com/btcsuite/btcd/btcec/v2/ecdsa"
|
||||
"github.com/btcsuite/btcd/btcutil"
|
||||
"github.com/btcsuite/btcd/chaincfg"
|
||||
"github.com/btcsuite/btcd/chaincfg/chainhash"
|
||||
"github.com/btcsuite/btcd/wire"
|
||||
"github.com/lightningnetwork/lnd/chainntnfs"
|
||||
@@ -447,24 +448,27 @@ func TestEdgeUpdateNotification(t *testing.T) {
|
||||
|
||||
// Finally, to conclude our test set up, we'll create a channel
|
||||
// update to announce the created channel between the two nodes.
|
||||
edge := &models.ChannelEdgeInfo{
|
||||
Version: lnwire.GossipVersion1,
|
||||
ChannelID: chanID.ToUint64(),
|
||||
NodeKey1Bytes: node1.PubKeyBytes,
|
||||
NodeKey2Bytes: node2.PubKeyBytes,
|
||||
AuthProof: models.NewV1ChannelAuthProof(
|
||||
testSig.Serialize(),
|
||||
testSig.Serialize(),
|
||||
testSig.Serialize(),
|
||||
testSig.Serialize(),
|
||||
),
|
||||
Features: lnwire.EmptyFeatureVector(),
|
||||
ChannelPoint: *chanPoint,
|
||||
Capacity: chanValue,
|
||||
FundingScript: fn.Some(script),
|
||||
}
|
||||
copy(edge.BitcoinKey1Bytes[:], bitcoinKey1.SerializeCompressed())
|
||||
copy(edge.BitcoinKey2Bytes[:], bitcoinKey2.SerializeCompressed())
|
||||
btcKey1 := route.NewVertex(bitcoinKey1)
|
||||
btcKey2 := route.NewVertex(bitcoinKey2)
|
||||
|
||||
proof := models.NewV1ChannelAuthProof(
|
||||
testSig.Serialize(),
|
||||
testSig.Serialize(),
|
||||
testSig.Serialize(),
|
||||
testSig.Serialize(),
|
||||
)
|
||||
|
||||
edge, err := models.NewV1Channel(
|
||||
chanID.ToUint64(), *chaincfg.SimNetParams.GenesisHash,
|
||||
node1.PubKeyBytes, node2.PubKeyBytes, &models.ChannelV1Fields{
|
||||
BitcoinKey1Bytes: btcKey1,
|
||||
BitcoinKey2Bytes: btcKey2,
|
||||
}, models.WithChanProof(proof),
|
||||
models.WithChannelPoint(*chanPoint),
|
||||
models.WithCapacity(chanValue),
|
||||
models.WithFundingScript(script),
|
||||
)
|
||||
require.NoError(t, err)
|
||||
|
||||
if err := ctx.builder.AddEdge(ctxb, edge); err != nil {
|
||||
t.Fatalf("unable to add edge: %v", err)
|
||||
@@ -642,22 +646,25 @@ func TestNodeUpdateNotification(t *testing.T) {
|
||||
testFeaturesBuf := new(bytes.Buffer)
|
||||
require.NoError(t, testFeatures.Encode(testFeaturesBuf))
|
||||
|
||||
edge := &models.ChannelEdgeInfo{
|
||||
Version: lnwire.GossipVersion1,
|
||||
ChannelID: chanID.ToUint64(),
|
||||
NodeKey1Bytes: node1.PubKeyBytes,
|
||||
NodeKey2Bytes: node2.PubKeyBytes,
|
||||
Features: lnwire.EmptyFeatureVector(),
|
||||
AuthProof: models.NewV1ChannelAuthProof(
|
||||
testSig.Serialize(),
|
||||
testSig.Serialize(),
|
||||
testSig.Serialize(),
|
||||
testSig.Serialize(),
|
||||
),
|
||||
FundingScript: fn.Some(script),
|
||||
}
|
||||
copy(edge.BitcoinKey1Bytes[:], bitcoinKey1.SerializeCompressed())
|
||||
copy(edge.BitcoinKey2Bytes[:], bitcoinKey2.SerializeCompressed())
|
||||
btcKey1 := route.NewVertex(bitcoinKey1)
|
||||
btcKey2 := route.NewVertex(bitcoinKey2)
|
||||
|
||||
proof := models.NewV1ChannelAuthProof(
|
||||
testSig.Serialize(),
|
||||
testSig.Serialize(),
|
||||
testSig.Serialize(),
|
||||
testSig.Serialize(),
|
||||
)
|
||||
|
||||
edge, err := models.NewV1Channel(
|
||||
chanID.ToUint64(), *chaincfg.SimNetParams.GenesisHash,
|
||||
node1.PubKeyBytes, node2.PubKeyBytes, &models.ChannelV1Fields{
|
||||
BitcoinKey1Bytes: btcKey1,
|
||||
BitcoinKey2Bytes: btcKey2,
|
||||
}, models.WithChanProof(proof),
|
||||
models.WithFundingScript(script),
|
||||
)
|
||||
require.NoError(t, err)
|
||||
|
||||
// Adding the edge will add the nodes to the graph, but with no info
|
||||
// except the pubkey known.
|
||||
@@ -829,24 +836,28 @@ func TestNotificationCancellation(t *testing.T) {
|
||||
// to the client.
|
||||
ntfnClient.Cancel()
|
||||
|
||||
edge := &models.ChannelEdgeInfo{
|
||||
Version: lnwire.GossipVersion1,
|
||||
ChannelID: chanID.ToUint64(),
|
||||
NodeKey1Bytes: node1.PubKeyBytes,
|
||||
NodeKey2Bytes: node2.PubKeyBytes,
|
||||
AuthProof: models.NewV1ChannelAuthProof(
|
||||
testSig.Serialize(),
|
||||
testSig.Serialize(),
|
||||
testSig.Serialize(),
|
||||
testSig.Serialize(),
|
||||
),
|
||||
Features: lnwire.EmptyFeatureVector(),
|
||||
ChannelPoint: *chanPoint,
|
||||
Capacity: chanValue,
|
||||
FundingScript: fn.Some(script),
|
||||
}
|
||||
copy(edge.BitcoinKey1Bytes[:], bitcoinKey1.SerializeCompressed())
|
||||
copy(edge.BitcoinKey2Bytes[:], bitcoinKey2.SerializeCompressed())
|
||||
btcKey1 := route.NewVertex(bitcoinKey1)
|
||||
btcKey2 := route.NewVertex(bitcoinKey2)
|
||||
|
||||
proof := models.NewV1ChannelAuthProof(
|
||||
testSig.Serialize(),
|
||||
testSig.Serialize(),
|
||||
testSig.Serialize(),
|
||||
testSig.Serialize(),
|
||||
)
|
||||
|
||||
edge, err := models.NewV1Channel(
|
||||
chanID.ToUint64(), *chaincfg.SimNetParams.GenesisHash,
|
||||
node1.PubKeyBytes, node2.PubKeyBytes, &models.ChannelV1Fields{
|
||||
BitcoinKey1Bytes: btcKey1,
|
||||
BitcoinKey2Bytes: btcKey2,
|
||||
}, models.WithChanProof(proof),
|
||||
models.WithChannelPoint(*chanPoint),
|
||||
models.WithCapacity(chanValue),
|
||||
models.WithFundingScript(script),
|
||||
)
|
||||
require.NoError(t, err)
|
||||
|
||||
if err := ctx.builder.AddEdge(ctxb, edge); err != nil {
|
||||
t.Fatalf("unable to add edge: %v", err)
|
||||
}
|
||||
@@ -906,24 +917,28 @@ func TestChannelCloseNotification(t *testing.T) {
|
||||
|
||||
// Finally, to conclude our test set up, we'll create a channel
|
||||
// announcement to announce the created channel between the two nodes.
|
||||
edge := &models.ChannelEdgeInfo{
|
||||
Version: lnwire.GossipVersion1,
|
||||
ChannelID: chanID.ToUint64(),
|
||||
NodeKey1Bytes: node1.PubKeyBytes,
|
||||
NodeKey2Bytes: node2.PubKeyBytes,
|
||||
AuthProof: models.NewV1ChannelAuthProof(
|
||||
testSig.Serialize(),
|
||||
testSig.Serialize(),
|
||||
testSig.Serialize(),
|
||||
testSig.Serialize(),
|
||||
),
|
||||
Features: lnwire.EmptyFeatureVector(),
|
||||
ChannelPoint: *chanUtxo,
|
||||
Capacity: chanValue,
|
||||
FundingScript: fn.Some(script),
|
||||
}
|
||||
copy(edge.BitcoinKey1Bytes[:], bitcoinKey1.SerializeCompressed())
|
||||
copy(edge.BitcoinKey2Bytes[:], bitcoinKey2.SerializeCompressed())
|
||||
btcKey1 := route.NewVertex(bitcoinKey1)
|
||||
btcKey2 := route.NewVertex(bitcoinKey2)
|
||||
|
||||
proof := models.NewV1ChannelAuthProof(
|
||||
testSig.Serialize(),
|
||||
testSig.Serialize(),
|
||||
testSig.Serialize(),
|
||||
testSig.Serialize(),
|
||||
)
|
||||
|
||||
edge, err := models.NewV1Channel(
|
||||
chanID.ToUint64(), *chaincfg.SimNetParams.GenesisHash,
|
||||
node1.PubKeyBytes, node2.PubKeyBytes, &models.ChannelV1Fields{
|
||||
BitcoinKey1Bytes: btcKey1,
|
||||
BitcoinKey2Bytes: btcKey2,
|
||||
}, models.WithChanProof(proof),
|
||||
models.WithChannelPoint(*chanUtxo),
|
||||
models.WithCapacity(chanValue),
|
||||
models.WithFundingScript(script),
|
||||
)
|
||||
require.NoError(t, err)
|
||||
|
||||
if err := ctx.builder.AddEdge(ctxb, edge); err != nil {
|
||||
t.Fatalf("unable to add edge: %v", err)
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user