Commit 21960d62467278cd5659e2eb5f80cc6ddd87e663
use nodejs crypto instead of webcrypto
Signed-off-by: wanderer <mjbecze@gmail.com>wanderer committed on 3/2/2018, 11:35:50 PM
Parent: 5013b5ed5b183c9bd021b9b9b20f7df558b0a664
Files changed
index.js | changed |
wasmContainer.js | changed |
index.js | ||
---|---|---|
@@ -1,7 +1,8 @@ | ||
1 | 1 | const Actor = require('./actor.js') |
2 | 2 | const Scheduler = require('./scheduler.js') |
3 | 3 | const {ID} = require('./systemObjects.js') |
4 | +const crypto = require('crypto') | |
4 | 5 | |
5 | 6 | module.exports = class Hypervisor { |
6 | 7 | /** |
7 | 8 | * The Hypervisor manages the container instances by instantiating them and |
@@ -30,10 +31,10 @@ | ||
30 | 31 | |
31 | 32 | async loadActor (id) { |
32 | 33 | const state = await this.tree.get(id.id, true) |
33 | 34 | const [code, storage] = await Promise.all([ |
34 | - this.tree.graph.get(state.root, '1'), | |
35 | - this.tree.graph.get(state.root, '2') | |
35 | + this.tree.graph.get(state.node, '1'), | |
36 | + this.tree.graph.get(state.node, '2') | |
36 | 37 | ]) |
37 | 38 | const [type, nonce] = state.value |
38 | 39 | const Container = this._containerTypes[type] |
39 | 40 | |
@@ -63,18 +64,18 @@ | ||
63 | 64 | */ |
64 | 65 | async createActor (type, code, id = {nonce: this.nonce++, parent: null}) { |
65 | 66 | const Container = this._containerTypes[type] |
66 | 67 | const encoded = encodedID(id) |
67 | - let idHash = await this._getHashFromObj(encoded) | |
68 | + let idHash = this._getHashFromObj(encoded) | |
68 | 69 | idHash = new ID(idHash) |
69 | 70 | const module = await Container.onCreation(code, idHash, this.tree) |
70 | 71 | const metaData = [type, 0] |
71 | 72 | |
72 | 73 | // save the container in the state |
73 | 74 | this.tree.set(idHash.id, metaData) |
74 | 75 | if (code) { |
75 | 76 | const node = await this.tree.get(idHash.id) |
76 | - await this.tree.graph.set(node.root, '1', code) | |
77 | + await this.tree.graph.set(node.node, '1', code) | |
77 | 78 | } |
78 | 79 | return { |
79 | 80 | id: idHash, |
80 | 81 | module |
@@ -82,9 +83,11 @@ | ||
82 | 83 | } |
83 | 84 | |
84 | 85 | // get a hash from a POJO |
85 | 86 | _getHashFromObj (obj) { |
86 | - return this.tree.constructor.getMerkleLink(obj) | |
87 | + const hash = crypto.createHash('sha256') | |
88 | + hash.update(obj) | |
89 | + return hash.digest().slice(0, 20) | |
87 | 90 | } |
88 | 91 | |
89 | 92 | /** |
90 | 93 | * creates a state root starting from a given container and a given number of |
wasmContainer.js | ||
---|---|---|
@@ -6,10 +6,8 @@ | ||
6 | 6 | const injectGlobals = require('./injectGlobals.js') |
7 | 7 | const typeCheckWrapper = require('./typeCheckWrapper.js') |
8 | 8 | const {FunctionRef, ModuleRef, DEFAULTS} = require('./systemObjects.js') |
9 | 9 | |
10 | -const FUNC_INDEX_OFFSET = 1 | |
11 | - | |
12 | 10 | const nativeTypes = new Set(['i32', 'i64', 'f32', 'f64']) |
13 | 11 | const LANGUAGE_TYPES = { |
14 | 12 | 0x0: 'actor', |
15 | 13 | 0x1: 'buf', |
@@ -23,8 +21,9 @@ | ||
23 | 21 | 0x70: 'anyFunc', |
24 | 22 | 0x60: 'func', |
25 | 23 | 0x40: 'block_type' |
26 | 24 | } |
25 | +const FUNC_INDEX_OFFSET = 1 | |
27 | 26 | |
28 | 27 | function generateWrapper (funcRef, container) { |
29 | 28 | let wrapper = typeCheckWrapper(funcRef.params) |
30 | 29 | const wasm = json2wasm(wrapper) |
@@ -60,10 +59,9 @@ | ||
60 | 59 | this.actor = actor |
61 | 60 | this.refs = new ReferanceMap() |
62 | 61 | } |
63 | 62 | |
64 | - static async onCreation (wasm, id, tree) { | |
65 | - const cachedb = tree.dag._dag | |
63 | + static validateWasm (wasm, id) { | |
66 | 64 | if (!WebAssembly.validate(wasm)) { |
67 | 65 | throw new Error('invalid wasm binary') |
68 | 66 | } |
69 | 67 | let moduleJSON = wasm2json(wasm) |
@@ -78,17 +76,28 @@ | ||
78 | 76 | moduleJSON = injectGlobals(moduleJSON, json.globals) |
79 | 77 | } |
80 | 78 | // recompile the wasm |
81 | 79 | wasm = json2wasm(moduleJSON) |
80 | + const modRef = ModuleRef.fromMetaJSON(json, id) | |
81 | + return { | |
82 | + wasm, | |
83 | + json, | |
84 | + modRef | |
85 | + } | |
86 | + } | |
87 | + | |
88 | + static async onCreation (unverifiedWasm, id, tree) { | |
89 | + const cachedb = tree.dag._dag | |
90 | + let {json, wasm, modRef} = this.validateWasm(unverifiedWasm, id) | |
82 | 91 | await Promise.all([ |
83 | 92 | new Promise((resolve, reject) => { |
84 | 93 | cachedb.put(id.id.toString() + 'meta', JSON.stringify(json), resolve) |
85 | 94 | }), |
86 | 95 | new Promise((resolve, reject) => { |
87 | 96 | cachedb.put(id.id.toString() + 'code', wasm.toString('hex'), resolve) |
88 | 97 | }) |
89 | 98 | ]) |
90 | - return ModuleRef.fromMetaJSON(json, id) | |
99 | + return modRef | |
91 | 100 | } |
92 | 101 | |
93 | 102 | getInterface (funcRef) { |
94 | 103 | const self = this |
@@ -138,9 +147,11 @@ | ||
138 | 147 | // todo |
139 | 148 | } |
140 | 149 | }, |
141 | 150 | module: { |
142 | - new: code => {}, | |
151 | + new: dataRef => { | |
152 | + | |
153 | + }, | |
143 | 154 | exports: (modRef, offset, length) => { |
144 | 155 | const mod = this.refs.get(modRef, 'mod') |
145 | 156 | let name = this.getMemory(offset, length) |
146 | 157 | name = Buffer.from(name).toString() |
@@ -182,8 +193,12 @@ | ||
182 | 193 | let table = this.refs.get(elemRef, 'elem') |
183 | 194 | const buf = table.slice(srcOffset, srcOffset + length).map(obj => this.refs.add(obj)) |
184 | 195 | const mem = new Uint32Array(this.instance.exports.memory.buffer, sinkOffset, length) |
185 | 196 | mem.set(buf) |
197 | + }, | |
198 | + length (elemRef) { | |
199 | + let elem = this.refs.get(elemRef, 'elem') | |
200 | + return elem.length | |
186 | 201 | } |
187 | 202 | }, |
188 | 203 | metering: { |
189 | 204 | usegas: amount => { |
Built with git-ssb-web