git ssb

0+

wanderer🌟 / js-primea-hypervisor



Commit a9764e4cea10db92064ace79bfa09f6e01704fb4

Merge branch 'master' into small-fixes

wanderer authored on 3/6/2018, 3:09:17 AM
GitHub committed on 3/6/2018, 3:09:17 AM
Parent: 18c73fcab36d8356ed6384cceb0fa31e40c3895c
Parent: 426c0bb6c8135b071cb6598828943842a782bb7f

Files changed

actor.jschanged
index.jschanged
package-lock.jsonchanged
package.jsonchanged
tests/index.jschanged
tests/wasm/funcRef_caller.wasmchanged
tests/wasmContainer.jschanged
tests/wast/funcRef_caller.wastchanged
wasmContainer.jschanged
actor.jsView
@@ -21,12 +21,10 @@
2121 * Runs the shutdown routine for the actor
2222 */
2323 async shutdown () {
2424 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)
2927 }
3028
3129 /**
3230 * Runs the startup routine for the actor
index.jsView
@@ -64,29 +64,33 @@
6464 */
6565 async createActor (type, code, id = {nonce: this.nonce++, parent: null}) {
6666 const Container = this._containerTypes[type]
6767 const encoded = encodedID(id)
68- let idHash = this._getHashFromObj(encoded)
68+ let idHash = this._hash(encoded)
6969 idHash = new ID(idHash)
7070 const module = await Container.onCreation(code, idHash, this.tree)
7171 const metaData = [type, 0]
7272
7373 // 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
7878 }
79+ // save the storage
80+ node[2] = {
81+ '/': []
82+ }
83+
7984 return {
8085 id: idHash,
8186 module
8287 }
8388 }
8489
85- // get a hash from a POJO
86- _getHashFromObj (obj) {
90+ _hash (buf) {
8791 const hash = crypto.createHash('sha256')
88- hash.update(obj)
92+ hash.update(buf)
8993 return hash.digest().slice(0, 20)
9094 }
9195
9296 /**
package-lock.jsonView
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.jsonView
@@ -32,21 +32,20 @@
3232 "dependencies": {
3333 "binary-search-insert": "^1.0.3",
3434 "borc": "^2.0.2",
3535 "events": "^2.0.0",
36- "levelup": "^2.0.2",
37- "reference-map": "^1.2.1",
36+ "reference-map": "^1.2.3",
3837 "safe-buffer": "^5.1.1",
3938 "wasm-json-toolkit": "^0.2.2",
4039 "wasm-metering": "^0.1.1"
4140 },
4241 "devDependencies": {
42+ "levelup": "^2.0.2",
4343 "coveralls": "^3.0.0",
44- "dfinity-radix-tree": "0.0.9",
44+ "dfinity-radix-tree": "0.1.1",
4545 "documentation": "^5.3.5",
4646 "level-browserify": "^1.1.1",
4747 "nyc": "^11.4.1",
48- "primea-abstract-container": "0.0.6",
4948 "standard": "11.0.0",
5049 "tape": "^4.9.0",
5150 "wabt": "^1.0.0"
5251 }
tests/index.jsView
@@ -31,9 +31,9 @@
3131
3232 tape('basic', async t => {
3333 t.plan(2)
3434 const expectedState = {
35- '/': Buffer.from('a5e1aaebec14b7f144d6a7e007b148aa7c56804c', 'hex')
35+ '/': Buffer.from('cd80335de00c2bf38570b41c55a79174c1c64e9f', 'hex')
3636 }
3737
3838 const tree = new RadixTree({
3939 db: db
@@ -62,9 +62,9 @@
6262
6363 tape('two communicating actors', async t => {
6464 t.plan(2)
6565 const expectedState = {
66- '/': Buffer.from('bc0af44b691ef57b362df8b11c274742147900cd', 'hex')
66+ '/': Buffer.from('b063f3e53b2ea40f50afe964b9f9b49aad491155', 'hex')
6767 }
6868
6969 const tree = new RadixTree({
7070 db: db
@@ -110,9 +110,9 @@
110110
111111 tape('three communicating actors', async t => {
112112 t.plan(3)
113113 const expectedState = {
114- '/': Buffer.from('86e066f60fe2f722befbc29f128948f6487a207a', 'hex')
114+ '/': Buffer.from('db532195ac569b142415cc9bdbec37f18f344a59', 'hex')
115115 }
116116
117117 const tree = new RadixTree({
118118 db: db
@@ -165,9 +165,9 @@
165165
166166 tape('three communicating actors, with tick counting', async t => {
167167 t.plan(3)
168168 const expectedState = {
169- '/': Buffer.from('86e066f60fe2f722befbc29f128948f6487a207a', 'hex')
169+ '/': Buffer.from('db532195ac569b142415cc9bdbec37f18f344a59', 'hex')
170170 }
171171
172172 const tree = new RadixTree({
173173 db: db
@@ -220,9 +220,9 @@
220220
221221 tape('errors', async t => {
222222 t.plan(3)
223223 const expectedState = {
224- '/': Buffer.from('bc0af44b691ef57b362df8b11c274742147900cd', 'hex')
224+ '/': Buffer.from('b063f3e53b2ea40f50afe964b9f9b49aad491155', 'hex')
225225 }
226226
227227 const tree = new RadixTree({
228228 db: db
@@ -268,9 +268,9 @@
268268
269269 tape('actor creation', async t => {
270270 t.plan(2)
271271 const expectedState = {
272- '/': Buffer.from('544ae05bc8831cc69c5524bc3a51c0d4806e9d60', 'hex')
272+ '/': Buffer.from('8a21d80cd7ca04e64be7cb2726a72060fc546ed6', 'hex')
273273 }
274274
275275 const tree = new RadixTree({
276276 db: db
@@ -317,9 +317,9 @@
317317
318318 tape('simple message arbiter test', async t => {
319319 t.plan(4)
320320 const expectedState = {
321- '/': Buffer.from('bc0af44b691ef57b362df8b11c274742147900cd', 'hex')
321+ '/': Buffer.from('b063f3e53b2ea40f50afe964b9f9b49aad491155', 'hex')
322322 }
323323
324324 const tree = new RadixTree({
325325 db: db
@@ -386,9 +386,9 @@
386386 tape('arbiter test for id comparision', async t => {
387387 t.plan(4)
388388 let message
389389 const expectedState = {
390- '/': Buffer.from('86e066f60fe2f722befbc29f128948f6487a207a', 'hex')
390+ '/': Buffer.from('db532195ac569b142415cc9bdbec37f18f344a59', 'hex')
391391 }
392392
393393 const tree = new RadixTree({
394394 db: db
@@ -453,9 +453,9 @@
453453
454454 tape('async work', async t => {
455455 t.plan(3)
456456 const expectedState = {
457- '/': Buffer.from('bc0af44b691ef57b362df8b11c274742147900cd', 'hex')
457+ '/': Buffer.from('b063f3e53b2ea40f50afe964b9f9b49aad491155', 'hex')
458458 }
459459
460460 const tree = new RadixTree({
461461 db: db
tests/wasm/funcRef_caller.wasmView
@@ -1,2 +1,2 @@
1-asm types`` typeMap````@func internalizetestcheckmoduleselfmoduleexportsp$memorytablecallcallback
2- AAA A  A A callback
1+asm types`` typeMap````Tfunc internalizetestcheckmoduleselfmoduleexportmemory externalizep$memorytablecallcallback
2+"AAA A  A A callback
tests/wasmContainer.jsView
@@ -150,9 +150,9 @@
150150
151151 const message = new Message({
152152 funcRef: callFuncRef,
153153 funcArguments: [recvFuncRef]
154- })
154+ }).on('execution:error', e => console.log(e))
155155
156156 hypervisor.send(message)
157157 const stateRoot = await hypervisor.createStateRoot()
158158 // t.deepEquals(stateRoot, expectedState, 'expected root!')
@@ -271,18 +271,18 @@
271271 funcRef.gas = 400
272272 const message = new Message({
273273 funcRef
274274 }).on('done', actor => {
275- resolve()
276275 const b = actor.container.get8Memory(5, 4)
277276 const result = Buffer.from(b).toString()
278277 t.deepEquals(result, 'test', 'should copy memory correctly')
278+ resolve()
279279 })
280280 hypervisor.send(message)
281281 })
282282 })
283283
284-tape.skip('ben', async t => {
284+tape('ben', async t => {
285285 // t.plan(1)
286286 tester = t
287287 const tree = new RadixTree({
288288 db
@@ -293,10 +293,13 @@
293293 hypervisor.registerContainer(TestWasmContainer)
294294
295295 const {module} = await hypervisor.createActor(TestWasmContainer.typeId, wasm)
296296
297+ const funcRef = module.getFuncRef('#main')
298+ funcRef.gas = 100000
299+
297300 const message = new Message({
298- funcRef: module.getFuncRef('main')
301+ funcRef
299302 }).on('done', () => {
300303 t.end()
301304 })
302305 hypervisor.send(message)
tests/wast/funcRef_caller.wastView
@@ -1,16 +1,18 @@
11 (module
22 (import "func" "internalize" (func $internalize (param i32 i32)))
33 (import "test" "check" (func $check (param i32 i32)))
44 (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)))
67 (memory (export "memory") 1)
78 (data (i32.const 0) "callback")
89 (table (export "table") 1 1 anyfunc)
910 (func $call (param i32)
1011 call $self
1112 i32.const 0
1213 i32.const 8
14+ call $externalize
1315 call $exports
1416
1517 i32.const 0
1618 get_local 0
wasmContainer.jsView
@@ -44,8 +44,10 @@
4444 }
4545 const message = new Message({
4646 funcRef: self,
4747 funcArguments: checkedArgs
48+ }).on('execution:error', e => {
49+ console.log(e)
4850 })
4951 container.actor.send(message)
5052 }
5153 }
@@ -59,12 +61,13 @@
5961 this.actor = actor
6062 this.refs = new ReferanceMap()
6163 }
6264
63- static validateWasm (wasm, id) {
65+ static createModule (wasm, id) {
6466 if (!WebAssembly.validate(wasm)) {
6567 throw new Error('invalid wasm binary')
6668 }
69+
6770 let moduleJSON = wasm2json(wasm)
6871 const json = customTypes.mergeTypeSections(moduleJSON)
6972 moduleJSON = wasmMetering.meterJSON(moduleJSON, {
7073 meterType: 'i32'
@@ -86,9 +89,9 @@
8689 }
8790
8891 static async onCreation (unverifiedWasm, id, tree) {
8992 const cachedb = tree.dag._dag
90- let {json, wasm, modRef} = this.validateWasm(unverifiedWasm, id)
93+ let {json, wasm, modRef} = this.createModule(unverifiedWasm, id)
9194 await Promise.all([
9295 new Promise((resolve, reject) => {
9396 cachedb.put(id.id.toString() + 'meta', JSON.stringify(json), resolve)
9497 }),
@@ -114,9 +117,9 @@
114117 const ref = new FunctionRef(true, func.tableIndex, params, self.actor.id)
115118 return self.refs.add(ref, 'func')
116119 }
117120 },
118- internalize: (ref, index) => {
121+ internalize: (index, ref) => {
119122 const funcRef = self.refs.get(ref, 'func')
120123 const wrapper = generateWrapper(funcRef, self)
121124 this.instance.exports.table.set(index, wrapper.exports.check)
122125 },
@@ -150,9 +153,9 @@
150153 module: {
151154 new: dataRef => {
152155
153156 },
154- exports: (modRef, offset, length) => {
157+ export: (modRef, bufRef) => {
155158 const mod = this.refs.get(modRef, 'mod')
156159 let name = this.get8Memory(offset, length)
157160 name = Buffer.from(name).toString()
158161 const funcRef = mod.getFuncRef(name)
@@ -202,9 +205,9 @@
202205 },
203206 metering: {
204207 usegas: amount => {
205208 this.actor.incrementTicks(amount)
206- funcRef.gas -= amount
209+ // funcRef.gas -= amount
207210 if (funcRef.gas < 0) {
208211 throw new Error('out of gas! :(')
209212 }
210213 }

Built with git-ssb-web