git ssb

0+

wanderer🌟 / js-primea-hypervisor



Commit 863cffe3f178010105fe06f1d375cb31e4579ea3

fix extCodeSize

wanderer committed on 11/10/2016, 7:22:40 PM
Parent: 7052b80b5685d1536cd7530b2a53537607899c9c

Files changed

EVMinterface.jschanged
environment.jschanged
interfaceAPI.jschanged
EVMinterface.jsView
@@ -1,8 +1,10 @@
11 /**
22 * This is the Ethereum interface that is exposed to the WASM instance which
33 * enables to interact with the Ethereum Environment
44 */
5+
6+const Vertex = require('merkle-trie')
57 const Address = require('./deps/address.js')
68 const U256 = require('./deps/u256.js')
79 const fs = require('fs')
810 const path = require('path')
@@ -230,14 +232,19 @@
230232 * Get size of an account’s code.
231233 * @param {integer} addressOffset the offset in memory to load the address from
232234 * @return {integer}
233235 */
234- getExternalCodeSize (addressOffset) {
236+ getExternalCodeSize (addressOffset, cbOffset) {
235237 this.takeGas(20)
236238
237- const address = Address.fromMemory(this.getMemory(addressOffset, ADDRESS_SIZE_BYTES))
238- const code = this.kernel.environment.getCode(address)
239- return code.length
239+ const address = Address.fromMemory(this.getMemory(addressOffset, ADDRESS_SIZE_BYTES)).toArray()
240+ address.push('code')
241+ const opPromise = this.kernel.environment.state.root.get(address)
242+ .then(vertex => vertex.value.length)
243+ .catch(() => 0)
244+
245+ // wait for all the prevouse async ops to finish before running the callback
246+ this.kernel.pushOpsQueue(opPromise, cbOffset, length => length)
240247 }
241248
242249 /**
243250 * Copys the code of an account to memory.
@@ -521,18 +528,15 @@
521528 * @param {interger} valueOffset the memory offset to load the value from
522529 */
523530 storageStore (pathOffset, valueOffset, cbIndex) {
524531 this.takeGas(5000)
525- const path = [...this.getMemory(pathOffset, U256_SIZE_BYTES)]
532+ const path = ['storage', ...this.getMemory(pathOffset, U256_SIZE_BYTES)]
526533 // copy the value
527534 const value = this.getMemory(valueOffset, U256_SIZE_BYTES).slice(0)
528535 const valIsZero = value.every((i) => i === 0)
529536 const opPromise = this.kernel.environment.state.get(path)
530- .catch(() => {
531- // TODO: handle errors
532- // the value was not found
533- return null
534- })
537+ .then(vertex => vertex.value)
538+ .catch(() => null)
535539
536540 this.kernel.pushOpsQueue(opPromise, cbIndex, oldValue => {
537541 if (valIsZero && oldValue) {
538542 // delete a value
@@ -543,9 +547,11 @@
543547 // creating a new value
544548 this.takeGas(15000)
545549 }
546550 // update
547- this.kernel.environment.state.set(path, value)
551+ this.kernel.environment.state.set(path, new Vertex({
552+ value: value
553+ }))
548554 }
549555 })
550556 }
551557
@@ -555,21 +561,18 @@
555561 * @param {interger} resultOffset the memory offset to load the value from
556562 */
557563 storageLoad (pathOffset, resultOffset, cbIndex) {
558564 this.takeGas(50)
559- console.log('offset: ' + resultOffset);
560565
561566 // convert the path to an array
562- const path = [...this.getMemory(pathOffset, U256_SIZE_BYTES)]
567+ const path = ['storage', ...this.getMemory(pathOffset, U256_SIZE_BYTES)]
568+ // get the value from the state
563569 const opPromise = this.kernel.environment.state.get(path)
564- .catch(() => {
565- // TODO: handle other possible errors
566- // if the value was not found return a empty array
567- return new Uint8Array(32)
568- })
570+ .then(vertex => vertex.value)
571+ .catch(() => new Uint8Array(32))
569572
570- this.kernel.pushOpsQueue(opPromise, cbIndex, result => {
571- this.setMemory(resultOffset, U256_SIZE_BYTES, result)
573+ this.kernel.pushOpsQueue(opPromise, cbIndex, value => {
574+ this.setMemory(resultOffset, U256_SIZE_BYTES, value)
572575 })
573576 }
574577
575578 /**
environment.jsView
@@ -2,8 +2,9 @@
22 const Store = require('merkle-trie/store')
33 const U256 = require('./deps/u256.js')
44 const Address = require('./deps/address.js')
55 const Block = require('./deps/block.js')
6+// TODO remove fakeblockchain
67 const fakeBlockChain = require('./fakeBlockChain.js')
78
89 module.exports = class Environment {
910 constructor (data = {}) {
@@ -29,9 +30,8 @@
2930 // more output calls
3031 returnValue: new Uint8Array(),
3132 state: new Vertex({store: new Store()})
3233 }
33-
3434 // this.environment.addAccount(identityContract, {})
3535 // this.environment.addAccount(meteringContract, {})
3636 // this.environment.addAccount(transcompilerContract, {})
3737
@@ -74,10 +74,9 @@
7474 // }
7575 }
7676
7777 async getBlockHash (height) {
78- // return this.blockchain.getBlock(height).hash()
79- const block = await this.root.getBlockAt(height)
78+ const block = await this.blockchain.getBlock(height)
8079 return block.hash()
8180 }
8281
8382 set createHandler (value) {
interfaceAPI.jsView
@@ -27,10 +27,10 @@
2727 }
2828
2929 pushOpsQueue (promise, callbackIndex, intefaceCallback) {
3030 this._opsQueue = Promise.all([this._opsQueue, promise]).then(values => {
31- intefaceCallback(values.pop())
32- this._instance.exports[callbackIndex.toString()]()
31+ const result = intefaceCallback(values.pop())
32+ this._instance.exports[callbackIndex.toString()](result)
3333 })
3434 }
3535
3636 sendMessage (message) {

Built with git-ssb-web