git ssb

0+

wanderer🌟 / js-primea-hypervisor



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.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
@@ -31,21 +31,20 @@
3131 "dependencies": {
3232 "binary-search-insert": "^1.0.3",
3333 "borc": "^2.0.2",
3434 "events": "^2.0.0",
35- "levelup": "^2.0.2",
36- "reference-map": "^1.2.1",
35+ "reference-map": "^1.2.3",
3736 "safe-buffer": "^5.1.1",
3837 "wasm-json-toolkit": "^0.2.2",
3938 "wasm-metering": "^0.1.1"
4039 },
4140 "devDependencies": {
41+ "levelup": "^2.0.2",
4242 "coveralls": "^3.0.0",
43- "dfinity-radix-tree": "0.0.9",
43+ "dfinity-radix-tree": "0.1.1",
4444 "documentation": "^5.3.5",
4545 "level-browserify": "^1.1.1",
4646 "nyc": "^11.4.1",
47- "primea-abstract-container": "0.0.6",
4847 "standard": "11.0.0",
4948 "tape": "^4.9.0",
5049 "wabt": "^1.0.0"
5150 }
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
@@ -118,9 +118,9 @@
118118
119119 const message = new Message({
120120 funcRef: callFuncRef,
121121 funcArguments: [recvFuncRef]
122- })
122+ }).on('execution:error', e => console.log(e))
123123
124124 hypervisor.send(message)
125125 const stateRoot = await hypervisor.createStateRoot()
126126 // t.deepEquals(stateRoot, expectedState, 'expected root!')
@@ -239,32 +239,35 @@
239239 funcRef.gas = 400
240240 const message = new Message({
241241 funcRef
242242 }).on('done', actor => {
243- resolve()
244243 const b = actor.container.getMemory(5, 4)
245244 const result = Buffer.from(b).toString()
246245 t.deepEquals(result, 'test', 'should copy memory correctly')
246+ resolve()
247247 })
248248 hypervisor.send(message)
249249 })
250250 })
251251
252-tape.skip('ben', async t => {
252+tape('ben', async t => {
253253 // t.plan(1)
254254 tester = t
255255 const tree = new RadixTree({
256256 db
257257 })
258258
259- const wasm = fs.readFileSync('./hi.wasm')
259+ const wasm = fs.readFileSync('./indirect.wasm')
260260 const hypervisor = new Hypervisor(tree)
261261 hypervisor.registerContainer(TestWasmContainer)
262262
263263 const {module} = await hypervisor.createActor(TestWasmContainer.typeId, wasm)
264264
265+ const funcRef = module.getFuncRef('#main')
266+ funcRef.gas = 100000
267+
265268 const message = new Message({
266- funcRef: module.getFuncRef('main')
269+ funcRef
267270 }).on('done', () => {
268271 t.end()
269272 })
270273 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,11 +153,11 @@
150153 module: {
151154 new: dataRef => {
152155
153156 },
154- exports: (modRef, offset, length) => {
157+ export: (modRef, bufRef) => {
155158 const mod = this.refs.get(modRef, 'mod')
156- let name = this.getMemory(offset, length)
159+ let name = this.refs.get(bufRef, 'buf')
157160 name = Buffer.from(name).toString()
158161 const funcRef = mod.getFuncRef(name)
159162 return this.refs.add(funcRef, 'func')
160163 },
@@ -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