-
Notifications
You must be signed in to change notification settings - Fork 26
/
main_test.go
392 lines (321 loc) · 10.4 KB
/
main_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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
package main
import (
"encoding/json"
"fmt"
"net/http"
"strconv"
"testing"
"github.com/parnurzeal/gorequest"
)
const local = "http://localhost:3001/api"
const remote = "https://blk-api.goerli.ethstats.io/api/v3"
func TestBlock(t *testing.T) {
start := int64(1350000)
end := int64(1350100)
for i := start; i <= end; i++ {
t.Run(strconv.FormatInt(i, 10), func(tt *testing.T) {
compareBlockDetails(i, tt)
if i%300 == 0 {
if i+300 > end {
compareBlockRange(i, end, tt)
} else {
compareBlockRange(i, i+300, tt)
}
}
})
}
}
func TestAccountTxs(t *testing.T) {
accounts := []struct {
Address, Filters string
}{
{"8ced5ad0d8da4ec211c17355ed3dbfec4cf0e5b9", "?includedInBlock=1353543&limit=5"},
{"454a62a6201d9953b1bbc0303c6ff9599d742ac3", "?includedInBlock=1353595&limit=5"},
{"a94a6a58dd1fc6a8a3e8e75d123f8e7f30026c00", "?includedInBlock=1353026&limit=5"},
{"62083c80353df771426d209ef578619ee68d5c7a", "?includedInBlock=1353596&limit=5"},
{"483b937b035a70526a9e8e60a2dddfcfb5fe3b11", "?includedInBlock=1353589&limit=5"},
}
for _, a := range accounts {
t.Run(a.Address, func(tt *testing.T) {
compareAccountTxs(a.Address, a.Filters, tt)
})
}
}
func compareBlockDetails(number int64, t *testing.T) {
localData := getBlock(number, local, t)
remoteData := getBlock(number, remote, t)
ignored := map[string]struct{}{
"blockBeneficiaryReward": {},
"blockTime": {},
"numberOfContractMsgs": {},
"alethioComment": {},
"hasBeneficiaryAlias": {},
"blockMixHash": {},
"blockNonce": {},
}
for blockProperty, remoteValue := range remoteData {
if _, ok := ignored[blockProperty]; ok {
t.Logf("IGNORED: `%s`", blockProperty)
continue
}
if localValue, exists := localData[blockProperty]; exists {
if blockProperty == "txs" {
compareBlockTxs(localValue, remoteValue, t)
continue
}
if blockProperty == "includesUncle" {
continue
}
if remoteValue != localValue {
t.Errorf("ERROR: local property `%s` (=%v) is different on remote (=%v)", blockProperty, localValue, remoteValue)
}
} else {
t.Errorf("ERROR: expected property `%s` in local response but did not find it", blockProperty)
}
}
}
func compareBlockTxs(local, remote interface{}, t *testing.T) {
l := local.([]interface{})
r := remote.([]interface{})
ignored := map[string]struct{}{
"createContractMsgsTriggered": {},
"totalContractMsgsTriggered": {},
"type": {},
}
for index, remoteTxValue := range r {
t.Logf("TX: checking tx at index %d", index)
issues := 0
remoteTx := remoteTxValue.(map[string]interface{})
compareTx(remoteTx["txHash"].(string), t)
compareTxLogEntries(remoteTx["txHash"].(string), t)
if len(l) > index {
localTx := l[index].(map[string]interface{})
for txProperty, remoteValue := range remoteTx {
if _, ok := ignored[txProperty]; ok {
t.Logf("IGNORED: `%s`", txProperty)
continue
}
if localValue, exists := localTx[txProperty]; exists {
if remoteValue != localValue {
issues++
t.Errorf("ERROR: local property `txs.%d.%s` (=%v) is different on remote (=%v)", index, txProperty, localValue, remoteValue)
}
} else {
issues++
t.Errorf("ERROR: expected property `txs.%d.%s` in local response but did not find it", index, txProperty)
}
}
} else {
issues++
t.Errorf("ERROR: could not find local tx at index %d", index)
}
if issues == 0 {
t.Logf("TX: all good")
}
}
}
func compareBlockRange(start, end int64, t *testing.T) {
localData := getBlockRange(start, end, local, t)
remoteData := getBlockRange(start, end, remote, t)
ignored := map[string]struct{}{
"totalTxValue": {},
}
for index, remoteBlockValue := range remoteData {
t.Logf("BLOCK-RANGE: checking block at index %d", index)
issues := 0
remoteBlock := remoteBlockValue.(map[string]interface{})
if len(localData) > index {
localBlock := localData[index].(map[string]interface{})
for property, remoteValue := range remoteBlock {
if _, ok := ignored[property]; ok {
t.Logf("IGNORED: `%s`", property)
continue
}
if localValue, exists := localBlock[property]; exists {
if remoteValue != localValue {
issues++
t.Errorf("ERROR: local property `block-range.%d.%s` (=%v) is different on remote (=%v)", index, property, localValue, remoteValue)
}
} else {
issues++
t.Errorf("ERROR: expected property `block-range.%d.%s` in local response but did not find it", index, property)
}
}
} else {
issues++
t.Errorf("ERROR: could not find local block at index %d in block range", index)
}
if issues == 0 {
t.Logf("BLOCK-RANGE: all good")
}
}
}
func compareTx(hash string, t *testing.T) {
localData := getTx(hash, local, t)
remoteData := getTx(hash, remote, t)
issues := 0
t.Logf("TX: checking %s", hash)
ignored := map[string]struct{}{
"msgOutput": {},
"contractMsgsTriggered": {},
"firstSeenAt": {},
"msgError": {},
"msgErrorString": {},
"blockMsgValidationIndex": {},
"msgPayloadDecoded": {},
"tokenTransfersTriggered": {},
"totalContractMsgsTriggered": {},
"type": {},
}
for property, remoteValue := range remoteData {
if _, ok := ignored[property]; ok {
t.Logf("IGNORED: `%s`", property)
continue
}
if localValue, exists := localData[property]; exists {
if remoteValue != localValue {
issues++
t.Errorf("ERROR: local property `%s` (=%v) is different on remote (=%v)", property, localValue, remoteValue)
}
} else {
issues++
t.Errorf("ERROR: expected property `%s` in local response but did not find it", property)
}
}
if issues == 0 {
t.Logf("TX: all good")
}
}
func compareTxLogEntries(hash string, t *testing.T) {
localData := getTxLogEntries(hash, local, t)
remoteData := getTxLogEntries(hash, remote, t)
ignored := map[string]struct{}{
"txMsgValidationIndex": {},
"eventDecoded": {},
"eventDecodedError": {},
}
t.Logf("LOG-ENTRIES: checking for tx %s", hash)
for index, remoteLogValue := range remoteData {
t.Logf("LOG-ENTRIES: checking log at index %d", index)
issues := 0
remoteLog := remoteLogValue.(map[string]interface{})
if len(localData) > index {
localLog := localData[index].(map[string]interface{})
for property, remoteValue := range remoteLog {
if _, ok := ignored[property]; ok {
t.Logf("IGNORED: `%s`", property)
continue
}
if localValue, exists := localLog[property]; exists {
if property == "hasLogTopics" {
localTopics := localValue.([]interface{})
for i, topic := range remoteValue.([]interface{}) {
if len(localTopics) > i {
if topic != localTopics[i] {
issues++
t.Errorf("ERROR: local property `tx.log-entries.%d.%s` (=%v) is different on remote (=%v)", index, property, localValue, remoteValue)
}
} else {
issues++
t.Errorf("ERROR: log topic at index %d does not exist on local", i)
}
}
continue
}
if remoteValue != localValue {
issues++
t.Errorf("ERROR: local property `tx.log-entries.%d.%s` (=%v) is different on remote (=%v)", index, property, localValue, remoteValue)
}
} else {
issues++
t.Errorf("ERROR: expected property `tx.log-entries.%d.%s` in local response but did not find it", index, property)
}
}
} else {
issues++
t.Errorf("ERROR: could not find local log at index %d in tx log entries", index)
}
if issues == 0 {
t.Logf("LOG-ENTRIES: all good")
}
}
}
func compareAccountTxs(address, filter string, t *testing.T) {
localData := getAccountTxs(address, local, filter, t)
remoteData := getAccountTxs(address, remote, filter, t)
ignored := map[string]struct{}{
"msgErrorString": {},
"fee": {},
"type": {},
"blockMsgValidationIndex": {},
}
t.Logf("ACCOUNT-TXS: checking for account %s", address)
for index, remoteTxValue := range remoteData {
t.Logf("ACCOUNT-TXS: checking tx at index %d", index)
issues := 0
remoteTx := remoteTxValue.(map[string]interface{})
if len(localData) > index {
localTx := localData[index].(map[string]interface{})
for property, remoteValue := range remoteTx {
if _, ok := ignored[property]; ok {
t.Logf("IGNORED: `%s`", property)
continue
}
if localValue, exists := localTx[property]; exists {
if remoteValue != localValue {
issues++
t.Errorf("ERROR: local property `account.txs.%d.%s` (=%v) is different on remote (=%v)", index, property, localValue, remoteValue)
}
} else {
issues++
t.Errorf("ERROR: expected property `account.txs.%d.%s` in local response but did not find it", index, property)
}
}
} else {
issues++
t.Errorf("ERROR: could not find local tx at index %d in account txs", index)
}
if issues == 0 {
t.Logf("ACCOUNT-TXS: all good")
}
}
}
func execQuery(url string, t *testing.T) map[string]interface{} {
request := gorequest.New()
resp, body, errs := request.Get(url).End()
if resp.StatusCode != http.StatusOK {
t.Errorf("expected status code 200, got %d", resp.StatusCode)
}
if len(errs) > 0 {
t.Errorf("did not expect errors, got %v", errs)
}
res := make(map[string]interface{})
err := json.Unmarshal([]byte(body), &res)
if err != nil {
t.Errorf("got error while unmarshalling response: %s", err)
}
return res
}
func getBlock(number int64, source string, t *testing.T) map[string]interface{} {
res := execQuery(fmt.Sprintf("%s/block/%d", source, number), t)
return res["data"].(map[string]interface{})
}
func getBlockRange(start, end int64, source string, t *testing.T) []interface{} {
res := execQuery(fmt.Sprintf("%s/block-range/%d/%d", source, start, end), t)
return res["data"].([]interface{})
}
func getTx(hash string, source string, t *testing.T) map[string]interface{} {
res := execQuery(fmt.Sprintf("%s/tx/%s", source, hash), t)
return res["data"].(map[string]interface{})
}
func getTxLogEntries(hash string, source string, t *testing.T) []interface{} {
res := execQuery(fmt.Sprintf("%s/tx/%s/log-entries", source, hash), t)
if res["data"] == nil {
return []interface{}{}
}
return res["data"].([]interface{})
}
func getAccountTxs(address string, source string, filter string, t *testing.T) []interface{} {
res := execQuery(fmt.Sprintf("%s/account/%s/txs%s", source, address, filter), t)
return res["data"].([]interface{})
}