Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions autopilot/prefattach_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,6 @@ func (d *testDBGraph) addRandChannel(node1, node2 *btcec.PublicKey,
fallthrough
case errors.Is(err, graphdb.ErrGraphNotFound):
graphNode := &models.Node{
HaveNodeAnnouncement: true,
Addresses: []net.Addr{&net.TCPAddr{
IP: bytes.Repeat(
[]byte("a"), 16,
Expand Down Expand Up @@ -451,7 +450,6 @@ func (d *testDBGraph) addRandChannel(node1, node2 *btcec.PublicKey,
return nil, err
}
dbNode := &models.Node{
HaveNodeAnnouncement: true,
Addresses: []net.Addr{
&net.TCPAddr{
IP: bytes.Repeat([]byte("a"), 16),
Expand Down Expand Up @@ -552,7 +550,6 @@ func (d *testDBGraph) addRandNode() (*btcec.PublicKey, error) {
return nil, err
}
dbNode := &models.Node{
HaveNodeAnnouncement: true,
Addresses: []net.Addr{
&net.TCPAddr{
IP: bytes.Repeat([]byte("a"), 16),
Expand Down
13 changes: 6 additions & 7 deletions channeldb/db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -812,13 +812,12 @@ func createNode(priv *btcec.PrivateKey) *models.Node {

pub := priv.PubKey().SerializeCompressed()
n := &models.Node{
HaveNodeAnnouncement: true,
AuthSigBytes: testSig.Serialize(),
LastUpdate: time.Unix(updateTime, 0),
Color: color.RGBA{1, 2, 3, 0},
Alias: "kek" + string(pub),
Features: testFeatures,
Addresses: testAddrs,
AuthSigBytes: testSig.Serialize(),
LastUpdate: time.Unix(updateTime, 0),
Color: color.RGBA{1, 2, 3, 0},
Alias: "kek" + string(pub),
Features: testFeatures,
Addresses: testAddrs,
}
copy(n.PubKeyBytes[:], priv.PubKey().SerializeCompressed())

Expand Down
4 changes: 2 additions & 2 deletions discovery/chan_series.go
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ func (c *ChanSeries) FetchChanAnns(chain chainhash.Hash,
// If this edge has a validated node announcement, that
// we haven't yet sent, then we'll send that as well.
nodePub := channel.Node2.PubKeyBytes
hasNodeAnn := channel.Node2.HaveNodeAnnouncement
hasNodeAnn := channel.Node2.HaveAnnouncement()
if _, ok := nodePubsSent[nodePub]; !ok && hasNodeAnn {
nodeAnn, err := channel.Node2.NodeAnnouncement(
true,
Expand All @@ -321,7 +321,7 @@ func (c *ChanSeries) FetchChanAnns(chain chainhash.Hash,
// If this edge has a validated node announcement, that
// we haven't yet sent, then we'll send that as well.
nodePub := channel.Node1.PubKeyBytes
hasNodeAnn := channel.Node1.HaveNodeAnnouncement
hasNodeAnn := channel.Node1.HaveAnnouncement()
if _, ok := nodePubsSent[nodePub]; !ok && hasNodeAnn {
nodeAnn, err := channel.Node1.NodeAnnouncement(
true,
Expand Down
48 changes: 22 additions & 26 deletions graph/builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,13 +98,12 @@ func TestIgnoreNodeAnnouncement(t *testing.T) {

pub := priv1.PubKey()
node := &models.Node{
HaveNodeAnnouncement: true,
LastUpdate: time.Unix(123, 0),
Addresses: testAddrs,
Color: color.RGBA{1, 2, 3, 0},
Alias: "node11",
AuthSigBytes: testSig.Serialize(),
Features: testFeatures,
LastUpdate: time.Unix(123, 0),
Addresses: testAddrs,
Color: color.RGBA{1, 2, 3, 0},
Alias: "node11",
AuthSigBytes: testSig.Serialize(),
Features: testFeatures,
}
copy(node.PubKeyBytes[:], pub.SerializeCompressed())

Expand Down Expand Up @@ -1085,13 +1084,12 @@ func TestIsStaleNode(t *testing.T) {
// With the node stub in the database, we'll add the fully node
// announcement to the database.
n1 := &models.Node{
HaveNodeAnnouncement: true,
LastUpdate: updateTimeStamp,
Addresses: testAddrs,
Color: color.RGBA{1, 2, 3, 0},
Alias: "node11",
AuthSigBytes: testSig.Serialize(),
Features: testFeatures,
LastUpdate: updateTimeStamp,
Addresses: testAddrs,
Color: color.RGBA{1, 2, 3, 0},
Alias: "node11",
AuthSigBytes: testSig.Serialize(),
Features: testFeatures,
}
copy(n1.PubKeyBytes[:], priv1.PubKey().SerializeCompressed())
if err := ctx.builder.AddNode(t.Context(), n1); err != nil {
Expand Down Expand Up @@ -1402,12 +1400,11 @@ func parseTestGraph(t *testing.T, useCache bool, path string) (
}

dbNode := &models.Node{
HaveNodeAnnouncement: true,
AuthSigBytes: testSig.Serialize(),
LastUpdate: testTime,
Addresses: testAddrs,
Alias: node.Alias,
Features: testFeatures,
AuthSigBytes: testSig.Serialize(),
LastUpdate: testTime,
Addresses: testAddrs,
Alias: node.Alias,
Features: testFeatures,
}
copy(dbNode.PubKeyBytes[:], pubBytes)

Expand Down Expand Up @@ -1788,12 +1785,11 @@ func createTestGraphFromChannels(t *testing.T, useCache bool,
}

dbNode := &models.Node{
HaveNodeAnnouncement: true,
AuthSigBytes: testSig.Serialize(),
LastUpdate: testTime,
Addresses: testAddrs,
Alias: alias,
Features: features,
AuthSigBytes: testSig.Serialize(),
LastUpdate: testTime,
Addresses: testAddrs,
Alias: alias,
Features: features,
}

copy(dbNode.PubKeyBytes[:], pubKey.SerializeCompressed())
Expand Down
48 changes: 22 additions & 26 deletions graph/db/graph_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,12 @@ var (
func createNode(priv *btcec.PrivateKey) *models.Node {
pub := priv.PubKey().SerializeCompressed()
n := &models.Node{
HaveNodeAnnouncement: true,
AuthSigBytes: testSig.Serialize(),
LastUpdate: nextUpdateTime(),
Color: color.RGBA{1, 2, 3, 0},
Alias: "kek" + hex.EncodeToString(pub),
Features: testFeatures,
Addresses: testAddrs,
AuthSigBytes: testSig.Serialize(),
LastUpdate: nextUpdateTime(),
Color: color.RGBA{1, 2, 3, 0},
Alias: "kek" + hex.EncodeToString(pub),
Features: testFeatures,
Addresses: testAddrs,
}
copy(n.PubKeyBytes[:], priv.PubKey().SerializeCompressed())

Expand Down Expand Up @@ -107,15 +106,14 @@ func TestNodeInsertionAndDeletion(t *testing.T) {
nodeWithAddrs := func(addrs []net.Addr) *models.Node {
timeStamp++
return &models.Node{
HaveNodeAnnouncement: true,
AuthSigBytes: testSig.Serialize(),
LastUpdate: time.Unix(timeStamp, 0),
Color: color.RGBA{1, 2, 3, 0},
Alias: "kek",
Features: testFeatures,
Addresses: addrs,
ExtraOpaqueData: []byte{1, 1, 1, 2, 2, 2, 2},
PubKeyBytes: testPub,
AuthSigBytes: testSig.Serialize(),
LastUpdate: time.Unix(timeStamp, 0),
Color: color.RGBA{1, 2, 3, 0},
Alias: "kek",
Features: testFeatures,
Addresses: addrs,
ExtraOpaqueData: []byte{1, 1, 1, 2, 2, 2, 2},
PubKeyBytes: testPub,
}
}

Expand Down Expand Up @@ -316,10 +314,9 @@ func TestPartialNode(t *testing.T) {
// The two nodes should match exactly! (with default values for
// LastUpdate and db set to satisfy compareNodes())
expectedNode1 := &models.Node{
HaveNodeAnnouncement: false,
LastUpdate: time.Unix(0, 0),
PubKeyBytes: pubKey1,
Features: lnwire.EmptyFeatureVector(),
LastUpdate: time.Unix(0, 0),
PubKeyBytes: pubKey1,
Features: lnwire.EmptyFeatureVector(),
}
compareNodes(t, expectedNode1, dbNode1)

Expand All @@ -330,10 +327,9 @@ func TestPartialNode(t *testing.T) {
// The two nodes should match exactly! (with default values for
// LastUpdate and db set to satisfy compareNodes())
expectedNode2 := &models.Node{
HaveNodeAnnouncement: false,
LastUpdate: time.Unix(0, 0),
PubKeyBytes: pubKey2,
Features: lnwire.EmptyFeatureVector(),
LastUpdate: time.Unix(0, 0),
PubKeyBytes: pubKey2,
Features: lnwire.EmptyFeatureVector(),
}
compareNodes(t, expectedNode2, dbNode2)

Expand Down Expand Up @@ -3782,11 +3778,11 @@ func TestAddChannelEdgeShellNodes(t *testing.T) {
// a shell node present.
node1, err := graph.FetchNode(ctx, node1.PubKeyBytes)
require.NoError(t, err, "unable to fetch node1")
require.True(t, node1.HaveNodeAnnouncement)
require.True(t, node1.HaveAnnouncement())

node2, err = graph.FetchNode(ctx, node2.PubKeyBytes)
require.NoError(t, err, "unable to fetch node2")
require.False(t, node2.HaveNodeAnnouncement)
require.False(t, node2.HaveAnnouncement())

// Show that attempting to add the channel again will result in an
// error.
Expand Down
16 changes: 4 additions & 12 deletions graph/db/kv_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -1206,8 +1206,7 @@ func (c *KVStore) addChannelEdge(tx kvdb.RwTx,
switch {
case errors.Is(node1Err, ErrGraphNodeNotFound):
node1Shell := models.Node{
PubKeyBytes: edge.NodeKey1Bytes,
HaveNodeAnnouncement: false,
PubKeyBytes: edge.NodeKey1Bytes,
}
err := addLightningNode(tx, &node1Shell)
if err != nil {
Expand All @@ -1222,8 +1221,7 @@ func (c *KVStore) addChannelEdge(tx kvdb.RwTx,
switch {
case errors.Is(node2Err, ErrGraphNodeNotFound):
node2Shell := models.Node{
PubKeyBytes: edge.NodeKey2Bytes,
HaveNodeAnnouncement: false,
PubKeyBytes: edge.NodeKey2Bytes,
}
err := addLightningNode(tx, &node2Shell)
if err != nil {
Expand Down Expand Up @@ -4369,7 +4367,7 @@ func putLightningNode(nodeBucket, aliasBucket, updateIndex kvdb.RwBucket,

// If we got a node announcement for this node, we will have the rest
// of the data available. If not we don't have more data to write.
if !node.HaveNodeAnnouncement {
if !node.HaveAnnouncement() {
// Write HaveNodeAnnouncement=0.
byteOrder.PutUint16(scratch[:2], 0)
if _, err := b.Write(scratch[:2]); err != nil {
Expand Down Expand Up @@ -4562,15 +4560,9 @@ func deserializeLightningNode(r io.Reader) (models.Node, error) {
}

hasNodeAnn := byteOrder.Uint16(scratch[:2])
if hasNodeAnn == 1 {
node.HaveNodeAnnouncement = true
} else {
node.HaveNodeAnnouncement = false
}

// The rest of the data is optional, and will only be there if we got a
// node announcement for this node.
if !node.HaveNodeAnnouncement {
if hasNodeAnn == 0 {
return node, nil
}

Expand Down
59 changes: 30 additions & 29 deletions graph/db/models/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,6 @@ type Node struct {
PubKeyBytes [33]byte
pubKey *btcec.PublicKey

// HaveNodeAnnouncement indicates whether we received a node
// announcement for this particular node. If true, the remaining fields
// will be set, if false only the PubKey is known for this node.
HaveNodeAnnouncement bool

// LastUpdate is the last time the vertex information for this node has
// been updated.
LastUpdate time.Time
Expand Down Expand Up @@ -54,53 +49,60 @@ type Node struct {
ExtraOpaqueData []byte
}

// HaveAnnouncement returns true if we have received a node announcement for
// this node. We determine this by checking if we a signature for the
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice change 👍

nit: have

// announcement.
func (n *Node) HaveAnnouncement() bool {
return len(n.AuthSigBytes) > 0
}

// PubKey is the node's long-term identity public key. This key will be used to
// authenticated any advertisements/updates sent by the node.
//
// NOTE: By having this method to access an attribute, we ensure we only need
// to fully deserialize the pubkey if absolutely necessary.
func (l *Node) PubKey() (*btcec.PublicKey, error) {
if l.pubKey != nil {
return l.pubKey, nil
func (n *Node) PubKey() (*btcec.PublicKey, error) {
if n.pubKey != nil {
return n.pubKey, nil
}

key, err := btcec.ParsePubKey(l.PubKeyBytes[:])
key, err := btcec.ParsePubKey(n.PubKeyBytes[:])
if err != nil {
return nil, err
}
l.pubKey = key
n.pubKey = key

return key, nil
}

// NodeAnnouncement retrieves the latest node announcement of the node.
func (l *Node) NodeAnnouncement(signed bool) (*lnwire.NodeAnnouncement1,
func (n *Node) NodeAnnouncement(signed bool) (*lnwire.NodeAnnouncement1,
error) {

if !l.HaveNodeAnnouncement {
if !n.HaveAnnouncement() {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if we add && signed here, I think we don't need the workaround for the self announcement?

return nil, fmt.Errorf("node does not have node announcement")
}

alias, err := lnwire.NewNodeAlias(l.Alias)
alias, err := lnwire.NewNodeAlias(n.Alias)
if err != nil {
return nil, err
}

nodeAnn := &lnwire.NodeAnnouncement1{
Features: l.Features.RawFeatureVector,
NodeID: l.PubKeyBytes,
RGBColor: l.Color,
Features: n.Features.RawFeatureVector,
NodeID: n.PubKeyBytes,
RGBColor: n.Color,
Alias: alias,
Addresses: l.Addresses,
Timestamp: uint32(l.LastUpdate.Unix()),
ExtraOpaqueData: l.ExtraOpaqueData,
Addresses: n.Addresses,
Timestamp: uint32(n.LastUpdate.Unix()),
ExtraOpaqueData: n.ExtraOpaqueData,
}

if !signed {
return nodeAnn, nil
}

sig, err := lnwire.NewSigFromECDSARawSignature(l.AuthSigBytes)
sig, err := lnwire.NewSigFromECDSARawSignature(n.AuthSigBytes)
if err != nil {
return nil, err
}
Expand All @@ -117,14 +119,13 @@ func NodeFromWireAnnouncement(msg *lnwire.NodeAnnouncement1) *Node {
features := lnwire.NewFeatureVector(msg.Features, lnwire.Features)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: could pass msg.Features to NewV1Node directly


return &Node{
HaveNodeAnnouncement: true,
LastUpdate: timestamp,
Addresses: msg.Addresses,
PubKeyBytes: msg.NodeID,
Alias: msg.Alias.String(),
AuthSigBytes: msg.Signature.ToSignatureBytes(),
Features: features,
Color: msg.RGBColor,
ExtraOpaqueData: msg.ExtraOpaqueData,
LastUpdate: timestamp,
Addresses: msg.Addresses,
PubKeyBytes: msg.NodeID,
Alias: msg.Alias.String(),
AuthSigBytes: msg.Signature.ToSignatureBytes(),
Features: features,
Color: msg.RGBColor,
ExtraOpaqueData: msg.ExtraOpaqueData,
}
}
4 changes: 2 additions & 2 deletions graph/db/sql_migration.go
Original file line number Diff line number Diff line change
Expand Up @@ -1449,7 +1449,7 @@ func insertNodeSQLMig(ctx context.Context, db SQLQueries,
PubKey: node.PubKeyBytes[:],
}

if node.HaveNodeAnnouncement {
if node.HaveAnnouncement() {
params.LastUpdate = sqldb.SQLInt64(node.LastUpdate.Unix())
params.Color = sqldb.SQLStrValid(EncodeHexColor(node.Color))
params.Alias = sqldb.SQLStrValid(node.Alias)
Expand All @@ -1463,7 +1463,7 @@ func insertNodeSQLMig(ctx context.Context, db SQLQueries,
}

// We can exit here if we don't have the announcement yet.
if !node.HaveNodeAnnouncement {
if !node.HaveAnnouncement() {
return nodeID, nil
}

Expand Down
Loading