Skip to content

Commit

Permalink
chore: update rand usage
Browse files Browse the repository at this point in the history
  • Loading branch information
galargh authored and Stebalien committed Aug 6, 2024
1 parent 435b99e commit 8f56e8c
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 24 deletions.
1 change: 0 additions & 1 deletion backoff.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ func newBackoff(ctx context.Context, sizeThreshold int, cleanupInterval time.Dur
info: make(map[peer.ID]*backoffHistory),
}

rand.Seed(time.Now().UnixNano()) // used for jitter
go b.cleanupLoop(ctx)

return b
Expand Down
11 changes: 6 additions & 5 deletions floodsub_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ package pubsub
import (
"bytes"
"context"
crand "crypto/rand"
"crypto/sha256"
"encoding/base64"
"fmt"
"io"
"math/rand"
mrand "math/rand"
"sort"
"sync"
"testing"
Expand All @@ -25,7 +26,7 @@ import (

func checkMessageRouting(t *testing.T, topic string, pubs []*PubSub, subs []*Subscription) {
data := make([]byte, 16)
rand.Read(data)
crand.Read(data)

for _, p := range pubs {
err := p.Publish(topic, data)
Expand Down Expand Up @@ -58,7 +59,7 @@ func denseConnect(t *testing.T, hosts []host.Host) {
func connectSome(t *testing.T, hosts []host.Host, d int) {
for i, a := range hosts {
for j := 0; j < d; j++ {
n := rand.Intn(len(hosts))
n := mrand.Intn(len(hosts))
if n == i {
j--
continue
Expand Down Expand Up @@ -157,7 +158,7 @@ func TestBasicFloodsub(t *testing.T) {
for i := 0; i < 100; i++ {
msg := []byte(fmt.Sprintf("%d the flooooooood %d", i, i))

owner := rand.Intn(len(psubs))
owner := mrand.Intn(len(psubs))

psubs[owner].Publish("foobar", msg)

Expand Down Expand Up @@ -1006,7 +1007,7 @@ func TestConfigurableMaxMessageSize(t *testing.T) {

// 2mb payload.
msg := make([]byte, 1<<21)
rand.Read(msg)
crand.Read(msg)
err := psubs[0].Publish(topic, msg)
if err != nil {
t.Fatal(err)
Expand Down
2 changes: 1 addition & 1 deletion gossipsub_spam_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package pubsub

import (
"context"
"math/rand"
"crypto/rand"
"strconv"
"sync"
"testing"
Expand Down
35 changes: 18 additions & 17 deletions gossipsub_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ package pubsub
import (
"bytes"
"context"
crand "crypto/rand"
"fmt"
"io"
"math/rand"
mrand "math/rand"
"sync"
"sync/atomic"
"testing"
Expand Down Expand Up @@ -63,7 +64,7 @@ func TestSparseGossipsub(t *testing.T) {
for i := 0; i < 100; i++ {
msg := []byte(fmt.Sprintf("%d it's not a floooooood %d", i, i))

owner := rand.Intn(len(psubs))
owner := mrand.Intn(len(psubs))

psubs[owner].Publish("foobar", msg)

Expand Down Expand Up @@ -104,7 +105,7 @@ func TestDenseGossipsub(t *testing.T) {
for i := 0; i < 100; i++ {
msg := []byte(fmt.Sprintf("%d it's not a floooooood %d", i, i))

owner := rand.Intn(len(psubs))
owner := mrand.Intn(len(psubs))

psubs[owner].Publish("foobar", msg)

Expand Down Expand Up @@ -358,7 +359,7 @@ func TestGossipsubGossip(t *testing.T) {
for i := 0; i < 100; i++ {
msg := []byte(fmt.Sprintf("%d it's not a floooooood %d", i, i))

owner := rand.Intn(len(psubs))
owner := mrand.Intn(len(psubs))

psubs[owner].Publish("foobar", msg)

Expand Down Expand Up @@ -416,7 +417,7 @@ func TestGossipsubGossipPiggyback(t *testing.T) {
for i := 0; i < 100; i++ {
msg := []byte(fmt.Sprintf("%d it's not a floooooood %d", i, i))

owner := rand.Intn(len(psubs))
owner := mrand.Intn(len(psubs))

psubs[owner].Publish("foobar", msg)
psubs[owner].Publish("bazcrux", msg)
Expand Down Expand Up @@ -563,7 +564,7 @@ func TestGossipsubPrune(t *testing.T) {
for i := 0; i < 10; i++ {
msg := []byte(fmt.Sprintf("%d it's not a floooooood %d", i, i))

owner := rand.Intn(len(psubs))
owner := mrand.Intn(len(psubs))

psubs[owner].Publish("foobar", msg)

Expand Down Expand Up @@ -661,7 +662,7 @@ func TestGossipsubPruneBackoffTime(t *testing.T) {
msg := []byte(fmt.Sprintf("%d it's not a floooooood %d", i, i))

// Don't publish from host 0, since everyone should have pruned it.
owner := rand.Intn(len(psubs)-1) + 1
owner := mrand.Intn(len(psubs)-1) + 1

psubs[owner].Publish("foobar", msg)

Expand Down Expand Up @@ -706,7 +707,7 @@ func TestGossipsubGraft(t *testing.T) {
for i := 0; i < 100; i++ {
msg := []byte(fmt.Sprintf("%d it's not a floooooood %d", i, i))

owner := rand.Intn(len(psubs))
owner := mrand.Intn(len(psubs))

psubs[owner].Publish("foobar", msg)

Expand Down Expand Up @@ -755,7 +756,7 @@ func TestGossipsubRemovePeer(t *testing.T) {
for i := 0; i < 10; i++ {
msg := []byte(fmt.Sprintf("%d it's not a floooooood %d", i, i))

owner := 5 + rand.Intn(len(psubs)-5)
owner := 5 + mrand.Intn(len(psubs)-5)

psubs[owner].Publish("foobar", msg)

Expand Down Expand Up @@ -803,7 +804,7 @@ func TestGossipsubGraftPruneRetry(t *testing.T) {
for i, topic := range topics {
msg := []byte(fmt.Sprintf("%d it's not a floooooood %d", i, i))

owner := rand.Intn(len(psubs))
owner := mrand.Intn(len(psubs))

psubs[owner].Publish(topic, msg)

Expand Down Expand Up @@ -849,7 +850,7 @@ func TestGossipsubControlPiggyback(t *testing.T) {
// create a background flood of messages that overloads the queues
done := make(chan struct{})
go func() {
owner := rand.Intn(len(psubs))
owner := mrand.Intn(len(psubs))
for i := 0; i < 10000; i++ {
msg := []byte("background flooooood")
psubs[owner].Publish("flood", msg)
Expand Down Expand Up @@ -887,7 +888,7 @@ func TestGossipsubControlPiggyback(t *testing.T) {
for i, topic := range topics {
msg := []byte(fmt.Sprintf("%d it's not a floooooood %d", i, i))

owner := rand.Intn(len(psubs))
owner := mrand.Intn(len(psubs))

psubs[owner].Publish(topic, msg)

Expand Down Expand Up @@ -930,7 +931,7 @@ func TestMixedGossipsub(t *testing.T) {
for i := 0; i < 100; i++ {
msg := []byte(fmt.Sprintf("%d it's not a floooooood %d", i, i))

owner := rand.Intn(len(psubs))
owner := mrand.Intn(len(psubs))

psubs[owner].Publish("foobar", msg)

Expand Down Expand Up @@ -2217,7 +2218,7 @@ func TestGossipsubRPCFragmentation(t *testing.T) {
msgSize := 20000
for i := 0; i < nMessages; i++ {
msg := make([]byte, msgSize)
rand.Read(msg)
crand.Read(msg)
ps.Publish("test", msg)
time.Sleep(20 * time.Millisecond)
}
Expand Down Expand Up @@ -2357,7 +2358,7 @@ func TestFragmentRPCFunction(t *testing.T) {
mkMsg := func(size int) *pb.Message {
msg := &pb.Message{}
msg.Data = make([]byte, size-4) // subtract the protobuf overhead, so msg.Size() returns requested size
rand.Read(msg.Data)
crand.Read(msg.Data)
return msg
}

Expand Down Expand Up @@ -2471,7 +2472,7 @@ func TestFragmentRPCFunction(t *testing.T) {
messageIds := make([]string, msgsPerTopic)
for m := 0; m < msgsPerTopic; m++ {
mid := make([]byte, messageIdSize)
rand.Read(mid)
crand.Read(mid)
messageIds[m] = string(mid)
}
rpc.Control.Ihave[i] = &pb.ControlIHave{MessageIDs: messageIds}
Expand All @@ -2492,7 +2493,7 @@ func TestFragmentRPCFunction(t *testing.T) {
// It should not be present in the fragmented messages, but smaller IDs should be
rpc.Reset()
giantIdBytes := make([]byte, limit*2)
rand.Read(giantIdBytes)
crand.Read(giantIdBytes)
rpc.Control = &pb.ControlMessage{
Iwant: []*pb.ControlIWant{
{MessageIDs: []string{"hello", string(giantIdBytes)}},
Expand Down

0 comments on commit 8f56e8c

Please sign in to comment.