github.com/theQRL/go-zond@v0.1.1/trie/testutil/utils.go (about)

     1  // Copyright 2023 The go-ethereum Authors
     2  // This file is part of the go-ethereum library.
     3  //
     4  // The go-ethereum library is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Lesser General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // The go-ethereum library is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU Lesser General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Lesser General Public License
    15  // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package testutil
    18  
    19  import (
    20  	crand "crypto/rand"
    21  	"encoding/binary"
    22  	mrand "math/rand"
    23  
    24  	"github.com/theQRL/go-zond/common"
    25  	"github.com/theQRL/go-zond/crypto"
    26  	"github.com/theQRL/go-zond/trie/trienode"
    27  )
    28  
    29  // Prng is a pseudo random number generator seeded by strong randomness.
    30  // The randomness is printed on startup in order to make failures reproducible.
    31  var prng = initRand()
    32  
    33  func initRand() *mrand.Rand {
    34  	var seed [8]byte
    35  	crand.Read(seed[:])
    36  	rnd := mrand.New(mrand.NewSource(int64(binary.LittleEndian.Uint64(seed[:]))))
    37  	return rnd
    38  }
    39  
    40  // RandBytes generates a random byte slice with specified length.
    41  func RandBytes(n int) []byte {
    42  	r := make([]byte, n)
    43  	prng.Read(r)
    44  	return r
    45  }
    46  
    47  // RandomHash generates a random blob of data and returns it as a hash.
    48  func RandomHash() common.Hash {
    49  	return common.BytesToHash(RandBytes(common.HashLength))
    50  }
    51  
    52  // RandomAddress generates a random blob of data and returns it as an address.
    53  func RandomAddress() common.Address {
    54  	return common.BytesToAddress(RandBytes(common.AddressLength))
    55  }
    56  
    57  // RandomNode generates a random node.
    58  func RandomNode() *trienode.Node {
    59  	val := RandBytes(100)
    60  	return trienode.New(crypto.Keccak256Hash(val), val)
    61  }