Commit a9764e4cea10db92064ace79bfa09f6e01704fb4
Merge branch 'master' into small-fixes
wanderer authored on 3/6/2018, 3:09:17 AMGitHub committed on 3/6/2018, 3:09:17 AM
Parent: 18c73fcab36d8356ed6384cceb0fa31e40c3895c
Parent: 426c0bb6c8135b071cb6598828943842a782bb7f
Files changed
actor.js | changed |
index.js | changed |
package-lock.json | changed |
package.json | changed |
tests/index.js | changed |
tests/wasm/funcRef_caller.wasm | changed |
tests/wasmContainer.js | changed |
tests/wast/funcRef_caller.wast | changed |
wasmContainer.js | changed |
actor.js | ||
---|---|---|
@@ -21,12 +21,10 @@ | ||
21 | 21 | * Runs the shutdown routine for the actor |
22 | 22 | */ |
23 | 23 | async shutdown () { |
24 | 24 | await this.tree.set(this.id.id, [this.type, this.nonce]) |
25 | - if (this.storage.length) { | |
26 | - const state = await this.tree.get(this.id.id) | |
27 | - return this.tree.graph.set(state.root, '2', this.storage) | |
28 | - } | |
25 | + const state = await this.tree.get(this.id.id) | |
26 | + return this.tree.graph.set(state.root, '2', this.storage) | |
29 | 27 | } |
30 | 28 | |
31 | 29 | /** |
32 | 30 | * Runs the startup routine for the actor |
index.js | ||
---|---|---|
@@ -64,29 +64,33 @@ | ||
64 | 64 | */ |
65 | 65 | async createActor (type, code, id = {nonce: this.nonce++, parent: null}) { |
66 | 66 | const Container = this._containerTypes[type] |
67 | 67 | const encoded = encodedID(id) |
68 | - let idHash = this._getHashFromObj(encoded) | |
68 | + let idHash = this._hash(encoded) | |
69 | 69 | idHash = new ID(idHash) |
70 | 70 | const module = await Container.onCreation(code, idHash, this.tree) |
71 | 71 | const metaData = [type, 0] |
72 | 72 | |
73 | 73 | // save the container in the state |
74 | - this.tree.set(idHash.id, metaData) | |
75 | - if (code) { | |
76 | - const node = await this.tree.get(idHash.id) | |
77 | - await this.tree.graph.set(node.node, '1', code) | |
74 | + const node = await this.tree.set(idHash.id, metaData) | |
75 | + // save the code | |
76 | + node[1] = { | |
77 | + '/': code | |
78 | 78 | } |
79 | + // save the storage | |
80 | + node[2] = { | |
81 | + '/': [] | |
82 | + } | |
83 | + | |
79 | 84 | return { |
80 | 85 | id: idHash, |
81 | 86 | module |
82 | 87 | } |
83 | 88 | } |
84 | 89 | |
85 | - // get a hash from a POJO | |
86 | - _getHashFromObj (obj) { | |
90 | + _hash (buf) { | |
87 | 91 | const hash = crypto.createHash('sha256') |
88 | - hash.update(obj) | |
92 | + hash.update(buf) | |
89 | 93 | return hash.digest().slice(0, 20) |
90 | 94 | } |
91 | 95 | |
92 | 96 | /** |
package-lock.json | ||
---|---|---|
The diff is too large to show. Use a local git client to view these changes. Old file size: 338011 bytes New file size: 337905 bytes |
package.json | ||
---|---|---|
@@ -32,21 +32,20 @@ | ||
32 | 32 | "dependencies": { |
33 | 33 | "binary-search-insert": "^1.0.3", |
34 | 34 | "borc": "^2.0.2", |
35 | 35 | "events": "^2.0.0", |
36 | - "levelup": "^2.0.2", | |
37 | - "reference-map": "^1.2.1", | |
36 | + "reference-map": "^1.2.3", | |
38 | 37 | "safe-buffer": "^5.1.1", |
39 | 38 | "wasm-json-toolkit": "^0.2.2", |
40 | 39 | "wasm-metering": "^0.1.1" |
41 | 40 | }, |
42 | 41 | "devDependencies": { |
42 | + "levelup": "^2.0.2", | |
43 | 43 | "coveralls": "^3.0.0", |
44 | - "dfinity-radix-tree": "0.0.9", | |
44 | + "dfinity-radix-tree": "0.1.1", | |
45 | 45 | "documentation": "^5.3.5", |
46 | 46 | "level-browserify": "^1.1.1", |
47 | 47 | "nyc": "^11.4.1", |
48 | - "primea-abstract-container": "0.0.6", | |
49 | 48 | "standard": "11.0.0", |
50 | 49 | "tape": "^4.9.0", |
51 | 50 | "wabt": "^1.0.0" |
52 | 51 | } |
tests/index.js | ||
---|---|---|
@@ -31,9 +31,9 @@ | ||
31 | 31 | |
32 | 32 | tape('basic', async t => { |
33 | 33 | t.plan(2) |
34 | 34 | const expectedState = { |
35 | - '/': Buffer.from('a5e1aaebec14b7f144d6a7e007b148aa7c56804c', 'hex') | |
35 | + '/': Buffer.from('cd80335de00c2bf38570b41c55a79174c1c64e9f', 'hex') | |
36 | 36 | } |
37 | 37 | |
38 | 38 | const tree = new RadixTree({ |
39 | 39 | db: db |
@@ -62,9 +62,9 @@ | ||
62 | 62 | |
63 | 63 | tape('two communicating actors', async t => { |
64 | 64 | t.plan(2) |
65 | 65 | const expectedState = { |
66 | - '/': Buffer.from('bc0af44b691ef57b362df8b11c274742147900cd', 'hex') | |
66 | + '/': Buffer.from('b063f3e53b2ea40f50afe964b9f9b49aad491155', 'hex') | |
67 | 67 | } |
68 | 68 | |
69 | 69 | const tree = new RadixTree({ |
70 | 70 | db: db |
@@ -110,9 +110,9 @@ | ||
110 | 110 | |
111 | 111 | tape('three communicating actors', async t => { |
112 | 112 | t.plan(3) |
113 | 113 | const expectedState = { |
114 | - '/': Buffer.from('86e066f60fe2f722befbc29f128948f6487a207a', 'hex') | |
114 | + '/': Buffer.from('db532195ac569b142415cc9bdbec37f18f344a59', 'hex') | |
115 | 115 | } |
116 | 116 | |
117 | 117 | const tree = new RadixTree({ |
118 | 118 | db: db |
@@ -165,9 +165,9 @@ | ||
165 | 165 | |
166 | 166 | tape('three communicating actors, with tick counting', async t => { |
167 | 167 | t.plan(3) |
168 | 168 | const expectedState = { |
169 | - '/': Buffer.from('86e066f60fe2f722befbc29f128948f6487a207a', 'hex') | |
169 | + '/': Buffer.from('db532195ac569b142415cc9bdbec37f18f344a59', 'hex') | |
170 | 170 | } |
171 | 171 | |
172 | 172 | const tree = new RadixTree({ |
173 | 173 | db: db |
@@ -220,9 +220,9 @@ | ||
220 | 220 | |
221 | 221 | tape('errors', async t => { |
222 | 222 | t.plan(3) |
223 | 223 | const expectedState = { |
224 | - '/': Buffer.from('bc0af44b691ef57b362df8b11c274742147900cd', 'hex') | |
224 | + '/': Buffer.from('b063f3e53b2ea40f50afe964b9f9b49aad491155', 'hex') | |
225 | 225 | } |
226 | 226 | |
227 | 227 | const tree = new RadixTree({ |
228 | 228 | db: db |
@@ -268,9 +268,9 @@ | ||
268 | 268 | |
269 | 269 | tape('actor creation', async t => { |
270 | 270 | t.plan(2) |
271 | 271 | const expectedState = { |
272 | - '/': Buffer.from('544ae05bc8831cc69c5524bc3a51c0d4806e9d60', 'hex') | |
272 | + '/': Buffer.from('8a21d80cd7ca04e64be7cb2726a72060fc546ed6', 'hex') | |
273 | 273 | } |
274 | 274 | |
275 | 275 | const tree = new RadixTree({ |
276 | 276 | db: db |
@@ -317,9 +317,9 @@ | ||
317 | 317 | |
318 | 318 | tape('simple message arbiter test', async t => { |
319 | 319 | t.plan(4) |
320 | 320 | const expectedState = { |
321 | - '/': Buffer.from('bc0af44b691ef57b362df8b11c274742147900cd', 'hex') | |
321 | + '/': Buffer.from('b063f3e53b2ea40f50afe964b9f9b49aad491155', 'hex') | |
322 | 322 | } |
323 | 323 | |
324 | 324 | const tree = new RadixTree({ |
325 | 325 | db: db |
@@ -386,9 +386,9 @@ | ||
386 | 386 | tape('arbiter test for id comparision', async t => { |
387 | 387 | t.plan(4) |
388 | 388 | let message |
389 | 389 | const expectedState = { |
390 | - '/': Buffer.from('86e066f60fe2f722befbc29f128948f6487a207a', 'hex') | |
390 | + '/': Buffer.from('db532195ac569b142415cc9bdbec37f18f344a59', 'hex') | |
391 | 391 | } |
392 | 392 | |
393 | 393 | const tree = new RadixTree({ |
394 | 394 | db: db |
@@ -453,9 +453,9 @@ | ||
453 | 453 | |
454 | 454 | tape('async work', async t => { |
455 | 455 | t.plan(3) |
456 | 456 | const expectedState = { |
457 | - '/': Buffer.from('bc0af44b691ef57b362df8b11c274742147900cd', 'hex') | |
457 | + '/': Buffer.from('b063f3e53b2ea40f50afe964b9f9b49aad491155', 'hex') | |
458 | 458 | } |
459 | 459 | |
460 | 460 | const tree = new RadixTree({ |
461 | 461 | db: db |
tests/wasm/funcRef_caller.wasm | ||
---|---|---|
@@ -1,2 +1,2 @@ | ||
1 | - asm types`` typeMap ` ` `` @funcinternalize testcheck moduleself moduleexports p $memory table call callback | |
2 | - A AA A A A callback | |
1 | + asm types`` typeMap ` ` `` Tfuncinternalize testcheck moduleself moduleexport memoryexternalize p $memory table call callback | |
2 | +" A AA A A A callback |
tests/wasmContainer.js | ||
---|---|---|
@@ -150,9 +150,9 @@ | ||
150 | 150 | |
151 | 151 | const message = new Message({ |
152 | 152 | funcRef: callFuncRef, |
153 | 153 | funcArguments: [recvFuncRef] |
154 | - }) | |
154 | + }).on('execution:error', e => console.log(e)) | |
155 | 155 | |
156 | 156 | hypervisor.send(message) |
157 | 157 | const stateRoot = await hypervisor.createStateRoot() |
158 | 158 | // t.deepEquals(stateRoot, expectedState, 'expected root!') |
@@ -271,18 +271,18 @@ | ||
271 | 271 | funcRef.gas = 400 |
272 | 272 | const message = new Message({ |
273 | 273 | funcRef |
274 | 274 | }).on('done', actor => { |
275 | - resolve() | |
276 | 275 | const b = actor.container.get8Memory(5, 4) |
277 | 276 | const result = Buffer.from(b).toString() |
278 | 277 | t.deepEquals(result, 'test', 'should copy memory correctly') |
278 | + resolve() | |
279 | 279 | }) |
280 | 280 | hypervisor.send(message) |
281 | 281 | }) |
282 | 282 | }) |
283 | 283 | |
284 | -tape.skip('ben', async t => { | |
284 | +tape('ben', async t => { | |
285 | 285 | // t.plan(1) |
286 | 286 | tester = t |
287 | 287 | const tree = new RadixTree({ |
288 | 288 | db |
@@ -293,10 +293,13 @@ | ||
293 | 293 | hypervisor.registerContainer(TestWasmContainer) |
294 | 294 | |
295 | 295 | const {module} = await hypervisor.createActor(TestWasmContainer.typeId, wasm) |
296 | 296 | |
297 | + const funcRef = module.getFuncRef('#main') | |
298 | + funcRef.gas = 100000 | |
299 | + | |
297 | 300 | const message = new Message({ |
298 | - funcRef: module.getFuncRef('main') | |
301 | + funcRef | |
299 | 302 | }).on('done', () => { |
300 | 303 | t.end() |
301 | 304 | }) |
302 | 305 | hypervisor.send(message) |
tests/wast/funcRef_caller.wast | ||
---|---|---|
@@ -1,16 +1,18 @@ | ||
1 | 1 | (module |
2 | 2 | (import "func" "internalize" (func $internalize (param i32 i32))) |
3 | 3 | (import "test" "check" (func $check (param i32 i32))) |
4 | 4 | (import "module" "self" (func $self (result i32))) |
5 | - (import "module" "exports" (func $exports (param i32 i32 i32) (result i32))) | |
5 | + (import "module" "export" (func $exports (param i32 i32) (result i32))) | |
6 | + (import "memory" "externalize" (func $externalize (param i32 i32) (result i32))) | |
6 | 7 | (memory (export "memory") 1) |
7 | 8 | (data (i32.const 0) "callback") |
8 | 9 | (table (export "table") 1 1 anyfunc) |
9 | 10 | (func $call (param i32) |
10 | 11 | call $self |
11 | 12 | i32.const 0 |
12 | 13 | i32.const 8 |
14 | + call $externalize | |
13 | 15 | call $exports |
14 | 16 | |
15 | 17 | i32.const 0 |
16 | 18 | get_local 0 |
wasmContainer.js | ||
---|---|---|
@@ -44,8 +44,10 @@ | ||
44 | 44 | } |
45 | 45 | const message = new Message({ |
46 | 46 | funcRef: self, |
47 | 47 | funcArguments: checkedArgs |
48 | + }).on('execution:error', e => { | |
49 | + console.log(e) | |
48 | 50 | }) |
49 | 51 | container.actor.send(message) |
50 | 52 | } |
51 | 53 | } |
@@ -59,12 +61,13 @@ | ||
59 | 61 | this.actor = actor |
60 | 62 | this.refs = new ReferanceMap() |
61 | 63 | } |
62 | 64 | |
63 | - static validateWasm (wasm, id) { | |
65 | + static createModule (wasm, id) { | |
64 | 66 | if (!WebAssembly.validate(wasm)) { |
65 | 67 | throw new Error('invalid wasm binary') |
66 | 68 | } |
69 | + | |
67 | 70 | let moduleJSON = wasm2json(wasm) |
68 | 71 | const json = customTypes.mergeTypeSections(moduleJSON) |
69 | 72 | moduleJSON = wasmMetering.meterJSON(moduleJSON, { |
70 | 73 | meterType: 'i32' |
@@ -86,9 +89,9 @@ | ||
86 | 89 | } |
87 | 90 | |
88 | 91 | static async onCreation (unverifiedWasm, id, tree) { |
89 | 92 | const cachedb = tree.dag._dag |
90 | - let {json, wasm, modRef} = this.validateWasm(unverifiedWasm, id) | |
93 | + let {json, wasm, modRef} = this.createModule(unverifiedWasm, id) | |
91 | 94 | await Promise.all([ |
92 | 95 | new Promise((resolve, reject) => { |
93 | 96 | cachedb.put(id.id.toString() + 'meta', JSON.stringify(json), resolve) |
94 | 97 | }), |
@@ -114,9 +117,9 @@ | ||
114 | 117 | const ref = new FunctionRef(true, func.tableIndex, params, self.actor.id) |
115 | 118 | return self.refs.add(ref, 'func') |
116 | 119 | } |
117 | 120 | }, |
118 | - internalize: (ref, index) => { | |
121 | + internalize: (index, ref) => { | |
119 | 122 | const funcRef = self.refs.get(ref, 'func') |
120 | 123 | const wrapper = generateWrapper(funcRef, self) |
121 | 124 | this.instance.exports.table.set(index, wrapper.exports.check) |
122 | 125 | }, |
@@ -150,9 +153,9 @@ | ||
150 | 153 | module: { |
151 | 154 | new: dataRef => { |
152 | 155 | |
153 | 156 | }, |
154 | - exports: (modRef, offset, length) => { | |
157 | + export: (modRef, bufRef) => { | |
155 | 158 | const mod = this.refs.get(modRef, 'mod') |
156 | 159 | let name = this.get8Memory(offset, length) |
157 | 160 | name = Buffer.from(name).toString() |
158 | 161 | const funcRef = mod.getFuncRef(name) |
@@ -202,9 +205,9 @@ | ||
202 | 205 | }, |
203 | 206 | metering: { |
204 | 207 | usegas: amount => { |
205 | 208 | this.actor.incrementTicks(amount) |
206 | - funcRef.gas -= amount | |
209 | + // funcRef.gas -= amount | |
207 | 210 | if (funcRef.gas < 0) { |
208 | 211 | throw new Error('out of gas! :(') |
209 | 212 | } |
210 | 213 | } |
Built with git-ssb-web