Skip to content

Commit

Permalink
use math/rand/v2 package
Browse files Browse the repository at this point in the history
  • Loading branch information
libotony committed Oct 8, 2024
1 parent 48cead2 commit cb55e6b
Show file tree
Hide file tree
Showing 30 changed files with 62 additions and 88 deletions.
6 changes: 0 additions & 6 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ linters:
- durationcheck
- copyloopvar
- whitespace
- gosec
- revive

# - structcheck # lots of false positives
Expand All @@ -39,11 +38,6 @@ linters:
linters-settings:
gofmt:
simplify: true
gosec:
excludes:
- G115
- G406 # ignore ripe160 deprecation
- G507 # ignore ripe160 deprecation
revive:
rules:
- name: var-naming
Expand Down
4 changes: 2 additions & 2 deletions api/accounts/accounts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -543,7 +543,7 @@ func httpPost(t *testing.T, url string, body interface{}) ([]byte, int) {
if err != nil {
t.Fatal(err)
}
res, err := http.Post(url, "application/x-www-form-urlencoded", bytes.NewReader(data)) // nolint:gosec
res, err := http.Post(url, "application/x-www-form-urlencoded", bytes.NewReader(data))
if err != nil {
t.Fatal(err)
}
Expand All @@ -556,7 +556,7 @@ func httpPost(t *testing.T, url string, body interface{}) ([]byte, int) {
}

func httpGet(t *testing.T, url string) ([]byte, int) {
res, err := http.Get(url) // nolint:gosec
res, err := http.Get(url)
if err != nil {
t.Fatal(err)
}
Expand Down
2 changes: 1 addition & 1 deletion api/blocks/blocks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ func checkExpandedBlock(t *testing.T, expBl *block.Block, actBl *blocks.JSONExpa
}

func httpGet(t *testing.T, url string) ([]byte, int) {
res, err := http.Get(url) // nolint:gosec
res, err := http.Get(url)
if err != nil {
t.Fatal(err)
}
Expand Down
2 changes: 1 addition & 1 deletion api/debug/debug_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -597,7 +597,7 @@ func httpPostAndCheckResponseStatus(t *testing.T, url string, obj interface{}, r
if err != nil {
t.Fatal(err)
}
res, err := http.Post(url, "application/x-www-form-urlencoded", bytes.NewReader(data)) // nolint:gosec
res, err := http.Post(url, "application/x-www-form-urlencoded", bytes.NewReader(data))
if err != nil {
t.Fatal(err)
}
Expand Down
2 changes: 1 addition & 1 deletion api/events/events_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ func httpPost(t *testing.T, url string, body interface{}) ([]byte, int) {
if err != nil {
t.Fatal(err)
}
res, err := http.Post(url, "application/x-www-form-urlencoded", bytes.NewReader(data)) // nolint:gosec
res, err := http.Post(url, "application/x-www-form-urlencoded", bytes.NewReader(data))
if err != nil {
t.Fatal(err)
}
Expand Down
2 changes: 1 addition & 1 deletion api/metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ func TestWebsocketMetrics(t *testing.T) {
}

func httpGet(t *testing.T, url string) ([]byte, int) {
res, err := http.Get(url) // nolint:gosec
res, err := http.Get(url)
if err != nil {
t.Fatal(err)
}
Expand Down
2 changes: 1 addition & 1 deletion api/node/node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func initCommServer(t *testing.T) {
}

func httpGet(t *testing.T, url string) []byte {
res, err := http.Get(url) // nolint:gosec
res, err := http.Get(url)
if err != nil {
t.Fatal(err)
}
Expand Down
4 changes: 2 additions & 2 deletions api/transactions/transactions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ func httpPostAndCheckResponseStatus(t *testing.T, url string, obj interface{}, r
if err != nil {
t.Fatal(err)
}
res, err := http.Post(url, "application/x-www-form-urlencoded", bytes.NewReader(data)) // nolint: gosec
res, err := http.Post(url, "application/x-www-form-urlencoded", bytes.NewReader(data))
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -371,7 +371,7 @@ func checkMatchingTx(t *testing.T, expectedTx *tx.Transaction, actualTx *transac
}

func httpGetAndCheckResponseStatus(t *testing.T, url string, responseStatusCode int) []byte {
res, err := http.Get(url) // nolint:gosec
res, err := http.Get(url)
if err != nil {
t.Fatal(err)
}
Expand Down
2 changes: 1 addition & 1 deletion api/transfers/transfers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ func httpPost(t *testing.T, url string, body interface{}) ([]byte, int) {
if err != nil {
t.Fatal(err)
}
res, err := http.Post(url, "application/x-www-form-urlencoded", bytes.NewReader(data)) // nolint: gosec
res, err := http.Post(url, "application/x-www-form-urlencoded", bytes.NewReader(data))
if err != nil {
t.Fatal(err)
}
Expand Down
12 changes: 6 additions & 6 deletions block/header_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
package block

import (
"math/rand"
"crypto/rand"
"sync/atomic"
"testing"

Expand Down Expand Up @@ -86,8 +86,8 @@ func TestHeaderEncoding(t *testing.T) {

var proof [81]byte
var alpha [32]byte
rand.Read(proof[:]) // nolint
rand.Read(alpha[:]) // nolint
rand.Read(proof[:])
rand.Read(alpha[:])

cplx, err := NewComplexSignature(sig[:], proof[:])
if err != nil {
Expand All @@ -110,7 +110,7 @@ func TestHeaderEncoding(t *testing.T) {
// type extension struct{Alpha []byte}
func TestEncodingBadExtension(t *testing.T) {
var sig [65]byte
rand.Read(sig[:]) // nolint
rand.Read(sig[:])

block := new(Builder).Build().WithSignature(sig[:])
h := block.Header()
Expand Down Expand Up @@ -157,8 +157,8 @@ func TestEncodingBadExtension(t *testing.T) {
func TestEncodingExtension(t *testing.T) {
var sig [ComplexSigSize]byte
var alpha [32]byte
rand.Read(sig[:]) // nolint
rand.Read(alpha[:]) // nolint
rand.Read(sig[:])
rand.Read(alpha[:])

block := new(Builder).Alpha(alpha[:]).Build().WithSignature(sig[:])
h := block.Header()
Expand Down
10 changes: 4 additions & 6 deletions cache/prio_cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@
package cache_test

import (
"math/rand"
"math/rand/v2"
"sort"
"testing"
"time"

"github.com/stretchr/testify/assert"
"github.com/vechain/thor/v2/cache"
Expand All @@ -35,7 +34,6 @@ func TestPrioCacheAddRemove(t *testing.T) {

func TestPrioCache(t *testing.T) {
c := cache.NewPrioCache(5)
rand.Seed(time.Now().UnixNano()) // nolint:staticcheck

type kvp struct {
k, v int
Expand All @@ -46,9 +44,9 @@ func TestPrioCache(t *testing.T) {

for i := 0; i < 100; i++ {
e := kvp{
rand.Int(), // nolint: gosec
rand.Int(), // nolint:gosec
rand.Float64()} // nolint:gosec
rand.Int(), // #nosec
rand.Int(), // #nosec
rand.Float64()} // #nosec
kvps = append(kvps, e)
c.Set(e.k, e.v, e.p)
}
Expand Down
11 changes: 3 additions & 8 deletions cache/rnd_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,10 @@
package cache

import (
"math/rand"
"math/rand/v2"
"sync"
"time"
)

func init() {
rand.Seed(time.Now().UnixNano()) // nolint:staticcheck
}

// RandCache a simple cache which randomly evicts entries when
// length exceeds limit.
type RandCache struct {
Expand Down Expand Up @@ -106,7 +101,7 @@ func (rc *RandCache) Pick() *Entry {
if len(rc.s) == 0 {
return nil
}
ent := rc.s[rand.Intn(len(rc.s))] // #nosec
ent := rc.s[rand.N(len(rc.s))] // #nosec
cpy := ent.Entry
return &cpy
}
Expand Down Expand Up @@ -141,6 +136,6 @@ func (rc *RandCache) randDrop() {
if len(rc.s) == 0 {
return
}
ent := rc.s[rand.Intn(len(rc.s))] // #nosec
ent := rc.s[rand.N(len(rc.s))] // #nosec
rc.remove(ent.Key)
}
4 changes: 2 additions & 2 deletions cmd/thor/node/tx_stash_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ package node

import (
"bytes"
"math/rand"
"math/rand/v2"
"sort"
"testing"

Expand All @@ -20,7 +20,7 @@ import (
)

func newTx() *tx.Transaction {
tx := new(tx.Builder).Nonce(rand.Uint64()).Build() // nolint:gosec
tx := new(tx.Builder).Nonce(rand.Uint64()).Build() // #nosec
sig, _ := crypto.Sign(tx.SigningHash().Bytes(), genesis.DevAccounts()[0].PrivateKey)
return tx.WithSignature(sig)
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/thor/solo/solo.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"context"
"fmt"
"math/big"
"math/rand"
"math/rand/v2"
"time"

"github.com/ethereum/go-ethereum/common"
Expand Down
8 changes: 2 additions & 6 deletions comm/peer.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
package comm

import (
"math/rand"
"math/rand/v2"
"sync"
"time"

Expand All @@ -24,10 +24,6 @@ const (
maxKnownBlocks = 1024 // Maximum block IDs to keep in the known list (prevent DOS)
)

func init() {
rand.Seed(time.Now().UnixNano()) // nolint:staticcheck
}

// Peer extends p2p.Peer with RPC integrated.
type Peer struct {
*p2p.Peer
Expand Down Expand Up @@ -84,7 +80,7 @@ func (p *Peer) UpdateHead(id thor.Bytes32, totalScore uint64) {
// MarkTransaction marks a transaction to known.
func (p *Peer) MarkTransaction(hash thor.Bytes32) {
// that's 10~100 block intervals
expiration := mclock.AbsTime(time.Second * time.Duration(thor.BlockInterval*uint64(rand.Intn(91)+10))) // #nosec
expiration := mclock.AbsTime(time.Second * time.Duration(thor.BlockInterval*uint64(rand.N(91)+10))) // #nosec

deadline := mclock.Now() + expiration
p.knownTxs.Add(hash, deadline)
Expand Down
6 changes: 3 additions & 3 deletions log/format_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
package log

import (
"math/rand"
"math/rand/v2"
"testing"
)

Expand All @@ -27,14 +27,14 @@ func BenchmarkPrettyInt64Logfmt(b *testing.B) {
buf := make([]byte, 100)
b.ReportAllocs()
for i := 0; i < b.N; i++ {
sink = appendInt64(buf, rand.Int63()) // nolint
sink = appendInt64(buf, rand.Int64()) // #nosec
}
}

func BenchmarkPrettyUint64Logfmt(b *testing.B) {
buf := make([]byte, 100)
b.ReportAllocs()
for i := 0; i < b.N; i++ {
sink = appendUint64(buf, rand.Uint64(), false) // nolint
sink = appendUint64(buf, rand.Uint64(), false) // #nosec
}
}
8 changes: 4 additions & 4 deletions metrics/noop_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
package metrics

import (
"math/rand"
"math/rand/v2"
"net/http"
"net/http/httptest"
"testing"
Expand All @@ -26,21 +26,21 @@ func TestNoopMetrics(t *testing.T) {
Counter("count2")

count1.Add(1)
randCount2 := rand.Intn(100) + 1 // nolint:gosec
randCount2 := rand.N(100) + 1 // #nosec
for i := 0; i < randCount2; i++ {
Counter("count2").Add(1)
}

hist := Histogram("hist1", nil)
histVect := HistogramVec("hist2", []string{"zeroOrOne"}, nil)
for i := 0; i < rand.Intn(100)+1; i++ { // nolint:gosec
for i := 0; i < rand.N(100)+1; i++ { // #nosec
hist.Observe(int64(i))
histVect.ObserveWithLabels(int64(i), map[string]string{"thisIsNonsense": "butDoesntBreak"})
}

countVect := CounterVec("countVec1", []string{"zeroOrOne"})
gaugeVec := GaugeVec("gaugeVec1", []string{"zeroOrOne"})
for i := 0; i < rand.Intn(100)+1; i++ { // nolint:gosec
for i := 0; i < rand.N(100)+1; i++ { // #nosec
countVect.AddWithLabel(int64(i), map[string]string{"thisIsNonsense": "butDoesntBreak"})
gaugeVec.AddWithLabel(int64(i), map[string]string{"thisIsNonsense": "butDoesntBreak"})
}
Expand Down
10 changes: 5 additions & 5 deletions metrics/prometheus_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
package metrics

import (
"math/rand"
"math/rand/v2"
"strconv"
"testing"

Expand All @@ -31,13 +31,13 @@ func TestPromMetrics(t *testing.T) {
gaugeVec := GaugeVec("gaugeVec1", []string{"zeroOrOne"})

count1.Add(1)
randCount2 := rand.Intn(100) + 1 // nolint:gosec
randCount2 := rand.N(100) + 1 // #nosec
for i := 0; i < randCount2; i++ {
Counter("count2").Add(1)
}

histTotal := 0
for i := 0; i < rand.Intn(100)+2; i++ { // nolint:gosec
for i := 0; i < rand.N(100)+2; i++ { // #nosec
zeroOrOne := i % 2
hist.Observe(int64(i))
HistogramVec("hist2", []string{"zeroOrOne"}, nil).
Expand All @@ -46,15 +46,15 @@ func TestPromMetrics(t *testing.T) {
}

totalCountVec := 0
randCountVec := rand.Intn(100) + 2 // nolint:gosec
randCountVec := rand.N(100) + 2 // #nosec
for i := 0; i < randCountVec; i++ {
zeroOrOne := i % 2
countVect.AddWithLabel(int64(i), map[string]string{"zeroOrOne": strconv.Itoa(zeroOrOne)})
totalCountVec += i
}

totalGaugeVec := 0
randGaugeVec := rand.Intn(100) + 2 // nolint:gosec
randGaugeVec := rand.N(100) + 2 // #nosec
for i := 0; i < randGaugeVec; i++ {
zeroOrOne := i % 2
gaugeVec.AddWithLabel(int64(i), map[string]string{"zeroOrOne": strconv.Itoa(zeroOrOne)})
Expand Down
7 changes: 1 addition & 6 deletions p2psrv/rpc/rpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ package rpc

import (
"context"
"math/rand"
"math/rand/v2"
"sync"
"time"

Expand All @@ -17,11 +17,6 @@ import (
"github.com/vechain/thor/v2/log"
)

func init() {
// required when generate call id
rand.Seed(time.Now().UnixNano()) // nolint:staticcheck
}

const (
rpcDefaultTimeout = time.Second * 10
)
Expand Down
Loading

0 comments on commit cb55e6b

Please sign in to comment.