Commit 426c0bb6c8135b071cb6598828943842a782bb7f
simpleify storage
Signed-off-by: wanderer <mjbecze@gmail.com>wanderer committed on 3/6/2018, 3:01:45 AM
Parent: 21960d62467278cd5659e2eb5f80cc6ddd87e663
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 | ||
---|---|---|
@@ -31,21 +31,20 @@ | ||
31 | 31 | "dependencies": { |
32 | 32 | "binary-search-insert": "^1.0.3", |
33 | 33 | "borc": "^2.0.2", |
34 | 34 | "events": "^2.0.0", |
35 | - "levelup": "^2.0.2", | |
36 | - "reference-map": "^1.2.1", | |
35 | + "reference-map": "^1.2.3", | |
37 | 36 | "safe-buffer": "^5.1.1", |
38 | 37 | "wasm-json-toolkit": "^0.2.2", |
39 | 38 | "wasm-metering": "^0.1.1" |
40 | 39 | }, |
41 | 40 | "devDependencies": { |
41 | + "levelup": "^2.0.2", | |
42 | 42 | "coveralls": "^3.0.0", |
43 | - "dfinity-radix-tree": "0.0.9", | |
43 | + "dfinity-radix-tree": "0.1.1", | |
44 | 44 | "documentation": "^5.3.5", |
45 | 45 | "level-browserify": "^1.1.1", |
46 | 46 | "nyc": "^11.4.1", |
47 | - "primea-abstract-container": "0.0.6", | |
48 | 47 | "standard": "11.0.0", |
49 | 48 | "tape": "^4.9.0", |
50 | 49 | "wabt": "^1.0.0" |
51 | 50 | } |
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 | ||
---|---|---|
@@ -118,9 +118,9 @@ | ||
118 | 118 | |
119 | 119 | const message = new Message({ |
120 | 120 | funcRef: callFuncRef, |
121 | 121 | funcArguments: [recvFuncRef] |
122 | - }) | |
122 | + }).on('execution:error', e => console.log(e)) | |
123 | 123 | |
124 | 124 | hypervisor.send(message) |
125 | 125 | const stateRoot = await hypervisor.createStateRoot() |
126 | 126 | // t.deepEquals(stateRoot, expectedState, 'expected root!') |
@@ -239,32 +239,35 @@ | ||
239 | 239 | funcRef.gas = 400 |
240 | 240 | const message = new Message({ |
241 | 241 | funcRef |
242 | 242 | }).on('done', actor => { |
243 | - resolve() | |
244 | 243 | const b = actor.container.getMemory(5, 4) |
245 | 244 | const result = Buffer.from(b).toString() |
246 | 245 | t.deepEquals(result, 'test', 'should copy memory correctly') |
246 | + resolve() | |
247 | 247 | }) |
248 | 248 | hypervisor.send(message) |
249 | 249 | }) |
250 | 250 | }) |
251 | 251 | |
252 | -tape.skip('ben', async t => { | |
252 | +tape('ben', async t => { | |
253 | 253 | // t.plan(1) |
254 | 254 | tester = t |
255 | 255 | const tree = new RadixTree({ |
256 | 256 | db |
257 | 257 | }) |
258 | 258 | |
259 | - const wasm = fs.readFileSync('./hi.wasm') | |
259 | + const wasm = fs.readFileSync('./indirect.wasm') | |
260 | 260 | const hypervisor = new Hypervisor(tree) |
261 | 261 | hypervisor.registerContainer(TestWasmContainer) |
262 | 262 | |
263 | 263 | const {module} = await hypervisor.createActor(TestWasmContainer.typeId, wasm) |
264 | 264 | |
265 | + const funcRef = module.getFuncRef('#main') | |
266 | + funcRef.gas = 100000 | |
267 | + | |
265 | 268 | const message = new Message({ |
266 | - funcRef: module.getFuncRef('main') | |
269 | + funcRef | |
267 | 270 | }).on('done', () => { |
268 | 271 | t.end() |
269 | 272 | }) |
270 | 273 | 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,11 +153,11 @@ | ||
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 | - let name = this.getMemory(offset, length) | |
159 | + let name = this.refs.get(bufRef, 'buf') | |
157 | 160 | name = Buffer.from(name).toString() |
158 | 161 | const funcRef = mod.getFuncRef(name) |
159 | 162 | return this.refs.add(funcRef, 'func') |
160 | 163 | }, |
@@ -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