Skip to content

Commit 836c2ed

Browse files
authored
Fix peers filtering, increase the version (#126)
1 parent ba87d24 commit 836c2ed

File tree

3 files changed

+42
-19
lines changed

3 files changed

+42
-19
lines changed

gossip/handler.go

+13-5
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"errors"
55
"fmt"
66
"github.com/ethereum/go-ethereum/metrics"
7+
"github.com/ethereum/go-ethereum/p2p/enode"
78
"math"
89
"math/rand"
910
"strings"
@@ -709,14 +710,21 @@ func (h *handler) highestPeerProgress() PeerProgress {
709710
return max
710711
}
711712

713+
// isUseless checks if the peer is banned from discovery and ban it if it should be
714+
func isUseless(node *enode.Node, name string) bool {
715+
useless := discfilter.Banned(node.ID(), node.Record())
716+
lowerName := strings.ToLower(name)
717+
if !useless && !strings.Contains(lowerName, "opera") && !strings.Contains(lowerName, "sonic") {
718+
useless = true
719+
discfilter.Ban(node.ID())
720+
}
721+
return useless
722+
}
723+
712724
// handle is the callback invoked to manage the life cycle of a peer. When
713725
// this function terminates, the peer is disconnected.
714726
func (h *handler) handle(p *peer) error {
715-
useless := discfilter.Banned(p.Node().ID(), p.Node().Record())
716-
if !useless && !strings.Contains(strings.ToLower(p.Name()), "opera") {
717-
useless = true
718-
discfilter.Ban(p.ID())
719-
}
727+
useless := isUseless(p.Node(), p.Name())
720728
if !p.Peer.Info().Network.Trusted && useless && h.peers.UselessNum() >= h.maxPeers/10 {
721729
// don't allow more than 10% of useless peers
722730
p.Log().Trace("Rejecting peer as useless")

gossip/handler_test.go

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package gossip
2+
3+
import (
4+
"github.com/ethereum/go-ethereum/p2p/discover/discfilter"
5+
"github.com/ethereum/go-ethereum/p2p/enode"
6+
"testing"
7+
)
8+
9+
func TestIsUseless(t *testing.T) {
10+
validEnode := enode.MustParse("enode://3f4306c065eaa5d8079e17feb56c03a97577e67af3c9c17496bb8916f102f1ff603e87d2a4ebfa0a2f70b780b85db212618857ea4e9627b24a9b0dd2faeb826e@127.0.0.1:5050")
11+
sonicName := "Sonic/v1.0.0-a-61af51c2-1715085138/linux-amd64/go1.21.7"
12+
operaName := "go-opera/v1.1.2-rc.6-8e84c9dc-1688013329/linux-amd64/go1.19.11"
13+
invalidName := "bot"
14+
15+
discfilter.Enable()
16+
if isUseless(validEnode, sonicName) {
17+
t.Errorf("sonic peer reported as useless")
18+
}
19+
if isUseless(validEnode, operaName) {
20+
t.Errorf("opera peer reported as useless")
21+
}
22+
if !isUseless(validEnode, invalidName) {
23+
t.Errorf("invalid peer not reported as useless")
24+
}
25+
if !isUseless(validEnode, operaName) {
26+
t.Errorf("peer not banned after marking as useless")
27+
}
28+
}

version/version.go

+1-14
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,16 @@ package version
22

33
import (
44
"fmt"
5-
"math/big"
6-
75
"github.com/ethereum/go-ethereum/params"
86
)
97

108
func init() {
119
params.VersionMajor = 1 // Major version component of the current release
12-
params.VersionMinor = 0 // Minor version component of the current release
10+
params.VersionMinor = 2 // Minor version component of the current release
1311
params.VersionPatch = 0 // Patch version component of the current release
1412
params.VersionMeta = "a" // Version metadata to append to the version string
1513
}
1614

17-
func BigToString(b *big.Int) string {
18-
if len(b.Bytes()) > 8 {
19-
return "_malformed_version_"
20-
}
21-
return U64ToString(b.Uint64())
22-
}
23-
2415
func AsString() string {
2516
return ToString(uint16(params.VersionMajor), uint16(params.VersionMinor), uint16(params.VersionPatch))
2617
}
@@ -29,10 +20,6 @@ func AsU64() uint64 {
2920
return ToU64(uint16(params.VersionMajor), uint16(params.VersionMinor), uint16(params.VersionPatch))
3021
}
3122

32-
func AsBigInt() *big.Int {
33-
return new(big.Int).SetUint64(AsU64())
34-
}
35-
3623
func ToU64(vMajor, vMinor, vPatch uint16) uint64 {
3724
return uint64(vMajor)*1e12 + uint64(vMinor)*1e6 + uint64(vPatch)
3825
}

0 commit comments

Comments
 (0)