Files: a01401f258acd7e0b1f4ecbe82cdd1bc26048b4c / tests / interfaceRunner.js
2479 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 | |
8 | const Kernel = require('../index.js') |
9 | const Environment = require('../testEnvironment.js') |
10 | |
11 | const dir = path.join(__dirname, '/interface') |
12 | // get the test names |
13 | let tests = fs.readdirSync(dir).filter((file) => file.endsWith('.wast')) |
14 | // tests = ['callDataSize.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.caller = new Address(envData.caller) |
28 | envData.address = new Address(envData.address) |
29 | envData.origin = new Address(envData.origin) |
30 | envData.callData = new Buffer(envData.callData.slice(2), 'hex') |
31 | envData.callValue = new U256(envData.callValue) |
32 | |
33 | for (let address in envData.state) { |
34 | const account = envData.state[address] |
35 | const accountVertex = new Vertex() |
36 | |
37 | accountVertex.set('code', new Vertex({ |
38 | value: new Buffer(account.code.slice(2), 'hex') |
39 | })) |
40 | |
41 | accountVertex.set('balance', new Vertex({ |
42 | value: new Buffer(account.balance.slice(2), 'hex') |
43 | })) |
44 | |
45 | for (let key in account.storage) { |
46 | accountVertex.set(['storage', ...new Buffer(key.slice(2), 'hex')], new Vertex({ |
47 | value: new Buffer(account.storage[key].slice(2), 'hex') |
48 | })) |
49 | } |
50 | |
51 | const path = ['accounts', ...new Buffer(address.slice(2), 'hex')] |
52 | rootVertex.set(path, accountVertex) |
53 | } |
54 | |
55 | const state = envData.state = await rootVertex.get(['accounts', ...envData.address.toBuffer()]) |
56 | state.value = code |
57 | |
58 | const env = new Environment(envData) |
59 | env.block.header.coinbase = new Address(envData.coinbase) |
60 | |
61 | rootVertex.set('block', new Vertex({ |
62 | value: env.block |
63 | })) |
64 | |
65 | const kernel = new Kernel({ |
66 | state: state |
67 | }) |
68 | |
69 | try { |
70 | await kernel.run(env) |
71 | } catch (e) { |
72 | t.fail('Exception: ' + e) |
73 | console.error('FAIL') |
74 | console.error(e) |
75 | } finally { |
76 | t.pass(testName) |
77 | console.log('done') |
78 | } |
79 | t.end() |
80 | }) |
81 | } |
82 | } |
83 |
Built with git-ssb-web