git ssb

0+

wanderer🌟 / js-primea-hypervisor



Commit 0df8aae87c8f14c08596394b83b87af8a4d64614

save code in the state

Signed-off-by: wanderer <mjbecze@gmail.com>
wanderer committed on 2/8/2018, 7:35:50 AM
Parent: ad560c8f8c3258d82e01e7dcf6778f3b3d1e8265

Files changed

index.jschanged
package-lock.jsonchanged
package.jsonchanged
tests/wasmContainer.jschanged
wasmContainer.jschanged
index.jsView
@@ -28,8 +28,9 @@
2828 }
2929
3030 async loadActor (id) {
3131 const state = await this.tree.getSubTree(id)
32+ const code = state.get(Buffer.from([0]))
3233 const {type, nonce} = Actor.deserializeMetaData(state.root['/'][3])
3334 const Container = this._containerTypes[type]
3435
3536 // create a new actor instance
@@ -39,8 +40,9 @@
3940 Container,
4041 id,
4142 nonce,
4243 type,
44+ code,
4345 cachedb: this.tree.dag._dag
4446 })
4547
4648 await actor.startup()
@@ -61,8 +63,11 @@
6163 const metaData = Actor.serializeMetaData(type)
6264
6365 // save the container in the state
6466 this.tree.set(idHash, metaData)
67+ if (code) {
68+ this.tree.set(Buffer.concat([idHash, Buffer.from([0])]), code)
69+ }
6570 return {
6671 id: idHash,
6772 exports: exports
6873 }
package-lock.jsonView
The diff is too large to show. Use a local git client to view these changes.
Old file size: 344090 bytes
New file size: 344090 bytes
package.jsonView
@@ -34,9 +34,9 @@
3434 "buffer-pipe": "0.0.2",
3535 "events": "^2.0.0",
3636 "leb128": "0.0.4",
3737 "levelup": "^2.0.1",
38- "reference-map": "^1.2.0",
38+ "reference-map": "^1.2.1",
3939 "safe-buffer": "^5.1.1",
4040 "wasm-json-toolkit": "^0.2.0",
4141 "wasm-metering": "^0.1.1"
4242 },
tests/wasmContainer.jsView
@@ -38,9 +38,9 @@
3838 tape('basic', async t => {
3939 t.plan(2)
4040 tester = t
4141 const expectedState = {
42- '/': Buffer.from('926de6b7eb39cfa8d7f8a44d1ef191d3bcb765a7', 'hex')
42+ '/': Buffer.from('4494963fb0e02312510e675fbca8b60b6e03bd00', 'hex')
4343 }
4444
4545 const tree = new RadixTree({
4646 db: db
@@ -57,15 +57,43 @@
5757 funcRef: exports.receive,
5858 funcArguments: [5]
5959 })
6060 hypervisor.send(message)
61+ const stateRoot = await hypervisor.createStateRoot()
62+ t.deepEquals(stateRoot, expectedState, 'expected root!')
63+})
6164
65+tape('basic', async t => {
66+ t.plan(2)
67+ tester = t
68+ const expectedState = {
69+ '/': Buffer.from('4494963fb0e02312510e675fbca8b60b6e03bd00', 'hex')
70+ }
71+
72+ const tree = new RadixTree({
73+ db: db
74+ })
75+
76+ const recieverWasm = fs.readFileSync('./wasm/reciever.wasm')
77+ const callerWasm = fs.readFileSync('./wasm/caller.wasm')
78+
79+ const hypervisor = new Hypervisor(tree)
80+ hypervisor.registerContainer(TestWasmContainer)
81+
82+ const {exports: receiverExports} = await hypervisor.createActor(TestWasmContainer.typeId, recieverWasm)
83+ const {exports: callerExports} = await hypervisor.createActor(TestWasmContainer.typeId, callerWasm)
84+
85+ const message = new Message({
86+ funcRef: receiverExports.receive,
87+ funcArguments: [5]
88+ })
89+ hypervisor.send(message)
6290 const stateRoot = await hypervisor.createStateRoot()
6391 t.deepEquals(stateRoot, expectedState, 'expected root!')
6492 })
6593
6694 // Increment a counter.
67-tape('increment', async t => {
95+tape.skip('increment', async t => {
6896
6997 const tree = new RadixTree({
7098 db: db
7199 })
@@ -87,5 +115,5 @@
87115 t.end()
88116
89117 console.log(stateRoot)
90118
91-})
119+})
wasmContainer.jsView
@@ -111,9 +111,9 @@
111111 })
112112 wasm = json2wasm(moduleJSON)
113113 await Promise.all([
114114 new Promise((resolve, reject) => {
115- cachedb.put(id.toString() + 'meta', json, resolve)
115+ cachedb.put(id.toString() + 'meta', JSON.stringify(json), resolve)
116116 }),
117117 new Promise((resolve, reject) => {
118118 cachedb.put(id.toString() + 'code', wasm.toString('hex'), resolve)
119119 })
@@ -164,9 +164,12 @@
164164 }
165165 },
166166 module: {
167167 new: code => {},
168- exports: (mod, name) => {}
168+ exports: (mod, name) => {},
169+ self: () => {
170+ return this.refs.add(this.json)
171+ }
169172 },
170173 memory: {
171174 externalize: (index, length) => {
172175 const buf = this.getMemory(index, length)
@@ -279,8 +282,9 @@
279282 })
280283 })
281284 ])
282285 wasm = Buffer.from(wasm, 'hex')
286+ json = JSON.parse(json)
283287 this.mod = WebAssembly.Module(wasm)
284288 this.json = json
285289 }
286290

Built with git-ssb-web