Browse Source

libs: remove most of libs/rand (#6364)

pull/6391/head
Ismail Khoffi 4 years ago
committed by GitHub
parent
commit
ee70430255
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
27 changed files with 157 additions and 425 deletions
  1. +1
    -0
      CHANGELOG_PENDING.md
  2. +3
    -2
      abci/client/socket_client_test.go
  3. +4
    -1
      abci/example/kvstore/helpers.go
  4. +3
    -1
      abci/tests/server/client.go
  5. +2
    -1
      blockchain/v0/pool_test.go
  6. +3
    -2
      consensus/wal_generator.go
  7. +6
    -3
      libs/bits/bit_array.go
  8. +4
    -5
      libs/clist/clist_test.go
  9. +41
    -263
      libs/rand/random.go
  10. +0
    -71
      libs/rand/random_test.go
  11. +3
    -2
      libs/tempfile/tempfile_test.go
  12. +5
    -4
      libs/test/mutate.go
  13. +6
    -5
      p2p/conn/secret_connection_test.go
  14. +11
    -10
      p2p/pex/addrbook.go
  15. +7
    -6
      p2p/pex/addrbook_test.go
  16. +6
    -3
      p2p/pex/pex_reactor.go
  17. +7
    -7
      p2p/switch.go
  18. +6
    -4
      p2p/test_util.go
  19. +3
    -2
      rpc/jsonrpc/client/ws_client.go
  20. +3
    -4
      rpc/jsonrpc/jsonrpc_test.go
  21. +5
    -5
      state/state_test.go
  22. +3
    -2
      types/block_test.go
  23. +4
    -5
      types/event_bus_test.go
  24. +2
    -1
      types/evidence_test.go
  25. +2
    -1
      types/tx_test.go
  26. +3
    -2
      types/validator.go
  27. +14
    -13
      types/validator_set_test.go

+ 1
- 0
CHANGELOG_PENDING.md View File

@ -46,6 +46,7 @@ Friendly reminder: We have a [bug bounty program](https://hackerone.com/tendermi
- [rpc/client/http] \#6176 Remove `endpoint` arg from `New`, `NewWithTimeout` and `NewWithClient` (@melekes) - [rpc/client/http] \#6176 Remove `endpoint` arg from `New`, `NewWithTimeout` and `NewWithClient` (@melekes)
- [rpc/client/http] \#6176 Unexpose `WSEvents` (@melekes) - [rpc/client/http] \#6176 Unexpose `WSEvents` (@melekes)
- [rpc/jsonrpc/client/ws_client] \#6176 `NewWS` no longer accepts options (use `NewWSWithOptions` and `OnReconnect` funcs to configure the client) (@melekes) - [rpc/jsonrpc/client/ws_client] \#6176 `NewWS` no longer accepts options (use `NewWSWithOptions` and `OnReconnect` funcs to configure the client) (@melekes)
- [libs/rand] \#6364 Removed most of libs/rand in favour of standard lib's `math/rand` (@liamsi)
- Blockchain Protocol - Blockchain Protocol


+ 3
- 2
abci/client/socket_client_test.go View File

@ -6,13 +6,14 @@ import (
"testing" "testing"
"time" "time"
"math/rand"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
abcicli "github.com/tendermint/tendermint/abci/client" abcicli "github.com/tendermint/tendermint/abci/client"
"github.com/tendermint/tendermint/abci/server" "github.com/tendermint/tendermint/abci/server"
"github.com/tendermint/tendermint/abci/types" "github.com/tendermint/tendermint/abci/types"
tmrand "github.com/tendermint/tendermint/libs/rand"
"github.com/tendermint/tendermint/libs/service" "github.com/tendermint/tendermint/libs/service"
) )
@ -101,7 +102,7 @@ func TestHangingSyncCalls(t *testing.T) {
func setupClientServer(t *testing.T, app types.Application) ( func setupClientServer(t *testing.T, app types.Application) (
service.Service, abcicli.Client) { service.Service, abcicli.Client) {
// some port between 20k and 30k // some port between 20k and 30k
port := 20000 + tmrand.Int32()%10000
port := 20000 + rand.Int31()%10000
addr := fmt.Sprintf("localhost:%d", port) addr := fmt.Sprintf("localhost:%d", port)
s, err := server.NewServer(addr, "socket", app) s, err := server.NewServer(addr, "socket", app)


+ 4
- 1
abci/example/kvstore/helpers.go View File

@ -1,6 +1,8 @@
package kvstore package kvstore
import ( import (
mrand "math/rand"
"github.com/tendermint/tendermint/abci/types" "github.com/tendermint/tendermint/abci/types"
tmrand "github.com/tendermint/tendermint/libs/rand" tmrand "github.com/tendermint/tendermint/libs/rand"
) )
@ -9,7 +11,8 @@ import (
// from the input value // from the input value
func RandVal(i int) types.ValidatorUpdate { func RandVal(i int) types.ValidatorUpdate {
pubkey := tmrand.Bytes(32) pubkey := tmrand.Bytes(32)
power := tmrand.Uint16() + 1
// Random value between [0, 2^16 - 1]
power := mrand.Uint32() & (1<<16 - 1) // nolint:gosec // G404: Use of weak random number generator
v := types.UpdateValidator(pubkey, int64(power), "") v := types.UpdateValidator(pubkey, int64(power), "")
return v return v
} }


+ 3
- 1
abci/tests/server/client.go View File

@ -5,6 +5,7 @@ import (
"context" "context"
"errors" "errors"
"fmt" "fmt"
mrand "math/rand"
abcicli "github.com/tendermint/tendermint/abci/client" abcicli "github.com/tendermint/tendermint/abci/client"
"github.com/tendermint/tendermint/abci/types" "github.com/tendermint/tendermint/abci/types"
@ -18,7 +19,8 @@ func InitChain(client abcicli.Client) error {
vals := make([]types.ValidatorUpdate, total) vals := make([]types.ValidatorUpdate, total)
for i := 0; i < total; i++ { for i := 0; i < total; i++ {
pubkey := tmrand.Bytes(33) pubkey := tmrand.Bytes(33)
power := tmrand.Int()
// nolint:gosec // G404: Use of weak random number generator
power := mrand.Int()
vals[i] = types.UpdateValidator(pubkey, int64(power), "") vals[i] = types.UpdateValidator(pubkey, int64(power), "")
} }
_, err := client.InitChainSync(ctx, types.RequestInitChain{ _, err := client.InitChainSync(ctx, types.RequestInitChain{


+ 2
- 1
blockchain/v0/pool_test.go View File

@ -2,6 +2,7 @@ package v0
import ( import (
"fmt" "fmt"
mrand "math/rand"
"testing" "testing"
"time" "time"
@ -67,7 +68,7 @@ func makePeers(numPeers int, minHeight, maxHeight int64) testPeers {
peers := make(testPeers, numPeers) peers := make(testPeers, numPeers)
for i := 0; i < numPeers; i++ { for i := 0; i < numPeers; i++ {
peerID := p2p.NodeID(tmrand.Str(12)) peerID := p2p.NodeID(tmrand.Str(12))
height := minHeight + tmrand.Int63n(maxHeight-minHeight)
height := minHeight + mrand.Int63n(maxHeight-minHeight)
base := minHeight + int64(i) base := minHeight + int64(i)
if base > height { if base > height {
base = height base = height


+ 3
- 2
consensus/wal_generator.go View File

@ -5,6 +5,7 @@ import (
"bytes" "bytes"
"fmt" "fmt"
"io" "io"
mrand "math/rand"
"path/filepath" "path/filepath"
"testing" "testing"
"time" "time"
@ -15,7 +16,6 @@ import (
"github.com/tendermint/tendermint/abci/example/kvstore" "github.com/tendermint/tendermint/abci/example/kvstore"
cfg "github.com/tendermint/tendermint/config" cfg "github.com/tendermint/tendermint/config"
"github.com/tendermint/tendermint/libs/log" "github.com/tendermint/tendermint/libs/log"
tmrand "github.com/tendermint/tendermint/libs/rand"
"github.com/tendermint/tendermint/privval" "github.com/tendermint/tendermint/privval"
"github.com/tendermint/tendermint/proxy" "github.com/tendermint/tendermint/proxy"
sm "github.com/tendermint/tendermint/state" sm "github.com/tendermint/tendermint/state"
@ -140,7 +140,8 @@ func WALWithNBlocks(t *testing.T, numBlocks int) (data []byte, err error) {
func randPort() int { func randPort() int {
// returns between base and base + spread // returns between base and base + spread
base, spread := 20000, 20000 base, spread := 20000, 20000
return base + tmrand.Intn(spread)
// nolint:gosec // G404: Use of weak random number generator
return base + mrand.Intn(spread)
} }
func makeAddrs() (string, string, string) { func makeAddrs() (string, string, string) {


+ 6
- 3
libs/bits/bit_array.go View File

@ -5,6 +5,7 @@ import (
"errors" "errors"
"fmt" "fmt"
"math" "math"
mrand "math/rand"
"regexp" "regexp"
"strings" "strings"
"sync" "sync"
@ -24,6 +25,8 @@ type BitArray struct {
// NewBitArray returns a new bit array. // NewBitArray returns a new bit array.
// It returns nil if the number of bits is zero. // It returns nil if the number of bits is zero.
func NewBitArray(bits int) *BitArray { func NewBitArray(bits int) *BitArray {
// Reseed non-deterministically.
tmrand.Reseed()
if bits <= 0 { if bits <= 0 {
return nil return nil
} }
@ -242,7 +245,7 @@ func (bA *BitArray) IsFull() bool {
// PickRandom returns a random index for a set bit in the bit array. // PickRandom returns a random index for a set bit in the bit array.
// If there is no such value, it returns 0, false. // If there is no such value, it returns 0, false.
// It uses the global randomness in `random.go` to get this index.
// It uses math/rand's global randomness Source to get this index.
func (bA *BitArray) PickRandom() (int, bool) { func (bA *BitArray) PickRandom() (int, bool) {
if bA == nil { if bA == nil {
return 0, false return 0, false
@ -255,8 +258,8 @@ func (bA *BitArray) PickRandom() (int, bool) {
if len(trueIndices) == 0 { // no bits set to true if len(trueIndices) == 0 { // no bits set to true
return 0, false return 0, false
} }
return trueIndices[tmrand.Intn(len(trueIndices))], true
// nolint:gosec // G404: Use of weak random number generator
return trueIndices[mrand.Intn(len(trueIndices))], true
} }
func (bA *BitArray) getTrueIndices() []int { func (bA *BitArray) getTrueIndices() []int {


+ 4
- 5
libs/clist/clist_test.go View File

@ -2,14 +2,13 @@ package clist
import ( import (
"fmt" "fmt"
mrand "math/rand"
"runtime" "runtime"
"sync/atomic" "sync/atomic"
"testing" "testing"
"time" "time"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
tmrand "github.com/tendermint/tendermint/libs/rand"
) )
func TestPanicOnMaxLength(t *testing.T) { func TestPanicOnMaxLength(t *testing.T) {
@ -148,7 +147,7 @@ func _TestGCRandom(t *testing.T) {
els = append(els, el) els = append(els, el)
} }
for _, i := range tmrand.Perm(numElements) {
for _, i := range mrand.Perm(numElements) {
el := els[i] el := els[i]
l.Remove(el) l.Remove(el)
_ = el.Next() _ = el.Next()
@ -206,7 +205,7 @@ func TestScanRightDeleteRandom(t *testing.T) {
// Remove an element, push back an element. // Remove an element, push back an element.
for i := 0; i < numTimes; i++ { for i := 0; i < numTimes; i++ {
// Pick an element to remove // Pick an element to remove
rmElIdx := tmrand.Intn(len(els))
rmElIdx := mrand.Intn(len(els))
rmEl := els[rmElIdx] rmEl := els[rmElIdx]
// Remove it // Remove it
@ -260,7 +259,7 @@ func TestWaitChan(t *testing.T) {
for i := 1; i < 100; i++ { for i := 1; i < 100; i++ {
l.PushBack(i) l.PushBack(i)
pushed++ pushed++
time.Sleep(time.Duration(tmrand.Intn(25)) * time.Millisecond)
time.Sleep(time.Duration(mrand.Intn(25)) * time.Millisecond)
} }
// apply a deterministic pause so the counter has time to catch up // apply a deterministic pause so the counter has time to catch up
time.Sleep(25 * time.Millisecond) time.Sleep(25 * time.Millisecond)


+ 41
- 263
libs/rand/random.go View File

@ -2,161 +2,57 @@ package rand
import ( import (
crand "crypto/rand" crand "crypto/rand"
"encoding/binary"
"fmt"
mrand "math/rand" mrand "math/rand"
"time"
tmsync "github.com/tendermint/tendermint/libs/sync"
) )
const ( const (
strChars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" // 62 characters strChars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" // 62 characters
) )
// Rand is a prng, that is seeded with OS randomness.
// The OS randomness is obtained from crypto/rand, however none of the provided
// methods are suitable for cryptographic usage.
// They all utilize math/rand's prng internally.
//
// All of the methods here are suitable for concurrent use.
// This is achieved by using a mutex lock on all of the provided methods.
type Rand struct {
tmsync.Mutex
rand *mrand.Rand
}
var grand *Rand
func init() { func init() {
grand = NewRand()
grand.init()
}
func NewRand() *Rand {
rand := &Rand{}
rand.init()
return rand
Reseed()
} }
func (r *Rand) init() {
bz := cRandBytes(8)
var seed uint64
for i := 0; i < 8; i++ {
seed |= uint64(bz[i])
seed <<= 8
}
r.reset(int64(seed))
}
func (r *Rand) reset(seed int64) {
r.rand = mrand.New(mrand.NewSource(seed)) // nolint:gosec // G404: Use of weak random number generator
// NewRand returns a prng, that is seeded with OS randomness.
// The OS randomness is obtained from crypto/rand, however, like with any math/rand.Rand
// object none of the provided methods are suitable for cryptographic usage.
//
// Note that the returned instance of math/rand's Rand is not
// suitable for concurrent use by multiple goroutines.
//
// For concurrent use, call Reseed to reseed math/rand's default source and
// use math/rand's top-level convenience functions instead.
func NewRand() *mrand.Rand {
seed := crandSeed()
// nolint:gosec // G404: Use of weak random number generator
return mrand.New(mrand.NewSource(seed))
} }
//----------------------------------------
// Global functions
func Seed(seed int64) {
grand.Seed(seed)
// Reseed conveniently re-seeds the default Source of math/rand with
// randomness obtained from crypto/rand.
//
// Note that this does not make math/rand suitable for cryptographic usage.
//
// Use math/rand's top-level convenience functions remain suitable
// for concurrent use by multiple goroutines.
func Reseed() {
seed := crandSeed()
mrand.Seed(seed)
} }
// Str constructs a random alphanumeric string of given length
// from math/rand's global default Source.
func Str(length int) string { func Str(length int) string {
return grand.Str(length)
}
func Uint16() uint16 {
return grand.Uint16()
}
func Uint32() uint32 {
return grand.Uint32()
}
func Uint64() uint64 {
return grand.Uint64()
}
func Uint() uint {
return grand.Uint()
}
func Int16() int16 {
return grand.Int16()
}
func Int32() int32 {
return grand.Int32()
}
func Int64() int64 {
return grand.Int64()
}
func Int() int {
return grand.Int()
}
func Int31() int32 {
return grand.Int31()
}
func Int31n(n int32) int32 {
return grand.Int31n(n)
}
func Int63() int64 {
return grand.Int63()
}
func Int63n(n int64) int64 {
return grand.Int63n(n)
}
func Bool() bool {
return grand.Bool()
}
func Float32() float32 {
return grand.Float32()
}
func Float64() float64 {
return grand.Float64()
}
func Time() time.Time {
return grand.Time()
}
func Bytes(n int) []byte {
return grand.Bytes(n)
}
func Intn(n int) int {
return grand.Intn(n)
}
func Perm(n int) []int {
return grand.Perm(n)
}
//----------------------------------------
// Rand methods
func (r *Rand) Seed(seed int64) {
r.Lock()
r.reset(seed)
r.Unlock()
}
// Str constructs a random alphanumeric string of given length.
func (r *Rand) Str(length int) string {
if length <= 0 { if length <= 0 {
return "" return ""
} }
chars := []byte{}
MAIN_LOOP:
chars := make([]byte, 0, length)
for { for {
val := r.Int63()
// nolint:gosec // G404: Use of weak random number generator
val := mrand.Int63()
for i := 0; i < 10; i++ { for i := 0; i < 10; i++ {
v := int(val & 0x3f) // rightmost 6 bits v := int(val & 0x3f) // rightmost 6 bits
if v >= 62 { // only 62 characters in strChars if v >= 62 { // only 62 characters in strChars
@ -165,147 +61,29 @@ MAIN_LOOP:
} else { } else {
chars = append(chars, strChars[v]) chars = append(chars, strChars[v])
if len(chars) == length { if len(chars) == length {
break MAIN_LOOP
return string(chars)
} }
val >>= 6 val >>= 6
} }
} }
} }
return string(chars)
}
func (r *Rand) Uint16() uint16 {
return uint16(r.Uint32() & (1<<16 - 1))
}
func (r *Rand) Uint32() uint32 {
r.Lock()
u32 := r.rand.Uint32()
r.Unlock()
return u32
}
func (r *Rand) Uint64() uint64 {
return uint64(r.Uint32())<<32 + uint64(r.Uint32())
}
func (r *Rand) Uint() uint {
r.Lock()
i := r.rand.Int()
r.Unlock()
return uint(i)
}
func (r *Rand) Int16() int16 {
return int16(r.Uint32() & (1<<16 - 1))
}
func (r *Rand) Int32() int32 {
return int32(r.Uint32())
}
func (r *Rand) Int64() int64 {
return int64(r.Uint64())
}
func (r *Rand) Int() int {
r.Lock()
i := r.rand.Int()
r.Unlock()
return i
}
func (r *Rand) Int31() int32 {
r.Lock()
i31 := r.rand.Int31()
r.Unlock()
return i31
}
func (r *Rand) Int31n(n int32) int32 {
r.Lock()
i31n := r.rand.Int31n(n)
r.Unlock()
return i31n
}
func (r *Rand) Int63() int64 {
r.Lock()
i63 := r.rand.Int63()
r.Unlock()
return i63
}
func (r *Rand) Int63n(n int64) int64 {
r.Lock()
i63n := r.rand.Int63n(n)
r.Unlock()
return i63n
}
func (r *Rand) Float32() float32 {
r.Lock()
f32 := r.rand.Float32()
r.Unlock()
return f32
}
func (r *Rand) Float64() float64 {
r.Lock()
f64 := r.rand.Float64()
r.Unlock()
return f64
} }
func (r *Rand) Time() time.Time {
return time.Unix(int64(r.Uint64()), 0)
}
// Bytes returns n random bytes generated from the internal
// prng.
func (r *Rand) Bytes(n int) []byte {
// cRandBytes isn't guaranteed to be fast so instead
// use random bytes generated from the internal PRNG
// Bytes returns n random bytes generated from math/rand's global default Source.
func Bytes(n int) []byte {
bs := make([]byte, n) bs := make([]byte, n)
for i := 0; i < len(bs); i++ { for i := 0; i < len(bs); i++ {
bs[i] = byte(r.Int() & 0xFF)
// nolint:gosec // G404: Use of weak random number generator
bs[i] = byte(mrand.Int() & 0xFF)
} }
return bs return bs
} }
// Intn returns, as an int, a uniform pseudo-random number in the range [0, n).
// It panics if n <= 0.
func (r *Rand) Intn(n int) int {
r.Lock()
i := r.rand.Intn(n)
r.Unlock()
return i
}
// Bool returns a uniformly random boolean
func (r *Rand) Bool() bool {
// See https://github.com/golang/go/issues/23804#issuecomment-365370418
// for reasoning behind computing like this
return r.Int63()%2 == 0
}
// Perm returns a pseudo-random permutation of n integers in [0, n).
func (r *Rand) Perm(n int) []int {
r.Lock()
perm := r.rand.Perm(n)
r.Unlock()
return perm
}
// NOTE: This relies on the os's random number generator.
// For real security, we should salt that with some seed.
// See github.com/tendermint/tendermint/crypto for a more secure reader.
func cRandBytes(numBytes int) []byte {
b := make([]byte, numBytes)
_, err := crand.Read(b)
func crandSeed() int64 {
var seed int64
err := binary.Read(crand.Reader, binary.BigEndian, &seed)
if err != nil { if err != nil {
panic(err)
panic(fmt.Sprintf("could nor read random seed from crypto/rand: %v", err))
} }
return b
return seed
} }

+ 0
- 71
libs/rand/random_test.go View File

@ -1,13 +1,7 @@
package rand package rand
import ( import (
"bytes"
"encoding/json"
"fmt"
mrand "math/rand"
"sync"
"testing" "testing"
"time"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
) )
@ -24,71 +18,6 @@ func TestRandBytes(t *testing.T) {
assert.Equal(t, l, len(b)) assert.Equal(t, l, len(b))
} }
func TestRandIntn(t *testing.T) {
n := 243
for i := 0; i < 100; i++ {
x := Intn(n)
assert.True(t, x < n)
}
}
// Test to make sure that we never call math.rand().
// We do this by ensuring that outputs are deterministic.
func TestDeterminism(t *testing.T) {
var firstOutput string
// Set math/rand's seed for the sake of debugging this test.
// (It isn't strictly necessary).
mrand.Seed(1)
for i := 0; i < 100; i++ {
output := testThemAll()
if i == 0 {
firstOutput = output
} else if firstOutput != output {
t.Errorf("run #%d's output was different from first run.\nfirst: %v\nlast: %v",
i, firstOutput, output)
}
}
}
func testThemAll() string {
// Such determinism.
grand.reset(1)
// Use it.
out := new(bytes.Buffer)
perm := Perm(10)
blob, _ := json.Marshal(perm)
fmt.Fprintf(out, "perm: %s\n", blob)
fmt.Fprintf(out, "randInt: %d\n", Int())
fmt.Fprintf(out, "randUint: %d\n", Uint())
fmt.Fprintf(out, "randIntn: %d\n", Intn(97))
fmt.Fprintf(out, "randInt31: %d\n", Int31())
fmt.Fprintf(out, "randInt32: %d\n", Int32())
fmt.Fprintf(out, "randInt63: %d\n", Int63())
fmt.Fprintf(out, "randInt64: %d\n", Int64())
fmt.Fprintf(out, "randUint32: %d\n", Uint32())
fmt.Fprintf(out, "randUint64: %d\n", Uint64())
return out.String()
}
func TestRngConcurrencySafety(t *testing.T) {
var wg sync.WaitGroup
for i := 0; i < 100; i++ {
wg.Add(1)
go func() {
defer wg.Done()
_ = Uint64()
<-time.After(time.Millisecond * time.Duration(Intn(100)))
_ = Perm(3)
}()
}
wg.Wait()
}
func BenchmarkRandBytes10B(b *testing.B) { func BenchmarkRandBytes10B(b *testing.B) {
benchmarkRandBytes(b, 10) benchmarkRandBytes(b, 10)
} }


+ 3
- 2
libs/tempfile/tempfile_test.go View File

@ -6,6 +6,7 @@ import (
"bytes" "bytes"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
mrand "math/rand"
"os" "os"
testing "testing" testing "testing"
@ -16,8 +17,8 @@ import (
func TestWriteFileAtomic(t *testing.T) { func TestWriteFileAtomic(t *testing.T) {
var ( var (
data = []byte(tmrand.Str(tmrand.Intn(2048)))
old = tmrand.Bytes(tmrand.Intn(2048))
data = []byte(tmrand.Str(mrand.Intn(2048)))
old = tmrand.Bytes(mrand.Intn(2048))
perm os.FileMode = 0600 perm os.FileMode = 0600
) )


+ 5
- 4
libs/test/mutate.go View File

@ -1,7 +1,8 @@
// nolint:gosec // G404: Use of weak random number generator
package test package test
import ( import (
tmrand "github.com/tendermint/tendermint/libs/rand"
mrand "math/rand"
) )
// Contract: !bytes.Equal(input, output) && len(input) >= len(output) // Contract: !bytes.Equal(input, output) && len(input) >= len(output)
@ -17,11 +18,11 @@ func MutateByteSlice(bytez []byte) []byte {
bytez = mBytez bytez = mBytez
// Try a random mutation // Try a random mutation
switch tmrand.Int() % 2 {
switch mrand.Int() % 2 {
case 0: // Mutate a single byte case 0: // Mutate a single byte
bytez[tmrand.Int()%len(bytez)] += byte(tmrand.Int()%255 + 1)
bytez[mrand.Int()%len(bytez)] += byte(mrand.Int()%255 + 1)
case 1: // Remove an arbitrary byte case 1: // Remove an arbitrary byte
pos := tmrand.Int() % len(bytez)
pos := mrand.Int() % len(bytez)
bytez = append(bytez[:pos], bytez[pos+1:]...) bytez = append(bytez[:pos], bytez[pos+1:]...)
} }
return bytez return bytez


+ 6
- 5
p2p/conn/secret_connection_test.go View File

@ -8,6 +8,7 @@ import (
"io" "io"
"io/ioutil" "io/ioutil"
"log" "log"
mrand "math/rand"
"os" "os"
"path/filepath" "path/filepath"
"strconv" "strconv"
@ -114,8 +115,8 @@ func TestSecretConnectionReadWrite(t *testing.T) {
// Pre-generate the things to write (for foo & bar) // Pre-generate the things to write (for foo & bar)
for i := 0; i < 100; i++ { for i := 0; i < 100; i++ {
fooWrites = append(fooWrites, tmrand.Str((tmrand.Int()%(dataMaxSize*5))+1))
barWrites = append(barWrites, tmrand.Str((tmrand.Int()%(dataMaxSize*5))+1))
fooWrites = append(fooWrites, tmrand.Str((mrand.Int()%(dataMaxSize*5))+1))
barWrites = append(barWrites, tmrand.Str((mrand.Int()%(dataMaxSize*5))+1))
} }
// A helper that will run with (fooConn, fooWrites, fooReads) and vice versa // A helper that will run with (fooConn, fooWrites, fooReads) and vice versa
@ -312,7 +313,7 @@ func createGoldenTestVectors(t *testing.T) string {
randSecret := new([32]byte) randSecret := new([32]byte)
copy((*randSecret)[:], randSecretVector) copy((*randSecret)[:], randSecretVector)
data += hex.EncodeToString((*randSecret)[:]) + "," data += hex.EncodeToString((*randSecret)[:]) + ","
locIsLeast := tmrand.Bool()
locIsLeast := mrand.Int63()%2 == 0
data += strconv.FormatBool(locIsLeast) + "," data += strconv.FormatBool(locIsLeast) + ","
recvSecret, sendSecret := deriveSecrets(randSecret, locIsLeast) recvSecret, sendSecret := deriveSecrets(randSecret, locIsLeast)
data += hex.EncodeToString((*recvSecret)[:]) + "," data += hex.EncodeToString((*recvSecret)[:]) + ","
@ -412,7 +413,7 @@ func BenchmarkWriteSecretConnection(b *testing.B) {
b.StartTimer() b.StartTimer()
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
idx := tmrand.Intn(len(fooWriteBytes))
idx := mrand.Intn(len(fooWriteBytes))
_, err := fooSecConn.Write(fooWriteBytes[idx]) _, err := fooSecConn.Write(fooWriteBytes[idx])
if err != nil { if err != nil {
b.Errorf("failed to write to fooSecConn: %v", err) b.Errorf("failed to write to fooSecConn: %v", err)
@ -446,7 +447,7 @@ func BenchmarkReadSecretConnection(b *testing.B) {
} }
go func() { go func() {
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
idx := tmrand.Intn(len(fooWriteBytes))
idx := mrand.Intn(len(fooWriteBytes))
_, err := fooSecConn.Write(fooWriteBytes[idx]) _, err := fooSecConn.Write(fooWriteBytes[idx])
if err != nil { if err != nil {
b.Errorf("failed to write to fooSecConn: %v, %v,%v", err, i, b.N) b.Errorf("failed to write to fooSecConn: %v, %v,%v", err, i, b.N)


+ 11
- 10
p2p/pex/addrbook.go View File

@ -9,13 +9,12 @@ import (
"encoding/binary" "encoding/binary"
"fmt" "fmt"
"math" "math"
"math/rand"
mrand "math/rand"
"net" "net"
"sync" "sync"
"time" "time"
"github.com/minio/highwayhash" "github.com/minio/highwayhash"
"github.com/tendermint/tendermint/crypto" "github.com/tendermint/tendermint/crypto"
tmmath "github.com/tendermint/tendermint/libs/math" tmmath "github.com/tendermint/tendermint/libs/math"
tmrand "github.com/tendermint/tendermint/libs/rand" tmrand "github.com/tendermint/tendermint/libs/rand"
@ -89,7 +88,6 @@ type addrBook struct {
// accessed concurrently // accessed concurrently
mtx tmsync.Mutex mtx tmsync.Mutex
rand *tmrand.Rand
ourAddrs map[string]struct{} ourAddrs map[string]struct{}
privateIDs map[p2p.NodeID]struct{} privateIDs map[p2p.NodeID]struct{}
addrLookup map[p2p.NodeID]*knownAddress // new & old addrLookup map[p2p.NodeID]*knownAddress // new & old
@ -118,7 +116,6 @@ func newHashKey() []byte {
// Use Start to begin processing asynchronous address updates. // Use Start to begin processing asynchronous address updates.
func NewAddrBook(filePath string, routabilityStrict bool) AddrBook { func NewAddrBook(filePath string, routabilityStrict bool) AddrBook {
am := &addrBook{ am := &addrBook{
rand: tmrand.NewRand(),
ourAddrs: make(map[string]struct{}), ourAddrs: make(map[string]struct{}),
privateIDs: make(map[p2p.NodeID]struct{}), privateIDs: make(map[p2p.NodeID]struct{}),
addrLookup: make(map[p2p.NodeID]*knownAddress), addrLookup: make(map[p2p.NodeID]*knownAddress),
@ -268,6 +265,7 @@ func (a *addrBook) Empty() bool {
// and determines how biased we are to pick an address from a new bucket. // and determines how biased we are to pick an address from a new bucket.
// PickAddress returns nil if the AddrBook is empty or if we try to pick // PickAddress returns nil if the AddrBook is empty or if we try to pick
// from an empty bucket. // from an empty bucket.
// nolint:gosec // G404: Use of weak random number generator
func (a *addrBook) PickAddress(biasTowardsNewAddrs int) *p2p.NetAddress { func (a *addrBook) PickAddress(biasTowardsNewAddrs int) *p2p.NetAddress {
a.mtx.Lock() a.mtx.Lock()
defer a.mtx.Unlock() defer a.mtx.Unlock()
@ -292,7 +290,7 @@ func (a *addrBook) PickAddress(biasTowardsNewAddrs int) *p2p.NetAddress {
// pick a random peer from a random bucket // pick a random peer from a random bucket
var bucket map[string]*knownAddress var bucket map[string]*knownAddress
pickFromOldBucket := (newCorrelation+oldCorrelation)*a.rand.Float64() < oldCorrelation
pickFromOldBucket := (newCorrelation+oldCorrelation)*mrand.Float64() < oldCorrelation
if (pickFromOldBucket && a.nOld == 0) || if (pickFromOldBucket && a.nOld == 0) ||
(!pickFromOldBucket && a.nNew == 0) { (!pickFromOldBucket && a.nNew == 0) {
return nil return nil
@ -300,13 +298,13 @@ func (a *addrBook) PickAddress(biasTowardsNewAddrs int) *p2p.NetAddress {
// loop until we pick a random non-empty bucket // loop until we pick a random non-empty bucket
for len(bucket) == 0 { for len(bucket) == 0 {
if pickFromOldBucket { if pickFromOldBucket {
bucket = a.bucketsOld[a.rand.Intn(len(a.bucketsOld))]
bucket = a.bucketsOld[mrand.Intn(len(a.bucketsOld))]
} else { } else {
bucket = a.bucketsNew[a.rand.Intn(len(a.bucketsNew))]
bucket = a.bucketsNew[mrand.Intn(len(a.bucketsNew))]
} }
} }
// pick a random index and loop over the map to return that index // pick a random index and loop over the map to return that index
randIndex := a.rand.Intn(len(bucket))
randIndex := mrand.Intn(len(bucket))
for _, ka := range bucket { for _, ka := range bucket {
if randIndex == 0 { if randIndex == 0 {
return ka.Addr return ka.Addr
@ -417,7 +415,8 @@ func (a *addrBook) GetSelection() []*p2p.NetAddress {
// `numAddresses' since we are throwing the rest. // `numAddresses' since we are throwing the rest.
for i := 0; i < numAddresses; i++ { for i := 0; i < numAddresses; i++ {
// pick a number between current index and the end // pick a number between current index and the end
j := tmrand.Intn(len(allAddr)-i) + i
// nolint:gosec // G404: Use of weak random number generator
j := mrand.Intn(len(allAddr)-i) + i
allAddr[i], allAddr[j] = allAddr[j], allAddr[i] allAddr[i], allAddr[j] = allAddr[j], allAddr[i]
} }
@ -681,7 +680,8 @@ func (a *addrBook) addAddress(addr, src *p2p.NetAddress) error {
} }
// The more entries we have, the less likely we are to add more. // The more entries we have, the less likely we are to add more.
factor := int32(2 * len(ka.Buckets)) factor := int32(2 * len(ka.Buckets))
if a.rand.Int31n(factor) != 0 {
// nolint:gosec // G404: Use of weak random number generator
if mrand.Int31n(factor) != 0 {
return nil return nil
} }
} else { } else {
@ -717,6 +717,7 @@ func (a *addrBook) randomPickAddresses(bucketType byte, num int) []*p2p.NetAddre
} }
selection := make([]*p2p.NetAddress, 0, num) selection := make([]*p2p.NetAddress, 0, num)
chosenSet := make(map[string]bool, num) chosenSet := make(map[string]bool, num)
rand := tmrand.NewRand()
rand.Shuffle(total, func(i, j int) { rand.Shuffle(total, func(i, j int) {
addresses[i], addresses[j] = addresses[j], addresses[i] addresses[i], addresses[j] = addresses[j], addresses[i]
}) })


+ 7
- 6
p2p/pex/addrbook_test.go View File

@ -5,6 +5,7 @@ import (
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"math" "math"
mrand "math/rand"
"net" "net"
"os" "os"
"testing" "testing"
@ -187,12 +188,12 @@ func randNetAddressPairs(t *testing.T, n int) []netAddressPair {
func randIPv4Address(t *testing.T) *p2p.NetAddress { func randIPv4Address(t *testing.T) *p2p.NetAddress {
for { for {
ip := fmt.Sprintf("%v.%v.%v.%v", ip := fmt.Sprintf("%v.%v.%v.%v",
tmrand.Intn(254)+1,
tmrand.Intn(255),
tmrand.Intn(255),
tmrand.Intn(255),
mrand.Intn(254)+1,
mrand.Intn(255),
mrand.Intn(255),
mrand.Intn(255),
) )
port := tmrand.Intn(65535-1) + 1
port := mrand.Intn(65535-1) + 1
id := p2p.NodeID(hex.EncodeToString(tmrand.Bytes(p2p.NodeIDByteLength))) id := p2p.NodeID(hex.EncodeToString(tmrand.Bytes(p2p.NodeIDByteLength)))
idAddr := p2p.IDAddressString(id, fmt.Sprintf("%v:%v", ip, port)) idAddr := p2p.IDAddressString(id, fmt.Sprintf("%v:%v", ip, port))
addr, err := p2p.NewNetAddressString(idAddr) addr, err := p2p.NewNetAddressString(idAddr)
@ -554,7 +555,7 @@ func TestMultipleAddrBookAddressSelection(t *testing.T) {
ranges := [...][]int{{33, 100}, {100, 175}} ranges := [...][]int{{33, 100}, {100, 175}}
bookSizes := make([]int, 0, len(ranges)) bookSizes := make([]int, 0, len(ranges))
for _, r := range ranges { for _, r := range ranges {
bookSizes = append(bookSizes, tmrand.Intn(r[1]-r[0])+r[0])
bookSizes = append(bookSizes, mrand.Intn(r[1]-r[0])+r[0])
} }
t.Logf("Testing address selection for the following book sizes %v\n", bookSizes) t.Logf("Testing address selection for the following book sizes %v\n", bookSizes)
for _, bookSize := range bookSizes { for _, bookSize := range bookSizes {


+ 6
- 3
p2p/pex/pex_reactor.go View File

@ -525,7 +525,8 @@ func (r *Reactor) ensurePeers() {
peers := r.Switch.Peers().List() peers := r.Switch.Peers().List()
peersCount := len(peers) peersCount := len(peers)
if peersCount > 0 { if peersCount > 0 {
peer := peers[tmrand.Int()%peersCount]
rand := tmrand.NewRand()
peer := peers[rand.Int()%peersCount]
r.Logger.Info("We need more addresses. Sending pexRequest to random peer", "peer", peer) r.Logger.Info("We need more addresses. Sending pexRequest to random peer", "peer", peer)
r.RequestAddrs(peer) r.RequestAddrs(peer)
} }
@ -558,7 +559,8 @@ func (r *Reactor) dialPeer(addr *p2p.NetAddress) error {
// exponential backoff if it's not our first attempt to dial given address // exponential backoff if it's not our first attempt to dial given address
if attempts > 0 { if attempts > 0 {
jitter := time.Duration(tmrand.Float64() * float64(time.Second)) // 1s == (1e9 ns)
rand := tmrand.NewRand()
jitter := time.Duration(rand.Float64() * float64(time.Second)) // 1s == (1e9 ns)
backoffDuration := jitter + ((1 << uint(attempts)) * time.Second) backoffDuration := jitter + ((1 << uint(attempts)) * time.Second)
backoffDuration = r.maxBackoffDurationForPeer(addr, backoffDuration) backoffDuration = r.maxBackoffDurationForPeer(addr, backoffDuration)
sinceLastDialed := time.Since(lastDialed) sinceLastDialed := time.Since(lastDialed)
@ -624,7 +626,8 @@ func (r *Reactor) checkSeeds() (numOnline int, netAddrs []*p2p.NetAddress, err e
// randomly dial seeds until we connect to one or exhaust them // randomly dial seeds until we connect to one or exhaust them
func (r *Reactor) dialSeeds() { func (r *Reactor) dialSeeds() {
perm := tmrand.Perm(len(r.seedAddrs))
rand := tmrand.NewRand()
perm := rand.Perm(len(r.seedAddrs))
// perm := r.Switch.rng.Perm(lSeeds) // perm := r.Switch.rng.Perm(lSeeds)
for _, i := range perm { for _, i := range perm {
// dial a random seed // dial a random seed


+ 7
- 7
p2p/switch.go View File

@ -5,6 +5,7 @@ import (
"fmt" "fmt"
"io" "io"
"math" "math"
mrand "math/rand"
"net" "net"
"sync" "sync"
"time" "time"
@ -12,7 +13,7 @@ import (
"github.com/tendermint/tendermint/config" "github.com/tendermint/tendermint/config"
"github.com/tendermint/tendermint/crypto" "github.com/tendermint/tendermint/crypto"
"github.com/tendermint/tendermint/libs/cmap" "github.com/tendermint/tendermint/libs/cmap"
"github.com/tendermint/tendermint/libs/rand"
tmrand "github.com/tendermint/tendermint/libs/rand"
"github.com/tendermint/tendermint/libs/service" "github.com/tendermint/tendermint/libs/service"
"github.com/tendermint/tendermint/p2p/conn" "github.com/tendermint/tendermint/p2p/conn"
) )
@ -115,8 +116,6 @@ type Switch struct {
connFilters []ConnFilterFunc connFilters []ConnFilterFunc
conns ConnSet conns ConnSet
rng *rand.Rand // seed for randomizing dial times and orders
metrics *Metrics metrics *Metrics
} }
@ -159,8 +158,8 @@ func NewSwitch(
conns: NewConnSet(), conns: NewConnSet(),
} }
// Ensure we have a completely undeterministic PRNG.
sw.rng = rand.NewRand()
// Ensure PRNG is reseeded.
tmrand.Reseed()
sw.BaseService = *service.NewBaseService(nil, "P2P Switch", sw) sw.BaseService = *service.NewBaseService(nil, "P2P Switch", sw)
@ -554,7 +553,7 @@ func (sw *Switch) dialPeersAsync(netAddrs []*NetAddress) {
} }
// permute the list, dial them in random order. // permute the list, dial them in random order.
perm := sw.rng.Perm(len(netAddrs))
perm := mrand.Perm(len(netAddrs))
for i := 0; i < len(perm); i++ { for i := 0; i < len(perm); i++ {
go func(i int) { go func(i int) {
j := perm[i] j := perm[i]
@ -597,7 +596,8 @@ func (sw *Switch) DialPeerWithAddress(addr *NetAddress) error {
// sleep for interval plus some random amount of ms on [0, dialRandomizerIntervalMilliseconds] // sleep for interval plus some random amount of ms on [0, dialRandomizerIntervalMilliseconds]
func (sw *Switch) randomSleep(interval time.Duration) { func (sw *Switch) randomSleep(interval time.Duration) {
r := time.Duration(sw.rng.Int63n(dialRandomizerIntervalMilliseconds)) * time.Millisecond
// nolint:gosec // G404: Use of weak random number generator
r := time.Duration(mrand.Int63n(dialRandomizerIntervalMilliseconds)) * time.Millisecond
time.Sleep(r + interval) time.Sleep(r + interval)
} }


+ 6
- 4
p2p/test_util.go View File

@ -3,6 +3,7 @@ package p2p
import ( import (
"context" "context"
"fmt" "fmt"
mrand "math/rand"
"net" "net"
"github.com/tendermint/tendermint/libs/log" "github.com/tendermint/tendermint/libs/log"
@ -35,15 +36,16 @@ func CreateRandomPeer(outbound bool) Peer {
return p return p
} }
// nolint:gosec // G404: Use of weak random number generator
func CreateRoutableAddr() (addr string, netAddr *NetAddress) { func CreateRoutableAddr() (addr string, netAddr *NetAddress) {
for { for {
var err error var err error
addr = fmt.Sprintf("%X@%v.%v.%v.%v:26656", addr = fmt.Sprintf("%X@%v.%v.%v.%v:26656",
tmrand.Bytes(20), tmrand.Bytes(20),
tmrand.Int()%256,
tmrand.Int()%256,
tmrand.Int()%256,
tmrand.Int()%256)
mrand.Int()%256,
mrand.Int()%256,
mrand.Int()%256,
mrand.Int()%256)
netAddr, err = NewNetAddressString(addr) netAddr, err = NewNetAddressString(addr)
if err != nil { if err != nil {
panic(err) panic(err)


+ 3
- 2
rpc/jsonrpc/client/ws_client.go View File

@ -4,6 +4,7 @@ import (
"context" "context"
"encoding/json" "encoding/json"
"fmt" "fmt"
mrand "math/rand"
"net" "net"
"net/http" "net/http"
"sync" "sync"
@ -12,7 +13,6 @@ import (
"github.com/gorilla/websocket" "github.com/gorilla/websocket"
metrics "github.com/rcrowley/go-metrics" metrics "github.com/rcrowley/go-metrics"
tmrand "github.com/tendermint/tendermint/libs/rand"
"github.com/tendermint/tendermint/libs/service" "github.com/tendermint/tendermint/libs/service"
tmsync "github.com/tendermint/tendermint/libs/sync" tmsync "github.com/tendermint/tendermint/libs/sync"
types "github.com/tendermint/tendermint/rpc/jsonrpc/types" types "github.com/tendermint/tendermint/rpc/jsonrpc/types"
@ -266,7 +266,8 @@ func (c *WSClient) reconnect() error {
}() }()
for { for {
jitter := time.Duration(tmrand.Float64() * float64(time.Second)) // 1s == (1e9 ns)
// nolint:gosec // G404: Use of weak random number generator
jitter := time.Duration(mrand.Float64() * float64(time.Second)) // 1s == (1e9 ns)
backoffDuration := jitter + ((1 << attempt) * time.Second) backoffDuration := jitter + ((1 << attempt) * time.Second)
c.Logger.Info("reconnecting", "attempt", attempt+1, "backoff_duration", backoffDuration) c.Logger.Info("reconnecting", "attempt", attempt+1, "backoff_duration", backoffDuration)


+ 3
- 4
rpc/jsonrpc/jsonrpc_test.go View File

@ -6,6 +6,7 @@ import (
crand "crypto/rand" crand "crypto/rand"
"encoding/json" "encoding/json"
"fmt" "fmt"
mrand "math/rand"
"net/http" "net/http"
"os" "os"
"os/exec" "os/exec"
@ -18,8 +19,6 @@ import (
tmbytes "github.com/tendermint/tendermint/libs/bytes" tmbytes "github.com/tendermint/tendermint/libs/bytes"
"github.com/tendermint/tendermint/libs/log" "github.com/tendermint/tendermint/libs/log"
tmrand "github.com/tendermint/tendermint/libs/rand"
client "github.com/tendermint/tendermint/rpc/jsonrpc/client" client "github.com/tendermint/tendermint/rpc/jsonrpc/client"
server "github.com/tendermint/tendermint/rpc/jsonrpc/server" server "github.com/tendermint/tendermint/rpc/jsonrpc/server"
types "github.com/tendermint/tendermint/rpc/jsonrpc/types" types "github.com/tendermint/tendermint/rpc/jsonrpc/types"
@ -215,7 +214,7 @@ func testWithHTTPClient(t *testing.T, cl client.HTTPClient) {
require.Nil(t, err) require.Nil(t, err)
assert.Equal(t, got3, val3) assert.Equal(t, got3, val3)
val4 := tmrand.Intn(10000)
val4 := mrand.Intn(10000)
got4, err := echoIntViaHTTP(cl, val4) got4, err := echoIntViaHTTP(cl, val4)
require.Nil(t, err) require.Nil(t, err)
assert.Equal(t, got4, val4) assert.Equal(t, got4, val4)
@ -400,7 +399,7 @@ func TestWSClientPingPong(t *testing.T) {
} }
func randBytes(t *testing.T) []byte { func randBytes(t *testing.T) []byte {
n := tmrand.Intn(10) + 2
n := mrand.Intn(10) + 2
buf := make([]byte, n) buf := make([]byte, n)
_, err := crand.Read(buf) _, err := crand.Read(buf)
require.Nil(t, err) require.Nil(t, err)


+ 5
- 5
state/state_test.go View File

@ -6,6 +6,7 @@ import (
"fmt" "fmt"
"math" "math"
"math/big" "math/big"
mrand "math/rand"
"os" "os"
"testing" "testing"
@ -18,7 +19,6 @@ import (
cfg "github.com/tendermint/tendermint/config" cfg "github.com/tendermint/tendermint/config"
"github.com/tendermint/tendermint/crypto/ed25519" "github.com/tendermint/tendermint/crypto/ed25519"
cryptoenc "github.com/tendermint/tendermint/crypto/encoding" cryptoenc "github.com/tendermint/tendermint/crypto/encoding"
tmrand "github.com/tendermint/tendermint/libs/rand"
tmstate "github.com/tendermint/tendermint/proto/tendermint/state" tmstate "github.com/tendermint/tendermint/proto/tendermint/state"
sm "github.com/tendermint/tendermint/state" sm "github.com/tendermint/tendermint/state"
"github.com/tendermint/tendermint/types" "github.com/tendermint/tendermint/types"
@ -356,18 +356,18 @@ func TestProposerFrequency(t *testing.T) {
maxPower := 1000 maxPower := 1000
nTestCases := 5 nTestCases := 5
for i := 0; i < nTestCases; i++ { for i := 0; i < nTestCases; i++ {
N := tmrand.Int()%maxVals + 1
N := mrand.Int()%maxVals + 1
vals := make([]*types.Validator, N) vals := make([]*types.Validator, N)
totalVotePower := int64(0) totalVotePower := int64(0)
for j := 0; j < N; j++ { for j := 0; j < N; j++ {
// make sure votePower > 0 // make sure votePower > 0
votePower := int64(tmrand.Int()%maxPower) + 1
votePower := int64(mrand.Int()%maxPower) + 1
totalVotePower += votePower totalVotePower += votePower
privVal := types.NewMockPV() privVal := types.NewMockPV()
pubKey, err := privVal.GetPubKey(context.Background()) pubKey, err := privVal.GetPubKey(context.Background())
require.NoError(t, err) require.NoError(t, err)
val := types.NewValidator(pubKey, votePower) val := types.NewValidator(pubKey, votePower)
val.ProposerPriority = tmrand.Int64()
val.ProposerPriority = mrand.Int63()
vals[j] = val vals[j] = val
} }
valSet := types.NewValidatorSet(vals) valSet := types.NewValidatorSet(vals)
@ -384,7 +384,7 @@ func genValSetWithPowers(powers []int64) *types.ValidatorSet {
for i := 0; i < size; i++ { for i := 0; i < size; i++ {
totalVotePower += powers[i] totalVotePower += powers[i]
val := types.NewValidator(ed25519.GenPrivKey().PubKey(), powers[i]) val := types.NewValidator(ed25519.GenPrivKey().PubKey(), powers[i])
val.ProposerPriority = tmrand.Int64()
val.ProposerPriority = mrand.Int63()
vals[i] = val vals[i] = val
} }
valSet := types.NewValidatorSet(vals) valSet := types.NewValidatorSet(vals)


+ 3
- 2
types/block_test.go View File

@ -7,6 +7,7 @@ import (
"crypto/rand" "crypto/rand"
"encoding/hex" "encoding/hex"
"math" "math"
mrand "math/rand"
"os" "os"
"reflect" "reflect"
"testing" "testing"
@ -643,7 +644,7 @@ func TestBlockIDValidateBasic(t *testing.T) {
} }
func TestBlockProtoBuf(t *testing.T) { func TestBlockProtoBuf(t *testing.T) {
h := tmrand.Int63()
h := mrand.Int63()
c1 := randCommit(time.Now()) c1 := randCommit(time.Now())
b1 := MakeBlock(h, []Tx{Tx([]byte{1})}, &Commit{Signatures: []CommitSig{}}, []Evidence{}) b1 := MakeBlock(h, []Tx{Tx([]byte{1})}, &Commit{Signatures: []CommitSig{}}, []Evidence{})
b1.ProposerAddress = tmrand.Bytes(crypto.AddressSize) b1.ProposerAddress = tmrand.Bytes(crypto.AddressSize)
@ -751,7 +752,7 @@ func TestEvidenceDataProtoBuf(t *testing.T) {
func makeRandHeader() Header { func makeRandHeader() Header {
chainID := "test" chainID := "test"
t := time.Now() t := time.Now()
height := tmrand.Int63()
height := mrand.Int63()
randBytes := tmrand.Bytes(tmhash.Size) randBytes := tmrand.Bytes(tmhash.Size)
randAddress := tmrand.Bytes(crypto.AddressSize) randAddress := tmrand.Bytes(crypto.AddressSize)
h := Header{ h := Header{


+ 4
- 5
types/event_bus_test.go View File

@ -3,7 +3,7 @@ package types
import ( import (
"context" "context"
"fmt" "fmt"
"math/rand"
mrand "math/rand"
"testing" "testing"
"time" "time"
@ -13,7 +13,6 @@ import (
abci "github.com/tendermint/tendermint/abci/types" abci "github.com/tendermint/tendermint/abci/types"
tmpubsub "github.com/tendermint/tendermint/libs/pubsub" tmpubsub "github.com/tendermint/tendermint/libs/pubsub"
tmquery "github.com/tendermint/tendermint/libs/pubsub/query" tmquery "github.com/tendermint/tendermint/libs/pubsub/query"
tmrand "github.com/tendermint/tendermint/libs/rand"
) )
func TestEventBusPublishEventTx(t *testing.T) { func TestEventBusPublishEventTx(t *testing.T) {
@ -410,7 +409,7 @@ func BenchmarkEventBus(b *testing.B) {
func benchmarkEventBus(numClients int, randQueries bool, randEvents bool, b *testing.B) { func benchmarkEventBus(numClients int, randQueries bool, randEvents bool, b *testing.B) {
// for random* functions // for random* functions
rand.Seed(time.Now().Unix())
mrand.Seed(time.Now().Unix())
eventBus := NewEventBusWithBufferCapacity(0) // set buffer capacity to 0 so we are not testing cache eventBus := NewEventBusWithBufferCapacity(0) // set buffer capacity to 0 so we are not testing cache
err := eventBus.Start() err := eventBus.Start()
@ -476,7 +475,7 @@ var events = []string{
EventVote} EventVote}
func randEvent() string { func randEvent() string {
return events[tmrand.Intn(len(events))]
return events[mrand.Intn(len(events))]
} }
var queries = []tmpubsub.Query{ var queries = []tmpubsub.Query{
@ -494,5 +493,5 @@ var queries = []tmpubsub.Query{
EventQueryVote} EventQueryVote}
func randQuery() tmpubsub.Query { func randQuery() tmpubsub.Query {
return queries[tmrand.Intn(len(queries))]
return queries[mrand.Intn(len(queries))]
} }

+ 2
- 1
types/evidence_test.go View File

@ -3,6 +3,7 @@ package types
import ( import (
"context" "context"
"math" "math"
mrand "math/rand"
"testing" "testing"
"time" "time"
@ -256,7 +257,7 @@ func makeHeaderRandom() *Header {
return &Header{ return &Header{
Version: version.Consensus{Block: version.BlockProtocol, App: 1}, Version: version.Consensus{Block: version.BlockProtocol, App: 1},
ChainID: tmrand.Str(12), ChainID: tmrand.Str(12),
Height: int64(tmrand.Uint16()) + 1,
Height: int64(mrand.Uint32() + 1),
Time: time.Now(), Time: time.Now(),
LastBlockID: makeBlockIDRandom(), LastBlockID: makeBlockIDRandom(),
LastCommitHash: crypto.CRandBytes(tmhash.Size), LastCommitHash: crypto.CRandBytes(tmhash.Size),


+ 2
- 1
types/tx_test.go View File

@ -2,6 +2,7 @@ package types
import ( import (
"bytes" "bytes"
mrand "math/rand"
"testing" "testing"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
@ -21,7 +22,7 @@ func makeTxs(cnt, size int) Txs {
} }
func randInt(low, high int) int { func randInt(low, high int) int {
off := tmrand.Int() % (high - low)
off := mrand.Int() % (high - low)
return low + off return low + off
} }


+ 3
- 2
types/validator.go View File

@ -5,11 +5,11 @@ import (
"context" "context"
"errors" "errors"
"fmt" "fmt"
mrand "math/rand"
"strings" "strings"
"github.com/tendermint/tendermint/crypto" "github.com/tendermint/tendermint/crypto"
ce "github.com/tendermint/tendermint/crypto/encoding" ce "github.com/tendermint/tendermint/crypto/encoding"
tmrand "github.com/tendermint/tendermint/libs/rand"
tmproto "github.com/tendermint/tendermint/proto/tendermint/types" tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
) )
@ -183,7 +183,8 @@ func RandValidator(randPower bool, minPower int64) (*Validator, PrivValidator) {
privVal := NewMockPV() privVal := NewMockPV()
votePower := minPower votePower := minPower
if randPower { if randPower {
votePower += int64(tmrand.Uint32())
// nolint:gosec // G404: Use of weak random number generator
votePower += int64(mrand.Uint32())
} }
pubKey, err := privVal.GetPubKey(context.Background()) pubKey, err := privVal.GetPubKey(context.Background())
if err != nil { if err != nil {


+ 14
- 13
types/validator_set_test.go View File

@ -5,6 +5,7 @@ import (
"context" "context"
"fmt" "fmt"
"math" "math"
"math/rand"
"sort" "sort"
"strings" "strings"
"testing" "testing"
@ -352,10 +353,10 @@ func TestProposerSelection3(t *testing.T) {
// times is usually 1 // times is usually 1
times := int32(1) times := int32(1)
mod := (tmrand.Int() % 5) + 1
if tmrand.Int()%mod > 0 {
mod := (rand.Int() % 5) + 1
if rand.Int()%mod > 0 {
// sometimes its up to 5 // sometimes its up to 5
times = (tmrand.Int31() % 4) + 1
times = (rand.Int31() % 4) + 1
} }
vset.IncrementProposerPriority(times) vset.IncrementProposerPriority(times)
@ -376,8 +377,8 @@ func randPubKey() crypto.PubKey {
func randValidator(totalVotingPower int64) *Validator { func randValidator(totalVotingPower int64) *Validator {
// this modulo limits the ProposerPriority/VotingPower to stay in the // this modulo limits the ProposerPriority/VotingPower to stay in the
// bounds of MaxTotalVotingPower minus the already existing voting power: // bounds of MaxTotalVotingPower minus the already existing voting power:
val := NewValidator(randPubKey(), int64(tmrand.Uint64()%uint64(MaxTotalVotingPower-totalVotingPower)))
val.ProposerPriority = tmrand.Int64() % (MaxTotalVotingPower - totalVotingPower)
val := NewValidator(randPubKey(), int64(rand.Uint64()%uint64(MaxTotalVotingPower-totalVotingPower)))
val.ProposerPriority = rand.Int63() % (MaxTotalVotingPower - totalVotingPower)
return val return val
} }
@ -882,7 +883,7 @@ func permutation(valList []testVal) []testVal {
return nil return nil
} }
permList := make([]testVal, len(valList)) permList := make([]testVal, len(valList))
perm := tmrand.Perm(len(valList))
perm := rand.Perm(len(valList))
for i, v := range perm { for i, v := range perm {
permList[v] = valList[i] permList[v] = valList[i]
} }
@ -1284,14 +1285,14 @@ func randTestVSetCfg(t *testing.T, nBase, nAddMax int) testVSetCfg {
const maxPower = 1000 const maxPower = 1000
var nOld, nDel, nChanged, nAdd int var nOld, nDel, nChanged, nAdd int
nOld = int(tmrand.Uint()%uint(nBase)) + 1
nOld = int(uint(rand.Int())%uint(nBase)) + 1
if nBase-nOld > 0 { if nBase-nOld > 0 {
nDel = int(tmrand.Uint() % uint(nBase-nOld))
nDel = int(uint(rand.Int()) % uint(nBase-nOld))
} }
nChanged = nBase - nOld - nDel nChanged = nBase - nOld - nDel
if nAddMax > 0 { if nAddMax > 0 {
nAdd = tmrand.Int()%nAddMax + 1
nAdd = rand.Int()%nAddMax + 1
} }
cfg := testVSetCfg{} cfg := testVSetCfg{}
@ -1303,12 +1304,12 @@ func randTestVSetCfg(t *testing.T, nBase, nAddMax int) testVSetCfg {
cfg.expectedVals = make([]testVal, nBase-nDel+nAdd) cfg.expectedVals = make([]testVal, nBase-nDel+nAdd)
for i := 0; i < nBase; i++ { for i := 0; i < nBase; i++ {
cfg.startVals[i] = testVal{fmt.Sprintf("v%d", i), int64(tmrand.Uint()%maxPower + 1)}
cfg.startVals[i] = testVal{fmt.Sprintf("v%d", i), int64(uint(rand.Int())%maxPower + 1)}
if i < nOld { if i < nOld {
cfg.expectedVals[i] = cfg.startVals[i] cfg.expectedVals[i] = cfg.startVals[i]
} }
if i >= nOld && i < nOld+nChanged { if i >= nOld && i < nOld+nChanged {
cfg.updatedVals[i-nOld] = testVal{fmt.Sprintf("v%d", i), int64(tmrand.Uint()%maxPower + 1)}
cfg.updatedVals[i-nOld] = testVal{fmt.Sprintf("v%d", i), int64(uint(rand.Int())%maxPower + 1)}
cfg.expectedVals[i] = cfg.updatedVals[i-nOld] cfg.expectedVals[i] = cfg.updatedVals[i-nOld]
} }
if i >= nOld+nChanged { if i >= nOld+nChanged {
@ -1317,7 +1318,7 @@ func randTestVSetCfg(t *testing.T, nBase, nAddMax int) testVSetCfg {
} }
for i := nBase; i < nBase+nAdd; i++ { for i := nBase; i < nBase+nAdd; i++ {
cfg.addedVals[i-nBase] = testVal{fmt.Sprintf("v%d", i), int64(tmrand.Uint()%maxPower + 1)}
cfg.addedVals[i-nBase] = testVal{fmt.Sprintf("v%d", i), int64(uint(rand.Int())%maxPower + 1)}
cfg.expectedVals[i-nDel] = cfg.addedVals[i-nBase] cfg.expectedVals[i-nDel] = cfg.addedVals[i-nBase]
} }
@ -1398,7 +1399,7 @@ func TestValSetUpdatePriorityOrderTests(t *testing.T) {
func verifyValSetUpdatePriorityOrder(t *testing.T, valSet *ValidatorSet, cfg testVSetCfg, nMaxElections int32) { func verifyValSetUpdatePriorityOrder(t *testing.T, valSet *ValidatorSet, cfg testVSetCfg, nMaxElections int32) {
// Run election up to nMaxElections times, sort validators by priorities // Run election up to nMaxElections times, sort validators by priorities
valSet.IncrementProposerPriority(tmrand.Int31()%nMaxElections + 1)
valSet.IncrementProposerPriority(rand.Int31()%nMaxElections + 1)
// apply the changes, get the updated validators, sort by priorities // apply the changes, get the updated validators, sort by priorities
applyChangesToValSet(t, nil, valSet, cfg.addedVals, cfg.updatedVals, cfg.deletedVals) applyChangesToValSet(t, nil, valSet, cfg.addedVals, cfg.updatedVals, cfg.deletedVals)


Loading…
Cancel
Save