git ssb

0+

wanderer🌟 / js-primea-hypervisor



Tree: ccc86c04a14c0346d7695e768be928faf4c41267

Files: ccc86c04a14c0346d7695e768be928faf4c41267 / tests / interfaceRunner.js

2570 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')
12
13const dir = path.join(__dirname, '/interface')
14// get the test names
15let tests = fs.readdirSync(dir).filter((file) => file.endsWith('.wast'))
16// tests = ['sstore.wast']
17
18runTests(tests)
19
20function runTests (tests) {
21 for (let testName of tests) {
22 testName = testName.split('.')[0]
23 tape(testName, async (t) => {
24 // Compile Command
25 const rootVertex = new Vertex()
26 const code = fs.readFileSync(`${dir}/${testName}.wasm`)
27 const envData = JSON.parse(fs.readFileSync(`${dir}/${testName}.json`).toString())
28
29 for (let address in envData.state) {
30 const account = envData.state[address]
31 const accountVertex = new Vertex()
32
33 accountVertex.set('code', new Vertex({
34 value: code
35 }))
36
37 accountVertex.set('balance', new Vertex({
38 value: new Buffer(account.balance.slice(2), 'hex')
39 }))
40
41 for (let key in account.storage) {
42 accountVertex.set(['storage', ...new Buffer(key.slice(2), 'hex')], new Vertex({
43 value: new Buffer(account.storage[key].slice(2), 'hex')
44 }))
45 }
46
47 const path = ['accounts', address]
48 rootVertex.set(path, accountVertex)
49 }
50
51 const block = new Block()
52 block.header.coinbase = new Address(envData.coinbase)
53
54 rootVertex.set('block', new Vertex({
55 value: block
56 }))
57
58 rootVertex.set('blockchain', new Vertex({
59 value: fakeBlockChain
60 }))
61
62 const message = new Message()
63 // message.from = new Address()
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(Kernel.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