routing: add color to node update

This commit is contained in:
Xavi Soler 2018-12-21 17:32:43 +01:00
parent ee2e49141e
commit 28021361d1
No known key found for this signature in database
GPG Key ID: 4B65A4B63378DF1E
2 changed files with 41 additions and 0 deletions

View File

@ -2,6 +2,7 @@ package routing
import (
"fmt"
"image/color"
"net"
"sync"
"sync/atomic"
@ -246,6 +247,9 @@ type NetworkNodeUpdate struct {
// Alias is the alias or nick name of the node.
Alias string
// Color is the node's color in hex code format.
Color string
}
// ChannelEdgeUpdate is an update for a new channel within the ChannelGraph.
@ -321,6 +325,7 @@ func addToTopologyChange(graph *channeldb.ChannelGraph, update *TopologyChange,
Addresses: m.Addresses,
IdentityKey: pubKey,
Alias: m.Alias,
Color: EncodeHexColor(m.Color),
}
nodeUpdate.IdentityKey.Curve = nil
@ -388,3 +393,8 @@ func addToTopologyChange(graph *channeldb.ChannelGraph, update *TopologyChange,
"unknown message type %T", msg)
}
}
// EncodeHexColor takes a color and returns it in hex code format.
func EncodeHexColor(color color.RGBA) string {
return fmt.Sprintf("#%02x%02x%02x", color.R, color.G, color.B)
}

View File

@ -628,6 +628,10 @@ func TestNodeUpdateNotification(t *testing.T) {
t.Fatalf("node alias doesn't match: expected %v, got %v",
ann.Alias, nodeUpdate.Alias)
}
if nodeUpdate.Color != EncodeHexColor(ann.Color) {
t.Fatalf("node color doesn't match: expected %v, got %v",
EncodeHexColor(ann.Color), nodeUpdate.Color)
}
}
// Create lookup map for notifications we are intending to receive. Entries
@ -926,3 +930,30 @@ func TestChannelCloseNotification(t *testing.T) {
t.Fatal("notification not sent")
}
}
// TestEncodeHexColor tests that the string used to represent a node color is
// correctly encoded.
func TestEncodeHexColor(t *testing.T) {
var colorTestCases = []struct {
R uint8
G uint8
B uint8
encoded string
isValid bool
}{
{0, 0, 0, "#000000", true},
{255, 255, 255, "#ffffff", true},
{255, 117, 215, "#ff75d7", true},
{0, 0, 0, "000000", false},
{1, 2, 3, "", false},
{1, 2, 3, "#", false},
}
for _, tc := range colorTestCases {
encoded := EncodeHexColor(color.RGBA{tc.R, tc.G, tc.B, 0})
if (encoded == tc.encoded) != tc.isValid {
t.Fatalf("incorrect color encoding, "+
"want: %v, got: %v", tc.encoded, encoded)
}
}
}