git ssb

0+

wanderer🌟 / js-primea-hypervisor



Commit f8c9ead18a27b0962868bf70b4fa256cb3fb1606

added sstore test

wanderer committed on 7/14/2016, 10:03:05 PM
Parent: 2c04524fc8e9b757c060e0e2cf80d26d6d411e57

Files changed

README.mdchanged
environment.jschanged
index.jschanged
interface.jschanged
package.jsonchanged
tests/sstore.jsonadded
tests/sstore.wastadded
debugInterface.jsadded
README.mdView
@@ -1,14 +1,13 @@
11 # SYNOPSIS - WIP
22 [![Gitter](https://img.shields.io/gitter/room/ethereum/ethereumjs-lib.svg?style=flat-square)](https://gitter.im/ethereum/ethereumjs-lib) or #ethereumjs on freenode
3-
43 [![js-standard-style](https://cdn.rawgit.com/feross/standard/master/badge.svg)](https://github.com/feross/standard)
54
65 This is a JS prototype of the [eWASM kernal](https://github.com/ethereum/evm2.0-design).
76
87
98 # INSTALL
10-You need to compile [nodejs](https://github.com/nodejs/node) from master to run
9+You need to compile [nodejs](https://github.com/nodejs/node) from master to run
1110 ~~`npm install ewasm-kernal`~~
1211
1312 clone and run `npm install`
1413
environment.jsView
@@ -3,26 +3,32 @@
33
44 module.exports = class Environment {
55 constructor (data) {
66 const defaults = {
7+ // gas tank
78 gasCounter: 0, // TODO: gasCounter is only 53 bits
89 gas: 0, // The amount of gas this contract has
910 gasPrice: 0,
1011 gasLimit: 0, // The gas limit for the block
12+ // call infromation
1113 address: new Uint8Array(20),
1214 origin: new Uint8Array(20),
1315 coinbase: new Uint8Array(20),
1416 difficulty: new Uint8Array(20),
1517 caller: new Uint8Array(20),
1618 callValue: new Uint8Array(MAX_BAL_BYTES),
1719 callData: new ArrayBuffer(),
20+ // the ROM
1821 code: new ArrayBuffer(), // the current running code
22+ // output calls
1923 logs: [],
20- returnValue: new ArrayBuffer(),
2124 suicideAddress: new ArrayBuffer(),
22- accounts: new Map()
25+ // more output calls
26+ returnValue: new ArrayBuffer()
2327 }
2428
29+ this.state = new Graph()
30+
2531 if (data) {
2632 data = JSON.parse(data)
2733 Object.assign(this, defaults, data)
2834 } else {
@@ -31,9 +37,8 @@
3137
3238 if (data.accounts) {
3339 this.accounts = new Graph()
3440 const self = this
35- debugger
3641 data.accounts.forEach((account) => {
3742 self.accounts.set(new Uint8Array(account[0]).toString(), account[1])
3843 })
3944 }
index.jsView
@@ -10,27 +10,33 @@
1010 * All State should be stored in the Environment.
1111 */
1212 // const Environment = require('./environment.js')
1313 const Interface = require('./interface.js')
14+const Environment = require('./environment.js')
1415
1516 module.exports = class Kernal {
1617 // runs some code in the VM
1718 constructor (nameState) {
1819 this.state = nameState
1920 }
2021
2122 // handles running code.
22- static codeHandler (code, environment) {
23+ static codeHandler (code, environment = new Environment()) {
2324 const ethInterface = new Interface(environment)
2425 const instance = Wasm.instantiateModule(code, {
2526 'ethereum': ethInterface
2627 })
27- ethInterface.setModule(ethInterface)
28+ ethInterface.setModule(instance)
29+ if (instance.exports.main) {
30+ instance.exports.main()
31+ }
2832 return instance
2933 }
3034
3135 // loads code from the merkle trie and delegates the message
36+ // Detects if code is EVM or WASM
3237 // Detects if the code injection is needed
38+ // Detects if transcompilation is needed
3339 static callHandler (path, data, environment) {
3440 // const instance = Wasm.instantiateModule(code, interface)
3541 // interface.setModule(instance)
3642 // return instance
@@ -47,9 +53,8 @@
4753 // verify block then run each tx
4854 block.tx.forEach((tx) => {
4955 this.runTx(tx, environment)
5056 })
51-
5257 }
5358
5459 // run blockchain
5560 // runBlockchain () {}
interface.jsView
@@ -9,24 +9,25 @@
99 let ENV
1010 let MOD
1111 // The interface exposed to the WebAessembly Core
1212 module.exports = class Interface {
13+
14+ debugPrint (a) {
15+ console.log(a)
16+ }
17+
18+ memPrint () {
19+ console.log((new Uint8Array(MOD.exports.memory)).toString())
20+ }
21+
1322 constructor (environment) {
1423 ENV = this.environment = environment
1524 }
1625
1726 setModule (mod) {
1827 this.module = MOD = mod
1928 }
2029
21- // debugPrint (a, b) {
22- // console.log(a)
23- // }
24-
25- // memPrint () {
26- // console.log((new Uint8Array(MOD.exports.memory)).toString())
27- // }
28-
2930 /**
3031 * Subtracts an amount to the gas counter
3132 * @param {integer} amount the amount to subtract to the gas counter
3233 */
@@ -265,9 +266,9 @@
265266 return result
266267 }
267268
268269 /**
269- * Sends a message with arbiatary date to a given address path
270+ * Sends a message with arbiatary data to a given address path
270271 * @param {integer} addressOffset the offset to load the address path from
271272 * @param {integer} valueOffset the offset to load the value from
272273 * @param {integer} dataOffset the offset to load data from
273274 * @param {integer} dataLength the length of data
@@ -305,12 +306,12 @@
305306 * @param {integer} resultLength
306307 * @return {integer} Returns 1 or 0 depending on if the VM trapped on the message or not
307308 */
308309 callDelegate (gas, addressOffset, dataOffset, dataLength, resultOffset, resultLength) {
309- const data = new Uint8Array(this.module.memory, dataOffset, dataLength)
310- const address = new Uint8Array(this.module.memory, addressOffset, constants.ADD_SIZE_BYTES)
310+ const data = new Uint8Array(MOD.exports.memory, dataOffset, dataLength)
311+ const address = new Uint8Array(MOD.exports.memory, addressOffset, constants.ADD_SIZE_BYTES)
311312 const [result, errorCode] = this.environment.callDelegate(gas, address, data)
312- const memory = new Uint8Array(this.module.memory, resultOffset, resultLength)
313+ const memory = new Uint8Array(MOD.exports.memory, resultOffset, resultLength)
313314 memory.set(result)
314315
315316 return errorCode
316317 }
@@ -321,22 +322,22 @@
321322 * @param {interger} pathOffest the memory offset to load the the path from
322323 * @param {interger} valueOffset the memory offset to load the value from
323324 */
324325 sstore (pathOffest, valueOffset) {
325- const path = new Uint8Array(this.module.memory, pathOffest, pathOffest + 32)
326- const value = new Uint8Array(this.module.memory, valueOffset, valueOffset + 32)
327- this.environment.state.set(path, value)
326+ const path = new Uint8Array(MOD.exports.memory, pathOffest, pathOffest + 32)
327+ const value = new Uint8Array(MOD.exports.memory, valueOffset, valueOffset + 32)
328+ ENV.state.set(path, value)
328329 }
329330
330331 /**
331332 * reterives a value at a given path in long term storage
332333 * @param {interger} pathOffest the memory offset to load the the path from
333334 * @param {interger} resultOffset the memory offset to load the value from
334335 */
335336 sget (pathOffest, resultOffset) {
336- const path = new Uint8Array(this.module.memory, pathOffest, pathOffest + 32)
337- const result = this.environment.state.get(path)
338- const memory = new Uint8Array(this.module.memory, resultOffset, resultOffset + 32)
337+ const path = new Uint8Array(MOD.exports.memory, pathOffest, pathOffest + 32)
338+ const result = ENV.state.getValue(path)
339+ const memory = new Uint8Array(MOD.exports.memory, resultOffset, resultOffset + 32)
339340 memory.set(result)
340341 }
341342
342343 /**
@@ -344,17 +345,17 @@
344345 * @param {integer} offset the offset of the output data.
345346 * @param {integer} length the length of the output data.
346347 */
347348 return (offset, length) {
348- this.environment.returnValue = new Uint8Array(this.module.memory, offset, length)
349+ this.environment.returnValue = new Uint8Array(MOD.exports.memory, offset, length)
349350 }
350351
351352 /**
352353 * Halt execution and register account for later deletion giving the remaining
353354 * balance to an address path
354355 * @param {integer} offset the offset to load the address from
355356 */
356357 suicide (addressOffset) {
357- const address = new Uint8Array(this.module.memory, addressOffset, constants.ADD_SIZE_BYTES)
358+ const address = new Uint8Array(MOD.exports.memory, addressOffset, constants.ADD_SIZE_BYTES)
358359 this.environment.suicideAddress = address
359360 }
360361 }
package.jsonView
@@ -33,7 +33,7 @@
3333 "Wasm"
3434 ]
3535 },
3636 "dependencies": {
37- "generic-digraph": "^3.1.0"
37+ "generic-digraph": "^3.1.1"
3838 }
3939 }
tests/sstore.jsonView
@@ -1,0 +1,2 @@
1+{
2+}
tests/sstore.wastView
@@ -1,0 +1,24 @@
1+;; starts with an caller of 5d48c1018904a172886829bbbd9c6f4a2d06c47b
2+(module
3+ (memory 1)
4+ (import $sstore "ethereum" "sstore" (param i32 i32))
5+ (import $sget "ethereum" "sget" (param i32 i32))
6+
7+ (export "test" 0)
8+ (export "a" memory)
9+ (func
10+ (local $temp i64)
11+ (block
12+ ;; should roundtrip store and load a value from storage
13+ (i64.store (i32.const 0) (i64.const 173553719826446289))
14+ (call_import $sstore (i32.const 64) (i32.const 0))
15+ (call_import $sget (i32.const 64) (i32.const 64))
16+ (set_local $temp
17+ (i64.load (i32.const 64)))
18+
19+ (if (i64.eq (i64.load (i32.const 0)) (i64.const 173553719826446289))
20+ (return))
21+ (unreachable)
22+ )
23+ )
24+)
debugInterface.jsView
@@ -1,0 +1,16 @@
1+/**
2+ * Debug Interface
3+ * This expose some functions that can help with debugging wast
4+ */
5+const Interface = require('./interface.js')
6+let MOD
7+
8+module.exports = class DebugInterface extends Interface {
9+ debugPrint (a, b) {
10+ console.log(a)
11+ }
12+
13+ memPrint () {
14+ console.log((new Uint8Array(MOD.exports.memory)).toString())
15+ }
16+}

Built with git-ssb-web