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

Remove most of libs/rand #284

Merged
merged 28 commits into from
Apr 21, 2021
Merged
Show file tree
Hide file tree
Changes from 23 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
0504b9d
Remove unused Time and Int16
liamsi Apr 16, 2021
fb74903
remove Uint which was just a cast and remove some tmrand calls on the…
liamsi Apr 16, 2021
1b498f8
remove Uint16
liamsi Apr 16, 2021
b099de4
remove Bool
liamsi Apr 16, 2021
781f6c8
remove Time on Rand as well
liamsi Apr 16, 2021
59c145c
Use math/rand for Bytes()
liamsi Apr 16, 2021
9af911e
remove Float32
liamsi Apr 16, 2021
9b8bd0f
Cleanup the remaining places
liamsi Apr 16, 2021
4833314
Review feedback: add clarifying comment
liamsi Apr 17, 2021
66e4710
Add clarifying comment that NewRand isn't safe for concurrent use
liamsi Apr 17, 2021
e6c824e
Add Reseed method as a convenient way to reseed math/rand's default S…
liamsi Apr 17, 2021
ee49c68
Use math/rand's thread-safe Source in pex instead
liamsi Apr 17, 2021
63823df
Reseed math/rand's source for p2p.Switch
liamsi Apr 17, 2021
01bae27
make it explicit that the usages of math/rand and tmrand are intentio…
liamsi Apr 17, 2021
8922c21
add a few missed nolints
liamsi Apr 17, 2021
b5b94b7
panic in the almost impossible case that we don't have enough entropy…
liamsi Apr 17, 2021
c14146e
remaining lints
liamsi Apr 17, 2021
7495931
actually appease the linter
liamsi Apr 17, 2021
8c0aeb3
actually appease the linter 2.0
liamsi Apr 17, 2021
36b44fb
fix merge glitches
liamsi Apr 17, 2021
d604163
fix test & lint glichtes (while merging)
liamsi Apr 17, 2021
0767632
fix another test
liamsi Apr 17, 2021
e81881c
relax timeouts here too
liamsi Apr 17, 2021
a910e47
Update p2p/switch.go
liamsi Apr 19, 2021
035711c
Apply review feedback: use mrand.Uint32() + 1 for height
liamsi Apr 19, 2021
edc9cb3
go fmt
liamsi Apr 19, 2021
e70c207
Update comment to reflect current state of Bytes/Str functions
liamsi Apr 19, 2021
bb3bcbe
Merge remote-tracking branch 'origin/master' into ismail/ll/remove_li…
liamsi Apr 21, 2021
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
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