Skip to content

Commit

Permalink
add version string parser and block versions older than 1.1.5
Browse files Browse the repository at this point in the history
  • Loading branch information
nibty committed Feb 1, 2024
1 parent a78ec25 commit a330e43
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 5 deletions.
10 changes: 7 additions & 3 deletions gossip/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package gossip
import (
"errors"
"fmt"
"github.com/Fantom-foundation/go-opera/version"
"math"
"math/rand"
"strings"
Expand Down Expand Up @@ -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 {
Expand Down
29 changes: 27 additions & 2 deletions version/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ package version

import (
"fmt"
"math/big"

"github.com/ethereum/go-ethereum/params"
"math/big"
"regexp"
"strconv"
"strings"
)

func init() {
Expand Down Expand Up @@ -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)
}
60 changes: 60 additions & 0 deletions version/version_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

0 comments on commit a330e43

Please sign in to comment.