diff --git a/gossip/handler.go b/gossip/handler.go index 0d9fbe052..0a7d49485 100644 --- a/gossip/handler.go +++ b/gossip/handler.go @@ -3,6 +3,7 @@ package gossip import ( "errors" "fmt" + "github.com/Fantom-foundation/go-opera/version" "math" "math/rand" "strings" @@ -787,9 +788,12 @@ func (h *handler) handle(p *peer) error { if !useless && (!eligibleForSnap(p.Peer) || !strings.Contains(strings.ToLower(p.Name()), "x1")) { useless = true discfilter.Ban(p.ID()) - } else if strings.Contains(strings.ToLower(p.Name()), "1.1.3") { - log.Warn("ignore v1.1.3 peer", "peer", p.ID(), "name", p.Name()) - return p2p.DiscTooManyPeers + } else { + ver := version.ParseVersionStringIntoU64(p.Peer.Name()) + if ver < 1000001000005 { // version 1.1.5 + log.Warn("Ignoring old peer. Sorry buddy.", "peer", p.ID(), "name", p.Name()) + return p2p.DiscTooManyPeers + } } if !p.Peer.Info().Network.Trusted && useless { if h.peers.UselessNum() >= h.maxPeers/10 { diff --git a/version/version.go b/version/version.go index acd414f33..1e39cdeac 100644 --- a/version/version.go +++ b/version/version.go @@ -2,9 +2,11 @@ package version import ( "fmt" - "math/big" - "github.com/ethereum/go-ethereum/params" + "math/big" + "regexp" + "strconv" + "strings" ) func init() { @@ -44,3 +46,26 @@ func ToString(major, minor, patch uint16) string { func U64ToString(v uint64) string { return ToString(uint16((v/1e12)%1e6), uint16((v/1e6)%1e6), uint16(v%1e6)) } + +func ParseVersionString(clientName string) (major, minor, patch uint16) { + re := regexp.MustCompile(`v([0-9]+)\.([0-9]+)\.([0-9]+)`) + groups := re.FindStringSubmatch(strings.ToLower(clientName)) + + if len(groups) < 4 { + return 0, 0, 0 + } + + value, _ := strconv.ParseUint(groups[1], 10, 16) + major = uint16(value) + value, _ = strconv.ParseUint(groups[2], 10, 16) + minor = uint16(value) + value, _ = strconv.ParseUint(groups[3], 10, 16) + patch = uint16(value) + + return major, minor, patch +} + +func ParseVersionStringIntoU64(version string) uint64 { + major, miner, patch := ParseVersionString(version) + return ToU64(major, miner, patch) +} diff --git a/version/version_test.go b/version/version_test.go index b1dd823d7..277ea7d6e 100644 --- a/version/version_test.go +++ b/version/version_test.go @@ -39,3 +39,63 @@ func TestAsBigInt(t *testing.T) { prev = next } } + +func TestParseVersion_WithValidVersion(t *testing.T) { + major, minor, patch := ParseVersionString("go-x1/v1.2.3-asdasadas") + require.Equal(t, uint16(1), major) + require.Equal(t, uint16(2), minor) + require.Equal(t, uint16(3), patch) +} + +func TestParseVersion_WithInvalidVersion(t *testing.T) { + major, minor, patch := ParseVersionString("go-x1/v1.2.asdasadas") + require.Equal(t, uint16(0), major) + require.Equal(t, uint16(0), minor) + require.Equal(t, uint16(0), patch) +} + +func TestParseVersion_WithNoVersion(t *testing.T) { + major, minor, patch := ParseVersionString("go-x1/asdasadas") + require.Equal(t, uint16(0), major) + require.Equal(t, uint16(0), minor) + require.Equal(t, uint16(0), patch) +} + +func TestParseVersion_WithMaxUint16Version(t *testing.T) { + major, minor, patch := ParseVersionString("go-x1/v65535.65535.65535-asdasadas") + require.Equal(t, uint16(65535), major) + require.Equal(t, uint16(65535), minor) + require.Equal(t, uint16(65535), patch) +} + +func TestParseVersion_WithAnotherInvalidVersion(t *testing.T) { + major, minor, patch := ParseVersionString("go-x1/v-1.2.0.asdasadas") + require.Equal(t, uint16(0), major) + require.Equal(t, uint16(0), minor) + require.Equal(t, uint16(0), patch) +} + +func TestParseVersionStringIntoU64_WithValidVersion(t *testing.T) { + result := ParseVersionStringIntoU64("go-x1/v1.2.3-asdasadas") + require.Equal(t, uint64(1000002000003), result) +} + +func TestParseVersionStringIntoU64_WithInvalidVersion(t *testing.T) { + result := ParseVersionStringIntoU64("go-x1/v1.2.asdasadas") + require.Equal(t, uint64(0), result) +} + +func TestParseVersionStringIntoU64_WithNoVersion(t *testing.T) { + result := ParseVersionStringIntoU64("go-x1/asdasadas") + require.Equal(t, uint64(0), result) +} + +func TestParseVersionStringIntoU64_WithMaxUint16Version(t *testing.T) { + result := ParseVersionStringIntoU64("go-x1/v65535.65535.65535-asdasadas") + require.Equal(t, uint64(65535065535065535), result) +} + +func TestParseVersionStringIntoU64_WithMaxUint16Version2(t *testing.T) { + result := ParseVersionStringIntoU64("go-x1/v1.1.3-asdasadas") + require.Equal(t, uint64(1000001000003), result) +}