git ssb

0+

wanderer🌟 / js-primea-hypervisor



Commit 48df42c8ab634c0d9fc7597e52106352878124fa

fix message response

wanderer committed on 2/28/2017, 4:01:11 PM
Parent: 9bb00a66c6590ce780568e10d649be74c66ce1e6

Files changed

EVMinterface.jschanged
hypervisor.jschanged
index.jschanged
message.jschanged
port.jschanged
portManager.jschanged
stateInterface.jschanged
tests/apiTests.jschanged
tests/interfaceRunner.jschanged
wasmAgent.jschanged
EVMinterface.jsView
@@ -33,13 +33,8 @@
3333 }
3434 })
3535 }
3636
37- async initialize () {
38- this.block = await this.kernel.send(common.getterMessage('block', [common.ROOT]))
39- this.blockchain = await this.kernel.send(common.getterMessage('blockchain', [common.ROOT]))
40- }
41-
4237 static get name () {
4338 return 'ethereum'
4439 }
4540
@@ -168,10 +163,9 @@
168163 * @param {integer} offset
169164 */
170165 getCaller (offset) {
171166 this.takeGas(2)
172-
173- const caller = this.message.from[1]
167+ const caller = this.message.from[2]
174168 this.setMemory(offset, ADDRESS_SIZE_BYTES, new Buffer(caller.slice(2), 'hex'))
175169 }
176170
177171 /**
hypervisor.jsView
@@ -30,23 +30,8 @@
3030 this.root.send(message)
3131 return message.result()
3232 }
3333
34- async get (path) {
35- let lastKernel = this.root
36- let state = this.state
37- while (path.length) {
38- const name = path.unshift()
39- state = await state.get(name)
40- const kernel = new Kernel({
41- state: state,
42- parent: lastKernel
43- })
44- lastKernel = kernel
45- }
46- return lastKernel
47- }
48-
4934 addVM (type, handler) {
5035 codeHandlers.handlers.type = handler
5136 }
5237 }
index.jsView
@@ -15,21 +15,20 @@
1515 this.path = state.path
1616 this.imports = opts.imports || [imports]
1717 this.ports = new PortManager(state, opts.parent, Kernel)
1818 this._sentAtomicMessages = []
19- // rename sandbox?
2019 this._vm = (opts.codeHandler || codeHandler).init(this.code)
2120 this._state = 'idle'
2221 this.ports.on('message', index => {
2322 this.runNextMessage(index)
2423 })
2524 }
2625
2726 runNextMessage (index = 0) {
28- this.ports.peek(index).then(message => {
27+ return this.ports.peek(index).then(message => {
2928 if (message && (message.isCyclic(this) || this._state === 'idle')) {
3029 this.ports.remove(index)
31- this.run(message)
30+ return this.run(message)
3231 } else {
3332 this._state = 'idle'
3433 this.emit('idle')
3534 }
@@ -83,24 +82,25 @@
8382 return result
8483 }
8584
8685 async send (message) {
86+ let portName = message.nextPort()
8787 // replace root with parent path to root
88- let portName = message.nextPort()
8988 if (portName === common.ROOT) {
9089 message.to.shift()
9190 message.to = new Array(this.path.length).fill(common.PARENT).concat(message.to)
9291 portName = common.PARENT
9392 }
94- message.addVistedKernel(message)
93+ message.addVistedKernel(this)
9594 this.lastMessage = message
9695 // console.log(portName, message)
9796 const port = await this.ports.get(portName)
9897 // save the atomic messages for possible reverts
9998 if (message.atomic) {
10099 this._sentAtomicMessages.push(message)
101100 }
102- return port.send(message)
101+ port.send(message)
102+ return message.result()
103103 }
104104
105105 shutdown () {}
106106 }
message.jsView
@@ -21,12 +21,12 @@
2121 }
2222
2323 _finish (result) {
2424 if (this.atomic) {
25- this._vistedKernels.pop()
26- if (!this._vistedKernels.length) {
25+ if (this._vistedKernels.length === this.hops) {
2726 this._resolve(result)
2827 }
28+ this._vistedKernels.pop()
2929 }
3030 }
3131
3232 result () {
@@ -43,7 +43,7 @@
4343 }
4444 }
4545
4646 isCyclic (kernel) {
47- return this.sync && this._vistedKernels.some(process => process === kernel)
47+ return this.atomic && this._vistedKernels.some(process => process === kernel)
4848 }
4949 }
port.jsView
@@ -1,20 +1,26 @@
11 const EventEmitter = require('events')
22
33 module.exports = class Port extends EventEmitter {
4- constructor () {
4+ constructor (name) {
55 super()
6+ this.name = name
7+ this.connected = false
68 }
79
810 connect (destPort) {
9- this.destPort = destPort
10- destPort.destPort = this
11+ if (!this.connected) {
12+ this.destPort = destPort
13+ destPort.destPort = this
14+ this.connected = true
15+ }
1116 }
1217
1318 async send (message) {
14- return this.destPort.recieve(message)
19+ this.destPort.recieve(message)
1520 }
1621
1722 async recieve (message) {
23+ message.from.push(this.name)
1824 this.emit('message', message)
1925 }
2026 }
portManager.jsView
@@ -8,12 +8,13 @@
88 this._queue = []
99 this.state = state
1010 this.Kernel = KernelContructor
1111 // set up the parent port
12- const parentPort = new Port()
12+ const parentPort = new Port(common.PARENT)
1313 parentPort.on('message', message => {
1414 this._recieveMessage(message)
1515 })
16+
1617 // create the cache
1718 this.cache = new Map()
1819 this.cache.set(common.PARENT, parentPort)
1920 }
@@ -23,11 +24,12 @@
2324 this.emit('message', index)
2425 }
2526
2627 async get (name) {
28+ console.log(name)
2729 let port = this.cache.get(name)
2830 if (!port) {
29- port = new Port()
31+ port = new Port(name)
3032 port.on('message', message => {
3133 this._recieveMessage(message)
3234 })
3335 // create destination kernel
stateInterface.jsView
@@ -1,6 +1,9 @@
1+const assert = require('assert')
2+
13 module.exports = class StateInterface {
2- constuctor (state) {
4+ constructor (state) {
5+ assert(state, 'must have state')
36 this.state = state
47 }
58
69 set (name, value) {
tests/apiTests.jsView
@@ -42,15 +42,12 @@
4242 }))
4343 }
4444 })
4545
46- await hypervisor.send(new Message({
46+ const message = new Message({
4747 to: path
48- }))
49-
50- try {
51- await hypervisor.state.get(path2.concat(['key']))
52- } catch (e) {
53- t.equal(typeof e, 'object')
54- t.end()
55- }
48+ })
49+ hypervisor.send(message)
50+ const result = await message.result()
51+ t.equals(result.exception, true)
52+ t.end()
5653 })
tests/interfaceRunner.jsView
@@ -8,14 +8,14 @@
88 // TODO remove fakeblockchain
99 const fakeBlockChain = require('../fakeBlockChain.js')
1010 const Hypervisor = require('../hypervisor.js')
1111 const Message = require('../message.js')
12-const common = require('../common.js')
12+const common = require('../common')
1313
1414 const dir = path.join(__dirname, '/interface')
1515 // get the test names
1616 let tests = fs.readdirSync(dir).filter((file) => file.endsWith('.wast'))
17-// tests = ['address.wast']
17+tests = ['call.wast']
1818
1919 runTests(tests)
2020
2121 function runTests (tests) {
@@ -60,16 +60,16 @@
6060 value: fakeBlockChain
6161 }))
6262
6363 const message = new Message()
64- message.to = ['accounts', envData.address, 'code']
65- message.origin = new Address(envData.origin)
64+ message.to = ['accounts', envData.caller, common.PARENT, envData.address, 'code']
6665 message.data = new Buffer(envData.callData.slice(2), 'hex')
6766 message.value = new U256(envData.callValue)
6867 message.gas = 1000000
6968
7069 try {
7170 await hypervisor.send(message)
71+ console.log('done')
7272 } catch (e) {
7373 t.fail('Exception: ' + e)
7474 console.error('FAIL')
7575 console.error(e)
wasmAgent.jsView
@@ -13,16 +13,14 @@
1313 const responses = {}
1414 /**
1515 * Builds a import map with an array of given interfaces
1616 */
17- async function buildImports (kernelApi, kernel, imports) {
17+ function buildImports (kernelApi, kernel, imports) {
1818 const importMap = {}
1919 for (const Import of imports) {
2020 const response = responses[Import.name] = {}
2121 const newIterface = new Import(kernelApi, message, response)
2222 importMap[Import.name] = newIterface.exports
23- // initailize the import
24- await newIterface.initialize()
2523 }
2624 return importMap
2725 }
2826
@@ -42,9 +40,9 @@
4240 },
4341 kernel: kernel
4442 }
4543
46- const initializedImports = await buildImports(interfaceApi, kernel, imports)
44+ const initializedImports = buildImports(interfaceApi, kernel, imports)
4745 instance = WebAssembly.Instance(this._module, initializedImports)
4846
4947 if (instance.exports.main) {
5048 instance.exports.main()

Built with git-ssb-web