Commit 841228acfa1dad00f990ebe18290a46130f85d5d
cleanup
wanderer committed on 3/1/2017, 4:02:35 PMParent: c2c35a4216304056cc0acac26cf1f3791353f429
Files changed
EVMinterface.js | changed |
index.js | changed |
tests/interfaceRunner.js | changed |
wasmAgent.js | changed |
stateInterface.js | deleted |
EVMinterface.js | ||
---|---|---|
@@ -15,14 +15,14 @@ | ||
15 | 15 | const U256_SIZE_BYTES = 32 |
16 | 16 | |
17 | 17 | // The interface exposed to the WebAessembly VM |
18 | 18 | 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 | |
25 | 25 | const shimBin = fs.readFileSync(path.join(__dirname, '/wasm/interface.wasm')) |
26 | 26 | const shimMod = WebAssembly.Module(shimBin) |
27 | 27 | this.shims = WebAssembly.Instance(shimMod, { |
28 | 28 | 'interface': { |
@@ -138,9 +138,9 @@ | ||
138 | 138 | sync: true |
139 | 139 | })) |
140 | 140 | .catch(() => new Buffer([])) |
141 | 141 | |
142 | - this.api.pushOpsQueue(opPromise, cbIndex, balance => { | |
142 | + this.vm.pushOpsQueue(opPromise, cbIndex, balance => { | |
143 | 143 | this.setMemory(offset, U128_SIZE_BYTES, new U256(balance).toMemory(U128_SIZE_BYTES)) |
144 | 144 | }) |
145 | 145 | } |
146 | 146 | |
@@ -225,9 +225,9 @@ | ||
225 | 225 | getCodeSize (cbIndex) { |
226 | 226 | this.takeGas(2) |
227 | 227 | |
228 | 228 | // 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) | |
230 | 230 | } |
231 | 231 | |
232 | 232 | /** |
233 | 233 | * Copys the code running in current environment to memory. |
@@ -238,9 +238,9 @@ | ||
238 | 238 | codeCopy (resultOffset, codeOffset, length, cbIndex) { |
239 | 239 | this.takeGas(3 + Math.ceil(length / 32) * 3) |
240 | 240 | |
241 | 241 | // 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 => { | |
243 | 243 | if (code.length) { |
244 | 244 | code = code.slice(codeOffset, codeOffset + length) |
245 | 245 | this.setMemory(resultOffset, length, code) |
246 | 246 | } |
@@ -259,9 +259,9 @@ | ||
259 | 259 | .then(vertex => vertex.value.length) |
260 | 260 | .catch(() => 0) |
261 | 261 | |
262 | 262 | // 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) | |
264 | 264 | } |
265 | 265 | |
266 | 266 | /** |
267 | 267 | * Copys the code of an account to memory. |
@@ -285,9 +285,9 @@ | ||
285 | 285 | opPromise = Promise.resolve([]) |
286 | 286 | } |
287 | 287 | |
288 | 288 | // 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 => { | |
290 | 290 | if (code.length) { |
291 | 291 | code = code.slice(codeOffset, codeOffset + length) |
292 | 292 | this.setMemory(resultOffset, length, code) |
293 | 293 | } |
@@ -321,9 +321,9 @@ | ||
321 | 321 | opPromise = this.state.get(['blockchain', number]).then(vertex => vertex.hash()) |
322 | 322 | } |
323 | 323 | |
324 | 324 | // 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 => { | |
326 | 326 | this.setMemory(offset, U256_SIZE_BYTES, hash.toMemory()) |
327 | 327 | }) |
328 | 328 | } |
329 | 329 | |
@@ -440,9 +440,9 @@ | ||
440 | 440 | opPromise = Promise.resolve(ethUtil.generateAddress(this.kernel.environment.address, this.kernel.environment.nonce)) |
441 | 441 | } |
442 | 442 | |
443 | 443 | // 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 => { | |
445 | 445 | this.setMemory(resultOffset, ADDRESS_SIZE_BYTES, address) |
446 | 446 | }) |
447 | 447 | } |
448 | 448 | |
@@ -481,9 +481,9 @@ | ||
481 | 481 | } |
482 | 482 | }) |
483 | 483 | |
484 | 484 | // 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, () => { | |
486 | 486 | return 1 |
487 | 487 | }) |
488 | 488 | } |
489 | 489 | |
@@ -516,9 +516,9 @@ | ||
516 | 516 | // the value was not found |
517 | 517 | return null |
518 | 518 | }) |
519 | 519 | |
520 | - this.api.pushOpsQueue(opPromise, cbIndex, oldValue => { | |
520 | + this.vm.pushOpsQueue(opPromise, cbIndex, oldValue => { | |
521 | 521 | return 1 |
522 | 522 | }) |
523 | 523 | } |
524 | 524 | |
@@ -556,24 +556,24 @@ | ||
556 | 556 | const key = new Buffer(this.getMemory(pathOffset, U256_SIZE_BYTES)).toString('hex') |
557 | 557 | // copy the value |
558 | 558 | const value = this.getMemory(valueOffset, U256_SIZE_BYTES).slice(0) |
559 | 559 | const valIsZero = value.every((i) => i === 0) |
560 | - const opPromise = this.kernel.stateInterface.get(key) | |
560 | + const opPromise = this.kernel.state.get(key) | |
561 | 561 | .then(vertex => vertex.value) |
562 | 562 | .catch(() => null) |
563 | 563 | |
564 | - this.api.pushOpsQueue(opPromise, cbIndex, oldValue => { | |
564 | + this.vm.pushOpsQueue(opPromise, cbIndex, oldValue => { | |
565 | 565 | if (valIsZero && oldValue) { |
566 | 566 | // delete a value |
567 | 567 | this.results.gasRefund += 15000 |
568 | - this.kernel.storageInterface.del(key) | |
568 | + this.kernel.state.del(key) | |
569 | 569 | } else { |
570 | 570 | if (!valIsZero && !oldValue) { |
571 | 571 | // creating a new value |
572 | 572 | this.takeGas(15000) |
573 | 573 | } |
574 | 574 | // update |
575 | - this.kernel.stateInterface.set(key, new Vertex({ | |
575 | + this.kernel.state.set(key, new Vertex({ | |
576 | 576 | value: value |
577 | 577 | })) |
578 | 578 | } |
579 | 579 | }) |
@@ -589,13 +589,13 @@ | ||
589 | 589 | |
590 | 590 | // convert the path to an array |
591 | 591 | const key = new Buffer(this.getMemory(pathOffset, U256_SIZE_BYTES)).toString('hex') |
592 | 592 | // get the value from the state |
593 | - const opPromise = this.kernel.stateInterface.get(key) | |
593 | + const opPromise = this.kernel.state.get([key]) | |
594 | 594 | .then(vertex => vertex.value) |
595 | 595 | .catch(() => new Uint8Array(32)) |
596 | 596 | |
597 | - this.api.pushOpsQueue(opPromise, cbIndex, value => { | |
597 | + this.vm.pushOpsQueue(opPromise, cbIndex, value => { | |
598 | 598 | this.setMemory(resultOffset, U256_SIZE_BYTES, value) |
599 | 599 | }) |
600 | 600 | } |
601 | 601 | |
@@ -621,13 +621,13 @@ | ||
621 | 621 | this.results.gasRefund += 24000 |
622 | 622 | } |
623 | 623 | |
624 | 624 | getMemory (offset, length) { |
625 | - return new Uint8Array(this.api.memory(), offset, length) | |
625 | + return new Uint8Array(this.vm.memory(), offset, length) | |
626 | 626 | } |
627 | 627 | |
628 | 628 | setMemory (offset, length, value) { |
629 | - const memory = new Uint8Array(this.api.memory(), offset, length) | |
629 | + const memory = new Uint8Array(this.vm.memory(), offset, length) | |
630 | 630 | memory.set(value) |
631 | 631 | } |
632 | 632 | |
633 | 633 | /* |
index.js | ||
---|---|---|
@@ -1,17 +1,15 @@ | ||
1 | 1 | const EventEmitter = require('events') |
2 | 2 | const Vertex = require('merkle-trie') |
3 | 3 | const PortManager = require('./portManager.js') |
4 | -const StateInterface = require('./stateInterface.js') | |
5 | 4 | const imports = require('./EVMinterface.js') |
6 | 5 | const codeHandler = require('./codeHandler.js') |
7 | 6 | |
8 | 7 | module.exports = class Kernel extends EventEmitter { |
9 | 8 | constructor (opts = {}) { |
10 | 9 | super() |
11 | 10 | // set up the state |
12 | 11 | const state = this.state = opts.state || new Vertex() |
13 | - this.stateInterface = new StateInterface(state) | |
14 | 12 | this.path = state.path |
15 | 13 | |
16 | 14 | // set up the vm |
17 | 15 | this.imports = opts.imports || [imports] |
@@ -26,8 +24,9 @@ | ||
26 | 24 | }) |
27 | 25 | } |
28 | 26 | |
29 | 27 | runNextMessage (index = 0) { |
28 | + // load the next message from port space | |
30 | 29 | return this.ports.peek(index).then(message => { |
31 | 30 | if (message && (message._isCyclic(this) || this._state === 'idle')) { |
32 | 31 | this._currentMessage = message |
33 | 32 | this.ports.remove(index) |
@@ -101,6 +100,8 @@ | ||
101 | 100 | } |
102 | 101 | return this.ports.send(message) |
103 | 102 | } |
104 | 103 | |
105 | - shutdown () {} | |
104 | + shutdown () { | |
105 | + this.ports.close() | |
106 | + } | |
106 | 107 | } |
tests/interfaceRunner.js | ||
---|---|---|
@@ -64,9 +64,8 @@ | ||
64 | 64 | message.block = block |
65 | 65 | message.blockchain = fakeBlockChain |
66 | 66 | |
67 | 67 | const results = await hypervisor.send(message) |
68 | - // console.log(results) | |
69 | 68 | t.equals(results.exception, undefined) |
70 | 69 | t.end() |
71 | 70 | }) |
72 | 71 | } |
wasmAgent.js | ||
---|---|---|
@@ -13,36 +13,39 @@ | ||
13 | 13 | const responses = {} |
14 | 14 | /** |
15 | 15 | * Builds a import map with an array of given interfaces |
16 | 16 | */ |
17 | - function buildImports (kernelApi, kernel, imports) { | |
17 | + function buildImports (opts, imports) { | |
18 | 18 | const importMap = {} |
19 | 19 | 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) | |
22 | 22 | importMap[Import.name] = newInterface.exports |
23 | 23 | } |
24 | 24 | return importMap |
25 | 25 | } |
26 | 26 | |
27 | 27 | 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 | + } | |
37 | 43 | }, |
38 | - memory: () => { | |
39 | - return instance.exports.memory.buffer | |
40 | - }, | |
41 | - kernel: kernel | |
44 | + kernel: kernel, | |
45 | + message: message | |
42 | 46 | } |
43 | - | |
44 | - const initializedImports = buildImports(interfaceApi, kernel, imports) | |
47 | + const initializedImports = buildImports(opts, imports) | |
45 | 48 | instance = WebAssembly.Instance(this._module, initializedImports) |
46 | 49 | |
47 | 50 | if (instance.exports.main) { |
48 | 51 | instance.exports.main() |
stateInterface.js | ||
---|---|---|
@@ -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