git ssb

0+

wanderer🌟 / js-primea-hypervisor



Commit 1a0c8632e72f551d5fc2c1e6f7c416016fc954bd

Interface: properly count gas for create/call

Alex Beregszaszi committed on 8/28/2016, 11:54:31 AM
Parent: c9ac540197b2a640dfb17a9bacf432e75ebaa0c0

Files changed

interface.jschanged
interface.jsView
@@ -356,9 +356,9 @@
356356 * @param (integer} resultOffset the offset to write the new contract address to
357357 * @return {integer} Return 1 or 0 depending on if the VM trapped on the message or not
358358 */
359359 create (valueOffset, dataOffset, length, resultOffset) {
360- this.takeGas(32000)
360+ this.takeGas(32000 + length * 200)
361361
362362 const value = U256.fromMemory(this.getMemory(valueOffset, U128_SIZE_BYTES))
363363 const data = this.getMemory(dataOffset, length).slice(0)
364364 const [errorCode, address] = this.environment.create(value, data)
@@ -377,15 +377,26 @@
377377 * @param {integer} gas
378378 * @return {integer} Returns 1 or 0 depending on if the VM trapped on the message or not
379379 */
380380 call (gas, addressOffset, valueOffset, dataOffset, dataLength, resultOffset, resultLength) {
381- // FIXME: count properly
382- this.takeGas(40)
381+ this.takeGas(40 + gas)
383382
384383 // Load the params from mem
385384 const address = Address.fromMemory(this.getMemory(addressOffset, ADDRESS_SIZE_BYTES))
386385 const value = U256.fromMemory(this.getMemory(valueOffset, U128_SIZE_BYTES))
387386 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+
388399 const [errorCode, result] = this.environment.call(gas, address, value, data)
389400 this.setMemory(resultOffset, resultLength, result)
390401 return errorCode
391402 }

Built with git-ssb-web