-
Notifications
You must be signed in to change notification settings - Fork 0
/
random.go
52 lines (46 loc) · 914 Bytes
/
random.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package txfuzz
import (
"crypto/rand"
"fmt"
mathRand "math/rand"
"github.com/ethereum/go-ethereum/common"
)
const (
maxDataPerTx = 1 << 17 // 128Kb
)
func randomHash() common.Hash {
b := make([]byte, 32)
_, err := rand.Read(b)
if err != nil {
panic(err)
}
return common.BytesToHash(b)
}
func randomAddress() common.Address {
switch mathRand.Int31n(5) {
case 0, 1, 2:
b := make([]byte, 20)
_, err := rand.Read(b)
if err != nil {
panic(err)
}
return common.BytesToAddress(b)
case 3:
return common.Address{}
case 4:
return common.HexToAddress(ADDR)
}
return common.Address{}
}
func randomBlobData() ([]byte, error) {
size := mathRand.Intn(maxDataPerTx)
data := make([]byte, size)
n, err := rand.Read(data)
if err != nil {
return nil, err
}
if n != size {
return nil, fmt.Errorf("could not create random blob data with size %d: %v", size, err)
}
return data, nil
}