git ssb

0+

wanderer🌟 / js-primea-hypervisor



Commit 7032c24fef95c3d6156011b20edea07662aeaec1

remove wasmAgent

wanderer committed on 3/16/2017, 4:02:35 PM
Parent: ea59a1089955c500d2cda985bf74f9fc83ba71e3

Files changed

EVMinterface.jschanged
codeHandler.jschanged
package.jsonchanged
tests/buildTests.jschanged
tests/interface/basic_gas_ops.wasmchanged
tests/interface/basic_gas_ops.wastchanged
tests/interfaceRunner.jschanged
wasmAgent.jsdeleted
EVMinterface.jsView
@@ -2,9 +2,8 @@
22 * This is the Ethereum interface that is exposed to the WASM instance which
33 * enables to interact with the Ethereum Environment
44 */
55 const fs = require('fs')
6-const path = require('path')
76 const ethUtil = require('ethereumjs-util')
87 const Vertex = require('merkle-trie')
98 const U256 = require('./deps/u256.js')
109 const Message = require('./message.js')
@@ -21,65 +20,31 @@
2120 this.message = opts.message
2221 this.kernel = opts.kernel
2322 this.vm = opts.vm
2423 this.results = opts.response
25- const shimBin = fs.readFileSync(path.join(__dirname, '/wasm/interface.wasm'))
24+
25+ const shimBin = fs.readFileSync(`${__dirname}/wasm/interface.wasm`)
2626 const shimMod = WebAssembly.Module(shimBin)
27+
2728 this.shims = WebAssembly.Instance(shimMod, {
2829 'interface': {
2930 'useGas': this._useGas.bind(this),
3031 'getGasLeftHigh': this._getGasLeftHigh.bind(this),
3132 'getGasLeftLow': this._getGasLeftLow.bind(this),
3233 'call': this._call.bind(this)
3334 }
3435 })
36+ this.useGas = this.shims.exports.useGas
37+ this.getGasLeft = this.shims.exports.getGasLeft
38+ this.call = this.shims.exports.call
3539 }
3640
3741 static get name () {
3842 return 'ethereum'
3943 }
4044
41- get exports () {
42- let exportMethods = [
43- // include all the public methods according to the Ethereum Environment Interface (EEI) r1
44- 'getAddress',
45- 'getBalance',
46- 'getTxOrigin',
47- 'getCaller',
48- 'getCallValue',
49- 'getCallDataSize',
50- 'callDataCopy',
51- 'callDataCopy256',
52- 'getCodeSize',
53- 'codeCopy',
54- 'getExternalCodeSize',
55- 'externalCodeCopy',
56- 'getTxGasPrice',
57- 'getBlockHash',
58- 'getBlockCoinbase',
59- 'getBlockTimestamp',
60- 'getBlockNumber',
61- 'getBlockDifficulty',
62- 'getBlockGasLimit',
63- 'log',
64- 'create',
65- 'callCode',
66- 'callDelegate',
67- 'storageStore',
68- 'storageLoad',
69- 'return',
70- 'selfDestruct'
71- ]
72- let ret = {}
73- exportMethods.forEach((method) => {
74- ret[method] = this[method].bind(this)
75- })
76-
77- // add shims
78- ret.useGas = this.shims.exports.useGas
79- ret.getGasLeft = this.shims.exports.getGasLeft
80- ret.call = this.shims.exports.call
81- return ret
45+ static get hostContainer () {
46+ return 'wasm'
8247 }
8348
8449 setModule (mod) {
8550 this.module = mod
@@ -484,8 +449,9 @@
484449 // wait for all the prevouse async ops to finish before running the callback
485450 this.vm.pushOpsQueue(messagePromise, cbIndex, () => {
486451 return 1
487452 })
453+ return 1
488454 }
489455
490456 /**
491457 * Message-call into this account with an alternative account’s code.
@@ -559,8 +525,9 @@
559525 const valIsZero = value.every((i) => i === 0)
560526 const opPromise = this.kernel.state.get(key)
561527 .then(vertex => vertex.value)
562528 .catch(() => null)
529+ .then()
563530
564531 this.vm.pushOpsQueue(opPromise, cbIndex, oldValue => {
565532 if (valIsZero && oldValue) {
566533 // delete a value
codeHandler.jsView
@@ -1,5 +1,5 @@
1-const Wasm = require('./wasmAgent.js')
1+const Wasm = require('prima-wasm-container')
22
33 const defaultHandler = {
44 test: (code) => {
55 return !code
package.jsonView
@@ -43,8 +43,9 @@
4343 "dependencies": {
4444 "bn.js": "^4.11.6",
4545 "ethereumjs-block": "^1.5.0",
4646 "ethereumjs-tx": "^1.2.5",
47+ "ethereumjs-util": "^5.1.0",
4748 "merkle-trie": "0.2.0",
48- "ethereumjs-util": "^5.1.0"
49+ "prima-wasm-container": "0.0.0"
4950 }
5051 }
tests/buildTests.jsView
@@ -7,8 +7,9 @@
77 let tests = fs.readdirSync(dir).filter((file) => file.endsWith('.wast'))
88 // tests = ['balance.wast']
99 // run the tests
1010 for (let testName of tests) {
11+ console.log(testName)
1112 testName = testName.split('.')[0]
1213 // Compile Command
1314 cp.execSync(`${__dirname}/../tools/wabt/out/wast2wasm ${dir}/${testName}.wast -o ${dir}/${testName}.wasm`)
1415 }
tests/interface/basic_gas_ops.wasmView
@@ -1,3 +1,3 @@
11 asm  `~`~`)ethereumuseGasethereum
2-getGasLefttest
3-(&@BB�Q@ @BB�Q@
2+getGasLeftmain
3+@BB�Q@
tests/interface/basic_gas_ops.wastView
@@ -1,10 +1,10 @@
11 ;; starts with 1000 gas
22 (module
33 (import "ethereum" "useGas" (func $useGas (param i64)))
4- (import "ethereum" "getGasLeft" (func $gas (result i64)))
4+ (import "ethereum" "getGasLeft" (func $gas (result i64)))
55
6- (export "test" (func $main))
6+ (export "main" (func $main))
77 (func $main
88 ;; test adding gas
99 (block
1010 (call $useGas (i64.const 1))
tests/interfaceRunner.jsView
@@ -13,9 +13,8 @@
1313 const dir = path.join(__dirname, '/interface')
1414 const EVMinterface = require('../EVMinterface.js')
1515 // get the test names
1616 let tests = fs.readdirSync(dir).filter((file) => file.endsWith('.wast'))
17-// tests = ['sstore.wast']
1817
1918 runTests(tests)
2019
2120 function runTests (tests) {
@@ -59,9 +58,9 @@
5958 const message = new Message()
6059 message.to = ['accounts', envData.caller, common.PARENT, envData.address, 'code']
6160 message.data = new Buffer(envData.callData.slice(2), 'hex')
6261 message.value = new U256(envData.callValue)
63- message.gas = 1000000
62+ message.gas = envData.gasLeft
6463 message.block = block
6564 message.blockchain = fakeBlockChain
6665
6766 const results = await hypervisor.send(message)
wasmAgent.jsView
@@ -1,67 +1,0 @@
1-module.exports = class Wasm {
2- /**
3- * The interface API is the api the exposed to interfaces. All queries about
4- * the enviroment and call to the kernel go through this API
5- */
6- constructor (code) {
7- this._module = WebAssembly.Module(code)
8- }
9- /**
10- * Runs the core VM with a given environment and imports
11- */
12- async run (message, kernel, imports) {
13- const responses = {}
14- /**
15- * Builds a import map with an array of given interfaces
16- */
17- function buildImports (opts, imports) {
18- const importMap = {}
19- for (const Import of imports) {
20- opts.response = responses[Import.name] = {}
21- const newInterface = new Import(opts)
22- importMap[Import.name] = newInterface.exports
23- }
24- return importMap
25- }
26-
27- let instance
28-
29- const opts = {
30- vm: {
31- /**
32- * adds an aync operation to the operations queue
33- */
34- pushOpsQueue: (promise, callbackIndex, intefaceCallback) => {
35- this._opsQueue = Promise.all([this._opsQueue, promise]).then(values => {
36- const result = intefaceCallback(values.pop())
37- instance.exports.callback.get(callbackIndex)(result)
38- })
39- },
40- memory: () => {
41- return instance.exports.memory.buffer
42- }
43- },
44- kernel: kernel,
45- message: message
46- }
47- const initializedImports = buildImports(opts, imports)
48- instance = WebAssembly.Instance(this._module, initializedImports)
49-
50- if (instance.exports.main) {
51- instance.exports.main()
52- }
53- await this.onDone()
54- return responses
55- }
56-
57- /**
58- * returns a promise that resolves when the wasm instance is done running
59- */
60- async onDone () {
61- let prevOps
62- while (prevOps !== this._opsQueue) {
63- prevOps = this._opsQueue
64- await this._opsQueue
65- }
66- }
67-}

Built with git-ssb-web