git ssb

0+

wanderer🌟 / js-primea-hypervisor



Tree: 9bb00a66c6590ce780568e10d649be74c66ce1e6

Files: 9bb00a66c6590ce780568e10d649be74c66ce1e6 / tests / interfaceRunner.js

2454 bytesRaw
1const tape = require('tape')
2const fs = require('fs')
3const path = require('path')
4const Vertex = require('merkle-trie')
5const Address = require('../deps/address')
6const Block = require('../deps/block')
7const U256 = require('../deps/u256')
8// TODO remove fakeblockchain
9const fakeBlockChain = require('../fakeBlockChain.js')
10const Hypervisor = require('../hypervisor.js')
11const Message = require('../message.js')
12const common = require('../common.js')
13
14const dir = path.join(__dirname, '/interface')
15// get the test names
16let tests = fs.readdirSync(dir).filter((file) => file.endsWith('.wast'))
17// tests = ['address.wast']
18
19runTests(tests)
20
21function 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.address, 'code']
65 message.origin = new Address(envData.origin)
66 message.data = new Buffer(envData.callData.slice(2), 'hex')
67 message.value = new U256(envData.callValue)
68 message.gas = 1000000
69
70 try {
71 await hypervisor.send(message)
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