Files: de87b9b68f0638e5deda64385e6c49c506325b8e / tests / interfaceRunner.js
2404 bytesRaw
1 | const tape = require('tape') |
2 | const fs = require('fs') |
3 | const path = require('path') |
4 | const Vertex = require('merkle-trie') |
5 | const Address = require('../deps/address') |
6 | const U256 = require('../deps/u256') |
7 | const Block = require('../deps/block.js') |
8 | |
9 | const Kernel = require('../index.js') |
10 | const Environment = require('../testEnvironment.js') |
11 | |
12 | const dir = path.join(__dirname, '/interface') |
13 | // get the test names |
14 | let tests = fs.readdirSync(dir).filter((file) => file.endsWith('.wast')) |
15 | |
16 | runTests(tests) |
17 | |
18 | function runTests (tests) { |
19 | for (let testName of tests) { |
20 | testName = testName.split('.')[0] |
21 | tape(testName, async (t) => { |
22 | // Compile Command |
23 | const rootVertex = new Vertex() |
24 | const code = fs.readFileSync(`${dir}/${testName}.wasm`) |
25 | const envData = JSON.parse(fs.readFileSync(`${dir}/${testName}.json`).toString()) |
26 | |
27 | envData.block = new Block() |
28 | envData.caller = new Address(envData.caller) |
29 | envData.address = new Address(envData.address) |
30 | envData.block.header.coinbase = new Address(envData.coinbase) |
31 | envData.origin = new Address(envData.origin) |
32 | envData.callData = new Buffer(envData.callData.slice(2), 'hex') |
33 | envData.callValue = new U256(envData.callValue) |
34 | |
35 | for (let address in envData.state) { |
36 | const account = envData.state[address] |
37 | const accountVertex = new Vertex() |
38 | |
39 | accountVertex.set('code', new Vertex({ |
40 | value: new Buffer(account.code.slice(2), 'hex') |
41 | })) |
42 | |
43 | accountVertex.set('balance', new Vertex({ |
44 | value: new Buffer(account.balance.slice(2), 'hex') |
45 | })) |
46 | |
47 | for (let key in account.storage) { |
48 | accountVertex.set(['storage', ...new Buffer(key.slice(2), 'hex')], new Vertex({ |
49 | value: new Buffer(account.storage[key].slice(2), 'hex') |
50 | })) |
51 | } |
52 | |
53 | const path = ['accounts', ...new Buffer(address.slice(2), 'hex')] |
54 | rootVertex.set(path, accountVertex) |
55 | } |
56 | |
57 | envData.state = await rootVertex.get(['accounts', ...envData.address.toBuffer()]) |
58 | const kernel = new Kernel({ |
59 | code: code |
60 | }) |
61 | const env = new Environment(envData) |
62 | |
63 | try { |
64 | await kernel.run(env) |
65 | } catch (e) { |
66 | t.fail('Exception: ' + e) |
67 | console.error('FAIL') |
68 | console.error(e) |
69 | } finally { |
70 | t.pass(testName) |
71 | console.log('done') |
72 | } |
73 | t.end() |
74 | }) |
75 | } |
76 | } |
77 |
Built with git-ssb-web