From f23612bdfe888c9b11f66a70cda0c2b42b13935b Mon Sep 17 00:00:00 2001 From: Alex Sharov Date: Wed, 10 May 2023 10:25:53 +0700 Subject: [PATCH] Enode logging broke when NAT Parameter set in 2.43.0 (#7480) for https://github.com/ledgerwatch/erigon/issues/7472 --- cmd/downloader/main.go | 3 ++- p2p/enode/localnode.go | 10 +++++----- p2p/server.go | 25 ++++++++++++++++--------- 3 files changed, 23 insertions(+), 15 deletions(-) diff --git a/cmd/downloader/main.go b/cmd/downloader/main.go index f47d3bace48..23f1451cc72 100644 --- a/cmd/downloader/main.go +++ b/cmd/downloader/main.go @@ -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 diff --git a/p2p/enode/localnode.go b/p2p/enode/localnode.go index d5faf8b47fb..8d3df4439ce 100644 --- a/p2p/enode/localnode.go +++ b/p2p/enode/localnode.go @@ -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 @@ -87,7 +87,7 @@ 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 } @@ -95,7 +95,7 @@ func (ln *LocalNode) Node() *Node { 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. @@ -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 } diff --git a/p2p/server.go b/p2p/server.go index 3d3626fa30a..e03ada1b23b 100644 --- a/p2p/server.go +++ b/p2p/server.go @@ -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 @@ -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) @@ -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 { @@ -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. @@ -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() } }() } @@ -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()