Enode logging broke when NAT Parameter set in 2.43.0 (#7480)

for https://github.com/ledgerwatch/erigon/issues/7472
This commit is contained in:
Alex Sharov 2023-05-10 10:25:53 +07:00 committed by GitHub
parent 10b9aa1586
commit f23612bdfe
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 23 additions and 15 deletions

View File

@ -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

View File

@ -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
}

View File

@ -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()