Commit a675a43183d94fd9bb926d224545b01f5bbabb36
Interface: fold in constants.js
Alex Beregszaszi committed on 8/25/2016, 2:43:24 AMParent: 8b6a2d4230e3065ae873a6c239315fd80e80eaa9
Files changed
interface.js | changed |
constants.js | deleted |
interface.js | ||
---|---|---|
@@ -1,12 +1,15 @@ | ||
1 | 1 | /** |
2 | 2 | * This is the Ethereum interface that is exposed to the WASM instance which |
3 | 3 | * enables to interact with the Ethereum Environment |
4 | 4 | */ |
5 | -const constants = require('./constants.js') | |
6 | 5 | const Address = require('./address.js') |
7 | 6 | const U256 = require('./u256.js') |
8 | 7 | |
8 | +const U128_SIZE_BYTES = 16 | |
9 | +const ADDRESS_SIZE_BYTES = 20 | |
10 | +const U256_SIZE_BYTES = 32 | |
11 | + | |
9 | 12 | // The interface exposed to the WebAessembly Core |
10 | 13 | module.exports = class Interface { |
11 | 14 | constructor (environment) { |
12 | 15 | this.environment = environment |
@@ -86,9 +89,9 @@ | ||
86 | 89 | */ |
87 | 90 | getAddress (offset) { |
88 | 91 | this.takeGas(2) |
89 | 92 | |
90 | - this.setMemory(offset, constants.ADDRESS_SIZE_BYTES, this.environment.address.toMemory()) | |
93 | + this.setMemory(offset, ADDRESS_SIZE_BYTES, this.environment.address.toMemory()) | |
91 | 94 | } |
92 | 95 | |
93 | 96 | /** |
94 | 97 | * Gets balance of the given account and loads it into memory at the given |
@@ -98,12 +101,12 @@ | ||
98 | 101 | */ |
99 | 102 | getBalance (addressOffset, offset) { |
100 | 103 | this.takeGas(20) |
101 | 104 | |
102 | - const address = Address.fromMemory(this.getMemory(addressOffset, constants.ADDRESS_SIZE_BYTES)) | |
105 | + const address = Address.fromMemory(this.getMemory(addressOffset, ADDRESS_SIZE_BYTES)) | |
103 | 106 | // call the parent contract and ask for the balance of one of its child contracts |
104 | 107 | 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)) | |
106 | 109 | } |
107 | 110 | |
108 | 111 | /** |
109 | 112 | * Gets the execution's origination address and loads it into memory at the |
@@ -113,9 +116,9 @@ | ||
113 | 116 | */ |
114 | 117 | getTxOrigin (offset) { |
115 | 118 | this.takeGas(2) |
116 | 119 | |
117 | - this.setMemory(offset, constants.ADDRESS_SIZE_BYTES, this.environment.origin.toMemory()) | |
120 | + this.setMemory(offset, ADDRESS_SIZE_BYTES, this.environment.origin.toMemory()) | |
118 | 121 | } |
119 | 122 | |
120 | 123 | /** |
121 | 124 | * Gets caller address and loads it into memory at the given offset. This is |
@@ -124,9 +127,9 @@ | ||
124 | 127 | */ |
125 | 128 | getCaller (offset) { |
126 | 129 | this.takeGas(2) |
127 | 130 | |
128 | - this.setMemory(offset, constants.ADDRESS_SIZE_BYTES, this.environment.caller.toMemory()) | |
131 | + this.setMemory(offset, ADDRESS_SIZE_BYTES, this.environment.caller.toMemory()) | |
129 | 132 | } |
130 | 133 | |
131 | 134 | /** |
132 | 135 | * Gets the deposited value by the instruction/transaction responsible for |
@@ -135,9 +138,9 @@ | ||
135 | 138 | */ |
136 | 139 | getCallValue (offset) { |
137 | 140 | this.takeGas(2) |
138 | 141 | |
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)) | |
140 | 143 | } |
141 | 144 | |
142 | 145 | /** |
143 | 146 | * Get size of input data in current environment. This pertains to the input |
@@ -172,9 +175,9 @@ | ||
172 | 175 | */ |
173 | 176 | callDataCopy256 (offset, dataOffset) { |
174 | 177 | this.takeGas(3) |
175 | 178 | 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) | |
177 | 180 | } |
178 | 181 | |
179 | 182 | /** |
180 | 183 | * Gets the size of code running in current environment. |
@@ -206,9 +209,9 @@ | ||
206 | 209 | */ |
207 | 210 | getExternalCodeSize (addressOffset) { |
208 | 211 | this.takeGas(20) |
209 | 212 | |
210 | - const address = Address.fromMemory(this.getMemory(addressOffset, constants.ADDRESS_SIZE_BYTES)) | |
213 | + const address = Address.fromMemory(this.getMemory(addressOffset, ADDRESS_SIZE_BYTES)) | |
211 | 214 | const code = this.environment.getCode(address) |
212 | 215 | return code.length |
213 | 216 | } |
214 | 217 | |
@@ -221,9 +224,9 @@ | ||
221 | 224 | */ |
222 | 225 | externalCodeCopy (addressOffset, resultOffset, codeOffset, length) { |
223 | 226 | this.takeGas(20 + Math.ceil(length / 32) * 3) |
224 | 227 | |
225 | - const address = Address.fromMemory(this.getMemory(addressOffset, constants.ADDRESS_SIZE_BYTES)) | |
228 | + const address = Address.fromMemory(this.getMemory(addressOffset, ADDRESS_SIZE_BYTES)) | |
226 | 229 | let code = this.environment.getCode(address) |
227 | 230 | code = new Uint8Array(code, codeOffset, length) |
228 | 231 | this.setMemory(resultOffset, length, code) |
229 | 232 | } |
@@ -253,9 +256,9 @@ | ||
253 | 256 | hash = new U256(0) |
254 | 257 | } else { |
255 | 258 | hash = new U256(this.environment.getBlockHash(number)) |
256 | 259 | } |
257 | - this.setMemory(offset, constants.U256_SIZE_BYTES, hash.toMemory()) | |
260 | + this.setMemory(offset, U256_SIZE_BYTES, hash.toMemory()) | |
258 | 261 | } |
259 | 262 | |
260 | 263 | /** |
261 | 264 | * Gets the block’s beneficiary address and loads into memory. |
@@ -263,9 +266,9 @@ | ||
263 | 266 | */ |
264 | 267 | getBlockCoinbase (offset) { |
265 | 268 | this.takeGas(2) |
266 | 269 | |
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()) | |
268 | 271 | } |
269 | 272 | |
270 | 273 | /** |
271 | 274 | * Get the block’s timestamp. |
@@ -293,9 +296,9 @@ | ||
293 | 296 | */ |
294 | 297 | getBlockDifficulty (offset) { |
295 | 298 | this.takeGas(2) |
296 | 299 | |
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()) | |
298 | 301 | } |
299 | 302 | |
300 | 303 | /** |
301 | 304 | * Get the block’s gas limit. |
@@ -323,21 +326,21 @@ | ||
323 | 326 | const data = this.getMemory(dataOffset, length).slice(0) |
324 | 327 | let topics = [] |
325 | 328 | |
326 | 329 | 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))) | |
328 | 331 | } |
329 | 332 | |
330 | 333 | 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))) | |
332 | 335 | } |
333 | 336 | |
334 | 337 | 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))) | |
336 | 339 | } |
337 | 340 | |
338 | 341 | 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))) | |
340 | 343 | } |
341 | 344 | |
342 | 345 | this.environment.logs.push({ |
343 | 346 | data: data, |
@@ -353,9 +356,9 @@ | ||
353 | 356 | */ |
354 | 357 | create (valueOffset, dataOffset, length) { |
355 | 358 | this.takeGas(32000) |
356 | 359 | |
357 | - const value = U256.fromMemory(this.getMemory(valueOffset, constants.U128_SIZE_BYTES)) | |
360 | + const value = U256.fromMemory(this.getMemory(valueOffset, U128_SIZE_BYTES)) | |
358 | 361 | const data = this.getMemory(dataOffset, length).slice(0) |
359 | 362 | const result = this.environment.create(value, data) |
360 | 363 | return result |
361 | 364 | } |
@@ -375,10 +378,10 @@ | ||
375 | 378 | // FIXME: count properly |
376 | 379 | this.takeGas(40) |
377 | 380 | |
378 | 381 | // 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)) | |
381 | 384 | const data = this.getMemory(dataOffset, dataLength).slice(0) |
382 | 385 | // Run the call |
383 | 386 | const [result, errorCode] = this.environment.call(gas, address, value, data) |
384 | 387 | this.setMemory(resultOffset, resultLength, result) |
@@ -400,10 +403,10 @@ | ||
400 | 403 | // FIXME: count properly |
401 | 404 | this.takeGas(40) |
402 | 405 | |
403 | 406 | // 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)) | |
406 | 409 | const data = this.getMemory(dataOffset, dataLength).slice(0) |
407 | 410 | // Run the call |
408 | 411 | const [result, errorCode] = this.environment.callCode(gas, address, value, data) |
409 | 412 | this.setMemory(resultOffset, resultLength, result) |
@@ -426,9 +429,9 @@ | ||
426 | 429 | // FIXME: count properly |
427 | 430 | this.takeGas(40) |
428 | 431 | |
429 | 432 | 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)) | |
431 | 434 | const [result, errorCode] = this.environment.callDelegate(gas, address, data) |
432 | 435 | this.setMemory(resultOffset, resultLength, result) |
433 | 436 | return errorCode |
434 | 437 | } |
@@ -439,11 +442,11 @@ | ||
439 | 442 | * @param {interger} pathOffest the memory offset to load the the path from |
440 | 443 | * @param {interger} valueOffset the memory offset to load the value from |
441 | 444 | */ |
442 | 445 | 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') | |
444 | 447 | // 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) | |
446 | 449 | const oldValue = this.environment.state.get(path) |
447 | 450 | const valIsZero = value.every((i) => i === 0) |
448 | 451 | |
449 | 452 | this.takeGas(5000) |
@@ -469,11 +472,11 @@ | ||
469 | 472 | */ |
470 | 473 | storageLoad (pathOffset, resultOffset) { |
471 | 474 | this.takeGas(50) |
472 | 475 | |
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') | |
474 | 477 | const result = this.environment.state.get(path) |
475 | - this.setMemory(resultOffset, constants.U256_SIZE_BYTES, result) | |
478 | + this.setMemory(resultOffset, U256_SIZE_BYTES, result) | |
476 | 479 | } |
477 | 480 | |
478 | 481 | /** |
479 | 482 | * Halt execution returning output data. |
@@ -489,9 +492,9 @@ | ||
489 | 492 | * balance to an address path |
490 | 493 | * @param {integer} offset the offset to load the address from |
491 | 494 | */ |
492 | 495 | 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)) | |
494 | 497 | this.environment.gasRefund += 24000 |
495 | 498 | } |
496 | 499 | |
497 | 500 | getMemory (offset, length) { |
Built with git-ssb-web