fix race condition in snapshot_test (#6761)

The Read method of math package used in randomAddress function is not
thread safe, instead I've used crand package which is used by matic and
is a fork of thread safe crypto package.
This commit is contained in:
Anshal Shukla 2023-02-02 03:45:19 +05:30 committed by GitHub
parent afe8a84dc4
commit b70e8d7955
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 11 additions and 9 deletions

View File

@ -1,13 +1,13 @@
package bor
import (
"math/rand"
"math/big"
"sort"
"testing"
"time"
libcommon "github.com/ledgerwatch/erigon-lib/common"
"github.com/ledgerwatch/erigon/consensus/bor/valset"
crand "github.com/maticnetwork/crand"
"github.com/stretchr/testify/require"
)
@ -123,13 +123,14 @@ func TestGetSignerSuccessionNumber_SignerNotFound(t *testing.T) {
}
func buildRandomValidatorSet(numVals int) []*valset.Validator {
rand.Seed(time.Now().Unix())
validators := make([]*valset.Validator, numVals)
for i := 0; i < numVals; i++ {
power := crand.BigInt(big.NewInt(99))
// cannot process validators with voting power 0, hence +1
powerN := power.Int64() + 1
validators[i] = &valset.Validator{
Address: randomAddress(),
// cannot process validators with voting power 0, hence +1
VotingPower: int64(rand.Intn(99) + 1),
Address: randomAddress(),
VotingPower: powerN,
}
}
@ -139,7 +140,5 @@ func buildRandomValidatorSet(numVals int) []*valset.Validator {
}
func randomAddress() libcommon.Address {
bytes := make([]byte, 32)
rand.Read(bytes)
return libcommon.BytesToAddress(bytes)
return crand.NewRand().Address()
}

1
go.mod
View File

@ -55,6 +55,7 @@ require (
github.com/libp2p/go-libp2p v0.23.2
github.com/libp2p/go-libp2p-core v0.20.1
github.com/libp2p/go-libp2p-pubsub v0.8.1
github.com/maticnetwork/crand v1.0.2
github.com/maticnetwork/polyproto v0.0.2
github.com/multiformats/go-multiaddr v0.7.0
github.com/nxadm/tail v1.4.9-0.20211216163028-4472660a31a6

2
go.sum
View File

@ -615,6 +615,8 @@ github.com/marten-seemann/qtls-go1-19 v0.1.0/go.mod h1:5HTDWtVudo/WFsHKRNuOhWlbd
github.com/marten-seemann/tcp v0.0.0-20210406111302-dfbc87cc63fd h1:br0buuQ854V8u83wA0rVZ8ttrq5CpaPZdvrK0LP2lOk=
github.com/marten-seemann/tcp v0.0.0-20210406111302-dfbc87cc63fd/go.mod h1:QuCEs1Nt24+FYQEqAAncTDPJIuGs+LxK1MCiFL25pMU=
github.com/marten-seemann/webtransport-go v0.1.1 h1:TnyKp3pEXcDooTaNn4s9dYpMJ7kMnTp7k5h+SgYP/mc=
github.com/maticnetwork/crand v1.0.2 h1:Af0tAivC8zrxXDpGWNWVT/0s1fOz8w0eRbahZgURS8I=
github.com/maticnetwork/crand v1.0.2/go.mod h1:/NRNL3bj2eYdqpWmoIP5puxndTpi0XRxpj5ZKxfHjyg=
github.com/maticnetwork/polyproto v0.0.2 h1:cPxuxbIDItdwGnucc3lZB58U8Zfe1mH73PWTGd15554=
github.com/maticnetwork/polyproto v0.0.2/go.mod h1:e1mU2EXSwEpn5jM7GfNwu3AupsV6WAGoPFFfswXOF0o=
github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU=