Skip to content

Commit

Permalink
supress useless peers in discovery
Browse files Browse the repository at this point in the history
  • Loading branch information
uprendis committed Nov 17, 2022
1 parent d862fc5 commit 139e457
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 21 deletions.
44 changes: 44 additions & 0 deletions p2p/discover/discfilter/discfilter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package discfilter

import (
lru "github.com/hashicorp/golang-lru"

"github.com/ethereum/go-ethereum/p2p/enode"
"github.com/ethereum/go-ethereum/p2p/enr"
)

var (
enabled = false
dynamic, _ = lru.New(50000)
)

func Enable() {
enabled = true
}

func Ban(id enode.ID) {
if enabled {
dynamic.Add(id, struct{}{})
}
}

func BannedDynamic(id enode.ID) bool {
if !enabled {
return false
}
return dynamic.Contains(id)
}

func BannedStatic(rec *enr.Record) bool {
if !enabled {
return false
}
return rec.Has("eth") || rec.Has("eth2")
}

func Banned(id enode.ID, rec *enr.Record) bool {
if !enabled {
return false
}
return BannedStatic(rec) || BannedDynamic(id)
}
5 changes: 5 additions & 0 deletions p2p/discover/v4_udp.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (

"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/p2p/discover/discfilter"
"github.com/ethereum/go-ethereum/p2p/discover/v4wire"
"github.com/ethereum/go-ethereum/p2p/enode"
"github.com/ethereum/go-ethereum/p2p/netutil"
Expand Down Expand Up @@ -739,6 +740,10 @@ func (t *UDPv4) handleFindnode(h *packetHandlerV4, from *net.UDPAddr, fromID eno
if len(t.privateNodes) > 0 && containsEnode(t.privateNodes, &n.Node) {
continue
}
// Don't advertise the bots
if discfilter.Banned(n.ID(), n.Record()) {
continue
}
if netutil.CheckRelayIP(from.IP, n.IP()) == nil {
p.Nodes = append(p.Nodes, nodeToRPC(n))
}
Expand Down
5 changes: 5 additions & 0 deletions p2p/discover/v5_udp.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (

"github.com/ethereum/go-ethereum/common/mclock"
"github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/p2p/discover/discfilter"
"github.com/ethereum/go-ethereum/p2p/discover/v5wire"
"github.com/ethereum/go-ethereum/p2p/enode"
"github.com/ethereum/go-ethereum/p2p/enr"
Expand Down Expand Up @@ -824,6 +825,10 @@ func (t *UDPv5) collectTableNodes(rip net.IP, distances []uint, limit int) []*en
if len(t.privateNodes) > 0 && containsEnode(t.privateNodes, n) {
continue
}
// Don't advertise the bots
if discfilter.Banned(n.ID(), n.Record()) {
continue
}
// TODO livenessChecks > 1
if netutil.CheckRelayIP(rip, n.IP()) != nil {
continue
Expand Down
23 changes: 2 additions & 21 deletions p2p/enode/idscheme.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ package enode
import (
"crypto/ecdsa"
"fmt"
"github.com/ethereum/go-ethereum/core/forkid"
"io"

"golang.org/x/crypto/sha3"

"github.com/ethereum/go-ethereum/common/math"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/p2p/enr"
"github.com/ethereum/go-ethereum/rlp"
"golang.org/x/crypto/sha3"
)

// List of known secure identity schemes.
Expand Down Expand Up @@ -63,12 +63,6 @@ func SignV4(r *enr.Record, privkey *ecdsa.PrivateKey) error {
}

func (V4ID) Verify(r *enr.Record, sig []byte) error {
var opera operaNodeEnrEntry
err := r.Load(&opera)
if err != nil {
return fmt.Errorf("invalid opera node; %s", err.Error())
}

var entry s256raw
if err := r.Load(&entry); err != nil {
return err
Expand Down Expand Up @@ -165,16 +159,3 @@ func SignNull(r *enr.Record, id ID) *Node {
}
return &Node{r: *r, id: id}
}

// operaNodeEnrEntry is the ENR entry which advertises `eth` protocol on the discovery.
type operaNodeEnrEntry struct {
ForkID forkid.ID // Fork identifier per EIP-2124

// Ignore additional fields (for forward compatibility).
Rest []rlp.RawValue `rlp:"tail"`
}

// ENRKey implements enr.Entry.
func (e operaNodeEnrEntry) ENRKey() string {
return "opera"
}
13 changes: 13 additions & 0 deletions p2p/enr/enr.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,19 @@ func (r *Record) Load(e Entry) error {
return &KeyError{Key: e.ENRKey(), Err: errNotFound}
}

func (r *Record) Has(key string) bool {
i := sort.Search(len(r.pairs), func(i int) bool { return r.pairs[i].k >= key })
return i < len(r.pairs) && r.pairs[i].k == key
}

func (r *Record) String() string {
str := ""
for _, p := range r.pairs {
str += p.k + " "
}
return str
}

// Set adds or updates the given entry in the record. It panics if the value can't be
// encoded. If the record is signed, Set increments the sequence number and invalidates
// the sequence number.
Expand Down
6 changes: 6 additions & 0 deletions p2p/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"encoding/hex"
"errors"
"fmt"
"math/rand"
"net"
"sort"
"sync"
Expand All @@ -35,6 +36,7 @@ import (
"github.com/ethereum/go-ethereum/event"
"github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/p2p/discover"
"github.com/ethereum/go-ethereum/p2p/discover/discfilter"
"github.com/ethereum/go-ethereum/p2p/enode"
"github.com/ethereum/go-ethereum/p2p/enr"
"github.com/ethereum/go-ethereum/p2p/nat"
Expand Down Expand Up @@ -843,6 +845,10 @@ func (srv *Server) postHandshakeChecks(peers map[enode.ID]*Peer, inboundCount in
case c.node.ID() == srv.localnode.ID():
return DiscSelf
default:
if !c.is(trustedConn) && discfilter.Banned(c.node.ID(), c.node.Record()) && rand.Intn(5) != 0 {
// rarely accept useless peers
return DiscTooManyPeers
}
return nil
}
}
Expand Down

0 comments on commit 139e457

Please sign in to comment.