Commit 44c70314f4cb4bdce4bf22769116f514334368fd
Merge pull request #40 from ewasm/interface
Interface fixes: log, u128wanderer 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.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.BALANCE_SIZE_BYTES, balance.toMemory(constants.BALANCE_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.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)) | |
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, 32, 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, 32, 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, 32, 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. |
@@ -310,18 +313,39 @@ | ||
310 | 313 | /** |
311 | 314 | * Creates a new log in the current environment |
312 | 315 | * @param {integer} dataOffset the offset in memory to load the memory |
313 | 316 | * @param {integer} length the data length |
314 | - * TODO: replace with variadic | |
317 | + * @param {integer} number of topics | |
315 | 318 | */ |
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 | + } | |
319 | 323 | |
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 | + | |
321 | 345 | this.environment.logs.push({ |
322 | 346 | data: data, |
323 | - topics: [topic1, topic2, topic3, topic4, topic5] | |
347 | + topics: topics | |
324 | 348 | }) |
325 | 349 | } |
326 | 350 | |
327 | 351 | /** |
@@ -332,10 +356,10 @@ | ||
332 | 356 | */ |
333 | 357 | create (valueOffset, dataOffset, length) { |
334 | 358 | this.takeGas(32000) |
335 | 359 | |
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) | |
338 | 362 | const result = this.environment.create(value, data) |
339 | 363 | return result |
340 | 364 | } |
341 | 365 | |
@@ -354,11 +378,11 @@ | ||
354 | 378 | // FIXME: count properly |
355 | 379 | this.takeGas(40) |
356 | 380 | |
357 | 381 | // 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) | |
361 | 385 | // Run the call |
362 | 386 | const [result, errorCode] = this.environment.call(gas, address, value, data) |
363 | 387 | this.setMemory(resultOffset, resultLength, result) |
364 | 388 | return errorCode |
@@ -379,11 +403,11 @@ | ||
379 | 403 | // FIXME: count properly |
380 | 404 | this.takeGas(40) |
381 | 405 | |
382 | 406 | // 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) | |
386 | 410 | // Run the call |
387 | 411 | const [result, errorCode] = this.environment.callCode(gas, address, value, data) |
388 | 412 | this.setMemory(resultOffset, resultLength, result) |
389 | 413 | return errorCode |
@@ -404,10 +428,10 @@ | ||
404 | 428 | callDelegate (gas, addressOffset, dataOffset, dataLength, resultOffset, resultLength) { |
405 | 429 | // FIXME: count properly |
406 | 430 | this.takeGas(40) |
407 | 431 | |
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)) | |
410 | 434 | const [result, errorCode] = this.environment.callDelegate(gas, address, data) |
411 | 435 | this.setMemory(resultOffset, resultLength, result) |
412 | 436 | return errorCode |
413 | 437 | } |
@@ -418,11 +442,11 @@ | ||
418 | 442 | * @param {interger} pathOffest the memory offset to load the the path from |
419 | 443 | * @param {interger} valueOffset the memory offset to load the value from |
420 | 444 | */ |
421 | 445 | 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') | |
423 | 447 | // copy the value |
424 | - const value = this.getMemory(valueOffset, 32).slice(0) | |
448 | + const value = this.getMemory(valueOffset, U256_SIZE_BYTES).slice(0) | |
425 | 449 | const oldValue = this.environment.state.get(path) |
426 | 450 | const valIsZero = value.every((i) => i === 0) |
427 | 451 | |
428 | 452 | this.takeGas(5000) |
@@ -448,29 +472,29 @@ | ||
448 | 472 | */ |
449 | 473 | storageLoad (pathOffset, resultOffset) { |
450 | 474 | this.takeGas(50) |
451 | 475 | |
452 | - const path = new Buffer(this.getMemory(pathOffset, 32)).toString('hex') | |
476 | + const path = new Buffer(this.getMemory(pathOffset, U256_SIZE_BYTES)).toString('hex') | |
453 | 477 | const result = this.environment.state.get(path) |
454 | - this.setMemory(resultOffset, 32, result) | |
478 | + this.setMemory(resultOffset, U256_SIZE_BYTES, result) | |
455 | 479 | } |
456 | 480 | |
457 | 481 | /** |
458 | 482 | * Halt execution returning output data. |
459 | 483 | * @param {integer} offset the offset of the output data. |
460 | 484 | * @param {integer} length the length of the output data. |
461 | 485 | */ |
462 | 486 | return (offset, length) { |
463 | - this.environment.returnValue = this.getMemory(offset, length) | |
487 | + this.environment.returnValue = this.getMemory(offset, length).slice(0) | |
464 | 488 | } |
465 | 489 | |
466 | 490 | /** |
467 | 491 | * Halt execution and register account for later deletion giving the remaining |
468 | 492 | * balance to an address path |
469 | 493 | * @param {integer} offset the offset to load the address from |
470 | 494 | */ |
471 | 495 | 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)) | |
473 | 497 | this.environment.gasRefund += 24000 |
474 | 498 | } |
475 | 499 | |
476 | 500 | getMemory (offset, length) { |
Built with git-ssb-web