libs/common: refactor libs/common 2 (#4231)

* libs/common: refactor libs/common 2

- move random function to there own pkg

Signed-off-by: Marko Baricevic <marbar3778@yahoo.com>

* change imports and usage throughout repo

* fix goimports

* add changelog entry
This commit is contained in:
Marko
2019-12-10 18:38:34 +01:00
committed by GitHub
parent dfebac86f7
commit afc4d7a61f
55 changed files with 221 additions and 196 deletions
+2 -1
View File
@@ -18,6 +18,7 @@ import (
"github.com/tendermint/tendermint/crypto/merkle"
"github.com/tendermint/tendermint/crypto/tmhash"
cmn "github.com/tendermint/tendermint/libs/common"
tmrand "github.com/tendermint/tendermint/libs/rand"
tmtime "github.com/tendermint/tendermint/types/time"
"github.com/tendermint/tendermint/version"
)
@@ -79,7 +80,7 @@ func TestBlockValidateBasic(t *testing.T) {
blk.Data.hash = nil // clear hash or change wont be noticed
}, true},
{"Tampered DataHash", func(blk *Block) {
blk.DataHash = cmn.RandBytes(len(blk.DataHash))
blk.DataHash = tmrand.RandBytes(len(blk.DataHash))
}, true},
{"Tampered EvidenceHash", func(blk *Block) {
blk.EvidenceHash = []byte("something else")
+4 -4
View File
@@ -10,10 +10,10 @@ import (
"github.com/stretchr/testify/require"
abci "github.com/tendermint/tendermint/abci/types"
cmn "github.com/tendermint/tendermint/libs/common"
"github.com/tendermint/tendermint/libs/kv"
tmpubsub "github.com/tendermint/tendermint/libs/pubsub"
tmquery "github.com/tendermint/tendermint/libs/pubsub/query"
"github.com/tendermint/tendermint/libs/rand"
)
func TestEventBusPublishEventTx(t *testing.T) {
@@ -348,7 +348,7 @@ func BenchmarkEventBus(b *testing.B) {
func benchmarkEventBus(numClients int, randQueries bool, randEvents bool, b *testing.B) {
// for random* functions
cmn.Seed(time.Now().Unix())
rand.Seed(time.Now().Unix())
eventBus := NewEventBusWithBufferCapacity(0) // set buffer capacity to 0 so we are not testing cache
eventBus.Start()
@@ -404,7 +404,7 @@ var events = []string{
EventVote}
func randEvent() string {
return events[cmn.RandIntn(len(events))]
return events[rand.RandIntn(len(events))]
}
var queries = []tmpubsub.Query{
@@ -422,5 +422,5 @@ var queries = []tmpubsub.Query{
EventQueryVote}
func randQuery() tmpubsub.Query {
return queries[cmn.RandIntn(len(queries))]
return queries[rand.RandIntn(len(queries))]
}
+5 -5
View File
@@ -8,7 +8,7 @@ import (
"github.com/stretchr/testify/require"
"github.com/tendermint/tendermint/crypto/merkle"
cmn "github.com/tendermint/tendermint/libs/common"
"github.com/tendermint/tendermint/libs/rand"
)
const (
@@ -17,7 +17,7 @@ const (
func TestBasicPartSet(t *testing.T) {
// Construct random data of size partSize * 100
data := cmn.RandBytes(testPartSize * 100)
data := rand.RandBytes(testPartSize * 100)
partSet := NewPartSetFromData(data, testPartSize)
assert.NotEmpty(t, partSet.Hash())
@@ -62,7 +62,7 @@ func TestBasicPartSet(t *testing.T) {
func TestWrongProof(t *testing.T) {
// Construct random data of size partSize * 100
data := cmn.RandBytes(testPartSize * 100)
data := rand.RandBytes(testPartSize * 100)
partSet := NewPartSetFromData(data, testPartSize)
// Test adding a part with wrong data.
@@ -98,7 +98,7 @@ func TestPartSetHeaderValidateBasic(t *testing.T) {
for _, tc := range testCases {
tc := tc
t.Run(tc.testName, func(t *testing.T) {
data := cmn.RandBytes(testPartSize * 100)
data := rand.RandBytes(testPartSize * 100)
ps := NewPartSetFromData(data, testPartSize)
psHeader := ps.Header()
tc.malleatePartSetHeader(&psHeader)
@@ -128,7 +128,7 @@ func TestPartValidateBasic(t *testing.T) {
for _, tc := range testCases {
tc := tc
t.Run(tc.testName, func(t *testing.T) {
data := cmn.RandBytes(testPartSize * 100)
data := rand.RandBytes(testPartSize * 100)
ps := NewPartSetFromData(data, testPartSize)
part := ps.GetPart(0)
tc.malleatePart(part)
+3 -3
View File
@@ -6,20 +6,20 @@ import (
"github.com/stretchr/testify/assert"
cmn "github.com/tendermint/tendermint/libs/common"
"github.com/tendermint/tendermint/libs/rand"
ctest "github.com/tendermint/tendermint/libs/test"
)
func makeTxs(cnt, size int) Txs {
txs := make(Txs, cnt)
for i := 0; i < cnt; i++ {
txs[i] = cmn.RandBytes(size)
txs[i] = rand.RandBytes(size)
}
return txs
}
func randInt(low, high int) int {
off := cmn.RandInt() % (high - low)
off := rand.RandInt() % (high - low)
return low + off
}
+2 -2
View File
@@ -6,7 +6,7 @@ import (
"strings"
"github.com/tendermint/tendermint/crypto"
cmn "github.com/tendermint/tendermint/libs/common"
"github.com/tendermint/tendermint/libs/rand"
)
// Volatile state for each Validator
@@ -103,7 +103,7 @@ func RandValidator(randPower bool, minPower int64) (*Validator, PrivValidator) {
privVal := NewMockPV()
votePower := minPower
if randPower {
votePower += int64(cmn.RandUint32())
votePower += int64(rand.RandUint32())
}
pubKey := privVal.GetPubKey()
val := NewValidator(pubKey, votePower)
+15 -14
View File
@@ -14,6 +14,7 @@ import (
"github.com/tendermint/tendermint/crypto"
"github.com/tendermint/tendermint/crypto/ed25519"
cmn "github.com/tendermint/tendermint/libs/common"
"github.com/tendermint/tendermint/libs/rand"
tmtime "github.com/tendermint/tendermint/types/time"
)
@@ -283,10 +284,10 @@ func TestProposerSelection3(t *testing.T) {
// times is usually 1
times := 1
mod := (cmn.RandInt() % 5) + 1
if cmn.RandInt()%mod > 0 {
mod := (rand.RandInt() % 5) + 1
if rand.RandInt()%mod > 0 {
// sometimes its up to 5
times = (cmn.RandInt() % 4) + 1
times = (rand.RandInt() % 4) + 1
}
vset.IncrementProposerPriority(times)
@@ -300,15 +301,15 @@ func newValidator(address []byte, power int64) *Validator {
func randPubKey() crypto.PubKey {
var pubKey [32]byte
copy(pubKey[:], cmn.RandBytes(32))
copy(pubKey[:], rand.RandBytes(32))
return ed25519.PubKeyEd25519(pubKey)
}
func randValidator(totalVotingPower int64) *Validator {
// this modulo limits the ProposerPriority/VotingPower to stay in the
// bounds of MaxTotalVotingPower minus the already existing voting power:
val := NewValidator(randPubKey(), int64(cmn.RandUint64()%uint64((MaxTotalVotingPower-totalVotingPower))))
val.ProposerPriority = cmn.RandInt64() % (MaxTotalVotingPower - totalVotingPower)
val := NewValidator(randPubKey(), int64(rand.RandUint64()%uint64((MaxTotalVotingPower-totalVotingPower))))
val.ProposerPriority = rand.RandInt64() % (MaxTotalVotingPower - totalVotingPower)
return val
}
@@ -708,7 +709,7 @@ func permutation(valList []testVal) []testVal {
return nil
}
permList := make([]testVal, len(valList))
perm := cmn.RandPerm(len(valList))
perm := rand.RandPerm(len(valList))
for i, v := range perm {
permList[v] = valList[i]
}
@@ -1110,14 +1111,14 @@ func randTestVSetCfg(t *testing.T, nBase, nAddMax int) testVSetCfg {
const maxPower = 1000
var nOld, nDel, nChanged, nAdd int
nOld = int(cmn.RandUint()%uint(nBase)) + 1
nOld = int(rand.RandUint()%uint(nBase)) + 1
if nBase-nOld > 0 {
nDel = int(cmn.RandUint() % uint(nBase-nOld))
nDel = int(rand.RandUint() % uint(nBase-nOld))
}
nChanged = nBase - nOld - nDel
if nAddMax > 0 {
nAdd = cmn.RandInt()%nAddMax + 1
nAdd = rand.RandInt()%nAddMax + 1
}
cfg := testVSetCfg{}
@@ -1129,12 +1130,12 @@ func randTestVSetCfg(t *testing.T, nBase, nAddMax int) testVSetCfg {
cfg.expectedVals = make([]testVal, nBase-nDel+nAdd)
for i := 0; i < nBase; i++ {
cfg.startVals[i] = testVal{fmt.Sprintf("v%d", i), int64(cmn.RandUint()%maxPower + 1)}
cfg.startVals[i] = testVal{fmt.Sprintf("v%d", i), int64(rand.RandUint()%maxPower + 1)}
if i < nOld {
cfg.expectedVals[i] = cfg.startVals[i]
}
if i >= nOld && i < nOld+nChanged {
cfg.updatedVals[i-nOld] = testVal{fmt.Sprintf("v%d", i), int64(cmn.RandUint()%maxPower + 1)}
cfg.updatedVals[i-nOld] = testVal{fmt.Sprintf("v%d", i), int64(rand.RandUint()%maxPower + 1)}
cfg.expectedVals[i] = cfg.updatedVals[i-nOld]
}
if i >= nOld+nChanged {
@@ -1143,7 +1144,7 @@ func randTestVSetCfg(t *testing.T, nBase, nAddMax int) testVSetCfg {
}
for i := nBase; i < nBase+nAdd; i++ {
cfg.addedVals[i-nBase] = testVal{fmt.Sprintf("v%d", i), int64(cmn.RandUint()%maxPower + 1)}
cfg.addedVals[i-nBase] = testVal{fmt.Sprintf("v%d", i), int64(rand.RandUint()%maxPower + 1)}
cfg.expectedVals[i-nDel] = cfg.addedVals[i-nBase]
}
@@ -1224,7 +1225,7 @@ func TestValSetUpdatePriorityOrderTests(t *testing.T) {
func verifyValSetUpdatePriorityOrder(t *testing.T, valSet *ValidatorSet, cfg testVSetCfg, nMaxElections int) {
// Run election up to nMaxElections times, sort validators by priorities
valSet.IncrementProposerPriority(cmn.RandInt()%nMaxElections + 1)
valSet.IncrementProposerPriority(rand.RandInt()%nMaxElections + 1)
origValsPriSorted := validatorListCopy(valSet.Validators)
sort.Sort(validatorsByPriority(origValsPriSorted))
+8 -8
View File
@@ -7,7 +7,7 @@ import (
"github.com/stretchr/testify/assert"
"github.com/tendermint/tendermint/crypto"
cmn "github.com/tendermint/tendermint/libs/common"
"github.com/tendermint/tendermint/libs/rand"
tmtime "github.com/tendermint/tendermint/types/time"
)
@@ -142,7 +142,7 @@ func Test2_3Majority(t *testing.T) {
{
addr := privValidators[6].GetPubKey().Address()
vote := withValidator(voteProto, addr, 6)
_, err := signAddVote(privValidators[6], withBlockHash(vote, cmn.RandBytes(32)), voteSet)
_, err := signAddVote(privValidators[6], withBlockHash(vote, rand.RandBytes(32)), voteSet)
if err != nil {
t.Error(err)
}
@@ -247,7 +247,7 @@ func Test2_3MajorityRedux(t *testing.T) {
{
addr := privValidators[69].GetPubKey().Address()
vote := withValidator(voteProto, addr, 69)
_, err := signAddVote(privValidators[69], withBlockHash(vote, cmn.RandBytes(32)), voteSet)
_, err := signAddVote(privValidators[69], withBlockHash(vote, rand.RandBytes(32)), voteSet)
if err != nil {
t.Error(err)
}
@@ -300,7 +300,7 @@ func TestBadVotes(t *testing.T) {
{
addr := privValidators[0].GetPubKey().Address()
vote := withValidator(voteProto, addr, 0)
added, err := signAddVote(privValidators[0], withBlockHash(vote, cmn.RandBytes(32)), voteSet)
added, err := signAddVote(privValidators[0], withBlockHash(vote, rand.RandBytes(32)), voteSet)
if added || err == nil {
t.Errorf("expected VoteSet.Add to fail, conflicting vote.")
}
@@ -340,8 +340,8 @@ func TestBadVotes(t *testing.T) {
func TestConflicts(t *testing.T) {
height, round := int64(1), 0
voteSet, _, privValidators := randVoteSet(height, round, PrevoteType, 4, 1)
blockHash1 := cmn.RandBytes(32)
blockHash2 := cmn.RandBytes(32)
blockHash1 := rand.RandBytes(32)
blockHash2 := rand.RandBytes(32)
voteProto := &Vote{
ValidatorAddress: nil,
@@ -503,8 +503,8 @@ func TestMakeCommit(t *testing.T) {
{
addr := privValidators[6].GetPubKey().Address()
vote := withValidator(voteProto, addr, 6)
vote = withBlockHash(vote, cmn.RandBytes(32))
vote = withBlockPartsHeader(vote, PartSetHeader{123, cmn.RandBytes(32)})
vote = withBlockHash(vote, rand.RandBytes(32))
vote = withBlockPartsHeader(vote, PartSetHeader{123, rand.RandBytes(32)})
_, err := signAddVote(privValidators[6], vote, voteSet)
if err != nil {