git ssb

0+

wanderer🌟 / js-primea-hypervisor



Commit 76b626ab5a6d24e48ccefffb8900dc9052b0b36d

restuctured

wanderer committed on 7/22/2016, 2:41:41 AM
Parent: 9277afcb2a63e13cddbed76dbfa1c1cc85f210d2

Files changed

environment.jschanged
index.jschanged
interface.jschanged
tests/interfaceRunner.jschanged
environment.jsView
@@ -6,8 +6,9 @@
66 const defaults = {
77 // gas tank
88 gasPrice: 0,
99 gasLimit: 0, // The gas limit for the block
10+ gasRefund: 0,
1011 // call infromation
1112 address: new Uint8Array(20),
1213 origin: new Uint8Array(20),
1314 coinbase: new Uint8Array(20),
@@ -23,18 +24,17 @@
2324 // more output calls
2425 returnValue: new ArrayBuffer()
2526 }
2627
27- this.state = new Graph()
28+ this.state = new Map()
2829
2930 if (data) {
3031 data = JSON.parse(data)
3132 } else {
3233 data = {}
3334 }
3435
3536 Object.assign(this, defaults, data)
36-
3737 if (data.accounts) {
3838 this.accounts = new Graph()
3939 const self = this
4040 data.accounts.forEach((account) => {
@@ -73,23 +73,8 @@
7373
7474 getBlockHash (height) {
7575 // STUB
7676 }
77-
78- // kernal
79- create (code, value) {
80- // STUB
81- }
82-
83- call (gas, address, value, data) {
84- // STUB
85- return // result
86- }
87-
88- delegateCall (gas, address, data) {
89- // STUB
90- return // result
91- }
9277 }
9378
9479 function hexStr2arrayBuf (string) {
9580 const ab = new ArrayBuffer(string.length / 2)
index.jsView
@@ -1,27 +1,32 @@
11 /**
22 * This implements the Ethereum Kernel
3- * The kernal handles the following
3+ * The Kernel Contract handles the following
44 * - Interprocess communications
55 * - Intializing the VM and exposes ROM to it (codeHandler)
66 * - Expose namespace which VM instance exists and Intializes the Environment (callHandler)
77 * - Provides some built in contract (runTx, runBlock)
88 * - Provides resource sharing and limiting via gas
99 *
1010 * All State should be stored in the Environment.
11+ *
1112 */
1213 // const Environment = require('./environment.js')
14+//
15+// The Kernel Exposes this Interface to VM instances it makes
1316 const Interface = require('./interface.js')
17+// The Kernel Stores all of its state in the Environment. The Interface is used
18+// to by the VM to retrive infromation from the Environment.
1419 const Environment = require('./environment.js')
1520
1621 module.exports = class Kernal {
1722 // runs some code in the VM
18- constructor (nameState) {
19- this.state = nameState
23+ constructor (environment = new Environment()) {
24+ this.environment = environment
2025 }
2126
2227 // handles running code.
23- static codeHandler (code, ethInterface = new Interface()) {
28+ static codeHandler (code, ethInterface) {
2429 const instance = Wasm.instantiateModule(code, {
2530 'ethereum': ethInterface
2631 })
2732
@@ -35,10 +40,14 @@
3540 // loads code from the merkle trie and delegates the message
3641 // Detects if code is EVM or WASM
3742 // Detects if the code injection is needed
3843 // Detects if transcompilation is needed
39- static callHandler (path, data, environment = new Environment()) {
40- // return instance
44+ static callHandler (path, data) {
45+ // creats a new Kernal
46+ // const environment = new Environment(data)
47+ // environment.parent = this
48+ // const kernel = new Kernel(this, environment)
49+ // kernel.codeHandler(code)
4150 }
4251
4352 // run tx; the tx message handler
4453 runTx (tx, environment = new Environment()) {
interface.jsView
@@ -3,13 +3,15 @@
33 * enables to interact with the Ethereum Environment
44 */
55 const Environment = require('./environment.js')
66 const constants = require('./constants.js')
7+// const Graph = require('generic-digraph')
78
89 // function.bind is not working corretly whith Wasm imports. So instead create
910 // a global for now. TODO REMOVE
1011 let ENV
1112 let MOD
13+let self
1214 // The interface exposed to the WebAessembly Core
1315 module.exports = class Interface {
1416
1517 debugPrint (a) {
@@ -19,10 +21,11 @@
1921 memPrint () {
2022 console.log((new Uint8Array(MOD.exports.memory)).toString())
2123 }
2224
23- constructor (environment = new Environment()) {
24- ENV = this.environment = environment
25+ constructor (kernal) {
26+ ENV = this.environment = kernal.environment
27+ self = this
2528 }
2629
2730 setModule (mod) {
2831 this.module = MOD = mod
@@ -315,21 +318,35 @@
315318 * @param {interger} pathOffest the memory offset to load the the path from
316319 * @param {interger} valueOffset the memory offset to load the value from
317320 */
318321 sstore (pathOffest, valueOffset) {
319- const path = new Uint8Array(MOD.exports.memory, pathOffest, 32)
320- const value = new Uint8Array(MOD.exports.memory, valueOffset, 32)
321- ENV.state.set(path, value)
322+ const path = new Uint8Array(MOD.exports.memory, pathOffest, 32).join('')
323+ const value = new Uint8Array(MOD.exports.memory, valueOffset, 32)
324+ const oldValue = ENV.state.get(path)
325+ const valIsZero = value.every((i) => i === 0)
326+
327+ // write
328+ if (!valIsZero && !oldValue) {
329+ ENV.gasLimit -= 15000
330+ }
331+
332+ // delete
333+ if (valIsZero && oldValue) {
334+ ENV.gasRefund += 15000
335+ ENV.state.delete(path)
336+ } else {
337+ ENV.state.set(path, value)
338+ }
322339 }
323340
324341 /**
325342 * reterives a value at a given path in long term storage
326343 * @param {interger} pathOffest the memory offset to load the the path from
327344 * @param {interger} resultOffset the memory offset to load the value from
328345 */
329346 sload (pathOffest, resultOffset) {
330- const path = new Uint8Array(MOD.exports.memory, pathOffest, 32)
331- const result = ENV.state.getValue(path)
347+ const path = new Uint8Array(MOD.exports.memory, pathOffest, 32).join('')
348+ const result = ENV.state.get(path)
332349 const memory = new Uint8Array(MOD.exports.memory, resultOffset, 32)
333350 memory.set(result)
334351 }
335352
tests/interfaceRunner.jsView
@@ -2,8 +2,9 @@
22 const tape = require('tape')
33 const fs = require('fs')
44 const cp = require('child_process')
55
6+const Kernel = require('../index.js')
67 const Environment = require('../environment.js')
78 const Interface = require('../interface.js')
89 const dir = __dirname + '/interface'
910 // get the test names
@@ -18,9 +19,10 @@
1819 const buffer = fs.readFileSync(`${dir}/${testName}.wasm`)
1920 const envData = fs.readFileSync(`${dir}/${testName}.json`)
2021
2122 const environment = new Environment(envData)
22- const ethInterface = new Interface(environment)
23+ const kernel = new Kernel(environment)
24+ const ethInterface = new Interface(kernel)
2325
2426 try {
2527 const mod = Wasm.instantiateModule(buffer, {'ethereum': ethInterface})
2628 ethInterface.setModule(mod)

Built with git-ssb-web