Skip to content

Commit

Permalink
Merge pull request #284 from lazyledger/ismail/ll/remove_libs-rand
Browse files Browse the repository at this point in the history
Remove most of libs/rand
  • Loading branch information
liamsi authored Apr 21, 2021
2 parents fe9a56c + bb3bcbe commit 874df76
Show file tree
Hide file tree
Showing 27 changed files with 165 additions and 433 deletions.
5 changes: 4 additions & 1 deletion abci/example/kvstore/helpers.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package kvstore

import (
mrand "math/rand"

"github.com/lazyledger/lazyledger-core/abci/types"
tmrand "github.com/lazyledger/lazyledger-core/libs/rand"
)
Expand All @@ -9,7 +11,8 @@ import (
// from the input value
func RandVal(i int) types.ValidatorUpdate {
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), "")
return v
}
Expand Down
4 changes: 3 additions & 1 deletion abci/tests/server/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"context"
"errors"
"fmt"
mrand "math/rand"

abcicli "github.com/lazyledger/lazyledger-core/abci/client"
"github.com/lazyledger/lazyledger-core/abci/types"
Expand All @@ -18,7 +19,8 @@ func InitChain(client abcicli.Client) error {
vals := make([]types.ValidatorUpdate, total)
for i := 0; i < total; i++ {
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), "")
}
_, err := client.InitChainSync(ctx, types.RequestInitChain{
Expand Down
5 changes: 3 additions & 2 deletions blockchain/v0/pool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package v0

import (
"fmt"
mrand "math/rand"
"testing"
"time"

Expand Down Expand Up @@ -45,7 +46,7 @@ func (p testPeer) simulateInput(input inputData) {
input.pool.AddBlock(input.request.PeerID, block, 123)
// TODO: uncommenting this creates a race which is detected by:
// https://github.com/golang/go/blob/2bd767b1022dd3254bcec469f0ee164024726486/src/testing/testing.go#L854-L856
// see: https://github.comlazyledger/lazyledger-cor/issues/3390#issue-418379890
// see: https://github.com/tendermint/tendermint/issues/3390#issue-418379890
// input.t.Logf("Added block from peer %v (height: %v)", input.request.PeerID, input.request.Height)
}

Expand All @@ -67,7 +68,7 @@ func makePeers(numPeers int, minHeight, maxHeight int64) testPeers {
peers := make(testPeers, numPeers)
for i := 0; i < numPeers; i++ {
peerID := p2p.ID(tmrand.Str(12))
height := minHeight + tmrand.Int63n(maxHeight-minHeight)
height := minHeight + mrand.Int63n(maxHeight-minHeight)
base := minHeight + int64(i)
if base > height {
base = height
Expand Down
7 changes: 4 additions & 3 deletions consensus/wal_generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"bytes"
"fmt"
"io"
mrand "math/rand"
"path/filepath"
"testing"
"time"
Expand All @@ -13,7 +14,6 @@ import (
cfg "github.com/lazyledger/lazyledger-core/config"
"github.com/lazyledger/lazyledger-core/libs/db/memdb"
"github.com/lazyledger/lazyledger-core/libs/log"
tmrand "github.com/lazyledger/lazyledger-core/libs/rand"
"github.com/lazyledger/lazyledger-core/privval"
"github.com/lazyledger/lazyledger-core/proxy"
sm "github.com/lazyledger/lazyledger-core/state"
Expand Down Expand Up @@ -88,7 +88,7 @@ func WALGenerateNBlocks(t *testing.T, wr io.Writer, numBlocks int) (err error) {
consensusState := NewState(config.Consensus, state.Copy(), blockExec, blockStore, mempool, evpool)
consensusState.SetLogger(logger)
consensusState.SetEventBus(eventBus)
if privValidator != nil {
if privValidator != nil && privValidator != (*privval.FilePV)(nil) {
consensusState.SetPrivValidator(privValidator)
}
// END OF COPY PASTE
Expand Down Expand Up @@ -137,7 +137,8 @@ func WALWithNBlocks(t *testing.T, numBlocks int) (data []byte, err error) {
func randPort() int {
// returns between base and base + spread
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) {
Expand Down
9 changes: 6 additions & 3 deletions libs/bits/bit_array.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
"fmt"
"math"
mrand "math/rand"
"regexp"
"strings"
"sync"
Expand All @@ -24,6 +25,8 @@ type BitArray struct {
// NewBitArray returns a new bit array.
// It returns nil if the number of bits is zero.
func NewBitArray(bits int) *BitArray {
// Reseed non-deterministically.
tmrand.Reseed()
if bits <= 0 {
return nil
}
Expand Down Expand Up @@ -242,7 +245,7 @@ func (bA *BitArray) IsFull() bool {

// PickRandom returns a random index for a set bit in the bit array.
// 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) {
if bA == nil {
return 0, false
Expand All @@ -255,8 +258,8 @@ func (bA *BitArray) PickRandom() (int, bool) {
if len(trueIndices) == 0 { // no bits set to true
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 {
Expand Down
9 changes: 4 additions & 5 deletions libs/clist/clist_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@ package clist

import (
"fmt"
mrand "math/rand"
"runtime"
"sync/atomic"
"testing"
"time"

"github.com/stretchr/testify/assert"

tmrand "github.com/lazyledger/lazyledger-core/libs/rand"
)

func TestPanicOnMaxLength(t *testing.T) {
Expand Down Expand Up @@ -148,7 +147,7 @@ func _TestGCRandom(t *testing.T) {
els = append(els, el)
}

for _, i := range tmrand.Perm(numElements) {
for _, i := range mrand.Perm(numElements) {
el := els[i]
l.Remove(el)
_ = el.Next()
Expand Down Expand Up @@ -206,7 +205,7 @@ func TestScanRightDeleteRandom(t *testing.T) {
// Remove an element, push back an element.
for i := 0; i < numTimes; i++ {
// Pick an element to remove
rmElIdx := tmrand.Intn(len(els))
rmElIdx := mrand.Intn(len(els))
rmEl := els[rmElIdx]

// Remove it
Expand Down Expand Up @@ -260,7 +259,7 @@ func TestWaitChan(t *testing.T) {
for i := 1; i < 100; i++ {
l.PushBack(i)
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
time.Sleep(25 * time.Millisecond)
Expand Down
Loading

0 comments on commit 874df76

Please sign in to comment.