git ssb

0+

wanderer🌟 / js-primea-hypervisor



Commit 2975e2487c3092abed28db4418561927a75e467d

Introduce toMemory/fromMemory in Address/U256 and make the internals big endian

Alex Beregszaszi committed on 8/24/2016, 11:15:58 PM
Parent: 9442b2c40b4fe830815c57a044d242665215462f

Files changed

address.jschanged
interface.jschanged
u256.jschanged
address.jsView
@@ -1,4 +1,5 @@
1+const BN = require('bn.js')
12 const U256 = require('./u256.js')
23
34 module.exports = class Address extends U256 {
45 constructor (value) {
@@ -7,12 +8,23 @@
78 throw new Error('Invalid address length: ' + this._value.byteLength() + ' for ' + value)
89 }
910 }
1011
12+ // This assumes Uint8Array in LSB (WASM code)
13+ static fromMemory (value) {
14+ return new Address(new BN(value, 16, 'le'))
15+ }
16+
17+ // This assumes Uint8Array in LSB (WASM code)
18+ toMemory () {
19+ return this._value.toBuffer('le', 20)
20+ }
21+
1122 toBuffer () {
1223 return super.toBuffer(20)
1324 }
1425
26+ // Needs to be displayed as a hex always
1527 toString () {
1628 return '0x' + this._value.toString('hex', 40)
1729 }
1830
interface.jsView
@@ -86,9 +86,9 @@
8686 */
8787 getAddress (offset) {
8888 this.takeGas(2)
8989
90- this.setMemory(offset, constants.ADDRESS_SIZE_BYTES, this.environment.address.toBuffer())
90+ this.setMemory(offset, constants.ADDRESS_SIZE_BYTES, this.environment.address.toMemory())
9191 }
9292
9393 /**
9494 * Gets balance of the given account and loads it into memory at the given
@@ -98,12 +98,12 @@
9898 */
9999 getBalance (addressOffset, offset) {
100100 this.takeGas(20)
101101
102- const address = new Address(this.getMemory(addressOffset, constants.ADDRESS_SIZE_BYTES))
102+ const address = Address.fromMemory(this.getMemory(addressOffset, constants.ADDRESS_SIZE_BYTES))
103103 // call the parent contract and ask for the balance of one of its child contracts
104104 const balance = this.environment.getBalance(address)
105- this.setMemory(offset, constants.BALANCE_SIZE_BYTES, balance.toBuffer(constants.BALANCE_SIZE_BYTES))
105+ this.setMemory(offset, constants.BALANCE_SIZE_BYTES, balance.toMemory(constants.BALANCE_SIZE_BYTES))
106106 }
107107
108108 /**
109109 * Gets the execution's origination address and loads it into memory at the
@@ -113,9 +113,9 @@
113113 */
114114 getTxOrigin (offset) {
115115 this.takeGas(2)
116116
117- this.setMemory(offset, constants.ADDRESS_SIZE_BYTES, this.environment.origin.toBuffer())
117+ this.setMemory(offset, constants.ADDRESS_SIZE_BYTES, this.environment.origin.toMemory())
118118 }
119119
120120 /**
121121 * Gets caller address and loads it into memory at the given offset. This is
@@ -124,9 +124,9 @@
124124 */
125125 getCaller (offset) {
126126 this.takeGas(2)
127127
128- this.setMemory(offset, constants.ADDRESS_SIZE_BYTES, this.environment.caller.toBuffer())
128+ this.setMemory(offset, constants.ADDRESS_SIZE_BYTES, this.environment.caller.toMemory())
129129 }
130130
131131 /**
132132 * Gets the deposited value by the instruction/transaction responsible for
@@ -135,9 +135,9 @@
135135 */
136136 getCallValue (offset) {
137137 this.takeGas(2)
138138
139- this.setMemory(offset, constants.BALANCE_SIZE_BYTES, this.environment.callValue.toBuffer(constants.BALANCE_SIZE_BYTES))
139+ this.setMemory(offset, constants.BALANCE_SIZE_BYTES, this.environment.callValue.toMemory(constants.BALANCE_SIZE_BYTES))
140140 }
141141
142142 /**
143143 * Get size of input data in current environment. This pertains to the input
@@ -206,9 +206,9 @@
206206 */
207207 getExternalCodeSize (addressOffset) {
208208 this.takeGas(20)
209209
210- const address = new Address(this.getMemory(addressOffset, constants.ADDRESS_SIZE_BYTES))
210+ const address = Address.fromMemory(this.getMemory(addressOffset, constants.ADDRESS_SIZE_BYTES))
211211 const code = this.environment.getCode(address)
212212 return code.length
213213 }
214214
@@ -221,9 +221,9 @@
221221 */
222222 externalCodeCopy (addressOffset, resultOffset, codeOffset, length) {
223223 this.takeGas(20 + ((length / 32) * 3))
224224
225- const address = new Address(this.getMemory(addressOffset, constants.ADDRESS_SIZE_BYTES))
225+ const address = Address.fromMemory(this.getMemory(addressOffset, constants.ADDRESS_SIZE_BYTES))
226226 let code = this.environment.getCode(address)
227227 code = new Uint8Array(code, codeOffset, length)
228228 this.setMemory(resultOffset, length, code)
229229 }
@@ -253,9 +253,9 @@
253253 hash = new U256(0)
254254 } else {
255255 hash = new U256(this.environment.getBlockHash(number))
256256 }
257- this.setMemory(offset, 32, hash.toBuffer())
257+ this.setMemory(offset, 32, hash.toMemory())
258258 }
259259
260260 /**
261261 * Gets the block’s beneficiary address and loads into memory.
@@ -263,9 +263,9 @@
263263 */
264264 getBlockCoinbase (offset) {
265265 this.takeGas(2)
266266
267- this.setMemory(offset, constants.ADDRESS_SIZE_BYTES, this.environment.block.coinbase.toBuffer())
267+ this.setMemory(offset, constants.ADDRESS_SIZE_BYTES, this.environment.block.coinbase.toMemory())
268268 }
269269
270270 /**
271271 * Get the block’s timestamp.
@@ -293,9 +293,9 @@
293293 */
294294 getBlockDifficulty (offset) {
295295 this.takeGas(2)
296296
297- this.setMemory(offset, 32, this.environment.block.difficulty.toBuffer())
297+ this.setMemory(offset, 32, this.environment.block.difficulty.toMemory())
298298 }
299299
300300 /**
301301 * Get the block’s gas limit.
@@ -332,9 +332,9 @@
332332 */
333333 create (valueOffset, dataOffset, length) {
334334 this.takeGas(32000)
335335
336- const value = new U256(this.getMemory(valueOffset, constants.BALANCE_SIZE_BYTES))
336+ const value = U256.fromMemory(this.getMemory(valueOffset, constants.BALANCE_SIZE_BYTES))
337337 const data = this.getMemory(dataOffset, length)
338338 const result = this.environment.create(value, data)
339339 return result
340340 }
@@ -354,10 +354,10 @@
354354 // FIXME: count properly
355355 this.takeGas(40)
356356
357357 // Load the params from mem
358- const address = new Address(this.getMemory(addressOffset, constants.ADDRESS_SIZE_BYTES))
359- const value = new U256(this.getMemory(valueOffset, constants.BALANCE_SIZE_BYTES))
358+ const address = Address.fromMemory(this.getMemory(addressOffset, constants.ADDRESS_SIZE_BYTES))
359+ const value = U256.fromMemory(this.getMemory(valueOffset, constants.BALANCE_SIZE_BYTES))
360360 const data = this.getMemory(dataOffset, dataLength)
361361 // Run the call
362362 const [result, errorCode] = this.environment.call(gas, address, value, data)
363363 this.setMemory(resultOffset, resultLength, result)
@@ -379,10 +379,10 @@
379379 // FIXME: count properly
380380 this.takeGas(40)
381381
382382 // Load the params from mem
383- const address = new Address(this.getMemory(addressOffset, constants.ADDRESS_SIZE_BYTES))
384- const value = new U256(this.getMemory(valueOffset, constants.BALANCE_SIZE_BYTES))
383+ const address = Address.fromMemory(this.getMemory(addressOffset, constants.ADDRESS_SIZE_BYTES))
384+ const value = U256.fromMemory(this.getMemory(valueOffset, constants.BALANCE_SIZE_BYTES))
385385 const data = this.getMemory(dataOffset, dataLength)
386386 // Run the call
387387 const [result, errorCode] = this.environment.callCode(gas, address, value, data)
388388 this.setMemory(resultOffset, resultLength, result)
@@ -405,9 +405,9 @@
405405 // FIXME: count properly
406406 this.takeGas(40)
407407
408408 const data = this.getMemory(dataOffset, dataLength)
409- const address = new Address(this.getMemory(addressOffset, constants.ADDRESS_SIZE_BYTES))
409+ const address = Address.fromMemory(this.getMemory(addressOffset, constants.ADDRESS_SIZE_BYTES))
410410 const [result, errorCode] = this.environment.callDelegate(gas, address, data)
411411 this.setMemory(resultOffset, resultLength, result)
412412 return errorCode
413413 }
@@ -468,9 +468,9 @@
468468 * balance to an address path
469469 * @param {integer} offset the offset to load the address from
470470 */
471471 selfDestruct (addressOffset) {
472- this.environment.suicideAddress = new Address(this.getMemory(addressOffset, constants.ADDRESS_SIZE_BYTES))
472+ this.environment.suicideAddress = Address.fromMemory(this.getMemory(addressOffset, constants.ADDRESS_SIZE_BYTES))
473473 this.environment.gasRefund += 24000
474474 }
475475
476476 getMemory (offset, length) {
u256.jsView
@@ -1,19 +1,27 @@
11 const BN = require('bn.js')
22 const ethUtil = require('ethereumjs-util')
33
4-module.exports = class U256 {
4+const U256 = module.exports = class U256 {
55 constructor (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') && ethUtil.isHexPrefixed(value)) {
6+ // bn.js still doesn't support hex prefixes...
7+ if ((typeof value === 'string') && ethUtil.isHexPrefixed(value)) {
108 this._value = new BN(ethUtil.stripHexPrefix(value), 16)
119 } else {
1210 this._value = new BN(value, 10)
1311 }
1412 }
1513
14+ // This assumes Uint8Array in LSB (WASM code)
15+ static fromMemory (value) {
16+ return new U256(new BN(value, 16, 'le'))
17+ }
18+
19+ // This assumes Uint8Array in LSB (WASM code)
20+ toMemory (width) {
21+ return this._value.toBuffer('le', width || 32)
22+ }
23+
1624 toString (radix = 10) {
1725 if (radix === 16) {
1826 return '0x' + this._value.toString(16)
1927 }
@@ -23,9 +31,9 @@
2331 toBuffer (width) {
2432 if (width <= 0 || width > 32) {
2533 throw new Error('Invalid U256 width')
2634 }
27- return this._value.toBuffer('le', width || 32)
35+ return this._value.toBuffer('be', width || 32)
2836 }
2937
3038 sub (u256) {
3139 return new U256(this._value.sub(u256._value))

Built with git-ssb-web