Skip to content

Commit

Permalink
lnd: use saved node ann data from previous run
Browse files Browse the repository at this point in the history
This commit ensures that we start with the alias, node color,
addresses, and features as advertised in the node's previous
runtime. This approach maintains consistency in the node's
advertised information across restarts.
  • Loading branch information
Abdulkbk committed Jul 9, 2024
1 parent 98c52df commit 5acc286
Showing 1 changed file with 55 additions and 12 deletions.
67 changes: 55 additions & 12 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"crypto/rand"
"encoding/hex"
"fmt"
"image/color"
"math/big"
prand "math/rand"
"net"
Expand Down Expand Up @@ -803,33 +804,75 @@ func newServer(cfg *Config, listenAddrs []net.Addr,
// replicate it, and instead it'll only be stored locally.
chanGraph := dbs.GraphDB.ChannelGraph()

// Fetch the source node from the channel graph. If a source node is
// found, we'll use its values for our initial node announcement.
persistedNode, err := chanGraph.SourceNode()
if err != nil {
srvrLog.Errorf("unable to fetch source node: %v", err)
}

// We'll now reconstruct a node announcement based on our current
// configuration so we can send it out as a sort of heart beat within
// the network.
//
// We'll start by parsing the node color from configuration.
color, err := lncfg.ParseHexColor(cfg.Color)
if err != nil {
srvrLog.Errorf("unable to parse color: %v\n", err)
return nil, err
}

// If no alias is provided, default to first 10 characters of public
// key.
alias := cfg.Alias
if alias == "" {
alias = hex.EncodeToString(serializedPubKey[:10])
// If the node is restarted, we should use the previously persisted
// values for the node's alias, color, addresses, and features.
// Otherwise, we'll use the configured values.
var (
color color.RGBA
alias string = cfg.Alias

Check failure on line 824 in server.go

View workflow job for this annotation

GitHub Actions / lint code

var-declaration: should omit type string from declaration of var alias; it will be inferred from the right-hand side (revive)
features *lnwire.FeatureVector
)

// If the node was previously persisted, we'll use the saved values.
if persistedNode != nil {
// If the color is still set to the default, we'll use the
// persisted color.
if cfg.Color == defaultColor {
color = persistedNode.Color
}

// If an alias was not specified in the configuration,
// we'll use the saved alias.
if alias == "" {
alias = persistedNode.Alias
}

features = persistedNode.Features

// Append the persisted addresses to the list of addresses our
// node can be reached at.
selfAddrs = append(selfAddrs, persistedNode.Addresses...)

} else {
color, err = lncfg.ParseHexColor(cfg.Color)
if err != nil {
srvrLog.Errorf("unable to parse color: %v\n", err)

return nil, err
}

// If an alias was not specified, we'll generate one based on
// the serialized public key.
if alias == "" {
alias = hex.EncodeToString(serializedPubKey[:10])
}

features = s.featureMgr.Get(feature.SetNodeAnn)
}

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

selfNode := &channeldb.LightningNode{
HaveNodeAnnouncement: true,
LastUpdate: time.Now(),
Addresses: selfAddrs,
Alias: nodeAlias.String(),
Features: s.featureMgr.Get(feature.SetNodeAnn),
Features: features,
Color: color,
}
copy(selfNode.PubKeyBytes[:], nodeKeyDesc.PubKey.SerializeCompressed())
Expand Down

0 comments on commit 5acc286

Please sign in to comment.