git ssb

0+

wanderer🌟 / js-primea-hypervisor



Commit 44c70314f4cb4bdce4bf22769116f514334368fd

Merge pull request #40 from ewasm/interface

Interface fixes: log, u128
wanderer authored on 8/25/2016, 1:27:57 PM
GitHub committed on 8/25/2016, 1:27:57 PM
Parent: 8ef09c67f032b90950cba23e30ffcc14c0b47078
Parent: a675a43183d94fd9bb926d224545b01f5bbabb36

Files changed

interface.jschanged
constants.jsdeleted
interface.jsView
@@ -1,12 +1,15 @@
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-const constants = require('./constants.js')
65 const Address = require('./address.js')
76 const U256 = require('./u256.js')
87
8+const U128_SIZE_BYTES = 16
9+const ADDRESS_SIZE_BYTES = 20
10+const U256_SIZE_BYTES = 32
11+
912 // The interface exposed to the WebAessembly Core
1013 module.exports = class Interface {
1114 constructor (environment) {
1215 this.environment = environment
@@ -86,9 +89,9 @@
8689 */
8790 getAddress (offset) {
8891 this.takeGas(2)
8992
90- this.setMemory(offset, constants.ADDRESS_SIZE_BYTES, this.environment.address.toMemory())
93+ this.setMemory(offset, ADDRESS_SIZE_BYTES, this.environment.address.toMemory())
9194 }
9295
9396 /**
9497 * Gets balance of the given account and loads it into memory at the given
@@ -98,12 +101,12 @@
98101 */
99102 getBalance (addressOffset, offset) {
100103 this.takeGas(20)
101104
102- const address = Address.fromMemory(this.getMemory(addressOffset, constants.ADDRESS_SIZE_BYTES))
105+ const address = Address.fromMemory(this.getMemory(addressOffset, ADDRESS_SIZE_BYTES))
103106 // call the parent contract and ask for the balance of one of its child contracts
104107 const balance = this.environment.getBalance(address)
105- this.setMemory(offset, constants.BALANCE_SIZE_BYTES, balance.toMemory(constants.BALANCE_SIZE_BYTES))
108+ this.setMemory(offset, U128_SIZE_BYTES, balance.toMemory(U128_SIZE_BYTES))
106109 }
107110
108111 /**
109112 * Gets the execution's origination address and loads it into memory at the
@@ -113,9 +116,9 @@
113116 */
114117 getTxOrigin (offset) {
115118 this.takeGas(2)
116119
117- this.setMemory(offset, constants.ADDRESS_SIZE_BYTES, this.environment.origin.toMemory())
120+ this.setMemory(offset, ADDRESS_SIZE_BYTES, this.environment.origin.toMemory())
118121 }
119122
120123 /**
121124 * Gets caller address and loads it into memory at the given offset. This is
@@ -124,9 +127,9 @@
124127 */
125128 getCaller (offset) {
126129 this.takeGas(2)
127130
128- this.setMemory(offset, constants.ADDRESS_SIZE_BYTES, this.environment.caller.toMemory())
131+ this.setMemory(offset, ADDRESS_SIZE_BYTES, this.environment.caller.toMemory())
129132 }
130133
131134 /**
132135 * Gets the deposited value by the instruction/transaction responsible for
@@ -135,9 +138,9 @@
135138 */
136139 getCallValue (offset) {
137140 this.takeGas(2)
138141
139- this.setMemory(offset, constants.BALANCE_SIZE_BYTES, this.environment.callValue.toMemory(constants.BALANCE_SIZE_BYTES))
142+ this.setMemory(offset, U128_SIZE_BYTES, this.environment.callValue.toMemory(U128_SIZE_BYTES))
140143 }
141144
142145 /**
143146 * Get size of input data in current environment. This pertains to the input
@@ -172,9 +175,9 @@
172175 */
173176 callDataCopy256 (offset, dataOffset) {
174177 this.takeGas(3)
175178 const callData = this.environment.callData.slice(dataOffset, dataOffset + 32)
176- this.setMemory(offset, 32, callData)
179+ this.setMemory(offset, U256_SIZE_BYTES, callData)
177180 }
178181
179182 /**
180183 * Gets the size of code running in current environment.
@@ -206,9 +209,9 @@
206209 */
207210 getExternalCodeSize (addressOffset) {
208211 this.takeGas(20)
209212
210- const address = Address.fromMemory(this.getMemory(addressOffset, constants.ADDRESS_SIZE_BYTES))
213+ const address = Address.fromMemory(this.getMemory(addressOffset, ADDRESS_SIZE_BYTES))
211214 const code = this.environment.getCode(address)
212215 return code.length
213216 }
214217
@@ -221,9 +224,9 @@
221224 */
222225 externalCodeCopy (addressOffset, resultOffset, codeOffset, length) {
223226 this.takeGas(20 + Math.ceil(length / 32) * 3)
224227
225- const address = Address.fromMemory(this.getMemory(addressOffset, constants.ADDRESS_SIZE_BYTES))
228+ const address = Address.fromMemory(this.getMemory(addressOffset, ADDRESS_SIZE_BYTES))
226229 let code = this.environment.getCode(address)
227230 code = new Uint8Array(code, codeOffset, length)
228231 this.setMemory(resultOffset, length, code)
229232 }
@@ -253,9 +256,9 @@
253256 hash = new U256(0)
254257 } else {
255258 hash = new U256(this.environment.getBlockHash(number))
256259 }
257- this.setMemory(offset, 32, hash.toMemory())
260+ this.setMemory(offset, U256_SIZE_BYTES, hash.toMemory())
258261 }
259262
260263 /**
261264 * Gets the block’s beneficiary address and loads into memory.
@@ -263,9 +266,9 @@
263266 */
264267 getBlockCoinbase (offset) {
265268 this.takeGas(2)
266269
267- this.setMemory(offset, constants.ADDRESS_SIZE_BYTES, this.environment.block.coinbase.toMemory())
270+ this.setMemory(offset, ADDRESS_SIZE_BYTES, this.environment.block.coinbase.toMemory())
268271 }
269272
270273 /**
271274 * Get the block’s timestamp.
@@ -293,9 +296,9 @@
293296 */
294297 getBlockDifficulty (offset) {
295298 this.takeGas(2)
296299
297- this.setMemory(offset, 32, this.environment.block.difficulty.toMemory())
300+ this.setMemory(offset, U256_SIZE_BYTES, this.environment.block.difficulty.toMemory())
298301 }
299302
300303 /**
301304 * Get the block’s gas limit.
@@ -310,18 +313,39 @@
310313 /**
311314 * Creates a new log in the current environment
312315 * @param {integer} dataOffset the offset in memory to load the memory
313316 * @param {integer} length the data length
314- * TODO: replace with variadic
317+ * @param {integer} number of topics
315318 */
316- log (dataOffset, length, topic1, topic2, topic3, topic4, topic5) {
317- // FIXME: calculate gas for topics set
318- this.takeGas(375 + length * 8)
319+ log (dataOffset, length, numberOfTopics, topic1, topic2, topic3, topic4) {
320+ if (numberOfTopics < 0 || numberOfTopics > 4) {
321+ throw new Error('Invalid numberOfTopics')
322+ }
319323
320- const data = this.getMemory(dataOffset, length)
324+ this.takeGas(375 + length * 8 + numberOfTopics * 375)
325+
326+ const data = this.getMemory(dataOffset, length).slice(0)
327+ let topics = []
328+
329+ if (numberOfTopics > 0) {
330+ topics.push(U256.fromMemory(this.getMemory(topic1, U256_SIZE_BYTES)))
331+ }
332+
333+ if (numberOfTopics > 1) {
334+ topics.push(U256.fromMemory(this.getMemory(topic2, U256_SIZE_BYTES)))
335+ }
336+
337+ if (numberOfTopics > 2) {
338+ topics.push(U256.fromMemory(this.getMemory(topic3, U256_SIZE_BYTES)))
339+ }
340+
341+ if (numberOfTopics > 3) {
342+ topics.push(U256.fromMemory(this.getMemory(topic4, U256_SIZE_BYTES)))
343+ }
344+
321345 this.environment.logs.push({
322346 data: data,
323- topics: [topic1, topic2, topic3, topic4, topic5]
347+ topics: topics
324348 })
325349 }
326350
327351 /**
@@ -332,10 +356,10 @@
332356 */
333357 create (valueOffset, dataOffset, length) {
334358 this.takeGas(32000)
335359
336- const value = U256.fromMemory(this.getMemory(valueOffset, constants.BALANCE_SIZE_BYTES))
337- const data = this.getMemory(dataOffset, length)
360+ const value = U256.fromMemory(this.getMemory(valueOffset, U128_SIZE_BYTES))
361+ const data = this.getMemory(dataOffset, length).slice(0)
338362 const result = this.environment.create(value, data)
339363 return result
340364 }
341365
@@ -354,11 +378,11 @@
354378 // FIXME: count properly
355379 this.takeGas(40)
356380
357381 // Load the params from mem
358- const address = Address.fromMemory(this.getMemory(addressOffset, constants.ADDRESS_SIZE_BYTES))
359- const value = U256.fromMemory(this.getMemory(valueOffset, constants.BALANCE_SIZE_BYTES))
360- const data = this.getMemory(dataOffset, dataLength)
382+ const address = Address.fromMemory(this.getMemory(addressOffset, ADDRESS_SIZE_BYTES))
383+ const value = U256.fromMemory(this.getMemory(valueOffset, U128_SIZE_BYTES))
384+ const data = this.getMemory(dataOffset, dataLength).slice(0)
361385 // Run the call
362386 const [result, errorCode] = this.environment.call(gas, address, value, data)
363387 this.setMemory(resultOffset, resultLength, result)
364388 return errorCode
@@ -379,11 +403,11 @@
379403 // FIXME: count properly
380404 this.takeGas(40)
381405
382406 // Load the params from mem
383- const address = Address.fromMemory(this.getMemory(addressOffset, constants.ADDRESS_SIZE_BYTES))
384- const value = U256.fromMemory(this.getMemory(valueOffset, constants.BALANCE_SIZE_BYTES))
385- const data = this.getMemory(dataOffset, dataLength)
407+ const address = Address.fromMemory(this.getMemory(addressOffset, ADDRESS_SIZE_BYTES))
408+ const value = U256.fromMemory(this.getMemory(valueOffset, U128_SIZE_BYTES))
409+ const data = this.getMemory(dataOffset, dataLength).slice(0)
386410 // Run the call
387411 const [result, errorCode] = this.environment.callCode(gas, address, value, data)
388412 this.setMemory(resultOffset, resultLength, result)
389413 return errorCode
@@ -404,10 +428,10 @@
404428 callDelegate (gas, addressOffset, dataOffset, dataLength, resultOffset, resultLength) {
405429 // FIXME: count properly
406430 this.takeGas(40)
407431
408- const data = this.getMemory(dataOffset, dataLength)
409- const address = Address.fromMemory(this.getMemory(addressOffset, constants.ADDRESS_SIZE_BYTES))
432+ const data = this.getMemory(dataOffset, dataLength).slice(0)
433+ const address = Address.fromMemory(this.getMemory(addressOffset, ADDRESS_SIZE_BYTES))
410434 const [result, errorCode] = this.environment.callDelegate(gas, address, data)
411435 this.setMemory(resultOffset, resultLength, result)
412436 return errorCode
413437 }
@@ -418,11 +442,11 @@
418442 * @param {interger} pathOffest the memory offset to load the the path from
419443 * @param {interger} valueOffset the memory offset to load the value from
420444 */
421445 storageStore (pathOffset, valueOffset) {
422- const path = new Buffer(this.getMemory(pathOffset, 32)).toString('hex')
446+ const path = new Buffer(this.getMemory(pathOffset, U256_SIZE_BYTES)).toString('hex')
423447 // copy the value
424- const value = this.getMemory(valueOffset, 32).slice(0)
448+ const value = this.getMemory(valueOffset, U256_SIZE_BYTES).slice(0)
425449 const oldValue = this.environment.state.get(path)
426450 const valIsZero = value.every((i) => i === 0)
427451
428452 this.takeGas(5000)
@@ -448,29 +472,29 @@
448472 */
449473 storageLoad (pathOffset, resultOffset) {
450474 this.takeGas(50)
451475
452- const path = new Buffer(this.getMemory(pathOffset, 32)).toString('hex')
476+ const path = new Buffer(this.getMemory(pathOffset, U256_SIZE_BYTES)).toString('hex')
453477 const result = this.environment.state.get(path)
454- this.setMemory(resultOffset, 32, result)
478+ this.setMemory(resultOffset, U256_SIZE_BYTES, result)
455479 }
456480
457481 /**
458482 * Halt execution returning output data.
459483 * @param {integer} offset the offset of the output data.
460484 * @param {integer} length the length of the output data.
461485 */
462486 return (offset, length) {
463- this.environment.returnValue = this.getMemory(offset, length)
487+ this.environment.returnValue = this.getMemory(offset, length).slice(0)
464488 }
465489
466490 /**
467491 * Halt execution and register account for later deletion giving the remaining
468492 * balance to an address path
469493 * @param {integer} offset the offset to load the address from
470494 */
471495 selfDestruct (addressOffset) {
472- this.environment.suicideAddress = Address.fromMemory(this.getMemory(addressOffset, constants.ADDRESS_SIZE_BYTES))
496+ this.environment.suicideAddress = Address.fromMemory(this.getMemory(addressOffset, ADDRESS_SIZE_BYTES))
473497 this.environment.gasRefund += 24000
474498 }
475499
476500 getMemory (offset, length) {
constants.jsView
@@ -1,4 +1,0 @@
1-module.exports = {
2- BALANCE_SIZE_BYTES: 16, // Max balance size in bytes
3- ADDRESS_SIZE_BYTES: 20 // Address size in bytes
4-}

Built with git-ssb-web