git ssb

0+

wanderer🌟 / js-primea-hypervisor



Commit a675a43183d94fd9bb926d224545b01f5bbabb36

Interface: fold in constants.js

Alex Beregszaszi committed on 8/25/2016, 2:43:24 AM
Parent: 8b6a2d4230e3065ae873a6c239315fd80e80eaa9

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.U128_SIZE_BYTES, balance.toMemory(constants.U128_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.U128_SIZE_BYTES, this.environment.callValue.toMemory(constants.U128_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, constants.U256_SIZE_BYTES, 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, constants.U256_SIZE_BYTES, 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, constants.U256_SIZE_BYTES, 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.
@@ -323,21 +326,21 @@
323326 const data = this.getMemory(dataOffset, length).slice(0)
324327 let topics = []
325328
326329 if (numberOfTopics > 0) {
327- topics.push(U256.fromMemory(this.getMemory(topic1, constants.U256_SIZE_BYTES)))
330+ topics.push(U256.fromMemory(this.getMemory(topic1, U256_SIZE_BYTES)))
328331 }
329332
330333 if (numberOfTopics > 1) {
331- topics.push(U256.fromMemory(this.getMemory(topic2, constants.U256_SIZE_BYTES)))
334+ topics.push(U256.fromMemory(this.getMemory(topic2, U256_SIZE_BYTES)))
332335 }
333336
334337 if (numberOfTopics > 2) {
335- topics.push(U256.fromMemory(this.getMemory(topic3, constants.U256_SIZE_BYTES)))
338+ topics.push(U256.fromMemory(this.getMemory(topic3, U256_SIZE_BYTES)))
336339 }
337340
338341 if (numberOfTopics > 3) {
339- topics.push(U256.fromMemory(this.getMemory(topic4, constants.U256_SIZE_BYTES)))
342+ topics.push(U256.fromMemory(this.getMemory(topic4, U256_SIZE_BYTES)))
340343 }
341344
342345 this.environment.logs.push({
343346 data: data,
@@ -353,9 +356,9 @@
353356 */
354357 create (valueOffset, dataOffset, length) {
355358 this.takeGas(32000)
356359
357- const value = U256.fromMemory(this.getMemory(valueOffset, constants.U128_SIZE_BYTES))
360+ const value = U256.fromMemory(this.getMemory(valueOffset, U128_SIZE_BYTES))
358361 const data = this.getMemory(dataOffset, length).slice(0)
359362 const result = this.environment.create(value, data)
360363 return result
361364 }
@@ -375,10 +378,10 @@
375378 // FIXME: count properly
376379 this.takeGas(40)
377380
378381 // Load the params from mem
379- const address = Address.fromMemory(this.getMemory(addressOffset, constants.ADDRESS_SIZE_BYTES))
380- const value = U256.fromMemory(this.getMemory(valueOffset, constants.U128_SIZE_BYTES))
382+ const address = Address.fromMemory(this.getMemory(addressOffset, ADDRESS_SIZE_BYTES))
383+ const value = U256.fromMemory(this.getMemory(valueOffset, U128_SIZE_BYTES))
381384 const data = this.getMemory(dataOffset, dataLength).slice(0)
382385 // Run the call
383386 const [result, errorCode] = this.environment.call(gas, address, value, data)
384387 this.setMemory(resultOffset, resultLength, result)
@@ -400,10 +403,10 @@
400403 // FIXME: count properly
401404 this.takeGas(40)
402405
403406 // Load the params from mem
404- const address = Address.fromMemory(this.getMemory(addressOffset, constants.ADDRESS_SIZE_BYTES))
405- const value = U256.fromMemory(this.getMemory(valueOffset, constants.U128_SIZE_BYTES))
407+ const address = Address.fromMemory(this.getMemory(addressOffset, ADDRESS_SIZE_BYTES))
408+ const value = U256.fromMemory(this.getMemory(valueOffset, U128_SIZE_BYTES))
406409 const data = this.getMemory(dataOffset, dataLength).slice(0)
407410 // Run the call
408411 const [result, errorCode] = this.environment.callCode(gas, address, value, data)
409412 this.setMemory(resultOffset, resultLength, result)
@@ -426,9 +429,9 @@
426429 // FIXME: count properly
427430 this.takeGas(40)
428431
429432 const data = this.getMemory(dataOffset, dataLength).slice(0)
430- const address = Address.fromMemory(this.getMemory(addressOffset, constants.ADDRESS_SIZE_BYTES))
433+ const address = Address.fromMemory(this.getMemory(addressOffset, ADDRESS_SIZE_BYTES))
431434 const [result, errorCode] = this.environment.callDelegate(gas, address, data)
432435 this.setMemory(resultOffset, resultLength, result)
433436 return errorCode
434437 }
@@ -439,11 +442,11 @@
439442 * @param {interger} pathOffest the memory offset to load the the path from
440443 * @param {interger} valueOffset the memory offset to load the value from
441444 */
442445 storageStore (pathOffset, valueOffset) {
443- const path = new Buffer(this.getMemory(pathOffset, constants.U256_SIZE_BYTES)).toString('hex')
446+ const path = new Buffer(this.getMemory(pathOffset, U256_SIZE_BYTES)).toString('hex')
444447 // copy the value
445- const value = this.getMemory(valueOffset, constants.U256_SIZE_BYTES).slice(0)
448+ const value = this.getMemory(valueOffset, U256_SIZE_BYTES).slice(0)
446449 const oldValue = this.environment.state.get(path)
447450 const valIsZero = value.every((i) => i === 0)
448451
449452 this.takeGas(5000)
@@ -469,11 +472,11 @@
469472 */
470473 storageLoad (pathOffset, resultOffset) {
471474 this.takeGas(50)
472475
473- const path = new Buffer(this.getMemory(pathOffset, constants.U256_SIZE_BYTES)).toString('hex')
476+ const path = new Buffer(this.getMemory(pathOffset, U256_SIZE_BYTES)).toString('hex')
474477 const result = this.environment.state.get(path)
475- this.setMemory(resultOffset, constants.U256_SIZE_BYTES, result)
478+ this.setMemory(resultOffset, U256_SIZE_BYTES, result)
476479 }
477480
478481 /**
479482 * Halt execution returning output data.
@@ -489,9 +492,9 @@
489492 * balance to an address path
490493 * @param {integer} offset the offset to load the address from
491494 */
492495 selfDestruct (addressOffset) {
493- this.environment.suicideAddress = Address.fromMemory(this.getMemory(addressOffset, constants.ADDRESS_SIZE_BYTES))
496+ this.environment.suicideAddress = Address.fromMemory(this.getMemory(addressOffset, ADDRESS_SIZE_BYTES))
494497 this.environment.gasRefund += 24000
495498 }
496499
497500 getMemory (offset, length) {
constants.jsView
@@ -1,5 +1,0 @@
1-module.exports = {
2- U128_SIZE_BYTES: 16, // Max balance size in bytes
3- ADDRESS_SIZE_BYTES: 20, // Address size in bytes
4- U256_SIZE_BYTES: 32
5-}

Built with git-ssb-web