-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
test_util.go
151 lines (129 loc) · 3.63 KB
/
test_util.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package blockhashstore
import (
"context"
"crypto/rand"
"math/big"
"github.com/ethereum/go-ethereum/common"
"github.com/pkg/errors"
)
type TestCoordinator struct {
RequestEvents []Event
FulfillmentEvents []Event
}
func (t *TestCoordinator) Addresses() []common.Address {
return []common.Address{}
}
func (t *TestCoordinator) Requests(_ context.Context, fromBlock uint64, toBlock uint64) ([]Event, error) {
var result []Event
for _, req := range t.RequestEvents {
if req.Block >= fromBlock && req.Block <= toBlock {
result = append(result, req)
}
}
return result, nil
}
func (t *TestCoordinator) Fulfillments(_ context.Context, fromBlock uint64) ([]Event, error) {
var result []Event
for _, ful := range t.FulfillmentEvents {
if ful.Block >= fromBlock {
result = append(result, ful)
}
}
return result, nil
}
type TestBHS struct {
Stored []uint64
StoredEarliest bool
// errorsStore defines which block numbers should return errors on Store.
ErrorsStore []uint64
// errorsIsStored defines which block numbers should return errors on IsStored.
ErrorsIsStored []uint64
}
func (t *TestBHS) Store(_ context.Context, blockNum uint64) error {
for _, e := range t.ErrorsStore {
if e == blockNum {
return errors.New("error storing")
}
}
t.Stored = append(t.Stored, blockNum)
return nil
}
func (t *TestBHS) IsTrusted() bool {
return false
}
func (t *TestBHS) StoreTrusted(
ctx context.Context, blockNums []uint64, blockhashes []common.Hash, recentBlock uint64, recentBlockhash common.Hash,
) error {
return errors.New("not implemented")
}
func (t *TestBHS) IsStored(_ context.Context, blockNum uint64) (bool, error) {
for _, e := range t.ErrorsIsStored {
if e == blockNum {
return false, errors.New("error checking if stored")
}
}
for _, s := range t.Stored {
if s == blockNum {
return true, nil
}
}
return false, nil
}
func (t *TestBHS) StoreEarliest(ctx context.Context) error {
t.StoredEarliest = true
return nil
}
type TestBatchBHS struct {
Stored []uint64
GetBlockhashesCallCounter uint16
StoreVerifyHeaderCallCounter uint16
GetBlockhashesError error
StoreVerifyHeadersError error
}
func (t *TestBatchBHS) GetBlockhashes(_ context.Context, blockNumbers []*big.Int) ([][32]byte, error) {
t.GetBlockhashesCallCounter++
if t.GetBlockhashesError != nil {
return nil, t.GetBlockhashesError
}
var blockhashes [][32]byte
for _, b := range blockNumbers {
for _, stored := range t.Stored {
var randomBlockhash [32]byte
if stored == b.Uint64() {
_, err := rand.Read(randomBlockhash[:])
if err != nil {
return nil, err
}
}
blockhashes = append(blockhashes, randomBlockhash)
}
}
return blockhashes, nil
}
func (t *TestBatchBHS) StoreVerifyHeader(ctx context.Context, blockNumbers []*big.Int, blockHeaders [][]byte, fromAddress common.Address) error {
t.StoreVerifyHeaderCallCounter++
if t.StoreVerifyHeadersError != nil {
return t.StoreVerifyHeadersError
}
if len(blockNumbers) != len(blockHeaders) {
return errors.Errorf("input length did not match. blockNumbers length: %d, blockHeaders length: %d", len(blockNumbers), len(blockHeaders))
}
for _, blockNumber := range blockNumbers {
t.Stored = append(t.Stored, blockNumber.Uint64())
}
return nil
}
type TestBlockHeaderProvider struct {
}
func (p *TestBlockHeaderProvider) RlpHeadersBatch(ctx context.Context, blockRange []*big.Int) ([][]byte, error) {
var headers [][]byte
for range blockRange {
var randomBytes [30]byte //random length
_, err := rand.Read(randomBytes[:])
if err != nil {
return nil, err
}
headers = append(headers, randomBytes[:])
}
return headers, nil
}