forked from holiman/goconsensus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
426 lines (404 loc) · 11.8 KB
/
main.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
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
package main
import (
"bytes"
"context"
"crypto/sha1"
"encoding/json"
"fmt"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/rpc"
"io/ioutil"
"math/big"
"os"
"path/filepath"
"strings"
"sync"
"time"
common2 "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core"
"github.com/ethereum/go-ethereum/log"
"github.com/ethereum/hive/simulators/common"
)
type envvars map[string]int
var ruleset = map[string]envvars{
"Frontier": {
"HIVE_FORK_HOMESTEAD": 2000,
"HIVE_FORK_TANGERINE": 2000,
"HIVE_FORK_SPURIOUS": 2000,
"HIVE_FORK_DAO_BLOCK": 2000,
"HIVE_FORK_BYZANTIUM": 2000,
"HIVE_FORK_CONSTANTINOPLE": 2000,
"HIVE_FORK_PETERSBURG": 2000,
},
"Homestead": {
"HIVE_FORK_HOMESTEAD": 0,
"HIVE_FORK_TANGERINE": 2000,
"HIVE_FORK_SPURIOUS": 2000,
"HIVE_FORK_DAO_BLOCK": 2000,
"HIVE_FORK_BYZANTIUM": 2000,
"HIVE_FORK_CONSTANTINOPLE": 2000,
"HIVE_FORK_PETERSBURG": 2000,
},
"EIP150": {
"HIVE_FORK_HOMESTEAD": 0,
"HIVE_FORK_TANGERINE": 0,
"HIVE_FORK_SPURIOUS": 2000,
"HIVE_FORK_DAO_BLOCK": 2000,
"HIVE_FORK_BYZANTIUM": 2000,
"HIVE_FORK_CONSTANTINOPLE": 2000,
"HIVE_FORK_PETERSBURG": 2000,
},
"EIP158": {
"HIVE_FORK_HOMESTEAD": 0,
"HIVE_FORK_TANGERINE": 0,
"HIVE_FORK_SPURIOUS": 0,
"HIVE_FORK_DAO_BLOCK": 2000,
"HIVE_FORK_BYZANTIUM": 2000,
"HIVE_FORK_CONSTANTINOPLE": 2000,
"HIVE_FORK_PETERSBURG": 2000,
},
"Byzantium": {
"HIVE_FORK_HOMESTEAD": 0,
"HIVE_FORK_TANGERINE": 0,
"HIVE_FORK_SPURIOUS": 0,
"HIVE_FORK_DAO_BLOCK": 2000,
"HIVE_FORK_BYZANTIUM": 0,
"HIVE_FORK_CONSTANTINOPLE": 2000,
"HIVE_FORK_PETERSBURG": 2000,
},
"Constantinople": {
"HIVE_FORK_HOMESTEAD": 0,
"HIVE_FORK_TANGERINE": 0,
"HIVE_FORK_SPURIOUS": 0,
"HIVE_FORK_DAO_BLOCK": 2000,
"HIVE_FORK_BYZANTIUM": 0,
"HIVE_FORK_CONSTANTINOPLE": 0,
"HIVE_FORK_PETERSBURG": 2000,
},
"ConstantinopleFix": {
"HIVE_FORK_HOMESTEAD": 0,
"HIVE_FORK_TANGERINE": 0,
"HIVE_FORK_SPURIOUS": 0,
"HIVE_FORK_DAO_BLOCK": 2000,
"HIVE_FORK_BYZANTIUM": 0,
"HIVE_FORK_CONSTANTINOPLE": 0,
"HIVE_FORK_PETERSBURG": 0,
},
"FrontierToHomesteadAt5": {
"HIVE_FORK_HOMESTEAD": 5,
"HIVE_FORK_TANGERINE": 2000,
"HIVE_FORK_SPURIOUS": 2000,
"HIVE_FORK_DAO_BLOCK": 2000,
"HIVE_FORK_BYZANTIUM": 2000,
"HIVE_FORK_CONSTANTINOPLE": 2000,
"HIVE_FORK_PETERSBURG": 2000,
},
"HomesteadToEIP150At5": {
"HIVE_FORK_HOMESTEAD": 0,
"HIVE_FORK_TANGERINE": 5,
"HIVE_FORK_SPURIOUS": 2000,
"HIVE_FORK_DAO_BLOCK": 2000,
"HIVE_FORK_BYZANTIUM": 2000,
"HIVE_FORK_CONSTANTINOPLE": 2000,
"HIVE_FORK_PETERSBURG": 2000,
},
"HomesteadToDaoAt5": {
"HIVE_FORK_HOMESTEAD": 0,
"HIVE_FORK_TANGERINE": 2000,
"HIVE_FORK_SPURIOUS": 2000,
"HIVE_FORK_DAO_BLOCK": 5,
"HIVE_FORK_BYZANTIUM": 2000,
"HIVE_FORK_CONSTANTINOPLE": 2000,
"HIVE_FORK_PETERSBURG": 2000,
},
"EIP158ToByzantiumAt5": {
"HIVE_FORK_HOMESTEAD": 0,
"HIVE_FORK_TANGERINE": 0,
"HIVE_FORK_SPURIOUS": 0,
"HIVE_FORK_DAO_BLOCK": 2000,
"HIVE_FORK_BYZANTIUM": 5,
"HIVE_FORK_CONSTANTINOPLE": 2000,
"HIVE_FORK_PETERSBURG": 2000,
},
"ByzantiumToConstantinopleAt5": {
"HIVE_FORK_HOMESTEAD": 0,
"HIVE_FORK_TANGERINE": 0,
"HIVE_FORK_SPURIOUS": 0,
"HIVE_FORK_DAO_BLOCK": 2000,
"HIVE_FORK_BYZANTIUM": 0,
"HIVE_FORK_CONSTANTINOPLE": 5,
"HIVE_FORK_PETERSBURG": 2000,
},
"ByzantiumToConstantinopleFixAt5": {
"HIVE_FORK_HOMESTEAD": 0,
"HIVE_FORK_TANGERINE": 0,
"HIVE_FORK_SPURIOUS": 0,
"HIVE_FORK_DAO_BLOCK": 2000,
"HIVE_FORK_BYZANTIUM": 0,
"HIVE_FORK_CONSTANTINOPLE": 5,
"HIVE_FORK_PETERSBURG": 5,
},
}
func deliverTests(root string) chan *Testcase {
out := make(chan *Testcase)
var i, j = 0, 0
go func() {
filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
if info.IsDir() {
return nil
}
if fname := info.Name(); !strings.HasSuffix(fname, ".json") {
return nil
}
tests := make(map[string]BlockTest)
data, err := ioutil.ReadFile(path)
if err = json.Unmarshal(data, &tests); err != nil {
log.Error("error", "err", err)
return err
}
j = j + 1
for name, blocktest := range tests {
// t is declared explicitly here, if implicit := - declaration is used,
// golang will reuse the underlying object, and overwrite the object while it's being tested
// by a separate thread.
// That is also the reason that blocktest within the struct is by-value instead of by-reference
var t Testcase
t = Testcase{blockTest: blocktest, name: name, filepath: path}
if err := t.validate(); err != nil {
log.Error("error", "err", err, "test", t.name)
continue
}
i = i + 1
out <- &t
}
return nil
})
log.Info("file iterator done", "files", j, "tests", i)
close(out)
}()
return out
}
type BlocktestExecutor struct {
api common.SimulatorAPI
clients []string
}
type Testcase struct {
name string
blockTest BlockTest
nodeId string
filepath string
}
// validate returns error if the network is not defined
func (t *Testcase) validate() error {
net := t.blockTest.json.Network
if _, exist := ruleset[net]; !exist {
return fmt.Errorf("network %v not defined in ruleset", net)
}
return nil
}
// updateEnv sets environment variables from the test
func (t *Testcase) updateEnv(env map[string]string) {
// Environment variables for rules
rules := ruleset[t.blockTest.json.Network]
for k, v := range rules {
env[k] = fmt.Sprintf("%d", v)
}
// Possibly disable POW
if t.blockTest.json.SealEngine == "NoProof" {
env["HIVE_SKIP_POW"] = "1"
}
}
func toGethGenesis(test *btJSON) *core.Genesis {
genesis := &core.Genesis{
Nonce: test.Genesis.Nonce.Uint64(),
Timestamp: test.Genesis.Timestamp.Uint64(),
ExtraData: test.Genesis.ExtraData,
GasLimit: test.Genesis.GasLimit,
Difficulty: test.Genesis.Difficulty,
Mixhash: test.Genesis.MixHash,
Coinbase: test.Genesis.Coinbase,
Alloc: test.Pre,
}
return genesis
}
func (t *Testcase) artefacts() (string, string, string, error) {
key := fmt.Sprintf("%x", sha1.Sum([]byte(fmt.Sprintf("%s%s", t.filepath, t.name))))
blockFolder := fmt.Sprintf("./%s/blocks", key)
if err := os.Mkdir(fmt.Sprintf("./%s", key), 0700); err != nil {
return "", "", "", err
}
if err := os.Mkdir(blockFolder, 0700); err != nil {
return "", "", "", err
}
genesis := toGethGenesis(&(t.blockTest.json))
genBytes, _ := json.Marshal(genesis)
genesisFile := fmt.Sprintf("./%v/genesis.json", key)
if err := ioutil.WriteFile(genesisFile, genBytes, 0777); err != nil {
return "", "", "", fmt.Errorf("failed writing genesis: %v", err)
}
for i, block := range t.blockTest.json.Blocks {
rlpdata := common2.FromHex(block.Rlp)
fname := fmt.Sprintf("%s/%04d.rlp", blockFolder, i+1)
if err := ioutil.WriteFile(fname, rlpdata, 0777); err != nil {
return "", "", "", fmt.Errorf("failed writing block %d: %v", i, err)
}
}
//log.Info("Test artefacts", "testname", t.name, "testfile", t.filepath, "blockfolder", blockFolder)
return genesisFile, "", blockFolder, nil
}
func (t *Testcase) verifyGenesis(got []byte) error {
if exp := t.blockTest.json.Genesis.Hash; bytes.Compare(exp[:], got) != 0 {
return fmt.Errorf("genesis mismatch, expectd 0x%x got 0x%x", exp, got)
}
return nil
}
func (t *Testcase) verifyBestblock(got []byte) error {
if exp := t.blockTest.json.BestBlock; bytes.Compare(exp[:], got) != 0 {
return fmt.Errorf("last block mismatch, expectd 0x%x got 0x%x (%v %v)", exp, got, t.name, t.filepath)
}
return nil
}
func (be *BlocktestExecutor) run(testChan chan *Testcase) {
var i = 0
for t := range testChan {
for _, client := range be.clients {
if err := be.runTest(t, client); err != nil {
log.Error("error", "err", err)
}
i += 1
}
}
log.Info("executor finished", "num_executed", i)
}
func (be *BlocktestExecutor) runTest(t *Testcase, clientType string) error {
// get the artefacts
log.Info("Starting test", "name", t.name, "file", t.filepath)
start := time.Now()
var (
err error
)
var done = func() {
var (
errString = ""
success = (err == nil)
)
if !success {
errString = err.Error()
}
if id := t.nodeId; id != "" {
log.Info("reporting", "id", t.nodeId, "err", err)
testname := fmt.Sprintf("%s:%s", t.filepath, t.name)
if strings.HasPrefix(testname, "/tests/") {
testname = fmt.Sprintf(".%s", testname)
}
if err = be.api.AddResults(success, id, testname, errString, time.Since(start)); err != nil {
log.Info("errors occurred when adding results", "err", err)
}
} else {
log.Info("Error occurred, but no node to report to", "test", t.name, "err", err)
}
}
defer done()
genesis, _, blocks, err := t.artefacts()
t1 := time.Now()
if err != nil {
return err
}
env := map[string]string{
"CLIENT": clientType,
"HIVE_INIT_GENESIS": genesis,
"HIVE_INIT_BLOCKS": blocks,
"HIVE_FORK_DAO_VOTE": "1",
// If we don't supply these, hive will spin up a temporary container to copy
// default-values from
"HIVE_INIT_CHAIN": "ignore",
"HIVE_INIT_KEYS": "ignore",
}
t.updateEnv(env)
// spin up a node
nodeid, ip, _, err := be.api.StartNewNode(env)
t2 := time.Now()
if err != nil {
return err
}
t.nodeId = nodeid
ctx := context.Background()
rawClient, err := rpc.DialContext(ctx, fmt.Sprintf("http://%s:8545", ip.String()))
if err != nil {
err = fmt.Errorf("failed to start client: %v", err)
return err
}
genesisHash, err := getHash(rawClient, hexutil.EncodeBig(new(big.Int)))
if err != nil {
err = fmt.Errorf("failed to check genesis: %v", err)
return err
}
t3 := time.Now()
if err = t.verifyGenesis(genesisHash); err != nil {
return err
}
t4 := time.Now()
// verify postconditions
lastHash, err := getHash(rawClient, "latest")
if err != nil {
return err
}
t5 := time.Now()
if err = t.verifyBestblock(lastHash); err != nil {
return err
}
t6 := time.Now()
log.Info("Test done", "name", t.name, "artefacts", t1.Sub(start), "newnode", t2.Sub(t1), "getGenesis", t3.Sub(t2), "verifyGenesis", t4.Sub(t3), "getLatest", t5.Sub(t4), "verifyLatest", t6.Sub(t5))
return nil
}
func getHash(rawClient *rpc.Client, arg string) ([]byte, error) {
blockData := make(map[string]interface{})
if err := rawClient.Call(&blockData, "eth_getBlockByNumber", arg, false); err != nil {
// Make one more attempt
log.Info("Client connect failed, making one more attempt...")
if err = rawClient.Call(&blockData, "eth_getBlockByNumber", arg, false); err != nil {
return nil, err
}
}
if hash, exist := blockData["hash"]; !exist {
return nil, fmt.Errorf("no hash found in response")
} else {
if hexHash, ok := hash.(string); !ok {
return nil, fmt.Errorf("error: string conversion failed for %v", hash)
} else {
return common2.HexToHash(hexHash).Bytes(), nil
}
}
}
func main() {
hivesim, isset := os.LookupEnv("HIVE_SIMULATOR")
if !isset {
log.Error("simulator API not set ($HIVE_SIMULATOR)")
os.Exit(1)
}
log.Info("Hive simulator", "url", hivesim)
testpath, isset := os.LookupEnv("TESTPATH")
if !isset {
log.Error("Test path not set ($TESTPATH)")
os.Exit(1)
}
//Try to connect to the simulator host and get the client list
host := &common.SimulatorHost{
HostURI: &hivesim,
}
availableClients, _ := host.GetClientTypes()
log.Info("Got clients", "clients", availableClients)
fileRoot := fmt.Sprintf("%s/BlockchainTests/", testpath)
testCh := deliverTests(fileRoot)
var wg sync.WaitGroup
for i := 0; i < 16; i++ {
wg.Add(1)
go func() {
b := BlocktestExecutor{api: host, clients: availableClients}
b.run(testCh)
wg.Done()
}()
}
log.Info("Tests started", "num threads", 16)
wg.Wait()
}