Skip to content

Commit

Permalink
Fix peers filtering, increase the version (#126)
Browse files Browse the repository at this point in the history
  • Loading branch information
thaarok authored May 10, 2024
1 parent ba87d24 commit 836c2ed
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 19 deletions.
18 changes: 13 additions & 5 deletions gossip/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"errors"
"fmt"
"github.com/ethereum/go-ethereum/metrics"
"github.com/ethereum/go-ethereum/p2p/enode"
"math"
"math/rand"
"strings"
Expand Down Expand Up @@ -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")
Expand Down
28 changes: 28 additions & 0 deletions gossip/handler_test.go
Original file line number Diff line number Diff line change
@@ -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")
}
}
15 changes: 1 addition & 14 deletions version/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
Expand All @@ -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)
}
Expand Down

0 comments on commit 836c2ed

Please sign in to comment.