git ssb

0+

wanderer🌟 / js-primea-hypervisor



Commit de2486f29ec9def6a646f6caeaf4ab72cf4f7fdb

Merge pull request #24 from ewasm/primitives

Primitives - use Address type in Environment
wanderer authored on 8/17/2016, 12:45:28 AM
GitHub committed on 8/17/2016, 12:45:28 AM
Parent: 257925ea00ae46cfd74f6ecd57e03f355389aaa3
Parent: f0f5d06d3b472948e6b2269470149e2cbea149b3

Files changed

address.jschanged
environment.jschanged
interface.jschanged
testEnvironment.jschanged
u256.jschanged
address.jsView
@@ -7,18 +7,18 @@
77 this._value = new Buffer(value._value)
88 return
99 }
1010
11- if (typeof value !== 'string') {
11+ if (value instanceof Uint8Array) {
12+ this._value = new Buffer(value)
13+ } else if (typeof value !== 'string') {
1214 throw new Error('Invalid input to address')
13- }
14-
15- if (!ethUtils.isHexPrefixed(value)) {
15+ } else if (!ethUtils.isHexPrefixed(value)) {
1616 throw new Error('Invalid address format')
17+ } else {
18+ this._value = new Buffer(ethUtils.stripHexPrefix(value), 'hex')
1719 }
1820
19- this._value = new Buffer(ethUtils.stripHexPrefix(value), 'hex')
20-
2121 if (this._value.length !== 20) {
2222 throw new Error('Invalid address length')
2323 }
2424 }
environment.jsView
@@ -1,6 +1,7 @@
11 const constants = require('./constants.js')
22 const U256 = require('./u256.js')
3+const Address = require('./address.js')
34
45 module.exports = class Environment {
56 constructor (data) {
67 const defaults = {
@@ -8,20 +9,20 @@
89 gasPrice: 0,
910 gasLimit: 1000000, // The gas limit for the block
1011 gasRefund: 0,
1112 // call infromation
12- address: new Uint8Array(constants.ADDRESS_SIZE_BYTES),
13- origin: new Uint8Array(constants.ADDRESS_SIZE_BYTES),
14- coinbase: new Uint8Array(constants.ADDRESS_SIZE_BYTES),
13+ address: new Address('0x0000000000000000000000000000000000000000'),
14+ origin: new Address('0x0000000000000000000000000000000000000000'),
15+ coinbase: new Address('0x0000000000000000000000000000000000000000'),
1516 difficulty: 0,
16- caller: new Uint8Array(constants.ADDRESS_SIZE_BYTES),
17+ caller: new Address('0x0000000000000000000000000000000000000000'),
1718 callValue: new U256(0),
1819 callData: new Uint8Array(),
1920 // the ROM
2021 code: new Uint8Array(), // the current running code
2122 // output calls
2223 logs: [],
23- selfDestructAddress: new Uint8Array(constants.ADDRESS_SIZE_BYTES),
24+ selfDestructAddress: new Address('0x0000000000000000000000000000000000000000'),
2425 // more output calls
2526 returnValue: new Uint8Array()
2627 }
2728
interface.jsView
@@ -2,8 +2,9 @@
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 constants = require('./constants.js')
6+const Address = require('./address.js')
67
78 // The interface exposed to the WebAessembly Core
89 module.exports = class Interface {
910 constructor (environment) {
@@ -80,9 +81,9 @@
8081 * the given offset.
8182 * @param {integer} offset
8283 */
8384 address (offset) {
84- this.setMemory(offset, constants.ADDRESS_SIZE_BYTES, this.environment.address)
85+ this.setMemory(offset, constants.ADDRESS_SIZE_BYTES, this.environment.address.toBuffer())
8586 }
8687
8788 /**
8889 * Gets balance of the given account and loads it into memory at the given
@@ -90,9 +91,9 @@
9091 * @param {integer} addressOffset the memory offset to laod the address
9192 * @param {integer} resultOffset
9293 */
9394 balance (addressOffset, offset) {
94- const address = this.getMemory(addressOffset, constants.ADDRESS_SIZE_BYTES)
95+ const address = new Address(this.getMemory(addressOffset, constants.ADDRESS_SIZE_BYTES))
9596 // call the parent contract and ask for the balance of one of its child contracts
9697 const balance = this.environment.parent.environment.getBalance(address)
9798 this.setMemory(offset, constants.BALANCE_SIZE_BYTES, balance)
9899 }
@@ -103,18 +104,18 @@
103104 * account with non-empty associated code.
104105 * @param {integer} offset
105106 */
106107 origin (offset) {
107- this.setMemory(offset, constants.ADDRESS_SIZE_BYTES, this.environment.origin)
108+ this.setMemory(offset, constants.ADDRESS_SIZE_BYTES, this.environment.origin.toBuffer())
108109 }
109110
110111 /**
111112 * Gets caller address and loads it into memory at the given offset. This is
112113 * the address of the account that is directly responsible for this execution.
113114 * @param {integer} offset
114115 */
115116 caller (offset) {
116- this.setMemory(offset, constants.ADDRESS_SIZE_BYTES, this.environment.caller)
117+ this.setMemory(offset, constants.ADDRESS_SIZE_BYTES, this.environment.caller.toBuffer())
117118 }
118119
119120 /**
120121 * Gets the deposited value by the instruction/transaction responsible for
@@ -170,9 +171,9 @@
170171 * @param {integer} addressOffset the offset in memory to load the address from
171172 * @return {integer}
172173 */
173174 extCodeSize (addressOffset) {
174- const address = this.getMemory(addressOffset, constants.ADDRESS_SIZE_BYTES)
175+ const address = new Address(this.getMemory(addressOffset, constants.ADDRESS_SIZE_BYTES))
175176 const code = this.environment.getCode(address)
176177 return code.length
177178 }
178179
@@ -183,9 +184,9 @@
183184 * @param {integer} codeOffset the code offset
184185 * @param {integer} length the length of code to copy
185186 */
186187 extCodeCopy (addressOffset, offset, codeOffset, length) {
187- const address = this.getMemory(addressOffset, constants.ADDRESS_SIZE_BYTES)
188+ const address = new Address(this.getMemory(addressOffset, constants.ADDRESS_SIZE_BYTES))
188189 let code = this.environment.getCode(address)
189190 code = new Uint8Array(code, codeOffset, length)
190191 this.setMemory(offset, length, code)
191192 }
@@ -212,9 +213,9 @@
212213 * Gets the block’s beneficiary address and loads into memory.
213214 * @param offset
214215 */
215216 coinbase (offset) {
216- this.setMemory(offset, constants.ADDRESS_SIZE_BYTES, this.environment.coinbase)
217+ this.setMemory(offset, constants.ADDRESS_SIZE_BYTES, this.environment.coinbase.toBuffer())
217218 }
218219
219220 /**
220221 * Get the block’s timestamp.
@@ -291,9 +292,9 @@
291292 if (gas === undefined) {
292293 gas = this.gasLeft()
293294 }
294295 // Load the params from mem
295- const address = this.getMemory(addressOffset, constants.ADDRESS_SIZE_BYTES)
296+ const address = new Address(this.getMemory(addressOffset, constants.ADDRESS_SIZE_BYTES))
296297 const value = this.getMemory(valueOffset, constants.BALANCE_SIZE_BYTES)
297298 const data = this.getMemory(dataOffset, dataLength)
298299 // Run the call
299300 const [result, errorCode] = this.environment.call(gas, address, value, data)
@@ -314,9 +315,9 @@
314315 * @return {integer} Returns 1 or 0 depending on if the VM trapped on the message or not
315316 */
316317 callDelegate (gas, addressOffset, dataOffset, dataLength, resultOffset, resultLength) {
317318 const data = this.getMemory(dataOffset, dataLength)
318- const address = this.getMemory(addressOffset, constants.ADDRESS_SIZE_BYTES)
319+ const address = new Address(this.getMemory(addressOffset, constants.ADDRESS_SIZE_BYTES))
319320 const [result, errorCode] = this.environment.callDelegate(gas, address, data)
320321 this.setMemory(resultOffset, resultLength, result)
321322 return errorCode
322323 }
@@ -373,9 +374,9 @@
373374 * balance to an address path
374375 * @param {integer} offset the offset to load the address from
375376 */
376377 suicide (addressOffset) {
377- this.environment.suicideAddress = this.getMemory(addressOffset, constants.ADDRESS_SIZE_BYTES)
378+ this.environment.suicideAddress = new Address(this.getMemory(addressOffset, constants.ADDRESS_SIZE_BYTES))
378379 }
379380
380381 getMemory (offset, length) {
381382 return new Uint8Array(this.module.exports.memory, offset, length)
testEnvironment.jsView
@@ -1,6 +1,7 @@
11 const Environment = require('./environment.js')
22 const U256 = require('./u256.js')
3+const Address = require('./address.js')
34
45 module.exports = class TestEnvironment extends Environment {
56 constructor (data) {
67 super()
@@ -12,22 +13,22 @@
1213 let self = this
1314
1415 if (data.accounts) {
1516 data.accounts.forEach((account) => {
16- self.state.set(new Uint8Array(account[0]).toString(), account[1])
17+ self.state.set(new Address(new Uint8Array(account[0])).toString(), account[1])
1718 })
1819 }
1920
2021 if (data.address) {
21- self.address = new Uint8Array(data.address)
22+ self.address = new Address(new Uint8Array(data.address))
2223 }
2324
2425 if (data.origin) {
25- self.origin = new Uint8Array(data.origin)
26+ self.origin = new Address(new Uint8Array(data.origin))
2627 }
2728
2829 if (data.caller) {
29- self.caller = new Uint8Array(data.caller)
30+ self.caller = new Address(new Uint8Array(data.caller))
3031 }
3132
3233 if (data.coinbase) {
3334 self.coinbase = new Uint8Array(data.coinbase)
u256.jsView
@@ -2,9 +2,12 @@
22 const ethUtils = require('ethereumjs-util')
33
44 module.exports = class U256 {
55 constructor (value) {
6- if ((typeof value === 'string') && ethUtils.isHexPrefixed(value)) {
6+ // This is the case when data is copied from WASM
7+ if (value instanceof Uint8Array) {
8+ this._value = new BN(value, 16, 'le')
9+ } else if ((typeof value === 'string') && ethUtils.isHexPrefixed(value)) {
710 this._value = new BN(value, 16)
811 } else {
912 this._value = new BN(value, 10)
1013 }

Built with git-ssb-web