-
Notifications
You must be signed in to change notification settings - Fork 4
/
prg_test_helpers.go
137 lines (117 loc) · 2.68 KB
/
prg_test_helpers.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package test
import (
"crypto/rand"
"testing"
. "github.com/bjartek/overflow/v2"
"github.com/stretchr/testify/require"
)
/* --- Scripts helpers --- */
// calls a script which creates a new PRG struct from the given seed and
// random salt and gets the next uint64
func GetNextUInt64NewPRGRandSalt(
o *OverflowState,
t *testing.T,
seed []byte,
) (uint64, error) {
return GetNextUInt64NewPRGWithSalt(
o,
t,
seed,
GetRandomBytes(t, 8),
)
}
// gets a random uint64 from a newly instantiated PRG struct
func GetNextUInt64NewPRGWithSalt(
o *OverflowState,
t *testing.T,
seed []byte,
salt []byte,
) (uint64, error) {
t.Helper()
var value uint64
err := o.Script(
"test/next_uint64",
WithArg("sourceOfRandomness", seed),
WithArg("salt", salt),
).MarshalAs(&value)
return value, err
}
// gets the results array from RandomResultStorage contract
func GetResultsFromRandomResultStorage(
o *OverflowState,
t *testing.T,
) []uint64 {
t.Helper()
var uint64Array []uint64
err := o.Script("test/get_results").MarshalAs(&uint64Array)
require.NoError(t, err)
return uint64Array
}
// gets the range of results from RandomResultStorage.results
func GetResultsInRangeFromRandomResultStorage(
o *OverflowState,
t *testing.T,
from int,
upTo int,
) []uint64 {
t.Helper()
var uint64Array []uint64
err := o.Script(
"test/get_results_in_range",
WithArg("from", from),
WithArg("upTo", upTo),
).MarshalAs(&uint64Array)
require.NoError(t, err)
return uint64Array
}
/* --- Transaction helpers --- */
// generates results and stores them in RandomResultStorage contract
func GenerateResultsAndStore(
o *OverflowState,
t *testing.T,
length int,
) {
t.Helper()
o.Tx(
"test/generate_results",
WithSignerServiceAccount(),
WithArg("generationLength", length),
).AssertSuccess(t)
}
// initializes PRG object in RandomResultStorage helper contract
func InitializePRG(
o *OverflowState,
t *testing.T,
seed []byte,
salt []byte,
) {
t.Helper()
o.Tx(
"test/initialize_prg",
WithSignerServiceAccount(),
WithArg("sourceOfRandomness", seed),
WithArg("salt", salt),
).AssertSuccess(t)
}
/* --- Utils --- */
// gets a random byte array using crypto/rand
func GetRandomBytes(t *testing.T, len uint64) []byte {
buf := make([]byte, len)
_, err := rand.Read(buf)
require.NoError(t, err)
return buf
}
// processes a task in batches
func ProcessBatches(totalSize, maxBatchSize int, processFunc func(startIdx, batchSize int)) {
for startIdx := 0; startIdx < totalSize; startIdx += maxBatchSize {
batchSize := min(maxBatchSize, totalSize-startIdx)
processFunc(startIdx, batchSize)
}
}
// returns the minimum of two integers
func min(a, b int) int {
if a < b {
return a
}
return b
}