-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheth_withdrawn_early_test.go
347 lines (304 loc) · 12.5 KB
/
eth_withdrawn_early_test.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
package tests
import (
"fmt"
"testing"
)
var (
monitorTenFile = "eth_withdrawn_early.gate"
)
func TestETHWithdrawnTooEarly(t *testing.T) {
// We expect an alert to be fired when a withdrawal is made before the delayedTime has passed
// set the params
params := map[string]any{
"disputeGame": "0x00000000000000000000000000000000000000AA",
"multicall3": "0x00000000000000000000000000000000000000BB",
}
// read in the gate file
data, err := ReadGateFile(monitorTenFile)
if err != nil {
t.Errorf("Error reading file %s: %v", monitorTenFile, err)
}
// setup the mocks
mocks := map[string]any{
"addressesInTrace": []any{"0x00000000000000000000000000000000000000AA"},
"delayedWETH": "0x0000000000000000000000000000000000000000",
"claims": [][]interface{}{
{"0x0000000000000000000000000000000000000001"},
},
// note claims and withdrawals are effectively the same thing, we just need to match claim calls
// to their corresponding withdrawal call on a separate contract
"withdrawals": [][]interface{}{
{"0x0000000000000000000000000000000000000001", 100},
},
"delayTime": 100,
"currTimestamp": 1099,
"unlockTimestamps": []int{1000, 1000}, // both unlocks happened earlier than the delayTime
"unlocks": [][]interface{}{
// this unlock on its own would be fine as the delayTime has elapsed
{50, "0x00000000000000000000000000000000000000AA", []interface{}{"0x0000000000000000000000000000000000000001", 50}},
// however, this unlock call is made before the delayTime has passed, and the delay resets each time a new unlock
// is called against the same address for the same dispute game, so this will trigger the alert
{101, "0x00000000000000000000000000000000000000AA", []interface{}{"0x0000000000000000000000000000000000000001", 50}},
},
}
// call out to hexagate API to run the gate file with params and mocks
failed, exceptions, trace, err := HandleValidateRequest(data, params, mocks)
if err != nil {
t.Errorf("Error handling validate request for %s: %v", monitorTenFile, err)
}
// check if the validate request threw any exceptions
if len(exceptions) > 0 {
fmt.Println(trace)
t.Errorf("Exceptions for %s: %v", monitorTenFile, exceptions)
}
// we expect to see the alert fired
if len(failed) == 0 {
fmt.Println(trace)
t.Errorf("Monitor did not fire an alert for %s when it was supposed to", monitorTenFile)
}
}
func TestETHWithdrawnTooEarlyNoMatchingUnlock(t *testing.T) {
// We expect an alert to be fired when a withdrawal is made but there is no matching unlock call
// for the recipient address
// set the params
params := map[string]any{
"disputeGame": "0x00000000000000000000000000000000000000AA",
"multicall3": "0x00000000000000000000000000000000000000BB",
}
// read in the gate file
data, err := ReadGateFile(monitorTenFile)
if err != nil {
t.Errorf("Error reading file %s: %v", monitorTenFile, err)
}
// setup the mocks
mocks := map[string]any{
"addressesInTrace": []any{"0x00000000000000000000000000000000000000AA"},
"delayedWETH": "0x0000000000000000000000000000000000000000",
"claims": [][]interface{}{
{"0x0000000000000000000000000000000000000001"},
{"0x0000000000000000000000000000000000000002"},
},
"withdrawals": [][]interface{}{
{"0x0000000000000000000000000000000000000001", 100},
{"0x0000000000000000000000000000000000000002", 200},
},
"delayTime": 10,
"currTimestamp": 2000,
"unlockTimestamps": []int{1000}, // unlock happened after delayTime
"unlocks": [][]interface{}{
{90, "0x00000000000000000000000000000000000000AA", []interface{}{"0x0000000000000000000000000000000000000001", 100}},
// we are missing the corresponding unlock for claim 0x00...02 which will trigger the alert
},
}
// call out to hexagate API to run the gate file with params and mocks
failed, exceptions, trace, err := HandleValidateRequest(data, params, mocks)
if err != nil {
t.Errorf("Error handling validate request for %s: %v", monitorTenFile, err)
}
// check if the validate request threw any exceptions
if len(exceptions) > 0 {
fmt.Println(trace)
t.Errorf("Exceptions for %s: %v", monitorTenFile, exceptions)
}
// we expect to see the alert fired
if len(failed) == 0 {
fmt.Println(trace)
t.Errorf("Monitor did not fire an alert for %s when it was supposed to", monitorTenFile)
}
}
func TestETHWithdrawnTooEarlyIncorrectAmount(t *testing.T) {
// We expect an alert to be fired when a withdrawal is made but does not match the sum of the
// unlock calls for the recipient address
// set the params
params := map[string]any{
"disputeGame": "0x00000000000000000000000000000000000000AA",
"multicall3": "0x00000000000000000000000000000000000000BB",
}
// read in the gate file
data, err := ReadGateFile(monitorTenFile)
if err != nil {
t.Errorf("Error reading file %s: %v", monitorTenFile, err)
}
// setup the mocks
mocks := map[string]any{
"addressesInTrace": []any{"0x00000000000000000000000000000000000000AA"},
"delayedWETH": "0x0000000000000000000000000000000000000000",
"claims": [][]interface{}{
{"0x0000000000000000000000000000000000000001"},
{"0x0000000000000000000000000000000000000002"},
},
"withdrawals": [][]interface{}{
{"0x0000000000000000000000000000000000000001", 100},
{"0x0000000000000000000000000000000000000002", 200},
},
"delayTime": 10,
"currTimestamp": 2000,
"unlockTimestamps": []int{1000, 1000}, // unlocks happened after delayTime
"unlocks": [][]interface{}{
{90, "0x00000000000000000000000000000000000000AA", []interface{}{"0x0000000000000000000000000000000000000001", 100}},
// notice the unlock amount for claim 0x00...02 doesn't match the withdrawal amount
{90, "0x00000000000000000000000000000000000000AA", []interface{}{"0x0000000000000000000000000000000000000002", 100}},
},
}
// call out to hexagate API to run the gate file with params and mocks
failed, exceptions, trace, err := HandleValidateRequest(data, params, mocks)
if err != nil {
t.Errorf("Error handling validate request for %s: %v", monitorTenFile, err)
}
// check if the validate request threw any exceptions
if len(exceptions) > 0 {
fmt.Println(trace)
t.Errorf("Exceptions for %s: %v", monitorTenFile, exceptions)
}
// we expect to see the alert fired
if len(failed) == 0 {
fmt.Println(trace)
t.Errorf("Monitor did not fire an alert for %s when it was supposed to", monitorTenFile)
}
}
func TestETHWithdrawnTooEarlyCorrectWithdrawal(t *testing.T) {
// We DO NOT expect an alert to be fired when a withdrawal occurrs past the delayedTime,
// with the correct sum and matching unlock calls
// set the params
params := map[string]any{
"disputeGame": "0x00000000000000000000000000000000000000AA",
"multicall3": "0x00000000000000000000000000000000000000BB",
}
// read in the gate file
data, err := ReadGateFile(monitorTenFile)
if err != nil {
t.Errorf("Error reading file %s: %v", monitorTenFile, err)
}
// setup the mocks
mocks := map[string]any{
"addressesInTrace": []any{"0x00000000000000000000000000000000000000AA"},
"delayedWETH": "0x0000000000000000000000000000000000000000",
"claims": [][]interface{}{
{"0x0000000000000000000000000000000000000001"},
{"0x0000000000000000000000000000000000000002"},
},
"withdrawals": [][]interface{}{
{"0x0000000000000000000000000000000000000001", 100},
{"0x0000000000000000000000000000000000000002", 200},
},
"delayTime": 10,
"currTimestamp": 2000,
"unlockTimestamps": []int{1000, 1000, 1000}, // unlocks happened after delayTime
"unlocks": [][]interface{}{
{90, "0x00000000000000000000000000000000000000AA", []interface{}{"0x0000000000000000000000000000000000000001", 100}},
// aggregating the two unlocks together will give us the correct withdrawal amount
{90, "0x00000000000000000000000000000000000000AA", []interface{}{"0x0000000000000000000000000000000000000002", 100}},
{89, "0x00000000000000000000000000000000000000AA", []interface{}{"0x0000000000000000000000000000000000000002", 100}},
},
}
// call out to hexagate API to run the gate file with params and mocks
failed, exceptions, trace, err := HandleValidateRequest(data, params, mocks)
if err != nil {
t.Errorf("Error handling validate request for %s: %v", monitorTenFile, err)
}
// check if the validate request threw any exceptions
if len(exceptions) > 0 {
fmt.Println(trace)
t.Errorf("Exceptions for %s: %v", monitorTenFile, exceptions)
}
// we DO NOT expect to see the alert fired
if len(failed) > 0 {
fmt.Println(trace)
t.Errorf("Monitor fired an alert for %s when it was not supposed to", monitorTenFile)
}
}
func TestETHWithdrawnTooEarlyNoClaimInBlock(t *testing.T) {
// We DO NOT expect an alert to be fired when there is no claim in the current block
// set the params
params := map[string]any{
"disputeGame": "0x00000000000000000000000000000000000000AA",
"multicall3": "0x00000000000000000000000000000000000000CC",
}
// read in the gate file
data, err := ReadGateFile(monitorTenFile)
if err != nil {
t.Errorf("Error reading file %s: %v", monitorTenFile, err)
}
// setup the mocks
mocks := map[string]any{
"addressesInTrace": []any{"0x00000000000000000000000000000000000000AA"},
"delayedWETH": "0x0000000000000000000000000000000000000000",
"claims": [][]interface{}{},
"withdrawals": [][]interface{}{
// does not have matching claim in this block so this will get parsed out
{"0x0000000000000000000000000000000000000001", 200},
},
"delayTime": 10,
"currTimestamp": 2000,
"unlockTimestamps": []int{1000}, // unlock happened after delayTime
"unlocks": [][]interface{}{
{90, "0x00000000000000000000000000000000000000AA", []interface{}{"0x0000000000000000000000000000000000000001", 100}},
{90, "0x00000000000000000000000000000000000000BB", []interface{}{"0x0000000000000000000000000000000000000002", 100}},
},
}
// call out to hexagate API to run the gate file with params and mocks
failed, exceptions, trace, err := HandleValidateRequest(data, params, mocks)
if err != nil {
t.Errorf("Error handling validate request for %s: %v", monitorTenFile, err)
}
// check if the validate request threw any exceptions
if len(exceptions) > 0 {
fmt.Println(trace)
t.Errorf("Exceptions for %s: %v", monitorTenFile, exceptions)
}
// we DO NOT expect to see the alert fired
if len(failed) > 0 {
fmt.Println(trace)
t.Errorf("Monitor fired an alert for %s when it was not supposed to", monitorTenFile)
}
}
func TestETHWithdrawnTooEarlyNoFilterAddress(t *testing.T) {
// We DO NOT expect an alert to be fired when there is no address in the filter trace
// set the params
params := map[string]any{
"disputeGame": "0x00000000000000000000000000000000000000AA",
"multicall3": "0x00000000000000000000000000000000000000BB",
}
// read in the gate file
data, err := ReadGateFile(monitorTenFile)
if err != nil {
t.Errorf("Error reading file %s: %v", monitorTenFile, err)
}
// setup the mocks
mocks := map[string]any{
"delayedWETH": "0x0000000000000000000000000000000000000000",
"claims": [][]interface{}{
{"0x0000000000000000000000000000000000000001"},
},
// note claims and withdrawals are effectively the same thing, we just need to match claim calls
// to their corresponding withdrawal call on a separate contract
"withdrawals": [][]interface{}{
{"0x0000000000000000000000000000000000000001", 100},
},
"delayTime": 100,
"currTimestamp": 1099,
"unlockTimestamps": []int{1000, 1000}, // both unlocks happened earlier than the delayTime
"unlocks": [][]interface{}{
// this unlock on its own would be fine as the delayTime has elapsed
{50, "0x00000000000000000000000000000000000000AA", []interface{}{"0x0000000000000000000000000000000000000001", 50}},
// however, this unlock call is made before the delayTime has passed, and the delay resets each time a new unlock
// is called against the same address for the same dispute game, so this will trigger the alert
{101, "0x00000000000000000000000000000000000000AA", []interface{}{"0x0000000000000000000000000000000000000001", 50}},
},
}
// call out to hexagate API to run the gate file with params and mocks
failed, exceptions, trace, err := HandleValidateRequest(data, params, mocks)
if err != nil {
t.Errorf("Error handling validate request for %s: %v", monitorTenFile, err)
}
// check if the validate request threw any exceptions
if len(exceptions) > 0 {
fmt.Println(trace)
t.Errorf("Exceptions for %s: %v", monitorTenFile, exceptions)
}
// we DO NOT expect to see the alert fired
if len(failed) > 0 {
fmt.Println(trace)
t.Errorf("Monitor fired an alert for %s when it was not supposed to", monitorTenFile)
}
}