Commit 1a0c8632e72f551d5fc2c1e6f7c416016fc954bd
Interface: properly count gas for create/call
Alex Beregszaszi committed on 8/28/2016, 11:54:31 AMParent: c9ac540197b2a640dfb17a9bacf432e75ebaa0c0
Files changed
interface.js | changed |
interface.js | ||
---|---|---|
@@ -356,9 +356,9 @@ | ||
356 | 356 | * @param (integer} resultOffset the offset to write the new contract address to |
357 | 357 | * @return {integer} Return 1 or 0 depending on if the VM trapped on the message or not |
358 | 358 | */ |
359 | 359 | create (valueOffset, dataOffset, length, resultOffset) { |
360 | - this.takeGas(32000) | |
360 | + this.takeGas(32000 + length * 200) | |
361 | 361 | |
362 | 362 | const value = U256.fromMemory(this.getMemory(valueOffset, U128_SIZE_BYTES)) |
363 | 363 | const data = this.getMemory(dataOffset, length).slice(0) |
364 | 364 | const [errorCode, address] = this.environment.create(value, data) |
@@ -377,15 +377,26 @@ | ||
377 | 377 | * @param {integer} gas |
378 | 378 | * @return {integer} Returns 1 or 0 depending on if the VM trapped on the message or not |
379 | 379 | */ |
380 | 380 | call (gas, addressOffset, valueOffset, dataOffset, dataLength, resultOffset, resultLength) { |
381 | - // FIXME: count properly | |
382 | - this.takeGas(40) | |
381 | + this.takeGas(40 + gas) | |
383 | 382 | |
384 | 383 | // Load the params from mem |
385 | 384 | const address = Address.fromMemory(this.getMemory(addressOffset, ADDRESS_SIZE_BYTES)) |
386 | 385 | const value = U256.fromMemory(this.getMemory(valueOffset, U128_SIZE_BYTES)) |
387 | 386 | const data = this.getMemory(dataOffset, dataLength).slice(0) |
387 | + | |
388 | + // Special case for calling into empty account | |
389 | + if (!this.environment.isAccountPresent(address)) { | |
390 | + this.takeGas(25000) | |
391 | + } | |
392 | + | |
393 | + // Special case for non-zero value | |
394 | + if (!value.isZero()) { | |
395 | + this.takeGas(9000) | |
396 | + gas += 2300; | |
397 | + } | |
398 | + | |
388 | 399 | const [errorCode, result] = this.environment.call(gas, address, value, data) |
389 | 400 | this.setMemory(resultOffset, resultLength, result) |
390 | 401 | return errorCode |
391 | 402 | } |
Built with git-ssb-web