Skip to content

Commit

Permalink
Enode logging broke when NAT Parameter set in 2.43.0 (#7480)
Browse files Browse the repository at this point in the history
for #7472
  • Loading branch information
AskAlexSharov authored May 10, 2023
1 parent 10b9aa1 commit f23612b
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 15 deletions.
3 changes: 2 additions & 1 deletion cmd/downloader/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,11 +150,12 @@ func Downloader(ctx context.Context, logger log.Logger) error {
if err != nil {
return err
}
downloadernat.DoNat(natif, cfg)

cfg.ClientConfig.DisableIPv6 = disableIPV6
cfg.ClientConfig.DisableIPv4 = disableIPV4

downloadernat.DoNat(natif, cfg)

d, err := downloader.New(ctx, cfg)
if err != nil {
return err
Expand Down
10 changes: 5 additions & 5 deletions p2p/enode/localnode.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ const (
// current process. Setting ENR entries via the Set method updates the record. A new version
// of the record is signed on demand when the Node method is called.
type LocalNode struct {
cur atomic.Value // holds a non-nil node pointer while the record is up-to-date.
cur atomic.Pointer[Node] // holds a non-nil node pointer while the record is up-to-date.
id ID
key *ecdsa.PrivateKey
db *DB
Expand Down Expand Up @@ -87,15 +87,15 @@ func (ln *LocalNode) Database() *DB {

// Node returns the current version of the local node record.
func (ln *LocalNode) Node() *Node {
n := ln.cur.Load().(*Node)
n := ln.cur.Load()
if n != nil {
return n
}
// Record was invalidated, sign a new copy.
ln.mu.Lock()
defer ln.mu.Unlock()
ln.sign()
return ln.cur.Load().(*Node)
return ln.cur.Load()
}

// Seq returns the current sequence number of the local node record.
Expand Down Expand Up @@ -259,11 +259,11 @@ func predictAddr(t *netutil.IPTracker) (net.IP, int) {
}

func (ln *LocalNode) invalidate() {
ln.cur.Store((*Node)(nil))
ln.cur.Store(nil)
}

func (ln *LocalNode) sign() {
if n := ln.cur.Load().(*Node); n != nil {
if n := ln.cur.Load(); n != nil {
return // no changes
}

Expand Down
25 changes: 16 additions & 9 deletions p2p/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,13 +196,13 @@ type Server struct {
peerFeed event.Feed
log log.Logger

nodedb *enode.DB
localnode *enode.LocalNode
localnodeAddress string
ntab *discover.UDPv4
DiscV5 *discover.UDPv5
discmix *enode.FairMix
dialsched *dialScheduler
nodedb *enode.DB
localnode *enode.LocalNode
localnodeAddrCache atomic.Pointer[string]
ntab *discover.UDPv4
DiscV5 *discover.UDPv5
discmix *enode.FairMix
dialsched *dialScheduler

// Channels into the run loop.
quitCtx context.Context
Expand Down Expand Up @@ -530,6 +530,11 @@ func (srv *Server) Start(ctx context.Context) error {
return nil
}

func (srv *Server) updateLocalNodeStaticAddrCache() {
localNodeAddr := srv.localnode.Node().URLv4()
srv.localnodeAddrCache.Store(&localNodeAddr)

}
func (srv *Server) setupLocalNode() error {
// Create the devp2p handshake.
pubkey := crypto.MarshalPubkey(&srv.PrivateKey.PublicKey)
Expand All @@ -546,7 +551,7 @@ func (srv *Server) setupLocalNode() error {
srv.nodedb = db
srv.localnode = enode.NewLocalNode(db, srv.PrivateKey)
srv.localnode.SetFallbackIP(net.IP{127, 0, 0, 1})
srv.localnodeAddress = srv.localnode.Node().URLv4()
srv.updateLocalNodeStaticAddrCache()
// TODO: check conflicts
for _, p := range srv.Protocols {
for _, e := range p.Attributes {
Expand All @@ -560,6 +565,7 @@ func (srv *Server) setupLocalNode() error {
// ExtIP doesn't block, set the IP right away.
ip, _ := srv.NAT.ExternalIP()
srv.localnode.SetStaticIP(ip)
srv.updateLocalNodeStaticAddrCache()
default:
// Ask the router about the IP. This takes a while and blocks startup,
// do it in the background.
Expand All @@ -569,6 +575,7 @@ func (srv *Server) setupLocalNode() error {
defer srv.loopWG.Done()
if ip, err := srv.NAT.ExternalIP(); err == nil {
srv.localnode.SetStaticIP(ip)
srv.updateLocalNodeStaticAddrCache()
}
}()
}
Expand Down Expand Up @@ -747,7 +754,7 @@ func (srv *Server) doPeerOp(fn peerOpFunc) {
func (srv *Server) run() {
defer debug.LogPanic()
if len(srv.Config.Protocols) > 0 {
srv.log.Info("Started P2P networking", "version", srv.Config.Protocols[0].Version, "self", srv.localnodeAddress, "name", srv.Name)
srv.log.Info("Started P2P networking", "version", srv.Config.Protocols[0].Version, "self", *srv.localnodeAddrCache.Load(), "name", srv.Name)
}
defer srv.loopWG.Done()
defer srv.nodedb.Close()
Expand Down

0 comments on commit f23612b

Please sign in to comment.