From 97e089b50c6d982ec407a18dff74e6f771cfc930 Mon Sep 17 00:00:00 2001 From: parmmarrushabh Date: Tue, 23 Oct 2018 12:21:59 +0530 Subject: [PATCH] Fixed random hash function for unit test. --- contracts/blocksigner/blocksigner_test.go | 15 +++++++++++++-- contracts/utils.go | 13 ------------- contracts/utils_test.go | 14 +++++++++++++- 3 files changed, 26 insertions(+), 16 deletions(-) diff --git a/contracts/blocksigner/blocksigner_test.go b/contracts/blocksigner/blocksigner_test.go index 4f2bf14b9a..03b481f7d2 100644 --- a/contracts/blocksigner/blocksigner_test.go +++ b/contracts/blocksigner/blocksigner_test.go @@ -9,9 +9,9 @@ import ( "github.com/ethereum/go-ethereum/accounts/abi/bind" "github.com/ethereum/go-ethereum/accounts/abi/bind/backends" "github.com/ethereum/go-ethereum/common" - "github.com/ethereum/go-ethereum/contracts" "github.com/ethereum/go-ethereum/core" "github.com/ethereum/go-ethereum/crypto" + "math/rand" ) var ( @@ -40,7 +40,7 @@ func TestBlockSigner(t *testing.T) { } contractBackend.ForEachStorageAt(ctx, blockSignerAddress, nil, f) - byte0 := contracts.RandomHash() + byte0 := randomHash() // Test sign. tx, err := blockSigner.Sign(big.NewInt(50), byte0) @@ -57,4 +57,15 @@ func TestBlockSigner(t *testing.T) { for _, it := range signers { t.Log("signer", it.String()) } +} + +// Generate random string. +func randomHash() common.Hash { + letterBytes := "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ123456789" + var b common.Hash + for i := range b { + rand.Seed(time.Now().UnixNano()) + b[i] = letterBytes[rand.Intn(len(letterBytes))] + } + return b } \ No newline at end of file diff --git a/contracts/utils.go b/contracts/utils.go index dfc50b1cd2..2234d84a24 100644 --- a/contracts/utils.go +++ b/contracts/utils.go @@ -14,8 +14,6 @@ import ( "github.com/ethereum/go-ethereum/log" "github.com/ethereum/go-ethereum/params" "math/big" - "math/rand" - "time" ) const ( @@ -224,15 +222,4 @@ func GetRewardBalancesRate(masterAddr common.Address, totalReward *big.Int, vali log.Info("Holders reward", "holders", string(jsonHolders), "master node", masterAddr.String()) return balances, nil -} - -// Generate random string. -func RandomHash() common.Hash { - letterBytes := "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ123456789" - var b common.Hash - for i := range b { - rand.Seed(time.Now().UnixNano()) - b[i] = letterBytes[rand.Intn(len(letterBytes))] - } - return b } \ No newline at end of file diff --git a/contracts/utils_test.go b/contracts/utils_test.go index 712d4a75c1..5c6daf8569 100644 --- a/contracts/utils_test.go +++ b/contracts/utils_test.go @@ -13,6 +13,7 @@ import ( "math/big" "math/rand" "testing" + "time" ) func TestSendTxSign(t *testing.T) { @@ -56,7 +57,7 @@ func TestSendTxSign(t *testing.T) { signCount := int64(0) blockHashes := make([]common.Hash, 10) for i := int64(0); i < 10; i++ { - blockHash := RandomHash() + blockHash := randomHash() blockHashes[i] = blockHash randIndex := rand.Intn(len(keys)) accKey := keys[randIndex] @@ -118,4 +119,15 @@ func TestSendTxSign(t *testing.T) { //if rewards.Cmp(new(big.Int).SetUint64(14999999999999999996)) != 0 { // t.Errorf("Total reward not same reward checkpoint: %v - %v", chainReward, rewards) //} +} + +// Generate random string. +func randomHash() common.Hash { + letterBytes := "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ123456789" + var b common.Hash + for i := range b { + rand.Seed(time.Now().UnixNano()) + b[i] = letterBytes[rand.Intn(len(letterBytes))] + } + return b } \ No newline at end of file