git ssb

0+

wanderer🌟 / js-primea-hypervisor



Commit 841228acfa1dad00f990ebe18290a46130f85d5d

cleanup

wanderer committed on 3/1/2017, 4:02:35 PM
Parent: c2c35a4216304056cc0acac26cf1f3791353f429

Files changed

EVMinterface.jschanged
index.jschanged
tests/interfaceRunner.jschanged
wasmAgent.jschanged
stateInterface.jsdeleted
EVMinterface.jsView
@@ -15,14 +15,14 @@
1515 const U256_SIZE_BYTES = 32
1616
1717 // The interface exposed to the WebAessembly VM
1818 module.exports = class Interface {
19- constructor (api, message, results) {
20- results.gasRefund = 0
21- this.message = message
22- this.kernel = api.kernel
23- this.api = api
24- this.results = results
19+ constructor (opts) {
20+ opts.response.gasRefund = 0
21+ this.message = opts.message
22+ this.kernel = opts.kernel
23+ this.vm = opts.vm
24+ this.results = opts.response
2525 const shimBin = fs.readFileSync(path.join(__dirname, '/wasm/interface.wasm'))
2626 const shimMod = WebAssembly.Module(shimBin)
2727 this.shims = WebAssembly.Instance(shimMod, {
2828 'interface': {
@@ -138,9 +138,9 @@
138138 sync: true
139139 }))
140140 .catch(() => new Buffer([]))
141141
142- this.api.pushOpsQueue(opPromise, cbIndex, balance => {
142+ this.vm.pushOpsQueue(opPromise, cbIndex, balance => {
143143 this.setMemory(offset, U128_SIZE_BYTES, new U256(balance).toMemory(U128_SIZE_BYTES))
144144 })
145145 }
146146
@@ -225,9 +225,9 @@
225225 getCodeSize (cbIndex) {
226226 this.takeGas(2)
227227
228228 // wait for all the prevouse async ops to finish before running the callback
229- this.api.pushOpsQueue(this.kernel.code.length, cbIndex, length => length)
229+ this.vm.pushOpsQueue(this.kernel.code.length, cbIndex, length => length)
230230 }
231231
232232 /**
233233 * Copys the code running in current environment to memory.
@@ -238,9 +238,9 @@
238238 codeCopy (resultOffset, codeOffset, length, cbIndex) {
239239 this.takeGas(3 + Math.ceil(length / 32) * 3)
240240
241241 // wait for all the prevouse async ops to finish before running the callback
242- this.api.pushOpsQueue(this.kernel.code, cbIndex, code => {
242+ this.vm.pushOpsQueue(this.kernel.code, cbIndex, code => {
243243 if (code.length) {
244244 code = code.slice(codeOffset, codeOffset + length)
245245 this.setMemory(resultOffset, length, code)
246246 }
@@ -259,9 +259,9 @@
259259 .then(vertex => vertex.value.length)
260260 .catch(() => 0)
261261
262262 // wait for all the prevouse async ops to finish before running the callback
263- this.api.pushOpsQueue(opPromise, cbOffset, length => length)
263+ this.vm.pushOpsQueue(opPromise, cbOffset, length => length)
264264 }
265265
266266 /**
267267 * Copys the code of an account to memory.
@@ -285,9 +285,9 @@
285285 opPromise = Promise.resolve([])
286286 }
287287
288288 // wait for all the prevouse async ops to finish before running the callback
289- this.api.pushOpsQueue(opPromise, cbIndex, code => {
289+ this.vm.pushOpsQueue(opPromise, cbIndex, code => {
290290 if (code.length) {
291291 code = code.slice(codeOffset, codeOffset + length)
292292 this.setMemory(resultOffset, length, code)
293293 }
@@ -321,9 +321,9 @@
321321 opPromise = this.state.get(['blockchain', number]).then(vertex => vertex.hash())
322322 }
323323
324324 // wait for all the prevouse async ops to finish before running the callback
325- this.api.pushOpsQueue(opPromise, cbOffset, hash => {
325+ this.vm.pushOpsQueue(opPromise, cbOffset, hash => {
326326 this.setMemory(offset, U256_SIZE_BYTES, hash.toMemory())
327327 })
328328 }
329329
@@ -440,9 +440,9 @@
440440 opPromise = Promise.resolve(ethUtil.generateAddress(this.kernel.environment.address, this.kernel.environment.nonce))
441441 }
442442
443443 // wait for all the prevouse async ops to finish before running the callback
444- this.api.pushOpsQueue(opPromise, cbIndex, address => {
444+ this.vm.pushOpsQueue(opPromise, cbIndex, address => {
445445 this.setMemory(resultOffset, ADDRESS_SIZE_BYTES, address)
446446 })
447447 }
448448
@@ -481,9 +481,9 @@
481481 }
482482 })
483483
484484 // wait for all the prevouse async ops to finish before running the callback
485- this.api.pushOpsQueue(messagePromise, cbIndex, () => {
485+ this.vm.pushOpsQueue(messagePromise, cbIndex, () => {
486486 return 1
487487 })
488488 }
489489
@@ -516,9 +516,9 @@
516516 // the value was not found
517517 return null
518518 })
519519
520- this.api.pushOpsQueue(opPromise, cbIndex, oldValue => {
520+ this.vm.pushOpsQueue(opPromise, cbIndex, oldValue => {
521521 return 1
522522 })
523523 }
524524
@@ -556,24 +556,24 @@
556556 const key = new Buffer(this.getMemory(pathOffset, U256_SIZE_BYTES)).toString('hex')
557557 // copy the value
558558 const value = this.getMemory(valueOffset, U256_SIZE_BYTES).slice(0)
559559 const valIsZero = value.every((i) => i === 0)
560- const opPromise = this.kernel.stateInterface.get(key)
560+ const opPromise = this.kernel.state.get(key)
561561 .then(vertex => vertex.value)
562562 .catch(() => null)
563563
564- this.api.pushOpsQueue(opPromise, cbIndex, oldValue => {
564+ this.vm.pushOpsQueue(opPromise, cbIndex, oldValue => {
565565 if (valIsZero && oldValue) {
566566 // delete a value
567567 this.results.gasRefund += 15000
568- this.kernel.storageInterface.del(key)
568+ this.kernel.state.del(key)
569569 } else {
570570 if (!valIsZero && !oldValue) {
571571 // creating a new value
572572 this.takeGas(15000)
573573 }
574574 // update
575- this.kernel.stateInterface.set(key, new Vertex({
575+ this.kernel.state.set(key, new Vertex({
576576 value: value
577577 }))
578578 }
579579 })
@@ -589,13 +589,13 @@
589589
590590 // convert the path to an array
591591 const key = new Buffer(this.getMemory(pathOffset, U256_SIZE_BYTES)).toString('hex')
592592 // get the value from the state
593- const opPromise = this.kernel.stateInterface.get(key)
593+ const opPromise = this.kernel.state.get([key])
594594 .then(vertex => vertex.value)
595595 .catch(() => new Uint8Array(32))
596596
597- this.api.pushOpsQueue(opPromise, cbIndex, value => {
597+ this.vm.pushOpsQueue(opPromise, cbIndex, value => {
598598 this.setMemory(resultOffset, U256_SIZE_BYTES, value)
599599 })
600600 }
601601
@@ -621,13 +621,13 @@
621621 this.results.gasRefund += 24000
622622 }
623623
624624 getMemory (offset, length) {
625- return new Uint8Array(this.api.memory(), offset, length)
625+ return new Uint8Array(this.vm.memory(), offset, length)
626626 }
627627
628628 setMemory (offset, length, value) {
629- const memory = new Uint8Array(this.api.memory(), offset, length)
629+ const memory = new Uint8Array(this.vm.memory(), offset, length)
630630 memory.set(value)
631631 }
632632
633633 /*
index.jsView
@@ -1,17 +1,15 @@
11 const EventEmitter = require('events')
22 const Vertex = require('merkle-trie')
33 const PortManager = require('./portManager.js')
4-const StateInterface = require('./stateInterface.js')
54 const imports = require('./EVMinterface.js')
65 const codeHandler = require('./codeHandler.js')
76
87 module.exports = class Kernel extends EventEmitter {
98 constructor (opts = {}) {
109 super()
1110 // set up the state
1211 const state = this.state = opts.state || new Vertex()
13- this.stateInterface = new StateInterface(state)
1412 this.path = state.path
1513
1614 // set up the vm
1715 this.imports = opts.imports || [imports]
@@ -26,8 +24,9 @@
2624 })
2725 }
2826
2927 runNextMessage (index = 0) {
28+ // load the next message from port space
3029 return this.ports.peek(index).then(message => {
3130 if (message && (message._isCyclic(this) || this._state === 'idle')) {
3231 this._currentMessage = message
3332 this.ports.remove(index)
@@ -101,6 +100,8 @@
101100 }
102101 return this.ports.send(message)
103102 }
104103
105- shutdown () {}
104+ shutdown () {
105+ this.ports.close()
106+ }
106107 }
tests/interfaceRunner.jsView
@@ -64,9 +64,8 @@
6464 message.block = block
6565 message.blockchain = fakeBlockChain
6666
6767 const results = await hypervisor.send(message)
68- // console.log(results)
6968 t.equals(results.exception, undefined)
7069 t.end()
7170 })
7271 }
wasmAgent.jsView
@@ -13,36 +13,39 @@
1313 const responses = {}
1414 /**
1515 * Builds a import map with an array of given interfaces
1616 */
17- function buildImports (kernelApi, kernel, imports) {
17+ function buildImports (opts, imports) {
1818 const importMap = {}
1919 for (const Import of imports) {
20- const response = responses[Import.name] = {}
21- const newInterface = new Import(kernelApi, message, response)
20+ opts.response = responses[Import.name] = {}
21+ const newInterface = new Import(opts)
2222 importMap[Import.name] = newInterface.exports
2323 }
2424 return importMap
2525 }
2626
2727 let instance
28- const interfaceApi = {
29- /**
30- * adds an aync operation to the operations queue
31- */
32- pushOpsQueue: (promise, callbackIndex, intefaceCallback) => {
33- this._opsQueue = Promise.all([this._opsQueue, promise]).then(values => {
34- const result = intefaceCallback(values.pop())
35- instance.exports.callback.get(callbackIndex)(result)
36- })
28+
29+ const opts = {
30+ vm: {
31+ /**
32+ * adds an aync operation to the operations queue
33+ */
34+ pushOpsQueue: (promise, callbackIndex, intefaceCallback) => {
35+ this._opsQueue = Promise.all([this._opsQueue, promise]).then(values => {
36+ const result = intefaceCallback(values.pop())
37+ instance.exports.callback.get(callbackIndex)(result)
38+ })
39+ },
40+ memory: () => {
41+ return instance.exports.memory.buffer
42+ }
3743 },
38- memory: () => {
39- return instance.exports.memory.buffer
40- },
41- kernel: kernel
44+ kernel: kernel,
45+ message: message
4246 }
43-
44- const initializedImports = buildImports(interfaceApi, kernel, imports)
47+ const initializedImports = buildImports(opts, imports)
4548 instance = WebAssembly.Instance(this._module, initializedImports)
4649
4750 if (instance.exports.main) {
4851 instance.exports.main()
stateInterface.jsView
@@ -1,20 +1,0 @@
1-const assert = require('assert')
2-
3-module.exports = class StateInterface {
4- constructor (state) {
5- assert(state, 'must have state')
6- this.state = state
7- }
8-
9- set (name, value) {
10- this.state.set([name], value)
11- }
12-
13- get (name) {
14- return this.state.get([name])
15- }
16-
17- del (name) {
18- return this.state.del([name])
19- }
20-}

Built with git-ssb-web