From 836c2eddcfb0680cee3b9f764fc328ec23dc71ae Mon Sep 17 00:00:00 2001 From: Jan K Date: Fri, 10 May 2024 18:10:54 +0200 Subject: [PATCH] Fix peers filtering, increase the version (#126) --- gossip/handler.go | 18 +++++++++++++----- gossip/handler_test.go | 28 ++++++++++++++++++++++++++++ version/version.go | 15 +-------------- 3 files changed, 42 insertions(+), 19 deletions(-) create mode 100644 gossip/handler_test.go diff --git a/gossip/handler.go b/gossip/handler.go index 3f866eaaa..e106f64d3 100644 --- a/gossip/handler.go +++ b/gossip/handler.go @@ -4,6 +4,7 @@ import ( "errors" "fmt" "github.com/ethereum/go-ethereum/metrics" + "github.com/ethereum/go-ethereum/p2p/enode" "math" "math/rand" "strings" @@ -709,14 +710,21 @@ func (h *handler) highestPeerProgress() PeerProgress { return max } +// isUseless checks if the peer is banned from discovery and ban it if it should be +func isUseless(node *enode.Node, name string) bool { + useless := discfilter.Banned(node.ID(), node.Record()) + lowerName := strings.ToLower(name) + if !useless && !strings.Contains(lowerName, "opera") && !strings.Contains(lowerName, "sonic") { + useless = true + discfilter.Ban(node.ID()) + } + return useless +} + // handle is the callback invoked to manage the life cycle of a peer. When // this function terminates, the peer is disconnected. func (h *handler) handle(p *peer) error { - useless := discfilter.Banned(p.Node().ID(), p.Node().Record()) - if !useless && !strings.Contains(strings.ToLower(p.Name()), "opera") { - useless = true - discfilter.Ban(p.ID()) - } + useless := isUseless(p.Node(), p.Name()) if !p.Peer.Info().Network.Trusted && useless && h.peers.UselessNum() >= h.maxPeers/10 { // don't allow more than 10% of useless peers p.Log().Trace("Rejecting peer as useless") diff --git a/gossip/handler_test.go b/gossip/handler_test.go new file mode 100644 index 000000000..73e952085 --- /dev/null +++ b/gossip/handler_test.go @@ -0,0 +1,28 @@ +package gossip + +import ( + "github.com/ethereum/go-ethereum/p2p/discover/discfilter" + "github.com/ethereum/go-ethereum/p2p/enode" + "testing" +) + +func TestIsUseless(t *testing.T) { + validEnode := enode.MustParse("enode://3f4306c065eaa5d8079e17feb56c03a97577e67af3c9c17496bb8916f102f1ff603e87d2a4ebfa0a2f70b780b85db212618857ea4e9627b24a9b0dd2faeb826e@127.0.0.1:5050") + sonicName := "Sonic/v1.0.0-a-61af51c2-1715085138/linux-amd64/go1.21.7" + operaName := "go-opera/v1.1.2-rc.6-8e84c9dc-1688013329/linux-amd64/go1.19.11" + invalidName := "bot" + + discfilter.Enable() + if isUseless(validEnode, sonicName) { + t.Errorf("sonic peer reported as useless") + } + if isUseless(validEnode, operaName) { + t.Errorf("opera peer reported as useless") + } + if !isUseless(validEnode, invalidName) { + t.Errorf("invalid peer not reported as useless") + } + if !isUseless(validEnode, operaName) { + t.Errorf("peer not banned after marking as useless") + } +} diff --git a/version/version.go b/version/version.go index 722f7e2aa..ba4185a8a 100644 --- a/version/version.go +++ b/version/version.go @@ -2,25 +2,16 @@ package version import ( "fmt" - "math/big" - "github.com/ethereum/go-ethereum/params" ) func init() { params.VersionMajor = 1 // Major version component of the current release - params.VersionMinor = 0 // Minor version component of the current release + params.VersionMinor = 2 // Minor version component of the current release params.VersionPatch = 0 // Patch version component of the current release params.VersionMeta = "a" // Version metadata to append to the version string } -func BigToString(b *big.Int) string { - if len(b.Bytes()) > 8 { - return "_malformed_version_" - } - return U64ToString(b.Uint64()) -} - func AsString() string { return ToString(uint16(params.VersionMajor), uint16(params.VersionMinor), uint16(params.VersionPatch)) } @@ -29,10 +20,6 @@ func AsU64() uint64 { return ToU64(uint16(params.VersionMajor), uint16(params.VersionMinor), uint16(params.VersionPatch)) } -func AsBigInt() *big.Int { - return new(big.Int).SetUint64(AsU64()) -} - func ToU64(vMajor, vMinor, vPatch uint16) uint64 { return uint64(vMajor)*1e12 + uint64(vMinor)*1e6 + uint64(vPatch) }