diff --git a/cmd/bootnode/main.go b/cmd/bootnode/main.go index 3fea735067..1b9fbb2732 100644 --- a/cmd/bootnode/main.go +++ b/cmd/bootnode/main.go @@ -129,7 +129,11 @@ func main() { listenerAddr = natAddr } } - + ethEntry, err := discover.GetEthEntry(*networkFilter) + if err != nil { + utils.Fatalf("-network: %v", err) + } + ln.Set(ethEntry) printNotice(&nodeKey.PublicKey, *listenerAddr) cfg := discover.Config{ PrivateKey: nodeKey, diff --git a/p2p/config.go b/p2p/config.go index c6f1bd5665..33be406172 100644 --- a/p2p/config.go +++ b/p2p/config.go @@ -76,6 +76,11 @@ type Config struct { // protocol. BootstrapNodesV5 []*enode.Node `toml:",omitempty"` + // EnableENRFilter enables the ENR filter for the discovery protocol. + // TODO(galaio): add a switch with a default value of false has been added to avoid compatibility issues. + // After the node version is upgraded for a while, it can be set to true by default. + EnableENRFilter bool `toml:",omitempty"` + // Static nodes are used as pre-configured connections which are always // maintained and re-connected on disconnects. StaticNodes []*enode.Node diff --git a/p2p/config_toml.go b/p2p/config_toml.go index 285ce53e45..d4acda59ee 100644 --- a/p2p/config_toml.go +++ b/p2p/config_toml.go @@ -28,6 +28,7 @@ func (c Config) MarshalTOML() (interface{}, error) { Name string `toml:"-"` BootstrapNodes []*enode.Node BootstrapNodesV5 []*enode.Node `toml:",omitempty"` + EnableENRFilter bool `toml:",omitempty"` StaticNodes []*enode.Node TrustedNodes []*enode.Node EVNNodeIdsWhitelist []enode.ID `toml:",omitempty"` @@ -56,6 +57,7 @@ func (c Config) MarshalTOML() (interface{}, error) { enc.Name = c.Name enc.BootstrapNodes = c.BootstrapNodes enc.BootstrapNodesV5 = c.BootstrapNodesV5 + enc.EnableENRFilter = c.EnableENRFilter enc.StaticNodes = c.StaticNodes enc.TrustedNodes = c.TrustedNodes enc.EVNNodeIdsWhitelist = c.EVNNodeIdsWhitelist @@ -88,6 +90,7 @@ func (c *Config) UnmarshalTOML(unmarshal func(interface{}) error) error { Name *string `toml:"-"` BootstrapNodes []*enode.Node BootstrapNodesV5 []*enode.Node `toml:",omitempty"` + EnableENRFilter *bool `toml:",omitempty"` StaticNodes []*enode.Node TrustedNodes []*enode.Node EVNNodeIdsWhitelist []enode.ID `toml:",omitempty"` @@ -141,6 +144,9 @@ func (c *Config) UnmarshalTOML(unmarshal func(interface{}) error) error { if dec.BootstrapNodesV5 != nil { c.BootstrapNodesV5 = dec.BootstrapNodesV5 } + if dec.EnableENRFilter != nil { + c.EnableENRFilter = *dec.EnableENRFilter + } if dec.StaticNodes != nil { c.StaticNodes = dec.StaticNodes } diff --git a/p2p/discover/common.go b/p2p/discover/common.go index 235f4a9750..25aa43832f 100644 --- a/p2p/discover/common.go +++ b/p2p/discover/common.go @@ -72,6 +72,22 @@ func ParseEthFilter(chain string) (NodeFilterFunc, error) { return f, nil } +func GetEthEntry(chain string) (enr.Entry, error) { + var eth struct { + ForkID forkid.ID + Tail []rlp.RawValue `rlp:"tail"` + } + switch chain { + case "bsc": + eth.ForkID = forkid.NewID(params.BSCChainConfig, core.DefaultBSCGenesisBlock().ToBlock(), uint64(0), uint64(0)) + case "chapel": + eth.ForkID = forkid.NewID(params.ChapelChainConfig, core.DefaultChapelGenesisBlock().ToBlock(), uint64(0), uint64(0)) + default: + return nil, fmt.Errorf("unknown network %q", chain) + } + return enr.WithEntry("eth", ð), nil +} + // Config holds settings for the discovery listener. type Config struct { // These settings are required and configure the UDP listener: diff --git a/p2p/discover/table.go b/p2p/discover/table.go index f5f79996c3..99276a8ecc 100644 --- a/p2p/discover/table.go +++ b/p2p/discover/table.go @@ -513,8 +513,9 @@ func (tab *Table) filterNode(n *enode.Node) bool { return false } if node, err := tab.net.RequestENR(n); err != nil { + // If the ENR request fails, we assume the node is not valid, and try to add it to the table next time. tab.log.Trace("ENR request failed", "id", n.ID(), "ipAddr", n.IPAddr(), "updPort", n.UDP(), "err", err) - return false + return true } else if !tab.enrFilter(node.Record()) { tab.log.Trace("ENR record filter out", "id", n.ID(), "ipAddr", n.IPAddr(), "updPort", n.UDP()) return true @@ -560,6 +561,7 @@ func (tab *Table) handleAddNode(req addNodeOp) bool { return false } + tab.log.Trace("ENR record filter passed", "id", req.node.ID(), "ipAddr", req.node.IPAddr(), "updPort", req.node.UDP()) tab.mutex.Lock() defer tab.mutex.Unlock() diff --git a/p2p/server.go b/p2p/server.go index 5533551824..b630746384 100644 --- a/p2p/server.go +++ b/p2p/server.go @@ -496,18 +496,21 @@ func (srv *Server) setupDiscovery() error { } // ENR filter function - f := func(r *enr.Record) bool { - if srv.forkFilter == nil { - return true - } - var eth struct { - ForkID forkid.ID - Tail []rlp.RawValue `rlp:"tail"` - } - if r.Load(enr.WithEntry("eth", ð)) != nil { - return true + var f discover.NodeFilterFunc + if srv.Config.EnableENRFilter { + f = func(r *enr.Record) bool { + if srv.forkFilter == nil { + return true + } + var eth struct { + ForkID forkid.ID + Tail []rlp.RawValue `rlp:"tail"` + } + if r.Load(enr.WithEntry("eth", ð)) != nil { + return false + } + return srv.forkFilter(eth.ForkID) == nil } - return srv.forkFilter(eth.ForkID) == nil } var (