git ssb

0+

wanderer🌟 / js-primea-hypervisor



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.jschanged
wasmContainer.jschanged
index.jsView
@@ -1,7 +1,8 @@
11 const Actor = require('./actor.js')
22 const Scheduler = require('./scheduler.js')
33 const {ID} = require('./systemObjects.js')
4+const crypto = require('crypto')
45
56 module.exports = class Hypervisor {
67 /**
78 * The Hypervisor manages the container instances by instantiating them and
@@ -30,10 +31,10 @@
3031
3132 async loadActor (id) {
3233 const state = await this.tree.get(id.id, true)
3334 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')
3637 ])
3738 const [type, nonce] = state.value
3839 const Container = this._containerTypes[type]
3940
@@ -63,18 +64,18 @@
6364 */
6465 async createActor (type, code, id = {nonce: this.nonce++, parent: null}) {
6566 const Container = this._containerTypes[type]
6667 const encoded = encodedID(id)
67- let idHash = await this._getHashFromObj(encoded)
68+ let idHash = this._getHashFromObj(encoded)
6869 idHash = new ID(idHash)
6970 const module = await Container.onCreation(code, idHash, this.tree)
7071 const metaData = [type, 0]
7172
7273 // save the container in the state
7374 this.tree.set(idHash.id, metaData)
7475 if (code) {
7576 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)
7778 }
7879 return {
7980 id: idHash,
8081 module
@@ -82,9 +83,11 @@
8283 }
8384
8485 // get a hash from a POJO
8586 _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)
8790 }
8891
8992 /**
9093 * creates a state root starting from a given container and a given number of
wasmContainer.jsView
@@ -6,10 +6,8 @@
66 const injectGlobals = require('./injectGlobals.js')
77 const typeCheckWrapper = require('./typeCheckWrapper.js')
88 const {FunctionRef, ModuleRef, DEFAULTS} = require('./systemObjects.js')
99
10-const FUNC_INDEX_OFFSET = 1
11-
1210 const nativeTypes = new Set(['i32', 'i64', 'f32', 'f64'])
1311 const LANGUAGE_TYPES = {
1412 0x0: 'actor',
1513 0x1: 'buf',
@@ -23,8 +21,9 @@
2321 0x70: 'anyFunc',
2422 0x60: 'func',
2523 0x40: 'block_type'
2624 }
25+const FUNC_INDEX_OFFSET = 1
2726
2827 function generateWrapper (funcRef, container) {
2928 let wrapper = typeCheckWrapper(funcRef.params)
3029 const wasm = json2wasm(wrapper)
@@ -60,10 +59,9 @@
6059 this.actor = actor
6160 this.refs = new ReferanceMap()
6261 }
6362
64- static async onCreation (wasm, id, tree) {
65- const cachedb = tree.dag._dag
63+ static validateWasm (wasm, id) {
6664 if (!WebAssembly.validate(wasm)) {
6765 throw new Error('invalid wasm binary')
6866 }
6967 let moduleJSON = wasm2json(wasm)
@@ -78,17 +76,28 @@
7876 moduleJSON = injectGlobals(moduleJSON, json.globals)
7977 }
8078 // recompile the wasm
8179 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)
8291 await Promise.all([
8392 new Promise((resolve, reject) => {
8493 cachedb.put(id.id.toString() + 'meta', JSON.stringify(json), resolve)
8594 }),
8695 new Promise((resolve, reject) => {
8796 cachedb.put(id.id.toString() + 'code', wasm.toString('hex'), resolve)
8897 })
8998 ])
90- return ModuleRef.fromMetaJSON(json, id)
99+ return modRef
91100 }
92101
93102 getInterface (funcRef) {
94103 const self = this
@@ -138,9 +147,11 @@
138147 // todo
139148 }
140149 },
141150 module: {
142- new: code => {},
151+ new: dataRef => {
152+
153+ },
143154 exports: (modRef, offset, length) => {
144155 const mod = this.refs.get(modRef, 'mod')
145156 let name = this.getMemory(offset, length)
146157 name = Buffer.from(name).toString()
@@ -182,8 +193,12 @@
182193 let table = this.refs.get(elemRef, 'elem')
183194 const buf = table.slice(srcOffset, srcOffset + length).map(obj => this.refs.add(obj))
184195 const mem = new Uint32Array(this.instance.exports.memory.buffer, sinkOffset, length)
185196 mem.set(buf)
197+ },
198+ length (elemRef) {
199+ let elem = this.refs.get(elemRef, 'elem')
200+ return elem.length
186201 }
187202 },
188203 metering: {
189204 usegas: amount => {

Built with git-ssb-web