git ssb

0+

wanderer🌟 / js-primea-hypervisor



Tree: c2c35a4216304056cc0acac26cf1f3791353f429

Files: c2c35a4216304056cc0acac26cf1f3791353f429 / tests / interfaceRunner.js

2342 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')
13
14const dir = path.join(__dirname, '/interface')
15// get the test names
16let tests = fs.readdirSync(dir).filter((file) => file.endsWith('.wast'))
17// tests = ['sstore.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 rootVertex.set('blockchain', new Vertex({
53 value: fakeBlockChain
54 }))
55
56 const block = new Block()
57 block.header.coinbase = new Address(envData.coinbase)
58
59 const message = new Message()
60 message.to = ['accounts', envData.caller, common.PARENT, envData.address, 'code']
61 message.data = new Buffer(envData.callData.slice(2), 'hex')
62 message.value = new U256(envData.callValue)
63 message.gas = 1000000
64 message.block = block
65 message.blockchain = fakeBlockChain
66
67 const results = await hypervisor.send(message)
68 // console.log(results)
69 t.equals(results.exception, undefined)
70 t.end()
71 })
72 }
73}
74

Built with git-ssb-web