Skip to content

Commit 8e96bd0

Browse files
authored
Merge pull request #9866 from ellemouton/graphSQL7-nodes-tables
sqldb+graph/db: add node related tables and implement some node CRUD
2 parents 3b2af90 + efc04b9 commit 8e96bd0

File tree

11 files changed

+1780
-35
lines changed

11 files changed

+1780
-35
lines changed

docs/release-notes/release-notes-0.20.0.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@
4242
byte blobs at the end of gossip messages are valid TLV streams.
4343
* Various [preparations](https://github.com/lightningnetwork/lnd/pull/9692)
4444
of the graph code before the SQL implementation is added.
45+
* Add graph schemas, queries and CRUD:
46+
* [1](https://github.com/lightningnetwork/lnd/pull/9866)
4547

4648
## RPC Updates
4749

graph/db/graph_test.go

Lines changed: 19 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -101,15 +101,17 @@ func createTestVertex(t testing.TB) *models.LightningNode {
101101
func TestNodeInsertionAndDeletion(t *testing.T) {
102102
t.Parallel()
103103

104-
graph := MakeTestGraph(t)
104+
graph := MakeTestGraphNew(t)
105105

106106
// We'd like to test basic insertion/deletion for vertexes from the
107107
// graph, so we'll create a test vertex to start with.
108+
timeStamp := int64(1232342)
108109
nodeWithAddrs := func(addrs []net.Addr) *models.LightningNode {
110+
timeStamp++
109111
return &models.LightningNode{
110112
HaveNodeAnnouncement: true,
111113
AuthSigBytes: testSig.Serialize(),
112-
LastUpdate: time.Unix(1232342, 0),
114+
LastUpdate: time.Unix(timeStamp, 0),
113115
Color: color.RGBA{1, 2, 3, 0},
114116
Alias: "kek",
115117
Features: testFeatures,
@@ -323,31 +325,27 @@ func TestPartialNode(t *testing.T) {
323325
require.ErrorIs(t, err, ErrGraphNodeNotFound)
324326
}
325327

328+
// TestAliasLookup tests the alias lookup functionality of the graph store.
326329
func TestAliasLookup(t *testing.T) {
327330
t.Parallel()
328331

329-
graph := MakeTestGraph(t)
332+
graph := MakeTestGraphNew(t)
330333

331334
// We'd like to test the alias index within the database, so first
332335
// create a new test node.
333336
testNode := createTestVertex(t)
334337

335338
// Add the node to the graph's database, this should also insert an
336339
// entry into the alias index for this node.
337-
if err := graph.AddLightningNode(testNode); err != nil {
338-
t.Fatalf("unable to add node: %v", err)
339-
}
340+
require.NoError(t, graph.AddLightningNode(testNode))
340341

341342
// Next, attempt to lookup the alias. The alias should exactly match
342343
// the one which the test node was assigned.
343344
nodePub, err := testNode.PubKey()
344345
require.NoError(t, err, "unable to generate pubkey")
345346
dbAlias, err := graph.LookupAlias(nodePub)
346347
require.NoError(t, err, "unable to find alias")
347-
if dbAlias != testNode.Alias {
348-
t.Fatalf("aliases don't match, expected %v got %v",
349-
testNode.Alias, dbAlias)
350-
}
348+
require.Equal(t, testNode.Alias, dbAlias)
351349

352350
// Ensure that looking up a non-existent alias results in an error.
353351
node := createTestVertex(t)
@@ -357,10 +355,11 @@ func TestAliasLookup(t *testing.T) {
357355
require.ErrorIs(t, err, ErrNodeAliasNotFound)
358356
}
359357

358+
// TestSourceNode tests the source node functionality of the graph store.
360359
func TestSourceNode(t *testing.T) {
361360
t.Parallel()
362361

363-
graph := MakeTestGraph(t)
362+
graph := MakeTestGraphNew(t)
364363

365364
// We'd like to test the setting/getting of the source node, so we
366365
// first create a fake node to use within the test.
@@ -371,11 +370,9 @@ func TestSourceNode(t *testing.T) {
371370
_, err := graph.SourceNode()
372371
require.ErrorIs(t, err, ErrSourceNodeNotSet)
373372

374-
// Set the source the source node, this should insert the node into the
373+
// Set the source node, this should insert the node into the
375374
// database in a special way indicating it's the source node.
376-
if err := graph.SetSourceNode(testNode); err != nil {
377-
t.Fatalf("unable to set source node: %v", err)
378-
}
375+
require.NoError(t, graph.SetSourceNode(testNode))
379376

380377
// Retrieve the source node from the database, it should exactly match
381378
// the one we set above.
@@ -2082,7 +2079,7 @@ func TestChanUpdatesInHorizon(t *testing.T) {
20822079
func TestNodeUpdatesInHorizon(t *testing.T) {
20832080
t.Parallel()
20842081

2085-
graph := MakeTestGraph(t)
2082+
graph := MakeTestGraphNew(t)
20862083

20872084
startTime := time.Unix(1234, 0)
20882085
endTime := startTime
@@ -2093,10 +2090,7 @@ func TestNodeUpdatesInHorizon(t *testing.T) {
20932090
time.Unix(999, 0), time.Unix(9999, 0),
20942091
)
20952092
require.NoError(t, err, "unable to query for node updates")
2096-
if len(nodeUpdates) != 0 {
2097-
t.Fatalf("expected 0 node updates, instead got %v",
2098-
len(nodeUpdates))
2099-
}
2093+
require.Len(t, nodeUpdates, 0)
21002094

21012095
// We'll create 10 node announcements, each with an update timestamp 10
21022096
// seconds after the other.
@@ -2115,9 +2109,7 @@ func TestNodeUpdatesInHorizon(t *testing.T) {
21152109

21162110
nodeAnns = append(nodeAnns, *nodeAnn)
21172111

2118-
if err := graph.AddLightningNode(nodeAnn); err != nil {
2119-
t.Fatalf("unable to add lightning node: %v", err)
2120-
}
2112+
require.NoError(t, graph.AddLightningNode(nodeAnn))
21212113
}
21222114

21232115
queryCases := []struct {
@@ -2171,15 +2163,8 @@ func TestNodeUpdatesInHorizon(t *testing.T) {
21712163
resp, err := graph.NodeUpdatesInHorizon(
21722164
queryCase.start, queryCase.end,
21732165
)
2174-
if err != nil {
2175-
t.Fatalf("unable to query for nodes: %v", err)
2176-
}
2177-
2178-
if len(resp) != len(queryCase.resp) {
2179-
t.Fatalf("expected %v nodes, got %v nodes",
2180-
len(queryCase.resp), len(resp))
2181-
2182-
}
2166+
require.NoError(t, err)
2167+
require.Len(t, resp, len(queryCase.resp))
21832168

21842169
for i := 0; i < len(resp); i++ {
21852170
compareNodes(t, &queryCase.resp[i], &resp[i])
@@ -3384,7 +3369,7 @@ func TestAddChannelEdgeShellNodes(t *testing.T) {
33843369
func TestNodePruningUpdateIndexDeletion(t *testing.T) {
33853370
t.Parallel()
33863371

3387-
graph := MakeTestGraph(t)
3372+
graph := MakeTestGraphNew(t)
33883373

33893374
// We'll first populate our graph with a single node that will be
33903375
// removed shortly.
@@ -4315,7 +4300,7 @@ func TestLightningNodePersistence(t *testing.T) {
43154300
t.Parallel()
43164301

43174302
// Create a new test graph instance.
4318-
graph := MakeTestGraph(t)
4303+
graph := MakeTestGraphNew(t)
43194304

43204305
nodeAnnBytes, err := hex.DecodeString(testNodeAnn)
43214306
require.NoError(t, err)

graph/db/notifications.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"fmt"
55
"image/color"
66
"net"
7+
"strconv"
78
"sync"
89
"sync/atomic"
910

@@ -463,3 +464,31 @@ func (c *ChannelGraph) addToTopologyChange(update *TopologyChange,
463464
func EncodeHexColor(color color.RGBA) string {
464465
return fmt.Sprintf("#%02x%02x%02x", color.R, color.G, color.B)
465466
}
467+
468+
// DecodeHexColor takes a hex color string like "#rrggbb" and returns a
469+
// color.RGBA.
470+
func DecodeHexColor(hex string) (color.RGBA, error) {
471+
r, err := strconv.ParseUint(hex[1:3], 16, 8)
472+
if err != nil {
473+
return color.RGBA{}, fmt.Errorf("invalid red component: %w",
474+
err)
475+
}
476+
477+
g, err := strconv.ParseUint(hex[3:5], 16, 8)
478+
if err != nil {
479+
return color.RGBA{}, fmt.Errorf("invalid green component: %w",
480+
err)
481+
}
482+
483+
b, err := strconv.ParseUint(hex[5:7], 16, 8)
484+
if err != nil {
485+
return color.RGBA{}, fmt.Errorf("invalid blue component: %w",
486+
err)
487+
}
488+
489+
return color.RGBA{
490+
R: uint8(r),
491+
G: uint8(g),
492+
B: uint8(b),
493+
}, nil
494+
}

0 commit comments

Comments
 (0)