Skip to content

Commit 1482382

Browse files
committed
lnd: use saved node ann data from previous run
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.
1 parent 98c52df commit 1482382

File tree

1 file changed

+54
-12
lines changed

1 file changed

+54
-12
lines changed

server.go

Lines changed: 54 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"crypto/rand"
77
"encoding/hex"
88
"fmt"
9+
"image/color"
910
"math/big"
1011
prand "math/rand"
1112
"net"
@@ -803,33 +804,74 @@ func newServer(cfg *Config, listenAddrs []net.Addr,
803804
// replicate it, and instead it'll only be stored locally.
804805
chanGraph := dbs.GraphDB.ChannelGraph()
805806

807+
// Fetch the source node from the channel graph. If a source node is
808+
// found, we'll use its values for our initial node announcement.
809+
persistedNode, err := chanGraph.SourceNode()
810+
if err != nil {
811+
srvrLog.Errorf("unable to fetch source node: %v", err)
812+
}
813+
806814
// We'll now reconstruct a node announcement based on our current
807815
// configuration so we can send it out as a sort of heart beat within
808816
// the network.
809817
//
810-
// We'll start by parsing the node color from configuration.
811-
color, err := lncfg.ParseHexColor(cfg.Color)
812-
if err != nil {
813-
srvrLog.Errorf("unable to parse color: %v\n", err)
814-
return nil, err
815-
}
816818

817-
// If no alias is provided, default to first 10 characters of public
818-
// key.
819-
alias := cfg.Alias
820-
if alias == "" {
821-
alias = hex.EncodeToString(serializedPubKey[:10])
819+
// If the node is restarted, we should use the previously persisted
820+
// values for the node's alias, color, addresses, and features.
821+
// Otherwise, we'll use the configured values.
822+
var (
823+
color color.RGBA
824+
alias = cfg.Alias
825+
features *lnwire.FeatureVector
826+
)
827+
828+
// If the node was previously persisted, we'll use the saved values.
829+
if persistedNode != nil {
830+
// If the color is still set to the default, we'll use the
831+
// persisted color.
832+
if cfg.Color == defaultColor {
833+
color = persistedNode.Color
834+
}
835+
836+
// If an alias was not specified in the configuration,
837+
// we'll use the saved alias.
838+
if alias == "" {
839+
alias = persistedNode.Alias
840+
}
841+
842+
features = persistedNode.Features
843+
844+
// Append the persisted addresses to the list of addresses our
845+
// node can be reached at.
846+
selfAddrs = append(selfAddrs, persistedNode.Addresses...)
847+
} else {
848+
color, err = lncfg.ParseHexColor(cfg.Color)
849+
if err != nil {
850+
srvrLog.Errorf("unable to parse color: %v\n", err)
851+
852+
return nil, err
853+
}
854+
855+
// If an alias was not specified, we'll generate one based on
856+
// the serialized public key.
857+
if alias == "" {
858+
alias = hex.EncodeToString(serializedPubKey[:10])
859+
}
860+
861+
features = s.featureMgr.Get(feature.SetNodeAnn)
822862
}
863+
823864
nodeAlias, err := lnwire.NewNodeAlias(alias)
824865
if err != nil {
825866
return nil, err
826867
}
868+
827869
selfNode := &channeldb.LightningNode{
828870
HaveNodeAnnouncement: true,
829871
LastUpdate: time.Now(),
830872
Addresses: selfAddrs,
831873
Alias: nodeAlias.String(),
832-
Features: s.featureMgr.Get(feature.SetNodeAnn),
874+
Features: features,
833875
Color: color,
834876
}
835877
copy(selfNode.PubKeyBytes[:], nodeKeyDesc.PubKey.SerializeCompressed())

0 commit comments

Comments
 (0)