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.js | changed |
package-lock.json | changed |
package.json | changed |
tests/wasmContainer.js | changed |
wasmContainer.js | changed |
index.js | ||
---|---|---|
@@ -28,8 +28,9 @@ | ||
28 | 28 | } |
29 | 29 | |
30 | 30 | async loadActor (id) { |
31 | 31 | const state = await this.tree.getSubTree(id) |
32 | + const code = state.get(Buffer.from([0])) | |
32 | 33 | const {type, nonce} = Actor.deserializeMetaData(state.root['/'][3]) |
33 | 34 | const Container = this._containerTypes[type] |
34 | 35 | |
35 | 36 | // create a new actor instance |
@@ -39,8 +40,9 @@ | ||
39 | 40 | Container, |
40 | 41 | id, |
41 | 42 | nonce, |
42 | 43 | type, |
44 | + code, | |
43 | 45 | cachedb: this.tree.dag._dag |
44 | 46 | }) |
45 | 47 | |
46 | 48 | await actor.startup() |
@@ -61,8 +63,11 @@ | ||
61 | 63 | const metaData = Actor.serializeMetaData(type) |
62 | 64 | |
63 | 65 | // save the container in the state |
64 | 66 | this.tree.set(idHash, metaData) |
67 | + if (code) { | |
68 | + this.tree.set(Buffer.concat([idHash, Buffer.from([0])]), code) | |
69 | + } | |
65 | 70 | return { |
66 | 71 | id: idHash, |
67 | 72 | exports: exports |
68 | 73 | } |
package-lock.json | ||
---|---|---|
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.json | ||
---|---|---|
@@ -34,9 +34,9 @@ | ||
34 | 34 | "buffer-pipe": "0.0.2", |
35 | 35 | "events": "^2.0.0", |
36 | 36 | "leb128": "0.0.4", |
37 | 37 | "levelup": "^2.0.1", |
38 | - "reference-map": "^1.2.0", | |
38 | + "reference-map": "^1.2.1", | |
39 | 39 | "safe-buffer": "^5.1.1", |
40 | 40 | "wasm-json-toolkit": "^0.2.0", |
41 | 41 | "wasm-metering": "^0.1.1" |
42 | 42 | }, |
tests/wasmContainer.js | ||
---|---|---|
@@ -38,9 +38,9 @@ | ||
38 | 38 | tape('basic', async t => { |
39 | 39 | t.plan(2) |
40 | 40 | tester = t |
41 | 41 | const expectedState = { |
42 | - '/': Buffer.from('926de6b7eb39cfa8d7f8a44d1ef191d3bcb765a7', 'hex') | |
42 | + '/': Buffer.from('4494963fb0e02312510e675fbca8b60b6e03bd00', 'hex') | |
43 | 43 | } |
44 | 44 | |
45 | 45 | const tree = new RadixTree({ |
46 | 46 | db: db |
@@ -57,15 +57,43 @@ | ||
57 | 57 | funcRef: exports.receive, |
58 | 58 | funcArguments: [5] |
59 | 59 | }) |
60 | 60 | hypervisor.send(message) |
61 | + const stateRoot = await hypervisor.createStateRoot() | |
62 | + t.deepEquals(stateRoot, expectedState, 'expected root!') | |
63 | +}) | |
61 | 64 | |
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) | |
62 | 90 | const stateRoot = await hypervisor.createStateRoot() |
63 | 91 | t.deepEquals(stateRoot, expectedState, 'expected root!') |
64 | 92 | }) |
65 | 93 | |
66 | 94 | // Increment a counter. |
67 | -tape('increment', async t => { | |
95 | +tape.skip('increment', async t => { | |
68 | 96 | |
69 | 97 | const tree = new RadixTree({ |
70 | 98 | db: db |
71 | 99 | }) |
@@ -87,5 +115,5 @@ | ||
87 | 115 | t.end() |
88 | 116 | |
89 | 117 | console.log(stateRoot) |
90 | 118 | |
91 | -}) | |
119 | +}) |
wasmContainer.js | ||
---|---|---|
@@ -111,9 +111,9 @@ | ||
111 | 111 | }) |
112 | 112 | wasm = json2wasm(moduleJSON) |
113 | 113 | await Promise.all([ |
114 | 114 | new Promise((resolve, reject) => { |
115 | - cachedb.put(id.toString() + 'meta', json, resolve) | |
115 | + cachedb.put(id.toString() + 'meta', JSON.stringify(json), resolve) | |
116 | 116 | }), |
117 | 117 | new Promise((resolve, reject) => { |
118 | 118 | cachedb.put(id.toString() + 'code', wasm.toString('hex'), resolve) |
119 | 119 | }) |
@@ -164,9 +164,12 @@ | ||
164 | 164 | } |
165 | 165 | }, |
166 | 166 | module: { |
167 | 167 | new: code => {}, |
168 | - exports: (mod, name) => {} | |
168 | + exports: (mod, name) => {}, | |
169 | + self: () => { | |
170 | + return this.refs.add(this.json) | |
171 | + } | |
169 | 172 | }, |
170 | 173 | memory: { |
171 | 174 | externalize: (index, length) => { |
172 | 175 | const buf = this.getMemory(index, length) |
@@ -279,8 +282,9 @@ | ||
279 | 282 | }) |
280 | 283 | }) |
281 | 284 | ]) |
282 | 285 | wasm = Buffer.from(wasm, 'hex') |
286 | + json = JSON.parse(json) | |
283 | 287 | this.mod = WebAssembly.Module(wasm) |
284 | 288 | this.json = json |
285 | 289 | } |
286 | 290 |
Built with git-ssb-web