mirror of
https://github.com/nspcc-dev/neo-go.git
synced 2024-12-22 09:19:08 +00:00
*: 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:
parent
a50723ff72
commit
8f45d57612
14 changed files with 33 additions and 46 deletions
|
@ -1,8 +1,7 @@
|
|||
package random
|
||||
|
||||
import (
|
||||
"math/rand"
|
||||
"time"
|
||||
"math/rand/v2"
|
||||
|
||||
"github.com/nspcc-dev/neo-go/pkg/crypto/hash"
|
||||
"github.com/nspcc-dev/neo-go/pkg/util"
|
||||
|
@ -27,14 +26,14 @@ func Bytes(n int) []byte {
|
|||
|
||||
// Fill fills buffer with random bytes.
|
||||
func Fill(buf []byte) {
|
||||
r := rand.New(rand.NewSource(time.Now().UnixNano()))
|
||||
// Rand reader returns no errors
|
||||
r.Read(buf)
|
||||
for i := range buf {
|
||||
buf[i] = byte(rand.Int())
|
||||
}
|
||||
}
|
||||
|
||||
// Int returns a random integer in [minI,maxI).
|
||||
func Int(minI, maxI int) int {
|
||||
return minI + rand.Intn(maxI-minI)
|
||||
return minI + rand.IntN(maxI-minI)
|
||||
}
|
||||
|
||||
// Uint256 returns a random Uint256.
|
||||
|
@ -48,8 +47,3 @@ func Uint160() util.Uint160 {
|
|||
str := String(20)
|
||||
return hash.RipeMD160([]byte(str))
|
||||
}
|
||||
|
||||
func init() {
|
||||
//nolint:staticcheck
|
||||
rand.Seed(time.Now().UTC().UnixNano())
|
||||
}
|
||||
|
|
|
@ -3,7 +3,7 @@ package consensus
|
|||
import (
|
||||
"encoding/hex"
|
||||
gio "io"
|
||||
"math/rand"
|
||||
"math/rand/v2"
|
||||
"testing"
|
||||
|
||||
"github.com/nspcc-dev/dbft"
|
||||
|
|
|
@ -3,7 +3,6 @@ package runtime
|
|||
import (
|
||||
"encoding/json"
|
||||
"math/big"
|
||||
"math/rand"
|
||||
"testing"
|
||||
|
||||
"github.com/nspcc-dev/neo-go/internal/random"
|
||||
|
@ -46,10 +45,10 @@ func TestPlatform(t *testing.T) {
|
|||
|
||||
func TestGetTime(t *testing.T) {
|
||||
b := block.New(false)
|
||||
b.Timestamp = rand.Uint64()
|
||||
b.Timestamp = 1725021259
|
||||
ic := &interop.Context{VM: vm.New(), Block: b}
|
||||
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) {
|
||||
|
|
|
@ -2,7 +2,7 @@ package state
|
|||
|
||||
import (
|
||||
"encoding/json"
|
||||
"math/rand"
|
||||
"math/rand/v2"
|
||||
"testing"
|
||||
|
||||
"github.com/nspcc-dev/neo-go/internal/random"
|
||||
|
|
|
@ -2,9 +2,8 @@ package state
|
|||
|
||||
import (
|
||||
"math/big"
|
||||
"math/rand"
|
||||
"math/rand/v2"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/nspcc-dev/neo-go/internal/random"
|
||||
"github.com/nspcc-dev/neo-go/internal/testserdes"
|
||||
|
@ -13,7 +12,7 @@ import (
|
|||
)
|
||||
|
||||
func TestTokenTransferLog_Append17(t *testing.T) {
|
||||
r := rand.New(rand.NewSource(time.Now().UnixNano()))
|
||||
r := rand.New(rand.NewPCG(42, 100500))
|
||||
expected := []*NEP17Transfer{
|
||||
random17Transfer(r),
|
||||
random17Transfer(r),
|
||||
|
@ -39,7 +38,7 @@ func TestTokenTransferLog_Append17(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{
|
||||
random11Transfer(r),
|
||||
random11Transfer(r),
|
||||
|
@ -65,7 +64,7 @@ func TestTokenTransferLog_Append(t *testing.T) {
|
|||
}
|
||||
|
||||
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)
|
||||
for i := range ts {
|
||||
ts[i] = random17Transfer(r)
|
||||
|
|
|
@ -2,7 +2,7 @@ package transaction
|
|||
|
||||
import (
|
||||
"encoding/json"
|
||||
"math/rand"
|
||||
"math/rand/v2"
|
||||
"testing"
|
||||
|
||||
"github.com/nspcc-dev/neo-go/internal/testserdes"
|
||||
|
|
|
@ -8,7 +8,7 @@ import (
|
|||
"fmt"
|
||||
"math"
|
||||
"math/big"
|
||||
"math/rand"
|
||||
"math/rand/v2"
|
||||
|
||||
"github.com/nspcc-dev/neo-go/pkg/crypto/hash"
|
||||
"github.com/nspcc-dev/neo-go/pkg/encoding/address"
|
||||
|
|
|
@ -1,29 +1,24 @@
|
|||
package hash
|
||||
package hash_test
|
||||
|
||||
import (
|
||||
"math/rand"
|
||||
"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/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func BenchmarkMerkle(t *testing.B) {
|
||||
var err error
|
||||
var hashes = make([]util.Uint256, 100000)
|
||||
var h = make([]byte, 32)
|
||||
r := rand.New(rand.NewSource(time.Now().UnixNano()))
|
||||
for i := range hashes {
|
||||
r.Read(h)
|
||||
hashes[i], err = util.Uint256DecodeBytesBE(h)
|
||||
require.NoError(t, err)
|
||||
hashes[i] = random.Uint256()
|
||||
}
|
||||
|
||||
t.Run("NewMerkleTree", func(t *testing.B) {
|
||||
t.ResetTimer()
|
||||
for n := 0; n < t.N; n++ {
|
||||
tr, err := NewMerkleTree(hashes)
|
||||
tr, err := hash.NewMerkleTree(hashes)
|
||||
require.NoError(t, err)
|
||||
_ = tr.Root()
|
||||
}
|
||||
|
@ -31,7 +26,7 @@ func BenchmarkMerkle(t *testing.B) {
|
|||
t.Run("CalcMerkleRoot", func(t *testing.B) {
|
||||
t.ResetTimer()
|
||||
for n := 0; n < t.N; n++ {
|
||||
_ = CalcMerkleRoot(hashes)
|
||||
_ = hash.CalcMerkleRoot(hashes)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
|
|
@ -2,7 +2,7 @@ package network
|
|||
|
||||
import (
|
||||
"math"
|
||||
"math/rand"
|
||||
"math/rand/v2"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
|
@ -300,7 +300,7 @@ func (d *DefaultDiscovery) updateNetSize() {
|
|||
}
|
||||
|
||||
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.
|
||||
p, err := d.transport.Dial(addr, d.dialTimeout)
|
||||
atomic.AddInt32(&d.outstanding, -1)
|
||||
|
|
|
@ -1,18 +1,18 @@
|
|||
package network
|
||||
|
||||
import (
|
||||
"math/rand"
|
||||
"math/rand/v2"
|
||||
"testing"
|
||||
|
||||
"github.com/nspcc-dev/neo-go/internal/random"
|
||||
"github.com/nspcc-dev/neo-go/pkg/io"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func FuzzMessageDecode(f *testing.F) {
|
||||
for i := 0; i < 100; i++ {
|
||||
seed := make([]byte, rand.Uint32()%1000)
|
||||
//nolint:staticcheck
|
||||
rand.Read(seed)
|
||||
seed := make([]byte, rand.IntN(1000))
|
||||
random.Fill(seed)
|
||||
f.Add(seed)
|
||||
}
|
||||
|
||||
|
|
|
@ -2,7 +2,7 @@ package network
|
|||
|
||||
import (
|
||||
"errors"
|
||||
"math/rand"
|
||||
"math/rand/v2"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
|
|
|
@ -7,7 +7,7 @@ import (
|
|||
"errors"
|
||||
"fmt"
|
||||
"math/big"
|
||||
mrand "math/rand"
|
||||
mrand "math/rand/v2"
|
||||
"net"
|
||||
"runtime"
|
||||
"slices"
|
||||
|
@ -1331,7 +1331,7 @@ func getRequestBlocksPayload(p Peer, currHeight uint32, lastRequestedHeight *ato
|
|||
}
|
||||
}
|
||||
} else {
|
||||
index := mrand.Intn(bqueue.CacheSize / payload.MaxHashesCount)
|
||||
index := mrand.IntN(bqueue.CacheSize / payload.MaxHashesCount)
|
||||
needHeight = currHeight + 1 + uint32(index*payload.MaxHashesCount)
|
||||
}
|
||||
break
|
||||
|
|
|
@ -4,7 +4,7 @@ import (
|
|||
"errors"
|
||||
"fmt"
|
||||
"math/big"
|
||||
"math/rand"
|
||||
"math/rand/v2"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
|
|
|
@ -8,7 +8,7 @@ import (
|
|||
"fmt"
|
||||
"math"
|
||||
"math/big"
|
||||
"math/rand"
|
||||
"math/rand/v2"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
|
@ -2842,7 +2842,7 @@ func randomBytes(n int) []byte {
|
|||
const charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
||||
b := make([]byte, n)
|
||||
for i := range b {
|
||||
b[i] = charset[rand.Intn(len(charset))]
|
||||
b[i] = charset[rand.IntN(len(charset))]
|
||||
}
|
||||
return b
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue