Skip to content

Commit 80e7009

Browse files
ellemoutonyyforyongyu
authored andcommitted
multi: add models.Node V1 constructor
Add a version field to models.Node and a V1 constructor for it.
1 parent 8f205e2 commit 80e7009

18 files changed

+444
-341
lines changed

autopilot/prefattach_test.go

Lines changed: 35 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -417,20 +417,20 @@ func (d *testDBGraph) addRandChannel(node1, node2 *btcec.PublicKey,
417417
case errors.Is(err, graphdb.ErrGraphNodeNotFound):
418418
fallthrough
419419
case errors.Is(err, graphdb.ErrGraphNotFound):
420-
graphNode := &models.Node{
421-
Addresses: []net.Addr{&net.TCPAddr{
422-
IP: bytes.Repeat(
423-
[]byte("a"), 16,
424-
),
425-
}},
426-
Features: lnwire.NewFeatureVector(
427-
nil, lnwire.Features,
428-
),
429-
AuthSigBytes: testSig.Serialize(),
430-
}
431-
copy(
432-
graphNode.PubKeyBytes[:],
433-
pub.SerializeCompressed(),
420+
//nolint:ll
421+
graphNode := models.NewV1Node(
422+
route.NewVertex(pub),
423+
&models.NodeV1Fields{
424+
Addresses: []net.Addr{&net.TCPAddr{
425+
IP: bytes.Repeat(
426+
[]byte("a"), 16,
427+
),
428+
}},
429+
Features: lnwire.NewFeatureVector(
430+
nil, lnwire.Features,
431+
).RawFeatureVector,
432+
AuthSigBytes: testSig.Serialize(),
433+
},
434434
)
435435
err := d.db.AddNode(
436436
context.Background(), graphNode,
@@ -449,18 +449,18 @@ func (d *testDBGraph) addRandChannel(node1, node2 *btcec.PublicKey,
449449
if err != nil {
450450
return nil, err
451451
}
452-
dbNode := &models.Node{
453-
Addresses: []net.Addr{
454-
&net.TCPAddr{
452+
453+
dbNode := models.NewV1Node(
454+
route.NewVertex(nodeKey), &models.NodeV1Fields{
455+
Addresses: []net.Addr{&net.TCPAddr{
455456
IP: bytes.Repeat([]byte("a"), 16),
456-
},
457+
}},
458+
Features: lnwire.NewFeatureVector(
459+
nil, lnwire.Features,
460+
).RawFeatureVector,
461+
AuthSigBytes: testSig.Serialize(),
457462
},
458-
Features: lnwire.NewFeatureVector(
459-
nil, lnwire.Features,
460-
),
461-
AuthSigBytes: testSig.Serialize(),
462-
}
463-
copy(dbNode.PubKeyBytes[:], nodeKey.SerializeCompressed())
463+
)
464464
if err := d.db.AddNode(
465465
context.Background(), dbNode,
466466
); err != nil {
@@ -549,18 +549,19 @@ func (d *testDBGraph) addRandNode() (*btcec.PublicKey, error) {
549549
if err != nil {
550550
return nil, err
551551
}
552-
dbNode := &models.Node{
553-
Addresses: []net.Addr{
554-
&net.TCPAddr{
555-
IP: bytes.Repeat([]byte("a"), 16),
552+
dbNode := models.NewV1Node(
553+
route.NewVertex(nodeKey), &models.NodeV1Fields{
554+
Addresses: []net.Addr{
555+
&net.TCPAddr{
556+
IP: bytes.Repeat([]byte("a"), 16),
557+
},
556558
},
559+
Features: lnwire.NewFeatureVector(
560+
nil, lnwire.Features,
561+
).RawFeatureVector,
562+
AuthSigBytes: testSig.Serialize(),
557563
},
558-
Features: lnwire.NewFeatureVector(
559-
nil, lnwire.Features,
560-
),
561-
AuthSigBytes: testSig.Serialize(),
562-
}
563-
copy(dbNode.PubKeyBytes[:], nodeKey.SerializeCompressed())
564+
)
564565
err = d.db.AddNode(context.Background(), dbNode)
565566
if err != nil {
566567
return nil, err

channeldb/db_test.go

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"github.com/lightningnetwork/lnd/kvdb"
2020
"github.com/lightningnetwork/lnd/lntypes"
2121
"github.com/lightningnetwork/lnd/lnwire"
22+
"github.com/lightningnetwork/lnd/routing/route"
2223
"github.com/lightningnetwork/lnd/shachain"
2324
"github.com/stretchr/testify/require"
2425
)
@@ -811,15 +812,17 @@ func createNode(priv *btcec.PrivateKey) *models.Node {
811812
updateTime := rand.Int63()
812813

813814
pub := priv.PubKey().SerializeCompressed()
814-
n := &models.Node{
815-
AuthSigBytes: testSig.Serialize(),
816-
LastUpdate: time.Unix(updateTime, 0),
817-
Color: color.RGBA{1, 2, 3, 0},
818-
Alias: "kek" + string(pub),
819-
Features: testFeatures,
820-
Addresses: testAddrs,
821-
}
822-
copy(n.PubKeyBytes[:], priv.PubKey().SerializeCompressed())
815+
n := models.NewV1Node(
816+
route.NewVertex(priv.PubKey()),
817+
&models.NodeV1Fields{
818+
AuthSigBytes: testSig.Serialize(),
819+
LastUpdate: time.Unix(updateTime, 0),
820+
Color: color.RGBA{1, 2, 3, 0},
821+
Alias: "kek" + string(pub),
822+
Features: testFeatures.RawFeatureVector,
823+
Addresses: testAddrs,
824+
},
825+
)
823826

824827
return n
825828
}

graph/builder_test.go

Lines changed: 35 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -97,15 +97,16 @@ func TestIgnoreNodeAnnouncement(t *testing.T) {
9797
ctx := createTestCtxFromFile(t, startingBlockHeight, basicGraphFilePath)
9898

9999
pub := priv1.PubKey()
100-
node := &models.Node{
101-
LastUpdate: time.Unix(123, 0),
102-
Addresses: testAddrs,
103-
Color: color.RGBA{1, 2, 3, 0},
104-
Alias: "node11",
105-
AuthSigBytes: testSig.Serialize(),
106-
Features: testFeatures,
107-
}
108-
copy(node.PubKeyBytes[:], pub.SerializeCompressed())
100+
node := models.NewV1Node(
101+
route.NewVertex(pub), &models.NodeV1Fields{
102+
Addresses: testAddrs,
103+
AuthSigBytes: testSig.Serialize(),
104+
Features: testFeatures.RawFeatureVector,
105+
LastUpdate: time.Unix(123, 0),
106+
Color: color.RGBA{1, 2, 3, 0},
107+
Alias: "node11",
108+
},
109+
)
109110

110111
err := ctx.builder.AddNode(t.Context(), node)
111112
if !IsError(err, ErrIgnored) {
@@ -1083,15 +1084,16 @@ func TestIsStaleNode(t *testing.T) {
10831084

10841085
// With the node stub in the database, we'll add the fully node
10851086
// announcement to the database.
1086-
n1 := &models.Node{
1087-
LastUpdate: updateTimeStamp,
1088-
Addresses: testAddrs,
1089-
Color: color.RGBA{1, 2, 3, 0},
1090-
Alias: "node11",
1091-
AuthSigBytes: testSig.Serialize(),
1092-
Features: testFeatures,
1093-
}
1094-
copy(n1.PubKeyBytes[:], priv1.PubKey().SerializeCompressed())
1087+
n1 := models.NewV1Node(
1088+
route.NewVertex(priv1.PubKey()), &models.NodeV1Fields{
1089+
LastUpdate: updateTimeStamp,
1090+
Addresses: testAddrs,
1091+
Color: color.RGBA{1, 2, 3, 0},
1092+
Alias: "node11",
1093+
AuthSigBytes: testSig.Serialize(),
1094+
Features: testFeatures.RawFeatureVector,
1095+
},
1096+
)
10951097
if err := ctx.builder.AddNode(t.Context(), n1); err != nil {
10961098
t.Fatalf("could not add node: %v", err)
10971099
}
@@ -1399,14 +1401,16 @@ func parseTestGraph(t *testing.T, useCache bool, path string) (
13991401
return nil, err
14001402
}
14011403

1402-
dbNode := &models.Node{
1404+
pubKey, err := route.NewVertexFromBytes(pubBytes)
1405+
require.NoError(t, err)
1406+
1407+
dbNode := models.NewV1Node(pubKey, &models.NodeV1Fields{
14031408
AuthSigBytes: testSig.Serialize(),
14041409
LastUpdate: testTime,
14051410
Addresses: testAddrs,
14061411
Alias: node.Alias,
1407-
Features: testFeatures,
1408-
}
1409-
copy(dbNode.PubKeyBytes[:], pubBytes)
1412+
Features: testFeatures.RawFeatureVector,
1413+
})
14101414

14111415
// We require all aliases within the graph to be unique for our
14121416
// tests.
@@ -1784,15 +1788,15 @@ func createTestGraphFromChannels(t *testing.T, useCache bool,
17841788
features = lnwire.EmptyFeatureVector()
17851789
}
17861790

1787-
dbNode := &models.Node{
1788-
AuthSigBytes: testSig.Serialize(),
1789-
LastUpdate: testTime,
1790-
Addresses: testAddrs,
1791-
Alias: alias,
1792-
Features: features,
1793-
}
1794-
1795-
copy(dbNode.PubKeyBytes[:], pubKey.SerializeCompressed())
1791+
dbNode := models.NewV1Node(
1792+
route.NewVertex(pubKey), &models.NodeV1Fields{
1793+
AuthSigBytes: testSig.Serialize(),
1794+
LastUpdate: testTime,
1795+
Addresses: testAddrs,
1796+
Alias: alias,
1797+
Features: features.RawFeatureVector,
1798+
},
1799+
)
17961800

17971801
privKeyMap[alias] = privKey
17981802

graph/db/graph_test.go

Lines changed: 32 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -70,18 +70,18 @@ var (
7070
)
7171

7272
func createNode(priv *btcec.PrivateKey) *models.Node {
73-
pub := priv.PubKey().SerializeCompressed()
74-
n := &models.Node{
75-
AuthSigBytes: testSig.Serialize(),
76-
LastUpdate: nextUpdateTime(),
77-
Color: color.RGBA{1, 2, 3, 0},
78-
Alias: "kek" + hex.EncodeToString(pub),
79-
Features: testFeatures,
80-
Addresses: testAddrs,
81-
}
82-
copy(n.PubKeyBytes[:], priv.PubKey().SerializeCompressed())
83-
84-
return n
73+
pubKey := route.NewVertex(priv.PubKey())
74+
75+
return models.NewV1Node(
76+
pubKey, &models.NodeV1Fields{
77+
LastUpdate: nextUpdateTime(),
78+
Color: color.RGBA{1, 2, 3, 0},
79+
Alias: "kek" + hex.EncodeToString(pubKey[:]),
80+
Addresses: testAddrs,
81+
Features: testFeatures.RawFeatureVector,
82+
AuthSigBytes: testSig.Serialize(),
83+
},
84+
)
8585
}
8686

8787
func createTestVertex(t testing.TB) *models.Node {
@@ -105,16 +105,18 @@ func TestNodeInsertionAndDeletion(t *testing.T) {
105105
timeStamp := int64(1232342)
106106
nodeWithAddrs := func(addrs []net.Addr) *models.Node {
107107
timeStamp++
108-
return &models.Node{
109-
AuthSigBytes: testSig.Serialize(),
110-
LastUpdate: time.Unix(timeStamp, 0),
111-
Color: color.RGBA{1, 2, 3, 0},
112-
Alias: "kek",
113-
Features: testFeatures,
114-
Addresses: addrs,
115-
ExtraOpaqueData: []byte{1, 1, 1, 2, 2, 2, 2},
116-
PubKeyBytes: testPub,
117-
}
108+
109+
return models.NewV1Node(
110+
testPub, &models.NodeV1Fields{
111+
AuthSigBytes: testSig.Serialize(),
112+
LastUpdate: time.Unix(timeStamp, 0),
113+
Color: color.RGBA{1, 2, 3, 0},
114+
Alias: "kek",
115+
Features: testFeatures.RawFeatureVector,
116+
Addresses: addrs,
117+
ExtraOpaqueData: []byte{1, 1, 1, 2, 2, 2, 2},
118+
},
119+
)
118120
}
119121

120122
// First, insert the node into the graph DB. This should succeed
@@ -313,11 +315,7 @@ func TestPartialNode(t *testing.T) {
313315

314316
// The two nodes should match exactly! (with default values for
315317
// LastUpdate and db set to satisfy compareNodes())
316-
expectedNode1 := &models.Node{
317-
LastUpdate: time.Unix(0, 0),
318-
PubKeyBytes: pubKey1,
319-
Features: lnwire.EmptyFeatureVector(),
320-
}
318+
expectedNode1 := models.NewV1ShellNode(pubKey1)
321319
compareNodes(t, expectedNode1, dbNode1)
322320

323321
_, exists, err = graph.HasNode(ctx, dbNode2.PubKeyBytes)
@@ -326,11 +324,7 @@ func TestPartialNode(t *testing.T) {
326324

327325
// The two nodes should match exactly! (with default values for
328326
// LastUpdate and db set to satisfy compareNodes())
329-
expectedNode2 := &models.Node{
330-
LastUpdate: time.Unix(0, 0),
331-
PubKeyBytes: pubKey2,
332-
Features: lnwire.EmptyFeatureVector(),
333-
}
327+
expectedNode2 := models.NewV1ShellNode(pubKey2)
334328
compareNodes(t, expectedNode2, dbNode2)
335329

336330
// Next, delete the node from the graph, this should purge all data
@@ -365,7 +359,7 @@ func TestAliasLookup(t *testing.T) {
365359
require.NoError(t, err, "unable to generate pubkey")
366360
dbAlias, err := graph.LookupAlias(ctx, nodePub)
367361
require.NoError(t, err, "unable to find alias")
368-
require.Equal(t, testNode.Alias, dbAlias)
362+
require.Equal(t, testNode.Alias.UnwrapOr(""), dbAlias)
369363

370364
// Ensure that looking up a non-existent alias results in an error.
371365
node := createTestVertex(t)
@@ -1600,7 +1594,7 @@ func fillTestGraph(t testing.TB, graph *ChannelGraph, numNodes,
16001594
node := createTestVertex(t)
16011595

16021596
nodes[i] = node
1603-
nodeIndex[node.Alias] = struct{}{}
1597+
nodeIndex[node.Alias.UnwrapOr("")] = struct{}{}
16041598
}
16051599

16061600
// Add each of the nodes into the graph, they should be inserted
@@ -1612,7 +1606,7 @@ func fillTestGraph(t testing.TB, graph *ChannelGraph, numNodes,
16121606
// Iterate over each node as returned by the graph, if all nodes are
16131607
// reached, then the map created above should be empty.
16141608
err := graph.ForEachNode(ctx, func(n *models.Node) error {
1615-
delete(nodeIndex, n.Alias)
1609+
delete(nodeIndex, n.Alias.UnwrapOr(""))
16161610
return nil
16171611
}, func() {})
16181612
require.NoError(t, err)
@@ -2289,7 +2283,7 @@ func TestNodeUpdatesInHorizon(t *testing.T) {
22892283
require.Len(t, resp, len(queryCase.resp))
22902284

22912285
for i := 0; i < len(resp); i++ {
2292-
compareNodes(t, &queryCase.resp[i], &resp[i])
2286+
compareNodes(t, &queryCase.resp[i], resp[i])
22932287
}
22942288
}
22952289
}
@@ -2487,7 +2481,7 @@ func TestNodeUpdatesInHorizonEarlyTermination(t *testing.T) {
24872481
)
24882482

24892483
// Collect only up to stopAt nodes, breaking afterwards.
2490-
var collected []models.Node
2484+
var collected []*models.Node
24912485
count := 0
24922486
for node := range iter {
24932487
if count >= stopAt {
@@ -3824,7 +3818,7 @@ func TestNodePruningUpdateIndexDeletion(t *testing.T) {
38243818
t.Fatalf("should have 1 nodes instead have: %v",
38253819
len(nodesInHorizon))
38263820
}
3827-
compareNodes(t, node1, &nodesInHorizon[0])
3821+
compareNodes(t, node1, nodesInHorizon[0])
38283822

38293823
// We'll now delete the node from the graph, this should result in it
38303824
// being removed from the update index as well.

graph/db/interfaces.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ type V1Store interface { //nolint:interfacebloat
111111
// by two nodes to quickly determine if they have the same set of up to
112112
// date node announcements.
113113
NodeUpdatesInHorizon(startTime, endTime time.Time,
114-
opts ...IteratorOption) iter.Seq2[models.Node, error]
114+
opts ...IteratorOption) iter.Seq2[*models.Node, error]
115115

116116
// FetchNode attempts to look up a target node by its identity
117117
// public key. If the node isn't found in the database, then

0 commit comments

Comments
 (0)