Skip to content

Commit f5d46a6

Browse files
authored
Merge pull request #512 from shutter-network/peer-connectivity-randomisation
Peer connectivity randomisation
2 parents 72c56da + b54a70d commit f5d46a6

File tree

2 files changed

+45
-3
lines changed

2 files changed

+45
-3
lines changed

rolling-shutter/p2p/dht.go

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@ package p2p
22

33
import (
44
"context"
5+
"math/rand"
56
"time"
67

78
dht "github.com/libp2p/go-libp2p-kad-dht"
89
pubsub "github.com/libp2p/go-libp2p-pubsub"
910
"github.com/libp2p/go-libp2p/core/discovery"
1011
"github.com/libp2p/go-libp2p/core/host"
1112
"github.com/libp2p/go-libp2p/core/network"
13+
"github.com/libp2p/go-libp2p/core/peer"
1214
"github.com/libp2p/go-libp2p/core/protocol"
1315
"github.com/libp2p/go-libp2p/p2p/discovery/util"
1416
"github.com/rs/zerolog/log"
@@ -58,6 +60,8 @@ func dhtRoutingOptions(config *p2pNodeConfig) []dht.Option {
5860

5961
if config.IsBootstrapNode {
6062
opts = append(opts, dht.Mode(dht.ModeServer))
63+
} else {
64+
opts = append(opts, dht.Mode(dht.ModeAutoServer))
6165
}
6266

6367
return opts
@@ -88,17 +92,21 @@ func findPeers(ctx context.Context, h host.Host, d discovery.Discoverer, ns stri
8892
newConnections := 0
8993
failedDials := 0
9094
ourId := h.ID().String()
91-
for _, p := range peers {
95+
96+
randomizedPeers := randomizePeers(peers)
97+
98+
for _, p := range randomizedPeers {
9299
collectPeerAddresses(p)
93100
if p.ID == h.ID() {
94101
continue
95102
}
96-
metricsP2PPeerConnectedness.WithLabelValues(ourId, p.ID.String()).Set(float64(h.Network().Connectedness(p.ID)))
103+
connectedness := h.Network().Connectedness(p.ID)
104+
metricsP2PPeerConnectedness.WithLabelValues(ourId, p.ID.String()).Set(float64(connectedness))
97105
peerPing := h.Peerstore().LatencyEWMA(p.ID)
98106
if peerPing != 0 {
99107
metricsP2PPeerPing.WithLabelValues(ourId, p.ID.String()).Set(peerPing.Seconds())
100108
}
101-
if h.Network().Connectedness(p.ID) != network.Connected {
109+
if connectedness != network.Connected {
102110
_, err = h.Network().DialPeer(ctx, p.ID)
103111
if err != nil {
104112
log.Debug().
@@ -120,3 +128,12 @@ func findPeers(ctx context.Context, h host.Host, d discovery.Discoverer, ns stri
120128
}
121129
}
122130
}
131+
132+
func randomizePeers(peers []peer.AddrInfo) []peer.AddrInfo {
133+
randomIndexes := rand.Perm(len(peers))
134+
randomized := make([]peer.AddrInfo, len(peers))
135+
for _, index := range randomIndexes {
136+
randomized = append(randomized, peers[index])
137+
}
138+
return randomized
139+
}

rolling-shutter/p2p/dht_test.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package p2p
2+
3+
import (
4+
"fmt"
5+
"reflect"
6+
"testing"
7+
8+
"github.com/libp2p/go-libp2p/core/peer"
9+
"gotest.tools/v3/assert"
10+
)
11+
12+
func TestRandomisePeers(t *testing.T) {
13+
peerAddr := make([]peer.AddrInfo, 0)
14+
15+
for i := 0; i < 5; i++ {
16+
peerAddr = append(peerAddr, peer.AddrInfo{
17+
ID: peer.ID(fmt.Sprintf("%d", i)),
18+
})
19+
}
20+
21+
randomisedPeers := randomizePeers(peerAddr)
22+
23+
equal := reflect.DeepEqual(peerAddr, randomisedPeers)
24+
assert.Assert(t, !equal, "randomised unsuccessful")
25+
}

0 commit comments

Comments
 (0)