Commit 48df42c8ab634c0d9fc7597e52106352878124fa
fix message response
wanderer committed on 2/28/2017, 4:01:11 PMParent: 9bb00a66c6590ce780568e10d649be74c66ce1e6
Files changed
EVMinterface.js | changed |
hypervisor.js | changed |
index.js | changed |
message.js | changed |
port.js | changed |
portManager.js | changed |
stateInterface.js | changed |
tests/apiTests.js | changed |
tests/interfaceRunner.js | changed |
wasmAgent.js | changed |
EVMinterface.js | ||
---|---|---|
@@ -33,13 +33,8 @@ | ||
33 | 33 | } |
34 | 34 | }) |
35 | 35 | } |
36 | 36 | |
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 | - | |
42 | 37 | static get name () { |
43 | 38 | return 'ethereum' |
44 | 39 | } |
45 | 40 | |
@@ -168,10 +163,9 @@ | ||
168 | 163 | * @param {integer} offset |
169 | 164 | */ |
170 | 165 | getCaller (offset) { |
171 | 166 | this.takeGas(2) |
172 | - | |
173 | - const caller = this.message.from[1] | |
167 | + const caller = this.message.from[2] | |
174 | 168 | this.setMemory(offset, ADDRESS_SIZE_BYTES, new Buffer(caller.slice(2), 'hex')) |
175 | 169 | } |
176 | 170 | |
177 | 171 | /** |
hypervisor.js | ||
---|---|---|
@@ -30,23 +30,8 @@ | ||
30 | 30 | this.root.send(message) |
31 | 31 | return message.result() |
32 | 32 | } |
33 | 33 | |
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 | - | |
49 | 34 | addVM (type, handler) { |
50 | 35 | codeHandlers.handlers.type = handler |
51 | 36 | } |
52 | 37 | } |
index.js | ||
---|---|---|
@@ -15,21 +15,20 @@ | ||
15 | 15 | this.path = state.path |
16 | 16 | this.imports = opts.imports || [imports] |
17 | 17 | this.ports = new PortManager(state, opts.parent, Kernel) |
18 | 18 | this._sentAtomicMessages = [] |
19 | - // rename sandbox? | |
20 | 19 | this._vm = (opts.codeHandler || codeHandler).init(this.code) |
21 | 20 | this._state = 'idle' |
22 | 21 | this.ports.on('message', index => { |
23 | 22 | this.runNextMessage(index) |
24 | 23 | }) |
25 | 24 | } |
26 | 25 | |
27 | 26 | runNextMessage (index = 0) { |
28 | - this.ports.peek(index).then(message => { | |
27 | + return this.ports.peek(index).then(message => { | |
29 | 28 | if (message && (message.isCyclic(this) || this._state === 'idle')) { |
30 | 29 | this.ports.remove(index) |
31 | - this.run(message) | |
30 | + return this.run(message) | |
32 | 31 | } else { |
33 | 32 | this._state = 'idle' |
34 | 33 | this.emit('idle') |
35 | 34 | } |
@@ -83,24 +82,25 @@ | ||
83 | 82 | return result |
84 | 83 | } |
85 | 84 | |
86 | 85 | async send (message) { |
86 | + let portName = message.nextPort() | |
87 | 87 | // replace root with parent path to root |
88 | - let portName = message.nextPort() | |
89 | 88 | if (portName === common.ROOT) { |
90 | 89 | message.to.shift() |
91 | 90 | message.to = new Array(this.path.length).fill(common.PARENT).concat(message.to) |
92 | 91 | portName = common.PARENT |
93 | 92 | } |
94 | - message.addVistedKernel(message) | |
93 | + message.addVistedKernel(this) | |
95 | 94 | this.lastMessage = message |
96 | 95 | // console.log(portName, message) |
97 | 96 | const port = await this.ports.get(portName) |
98 | 97 | // save the atomic messages for possible reverts |
99 | 98 | if (message.atomic) { |
100 | 99 | this._sentAtomicMessages.push(message) |
101 | 100 | } |
102 | - return port.send(message) | |
101 | + port.send(message) | |
102 | + return message.result() | |
103 | 103 | } |
104 | 104 | |
105 | 105 | shutdown () {} |
106 | 106 | } |
message.js | ||
---|---|---|
@@ -21,12 +21,12 @@ | ||
21 | 21 | } |
22 | 22 | |
23 | 23 | _finish (result) { |
24 | 24 | if (this.atomic) { |
25 | - this._vistedKernels.pop() | |
26 | - if (!this._vistedKernels.length) { | |
25 | + if (this._vistedKernels.length === this.hops) { | |
27 | 26 | this._resolve(result) |
28 | 27 | } |
28 | + this._vistedKernels.pop() | |
29 | 29 | } |
30 | 30 | } |
31 | 31 | |
32 | 32 | result () { |
@@ -43,7 +43,7 @@ | ||
43 | 43 | } |
44 | 44 | } |
45 | 45 | |
46 | 46 | isCyclic (kernel) { |
47 | - return this.sync && this._vistedKernels.some(process => process === kernel) | |
47 | + return this.atomic && this._vistedKernels.some(process => process === kernel) | |
48 | 48 | } |
49 | 49 | } |
port.js | ||
---|---|---|
@@ -1,20 +1,26 @@ | ||
1 | 1 | const EventEmitter = require('events') |
2 | 2 | |
3 | 3 | module.exports = class Port extends EventEmitter { |
4 | - constructor () { | |
4 | + constructor (name) { | |
5 | 5 | super() |
6 | + this.name = name | |
7 | + this.connected = false | |
6 | 8 | } |
7 | 9 | |
8 | 10 | 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 | + } | |
11 | 16 | } |
12 | 17 | |
13 | 18 | async send (message) { |
14 | - return this.destPort.recieve(message) | |
19 | + this.destPort.recieve(message) | |
15 | 20 | } |
16 | 21 | |
17 | 22 | async recieve (message) { |
23 | + message.from.push(this.name) | |
18 | 24 | this.emit('message', message) |
19 | 25 | } |
20 | 26 | } |
portManager.js | ||
---|---|---|
@@ -8,12 +8,13 @@ | ||
8 | 8 | this._queue = [] |
9 | 9 | this.state = state |
10 | 10 | this.Kernel = KernelContructor |
11 | 11 | // set up the parent port |
12 | - const parentPort = new Port() | |
12 | + const parentPort = new Port(common.PARENT) | |
13 | 13 | parentPort.on('message', message => { |
14 | 14 | this._recieveMessage(message) |
15 | 15 | }) |
16 | + | |
16 | 17 | // create the cache |
17 | 18 | this.cache = new Map() |
18 | 19 | this.cache.set(common.PARENT, parentPort) |
19 | 20 | } |
@@ -23,11 +24,12 @@ | ||
23 | 24 | this.emit('message', index) |
24 | 25 | } |
25 | 26 | |
26 | 27 | async get (name) { |
28 | + console.log(name) | |
27 | 29 | let port = this.cache.get(name) |
28 | 30 | if (!port) { |
29 | - port = new Port() | |
31 | + port = new Port(name) | |
30 | 32 | port.on('message', message => { |
31 | 33 | this._recieveMessage(message) |
32 | 34 | }) |
33 | 35 | // create destination kernel |
stateInterface.js | ||
---|---|---|
@@ -1,6 +1,9 @@ | ||
1 | +const assert = require('assert') | |
2 | + | |
1 | 3 | module.exports = class StateInterface { |
2 | - constuctor (state) { | |
4 | + constructor (state) { | |
5 | + assert(state, 'must have state') | |
3 | 6 | this.state = state |
4 | 7 | } |
5 | 8 | |
6 | 9 | set (name, value) { |
tests/apiTests.js | ||
---|---|---|
@@ -42,15 +42,12 @@ | ||
42 | 42 | })) |
43 | 43 | } |
44 | 44 | }) |
45 | 45 | |
46 | - await hypervisor.send(new Message({ | |
46 | + const message = new Message({ | |
47 | 47 | 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() | |
56 | 53 | }) |
tests/interfaceRunner.js | ||
---|---|---|
@@ -8,14 +8,14 @@ | ||
8 | 8 | // TODO remove fakeblockchain |
9 | 9 | const fakeBlockChain = require('../fakeBlockChain.js') |
10 | 10 | const Hypervisor = require('../hypervisor.js') |
11 | 11 | const Message = require('../message.js') |
12 | -const common = require('../common.js') | |
12 | +const common = require('../common') | |
13 | 13 | |
14 | 14 | const dir = path.join(__dirname, '/interface') |
15 | 15 | // get the test names |
16 | 16 | let tests = fs.readdirSync(dir).filter((file) => file.endsWith('.wast')) |
17 | -// tests = ['address.wast'] | |
17 | +tests = ['call.wast'] | |
18 | 18 | |
19 | 19 | runTests(tests) |
20 | 20 | |
21 | 21 | function runTests (tests) { |
@@ -60,16 +60,16 @@ | ||
60 | 60 | value: fakeBlockChain |
61 | 61 | })) |
62 | 62 | |
63 | 63 | 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'] | |
66 | 65 | message.data = new Buffer(envData.callData.slice(2), 'hex') |
67 | 66 | message.value = new U256(envData.callValue) |
68 | 67 | message.gas = 1000000 |
69 | 68 | |
70 | 69 | try { |
71 | 70 | await hypervisor.send(message) |
71 | + console.log('done') | |
72 | 72 | } catch (e) { |
73 | 73 | t.fail('Exception: ' + e) |
74 | 74 | console.error('FAIL') |
75 | 75 | console.error(e) |
wasmAgent.js | ||
---|---|---|
@@ -13,16 +13,14 @@ | ||
13 | 13 | const responses = {} |
14 | 14 | /** |
15 | 15 | * Builds a import map with an array of given interfaces |
16 | 16 | */ |
17 | - async function buildImports (kernelApi, kernel, imports) { | |
17 | + function buildImports (kernelApi, kernel, imports) { | |
18 | 18 | const importMap = {} |
19 | 19 | for (const Import of imports) { |
20 | 20 | const response = responses[Import.name] = {} |
21 | 21 | const newIterface = new Import(kernelApi, message, response) |
22 | 22 | importMap[Import.name] = newIterface.exports |
23 | - // initailize the import | |
24 | - await newIterface.initialize() | |
25 | 23 | } |
26 | 24 | return importMap |
27 | 25 | } |
28 | 26 | |
@@ -42,9 +40,9 @@ | ||
42 | 40 | }, |
43 | 41 | kernel: kernel |
44 | 42 | } |
45 | 43 | |
46 | - const initializedImports = await buildImports(interfaceApi, kernel, imports) | |
44 | + const initializedImports = buildImports(interfaceApi, kernel, imports) | |
47 | 45 | instance = WebAssembly.Instance(this._module, initializedImports) |
48 | 46 | |
49 | 47 | if (instance.exports.main) { |
50 | 48 | instance.exports.main() |
Built with git-ssb-web