git ssb

0+

wanderer🌟 / js-primea-hypervisor



Tree: f009ff949132d16bfd8b42b7c1871ca88a615575

Files: f009ff949132d16bfd8b42b7c1871ca88a615575 / tests / interfaceRunner.js

2364 bytesRaw
1const tape = require('tape')
2const fs = require('fs')
3const path = require('path')
4const Vertex = require('merkle-trie')
5const Address = require('../deps/address')
6const U256 = require('../deps/u256')
7const Block = require('../deps/block.js')
8
9const Kernel = require('../index.js')
10const Environment = require('../testEnvironment.js')
11
12const dir = path.join(__dirname, '/interface')
13// get the test names
14let tests = fs.readdirSync(dir).filter((file) => file.endsWith('.wast'))
15
16runTests(tests)
17
18function 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 = [...new Buffer(address.slice(2), 'hex')]
54 rootVertex.set(path, accountVertex)
55 }
56
57 envData.state = await rootVertex.get([...envData.address.toBuffer()])
58 const kernel = new Kernel({code: code})
59 const env = new Environment(envData)
60
61 try {
62 await kernel.run(env)
63 } catch (e) {
64 t.fail('Exception: ' + e)
65 console.error('FAIL')
66 console.error(e)
67 } finally {
68 t.pass(testName)
69 console.log('done')
70 }
71 t.end()
72 })
73 }
74}
75

Built with git-ssb-web