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 4 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
2 changes: 1 addition & 1 deletion api/node/node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func initCommServer(t *testing.T) {
Limit: 10000,
LimitPerAccount: 16,
MaxLifetime: 10 * time.Minute,
}))
}), "2.0.0")
router := mux.NewRouter()
node.New(comm).Mount(router, "/node")
ts = httptest.NewServer(router)
Expand Down
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.

32 changes: 31 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,24 @@ 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+`)
darrenvechain marked this conversation as resolved.
Show resolved Hide resolved
if appVersion == "" || !versionRegex.MatchString(appVersion) {
// Got a bad app version, so accept any peer
return true
}
darrenvechain marked this conversation as resolved.
Show resolved Hide resolved

// 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)
})
}
}
Loading