Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion cmd/bootnode/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
5 changes: 5 additions & 0 deletions p2p/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions p2p/config_toml.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions p2p/discover/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -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", &eth), nil
}

// Config holds settings for the discovery listener.
type Config struct {
// These settings are required and configure the UDP listener:
Expand Down
4 changes: 3 additions & 1 deletion p2p/discover/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()

Expand Down
25 changes: 14 additions & 11 deletions p2p/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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", &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", &eth)) != nil {
return false
}
return srv.forkFilter(eth.ForkID) == nil
}
return srv.forkFilter(eth.ForkID) == nil
}

var (
Expand Down