Files: 8ced6c46cc8f744d128a65d00c117a79e68918b8 / tests / interfaceRunner.js
2453 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 Block = require('../deps/block') |
7 | const U256 = require('../deps/u256') |
8 | // TODO remove fakeblockchain |
9 | const fakeBlockChain = require('../fakeBlockChain.js') |
10 | const Hypervisor = require('../hypervisor.js') |
11 | const Message = require('../message.js') |
12 | const common = require('../common') |
13 | |
14 | const dir = path.join(__dirname, '/interface') |
15 | // get the test names |
16 | let tests = fs.readdirSync(dir).filter((file) => file.endsWith('.wast')) |
17 | tests = ['call.wast'] |
18 | |
19 | runTests(tests) |
20 | |
21 | function runTests (tests) { |
22 | for (let testName of tests) { |
23 | testName = testName.split('.')[0] |
24 | tape(testName, async (t) => { |
25 | const hypervisor = new Hypervisor() |
26 | const rootVertex = hypervisor.state |
27 | const code = fs.readFileSync(`${dir}/${testName}.wasm`) |
28 | const envData = JSON.parse(fs.readFileSync(`${dir}/${testName}.json`).toString()) |
29 | |
30 | for (let address in envData.state) { |
31 | const account = envData.state[address] |
32 | const accountVertex = new Vertex() |
33 | |
34 | accountVertex.set('code', new Vertex({ |
35 | value: code |
36 | })) |
37 | |
38 | accountVertex.set('balance', new Vertex({ |
39 | value: new Buffer(account.balance.slice(2), 'hex') |
40 | })) |
41 | |
42 | for (let key in account.storage) { |
43 | accountVertex.set(['storage', ...new Buffer(key.slice(2), 'hex')], new Vertex({ |
44 | value: new Buffer(account.storage[key].slice(2), 'hex') |
45 | })) |
46 | } |
47 | |
48 | const path = ['accounts', address] |
49 | rootVertex.set(path, accountVertex) |
50 | } |
51 | |
52 | const block = new Block() |
53 | block.header.coinbase = new Address(envData.coinbase) |
54 | |
55 | rootVertex.set('block', new Vertex({ |
56 | value: block |
57 | })) |
58 | |
59 | rootVertex.set('blockchain', new Vertex({ |
60 | value: fakeBlockChain |
61 | })) |
62 | |
63 | const message = new Message() |
64 | message.to = ['accounts', envData.caller, common.PARENT, envData.address, 'code'] |
65 | message.data = new Buffer(envData.callData.slice(2), 'hex') |
66 | message.value = new U256(envData.callValue) |
67 | message.gas = 1000000 |
68 | |
69 | try { |
70 | await hypervisor.send(message) |
71 | console.log('done') |
72 | } catch (e) { |
73 | t.fail('Exception: ' + e) |
74 | console.error('FAIL') |
75 | console.error(e) |
76 | } finally { |
77 | t.pass(testName) |
78 | } |
79 | t.end() |
80 | }) |
81 | } |
82 | } |
83 |
Built with git-ssb-web