Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: do not connect to v1 peers #767

Closed
Closed
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: 0 additions & 6 deletions comm/comm_test.go

This file was deleted.

33 changes: 32 additions & 1 deletion p2psrv/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (
"errors"
"math"
"net"
"regexp"
"strings"
"time"

"github.com/ethereum/go-ethereum/common/mclock"
Expand Down Expand Up @@ -86,7 +88,20 @@ func (s *Server) Start(protocols []*p2p.Protocol, topic discv5.Topic) error {
if peer.Inbound() {
dir = "inbound"
}
log := log.New("peer", peer, "dir", dir)

if isIncompatible(peer.Name()) {
log.Warn("incompatible peer version",
"name", peer.Name(),
"address", peer.RemoteAddr().String(),
"id", peer.ID().String())

peer.Disconnect(p2p.DiscIncompatibleVersion)
s.knownNodes.Remove(peer.ID())
s.dialingNodes.Remove(peer.ID())
return nil
}

log := log.New("peer", peer, "dir", dir, "name", peer.Name())

log.Debug("peer connected")
metricConnectedPeers().Add(1)
Expand Down Expand Up @@ -374,3 +389,19 @@ func (s *Server) fetchBootstrap() {
func (s *Server) Options() *Options {
return s.opts
}

var versionRegex = regexp.MustCompile(`v\d+\.\d+\.\d+`)

// isIncompatible checks if the peer is running an incompatible version.
func isIncompatible(peerName string) bool {
// Extract the semantic version from the name
peerVersion := versionRegex.FindString(peerName)
if peerVersion == "" {
return true
}

major := strings.Split(peerVersion, ".")[0]

// Only v1 nodes are incompatible for now
return major == "v1"
}
35 changes: 35 additions & 0 deletions p2psrv/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,38 @@ func TestNewServerConnectOnly(t *testing.T) {
assert.True(t, server.discoveredNodes.Contains(knownNode.ID))
assert.True(t, server.knownNodes.Contains(knownNode.ID))
}

func TestIsIncompatible(t *testing.T) {
testCases := []struct {
peerName string
expected bool
}{
{
peerName: "thor/v2.1.1-88c7c86-release/linux/go1.21.9",
expected: false,
},
{
peerName: "thor/v1.1.1-88c7c86-release/linux/go1.21.9",
expected: true,
},
{
peerName: "thor/v3.1.1-88c7c86-release/linux/go1.21.9",
expected: false,
},
{
peerName: "",
expected: true,
},
{
peerName: "thor/v1.bad.v-88c7c86-release/linux/go1.21.9",
expected: true,
},
}

for _, tc := range testCases {
t.Run(tc.peerName, func(t *testing.T) {
result := isIncompatible(tc.peerName)
assert.Equal(t, tc.expected, result)
})
}
}
Loading