git ssb

0+

wanderer🌟 / js-primea-hypervisor



Tree: 7cdddef81c118db3b188949697b28350cbe2e343

Files: 7cdddef81c118db3b188949697b28350cbe2e343 / tests / interfaceRunner.js

2571 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 Kernel = require('../index.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 = ['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 // Compile Command
26 const rootVertex = new Vertex()
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 const callerState = await rootVertex.get(['accounts', envData.caller, 'code'])
71 const caller = new Kernel({state: callerState})
72 try {
73 await caller.send(common.ROOT, message)
74 } catch (e) {
75 t.fail('Exception: ' + e)
76 console.error('FAIL')
77 console.error(e)
78 } finally {
79 t.pass(testName)
80 }
81 t.end()
82 })
83 }
84}
85

Built with git-ssb-web