*: stop using math/rand

Mostly this switches to math/rand/v2, but sometimes randomness is not really needed.

Signed-off-by: Roman Khimov <roman@nspcc.ru>
This commit is contained in:
Roman Khimov 2024-08-30 15:48:13 +03:00
parent a50723ff72
commit 8f45d57612
14 changed files with 33 additions and 46 deletions

View file

@ -1,8 +1,7 @@
package random package random
import ( import (
"math/rand" "math/rand/v2"
"time"
"github.com/nspcc-dev/neo-go/pkg/crypto/hash" "github.com/nspcc-dev/neo-go/pkg/crypto/hash"
"github.com/nspcc-dev/neo-go/pkg/util" "github.com/nspcc-dev/neo-go/pkg/util"
@ -27,14 +26,14 @@ func Bytes(n int) []byte {
// Fill fills buffer with random bytes. // Fill fills buffer with random bytes.
func Fill(buf []byte) { func Fill(buf []byte) {
r := rand.New(rand.NewSource(time.Now().UnixNano())) for i := range buf {
// Rand reader returns no errors buf[i] = byte(rand.Int())
r.Read(buf) }
} }
// Int returns a random integer in [minI,maxI). // Int returns a random integer in [minI,maxI).
func Int(minI, maxI int) int { func Int(minI, maxI int) int {
return minI + rand.Intn(maxI-minI) return minI + rand.IntN(maxI-minI)
} }
// Uint256 returns a random Uint256. // Uint256 returns a random Uint256.
@ -48,8 +47,3 @@ func Uint160() util.Uint160 {
str := String(20) str := String(20)
return hash.RipeMD160([]byte(str)) return hash.RipeMD160([]byte(str))
} }
func init() {
//nolint:staticcheck
rand.Seed(time.Now().UTC().UnixNano())
}

View file

@ -3,7 +3,7 @@ package consensus
import ( import (
"encoding/hex" "encoding/hex"
gio "io" gio "io"
"math/rand" "math/rand/v2"
"testing" "testing"
"github.com/nspcc-dev/dbft" "github.com/nspcc-dev/dbft"

View file

@ -3,7 +3,6 @@ package runtime
import ( import (
"encoding/json" "encoding/json"
"math/big" "math/big"
"math/rand"
"testing" "testing"
"github.com/nspcc-dev/neo-go/internal/random" "github.com/nspcc-dev/neo-go/internal/random"
@ -46,10 +45,10 @@ func TestPlatform(t *testing.T) {
func TestGetTime(t *testing.T) { func TestGetTime(t *testing.T) {
b := block.New(false) b := block.New(false)
b.Timestamp = rand.Uint64() b.Timestamp = 1725021259
ic := &interop.Context{VM: vm.New(), Block: b} ic := &interop.Context{VM: vm.New(), Block: b}
require.NoError(t, GetTime(ic)) require.NoError(t, GetTime(ic))
checkStack(t, ic.VM, new(big.Int).SetUint64(b.Timestamp)) checkStack(t, ic.VM, new(big.Int).SetUint64(1725021259))
} }
func TestGetScriptHash(t *testing.T) { func TestGetScriptHash(t *testing.T) {

View file

@ -2,7 +2,7 @@ package state
import ( import (
"encoding/json" "encoding/json"
"math/rand" "math/rand/v2"
"testing" "testing"
"github.com/nspcc-dev/neo-go/internal/random" "github.com/nspcc-dev/neo-go/internal/random"

View file

@ -2,9 +2,8 @@ package state
import ( import (
"math/big" "math/big"
"math/rand" "math/rand/v2"
"testing" "testing"
"time"
"github.com/nspcc-dev/neo-go/internal/random" "github.com/nspcc-dev/neo-go/internal/random"
"github.com/nspcc-dev/neo-go/internal/testserdes" "github.com/nspcc-dev/neo-go/internal/testserdes"
@ -13,7 +12,7 @@ import (
) )
func TestTokenTransferLog_Append17(t *testing.T) { func TestTokenTransferLog_Append17(t *testing.T) {
r := rand.New(rand.NewSource(time.Now().UnixNano())) r := rand.New(rand.NewPCG(42, 100500))
expected := []*NEP17Transfer{ expected := []*NEP17Transfer{
random17Transfer(r), random17Transfer(r),
random17Transfer(r), random17Transfer(r),
@ -39,7 +38,7 @@ func TestTokenTransferLog_Append17(t *testing.T) {
} }
func TestTokenTransferLog_Append(t *testing.T) { func TestTokenTransferLog_Append(t *testing.T) {
r := rand.New(rand.NewSource(time.Now().UnixNano())) r := rand.New(rand.NewPCG(42, 100500))
expected := []*NEP11Transfer{ expected := []*NEP11Transfer{
random11Transfer(r), random11Transfer(r),
random11Transfer(r), random11Transfer(r),
@ -65,7 +64,7 @@ func TestTokenTransferLog_Append(t *testing.T) {
} }
func BenchmarkTokenTransferLog_Append(b *testing.B) { func BenchmarkTokenTransferLog_Append(b *testing.B) {
r := rand.New(rand.NewSource(time.Now().UnixNano())) r := rand.New(rand.NewPCG(42, 100500))
ts := make([]*NEP17Transfer, TokenTransferBatchSize) ts := make([]*NEP17Transfer, TokenTransferBatchSize)
for i := range ts { for i := range ts {
ts[i] = random17Transfer(r) ts[i] = random17Transfer(r)

View file

@ -2,7 +2,7 @@ package transaction
import ( import (
"encoding/json" "encoding/json"
"math/rand" "math/rand/v2"
"testing" "testing"
"github.com/nspcc-dev/neo-go/internal/testserdes" "github.com/nspcc-dev/neo-go/internal/testserdes"

View file

@ -8,7 +8,7 @@ import (
"fmt" "fmt"
"math" "math"
"math/big" "math/big"
"math/rand" "math/rand/v2"
"github.com/nspcc-dev/neo-go/pkg/crypto/hash" "github.com/nspcc-dev/neo-go/pkg/crypto/hash"
"github.com/nspcc-dev/neo-go/pkg/encoding/address" "github.com/nspcc-dev/neo-go/pkg/encoding/address"

View file

@ -1,29 +1,24 @@
package hash package hash_test
import ( import (
"math/rand"
"testing" "testing"
"time"
"github.com/nspcc-dev/neo-go/internal/random"
"github.com/nspcc-dev/neo-go/pkg/crypto/hash"
"github.com/nspcc-dev/neo-go/pkg/util" "github.com/nspcc-dev/neo-go/pkg/util"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
) )
func BenchmarkMerkle(t *testing.B) { func BenchmarkMerkle(t *testing.B) {
var err error
var hashes = make([]util.Uint256, 100000) var hashes = make([]util.Uint256, 100000)
var h = make([]byte, 32)
r := rand.New(rand.NewSource(time.Now().UnixNano()))
for i := range hashes { for i := range hashes {
r.Read(h) hashes[i] = random.Uint256()
hashes[i], err = util.Uint256DecodeBytesBE(h)
require.NoError(t, err)
} }
t.Run("NewMerkleTree", func(t *testing.B) { t.Run("NewMerkleTree", func(t *testing.B) {
t.ResetTimer() t.ResetTimer()
for n := 0; n < t.N; n++ { for n := 0; n < t.N; n++ {
tr, err := NewMerkleTree(hashes) tr, err := hash.NewMerkleTree(hashes)
require.NoError(t, err) require.NoError(t, err)
_ = tr.Root() _ = tr.Root()
} }
@ -31,7 +26,7 @@ func BenchmarkMerkle(t *testing.B) {
t.Run("CalcMerkleRoot", func(t *testing.B) { t.Run("CalcMerkleRoot", func(t *testing.B) {
t.ResetTimer() t.ResetTimer()
for n := 0; n < t.N; n++ { for n := 0; n < t.N; n++ {
_ = CalcMerkleRoot(hashes) _ = hash.CalcMerkleRoot(hashes)
} }
}) })
} }

View file

@ -2,7 +2,7 @@ package network
import ( import (
"math" "math"
"math/rand" "math/rand/v2"
"sync" "sync"
"sync/atomic" "sync/atomic"
"time" "time"
@ -300,7 +300,7 @@ func (d *DefaultDiscovery) updateNetSize() {
} }
func (d *DefaultDiscovery) tryAddress(addr string) { func (d *DefaultDiscovery) tryAddress(addr string) {
var tout = rand.Int63n(int64(tryMaxWait)) var tout = rand.Int64N(int64(tryMaxWait))
time.Sleep(time.Duration(tout)) // Have a sleep before working hard. time.Sleep(time.Duration(tout)) // Have a sleep before working hard.
p, err := d.transport.Dial(addr, d.dialTimeout) p, err := d.transport.Dial(addr, d.dialTimeout)
atomic.AddInt32(&d.outstanding, -1) atomic.AddInt32(&d.outstanding, -1)

View file

@ -1,18 +1,18 @@
package network package network
import ( import (
"math/rand" "math/rand/v2"
"testing" "testing"
"github.com/nspcc-dev/neo-go/internal/random"
"github.com/nspcc-dev/neo-go/pkg/io" "github.com/nspcc-dev/neo-go/pkg/io"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
) )
func FuzzMessageDecode(f *testing.F) { func FuzzMessageDecode(f *testing.F) {
for i := 0; i < 100; i++ { for i := 0; i < 100; i++ {
seed := make([]byte, rand.Uint32()%1000) seed := make([]byte, rand.IntN(1000))
//nolint:staticcheck random.Fill(seed)
rand.Read(seed)
f.Add(seed) f.Add(seed)
} }

View file

@ -2,7 +2,7 @@ package network
import ( import (
"errors" "errors"
"math/rand" "math/rand/v2"
"testing" "testing"
"time" "time"

View file

@ -7,7 +7,7 @@ import (
"errors" "errors"
"fmt" "fmt"
"math/big" "math/big"
mrand "math/rand" mrand "math/rand/v2"
"net" "net"
"runtime" "runtime"
"slices" "slices"
@ -1331,7 +1331,7 @@ func getRequestBlocksPayload(p Peer, currHeight uint32, lastRequestedHeight *ato
} }
} }
} else { } else {
index := mrand.Intn(bqueue.CacheSize / payload.MaxHashesCount) index := mrand.IntN(bqueue.CacheSize / payload.MaxHashesCount)
needHeight = currHeight + 1 + uint32(index*payload.MaxHashesCount) needHeight = currHeight + 1 + uint32(index*payload.MaxHashesCount)
} }
break break

View file

@ -4,7 +4,7 @@ import (
"errors" "errors"
"fmt" "fmt"
"math/big" "math/big"
"math/rand" "math/rand/v2"
"sync" "sync"
"testing" "testing"
"time" "time"

View file

@ -8,7 +8,7 @@ import (
"fmt" "fmt"
"math" "math"
"math/big" "math/big"
"math/rand" "math/rand/v2"
"strings" "strings"
"testing" "testing"
@ -2842,7 +2842,7 @@ func randomBytes(n int) []byte {
const charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" const charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
b := make([]byte, n) b := make([]byte, n)
for i := range b { for i := range b {
b[i] = charset[rand.Intn(len(charset))] b[i] = charset[rand.IntN(len(charset))]
} }
return b return b
} }