Skip to content

Commit

Permalink
feat: do not connect to peers that have a different major version
Browse files Browse the repository at this point in the history
  • Loading branch information
darrenvechain committed Jun 4, 2024
1 parent 43d1fde commit 1e4dc3f
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 8 deletions.
2 changes: 1 addition & 1 deletion cmd/thor/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -461,7 +461,7 @@ func newP2PCommunicator(ctx *cli.Context, repo *chain.Repository, txPool *txpool
}

return p2p.New(
comm.New(repo, txPool),
comm.New(repo, txPool, version),
key,
instanceDir,
userNAT,
Expand Down
6 changes: 0 additions & 6 deletions comm/comm_test.go

This file was deleted.

33 changes: 32 additions & 1 deletion comm/communicator.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ import (
"context"
"fmt"
"math"
"regexp"
"sort"
"strings"
"sync"
"time"

Expand Down Expand Up @@ -41,10 +43,11 @@ type Communicator struct {
feedScope event.SubscriptionScope
goes co.Goes
onceSynced sync.Once
version string
}

// New create a new Communicator instance.
func New(repo *chain.Repository, txPool *txpool.TxPool) *Communicator {
func New(repo *chain.Repository, txPool *txpool.TxPool, version string) *Communicator {
ctx, cancel := context.WithCancel(context.Background())
return &Communicator{
repo: repo,
Expand All @@ -54,6 +57,7 @@ func New(repo *chain.Repository, txPool *txpool.TxPool) *Communicator {
peerSet: newPeerSet(),
syncedCh: make(chan struct{}),
announcementCh: make(chan *announcement),
version: version,
}
}

Expand Down Expand Up @@ -161,6 +165,11 @@ type txsToSync struct {
}

func (c *Communicator) servePeer(p *p2p.Peer, rw p2p.MsgReadWriter) error {
if !sameMajor(c.version, p.Name()) {
log.Warn("peer version mismatch", "name", p.Name(), "address", p.RemoteAddr().String(), "id", p.ID().String())
return nil
}

peer := newPeer(p, rw)
c.goes.Go(func() {
c.runPeer(peer)
Expand Down Expand Up @@ -283,3 +292,25 @@ func (c *Communicator) PeersStats() []*PeerStats {
})
return stats
}

// SameMajor returns true if the peer has the same major version
func sameMajor(appVersion, peerName string) bool {
versionRegex := regexp.MustCompile(`\d+\.\d+\.\d+`)
// Got a bad app version, so accept any peer
if appVersion == "" || !versionRegex.MatchString(appVersion) {
log.Info("bad app version", "appVersion", appVersion)
return true
}

// Extract the semantic version from the name
peerVersion := versionRegex.FindString(peerName)
if peerVersion == "" {
return false
}

peerMajorVersion := strings.Split(peerVersion, ".")[0]
givenMajorVersion := strings.Split(appVersion, ".")[0]

// Compare the major versions
return peerMajorVersion == givenMajorVersion
}
59 changes: 59 additions & 0 deletions comm/communicator_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// Copyright (c) 2024 The VeChainThor developers

// Distributed under the GNU Lesser General Public License v3.0 software license, see the accompanying
// file LICENSE or <https://www.gnu.org/licenses/lgpl-3.0.html>
package comm

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestSameMajor(t *testing.T) {
testCases := []struct {
id string
appVersion string
peerName string
expected bool
}{
{
id: "only-major-same",
appVersion: "2.0.0",
peerName: "thor/v2.1.1-88c7c86-release/linux/go1.21.9",
expected: true,
},
{
id: "only-major-different",
appVersion: "2.1.1",
peerName: "thor/v1.1.1-88c7c86-release/linux/go1.21.9",
expected: false,
},
{
id: "app-version-empty",
appVersion: "",
peerName: "thor/v1.1.1-88c7c86-release/linux/go1.21.9",
expected: true,
},
{
id: "exact-match",
appVersion: "1.1.1",
peerName: "thor/v1.1.1-88c7c86-release/linux/go1.21.9",
expected: true,
},
{
id: "bad-app-version",
// bad app version, so accept any peer
appVersion: "bad.bad.bad",
peerName: "thor/v1.1.1-88c7c86-release/linux/go1.21.9",
expected: true,
},
}

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

0 comments on commit 1e4dc3f

Please sign in to comment.