diff --git a/internal/random/random_util.go b/internal/random/random_util.go index 1928d73ab..0b7981a33 100644 --- a/internal/random/random_util.go +++ b/internal/random/random_util.go @@ -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()) -} diff --git a/pkg/consensus/payload_test.go b/pkg/consensus/payload_test.go index 0e7253967..1237fb71b 100644 --- a/pkg/consensus/payload_test.go +++ b/pkg/consensus/payload_test.go @@ -3,7 +3,7 @@ package consensus import ( "encoding/hex" gio "io" - "math/rand" + "math/rand/v2" "testing" "github.com/nspcc-dev/dbft" diff --git a/pkg/core/interop/runtime/engine_test.go b/pkg/core/interop/runtime/engine_test.go index f5a5fe79c..e1fb12a7d 100644 --- a/pkg/core/interop/runtime/engine_test.go +++ b/pkg/core/interop/runtime/engine_test.go @@ -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) { diff --git a/pkg/core/state/mpt_root_test.go b/pkg/core/state/mpt_root_test.go index b38621497..d7bb94b6e 100644 --- a/pkg/core/state/mpt_root_test.go +++ b/pkg/core/state/mpt_root_test.go @@ -2,7 +2,7 @@ package state import ( "encoding/json" - "math/rand" + "math/rand/v2" "testing" "github.com/nspcc-dev/neo-go/internal/random" diff --git a/pkg/core/state/tokens_test.go b/pkg/core/state/tokens_test.go index 9f8f78671..fa69cd522 100644 --- a/pkg/core/state/tokens_test.go +++ b/pkg/core/state/tokens_test.go @@ -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) diff --git a/pkg/core/transaction/oracle_test.go b/pkg/core/transaction/oracle_test.go index 89c4d392e..bfb4127ab 100644 --- a/pkg/core/transaction/oracle_test.go +++ b/pkg/core/transaction/oracle_test.go @@ -2,7 +2,7 @@ package transaction import ( "encoding/json" - "math/rand" + "math/rand/v2" "testing" "github.com/nspcc-dev/neo-go/internal/testserdes" diff --git a/pkg/core/transaction/transaction.go b/pkg/core/transaction/transaction.go index a11285e42..58d63a0a6 100644 --- a/pkg/core/transaction/transaction.go +++ b/pkg/core/transaction/transaction.go @@ -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" diff --git a/pkg/crypto/hash/merkle_bench_test.go b/pkg/crypto/hash/merkle_bench_test.go index 1cf442fba..1a979a510 100644 --- a/pkg/crypto/hash/merkle_bench_test.go +++ b/pkg/crypto/hash/merkle_bench_test.go @@ -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) } }) } diff --git a/pkg/network/discovery.go b/pkg/network/discovery.go index 5659ee6c0..b48d7682f 100644 --- a/pkg/network/discovery.go +++ b/pkg/network/discovery.go @@ -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) diff --git a/pkg/network/fuzz_test.go b/pkg/network/fuzz_test.go index 7a5e89d79..badf0b1fc 100644 --- a/pkg/network/fuzz_test.go +++ b/pkg/network/fuzz_test.go @@ -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) } diff --git a/pkg/network/message_test.go b/pkg/network/message_test.go index 57cd82d75..f78537d0b 100644 --- a/pkg/network/message_test.go +++ b/pkg/network/message_test.go @@ -2,7 +2,7 @@ package network import ( "errors" - "math/rand" + "math/rand/v2" "testing" "time" diff --git a/pkg/network/server.go b/pkg/network/server.go index 556ac4e37..eff8819ea 100644 --- a/pkg/network/server.go +++ b/pkg/network/server.go @@ -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 diff --git a/pkg/services/notary/core_test.go b/pkg/services/notary/core_test.go index 57ad22526..a51b4c40c 100644 --- a/pkg/services/notary/core_test.go +++ b/pkg/services/notary/core_test.go @@ -4,7 +4,7 @@ import ( "errors" "fmt" "math/big" - "math/rand" + "math/rand/v2" "sync" "testing" "time" diff --git a/pkg/vm/vm_test.go b/pkg/vm/vm_test.go index e82b78eca..62098f6e4 100644 --- a/pkg/vm/vm_test.go +++ b/pkg/vm/vm_test.go @@ -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 }