Commit 4026b89455deff8f73612b2be7855c5bf6aaa296
Merge branch 'shim' into testing
wanderer committed on 9/13/2016, 8:10:11 PMParent: be42af81974876ce832fa149178075c8fca67321
Parent: 32fabfab3c71c9d5e49fd11bdc60938c5db6ca43
Files changed
index.js | changed |
interface.js | changed |
wasm/interface.wasm | changed |
wasm/interface.wast | changed |
index.js | ||
---|---|---|
@@ -59,8 +59,9 @@ | ||
59 | 59 | } |
60 | 60 | // add shims |
61 | 61 | imports.ethereum.useGas = ethInterface.shims.exports.useGas |
62 | 62 | imports.ethereum.getGasLeft = ethInterface.shims.exports.getGasLeft |
63 | + imports.ethereum.call = ethInterface.shims.exports.call | |
63 | 64 | |
64 | 65 | const instance = WebAssembly.Instance(module, imports) |
65 | 66 | |
66 | 67 | ethInterface.setModule(instance) |
interface.js | ||
---|---|---|
@@ -20,9 +20,10 @@ | ||
20 | 20 | this.shims = WebAssembly.Instance(shimMod, { |
21 | 21 | 'interface': { |
22 | 22 | 'useGas': this._useGas.bind(this), |
23 | 23 | 'getGasLeftHigh': this._getGasLeftHigh.bind(this), |
24 | - 'getGasLeftLow': this._getGasLeftLow.bind(this) | |
24 | + 'getGasLeftLow': this._getGasLeftLow.bind(this), | |
25 | + 'call': this._getGasLeftLow.bind(this) | |
25 | 26 | } |
26 | 27 | }) |
27 | 28 | } |
28 | 29 | |
@@ -49,9 +50,8 @@ | ||
49 | 50 | 'getBlockDifficulty', |
50 | 51 | 'getBlockGasLimit', |
51 | 52 | 'log', |
52 | 53 | 'create', |
53 | - 'call', | |
54 | 54 | 'callCode', |
55 | 55 | 'callDelegate', |
56 | 56 | 'storageStore', |
57 | 57 | 'storageLoad', |
@@ -73,21 +73,9 @@ | ||
73 | 73 | * Subtracts an amount to the gas counter |
74 | 74 | * @param {integer} amount the amount to subtract to the gas counter |
75 | 75 | */ |
76 | 76 | _useGas (high, low) { |
77 | - if (high < 0) { | |
78 | - // convert from a 32-bit two's compliment | |
79 | - high = 0x100000000 - high | |
80 | - } | |
81 | - | |
82 | - if (low < 0) { | |
83 | - // convert from a 32-bit two's compliment | |
84 | - low = 0x100000000 - low | |
85 | - } | |
86 | - | |
87 | - // JS only bitshift 32bits, so instead of high << 32 we have high * 2 ^ 32 | |
88 | - const amount = (high * 4294967296) + low | |
89 | - this.takeGas(amount) | |
77 | + this.takeGas(from64bit(high, low)) | |
90 | 78 | } |
91 | 79 | |
92 | 80 | /** |
93 | 81 | * Returns the current amount of gas |
@@ -413,35 +401,30 @@ | ||
413 | 401 | * @param {integer} resultLength |
414 | 402 | * @param {integer} gas |
415 | 403 | * @return {integer} Returns 1 or 0 depending on if the VM trapped on the message or not |
416 | 404 | */ |
417 | - call (gas, addressOffset, valueOffset, dataOffset, dataLength, resultOffset, resultLength) { | |
405 | + _call (gasHigh, gasLow, addressOffset, valueOffset, dataOffset, dataLength, resultOffset, resultLength) { | |
406 | + const gas = from64bit(gasHigh, gasLow) | |
407 | + this.takeGas(40 + gas) | |
408 | + | |
418 | 409 | // Load the params from mem |
419 | 410 | const address = Address.fromMemory(this.getMemory(addressOffset, ADDRESS_SIZE_BYTES)) |
420 | 411 | const value = U256.fromMemory(this.getMemory(valueOffset, U128_SIZE_BYTES)) |
412 | + const data = this.getMemory(dataOffset, dataLength).slice(0) | |
421 | 413 | |
422 | - this.takeGas(40) | |
423 | - | |
424 | - // const data = this.getMemory(dataOffset, dataLength).slice(0) | |
425 | - | |
426 | - // // Special case for calling into empty account | |
427 | - // if (!this.environment.isAccountPresent(address)) { | |
428 | - // this.takeGas(25000) | |
429 | - // } | |
430 | - if (address.lt(new U256(4))) { | |
414 | + // Special case for calling into empty account | |
415 | + if (!this.environment.isAccountPresent(address)) { | |
431 | 416 | this.takeGas(25000) |
432 | 417 | } |
433 | - // // Special case for non-zero value | |
418 | + | |
419 | + // Special case for non-zero value | |
434 | 420 | if (!value.isZero()) { |
435 | - this.takeGas(9000 - 2300 + gas) | |
436 | - this.takeGas(-gas) | |
421 | + this.takeGas(9000) | |
437 | 422 | } |
438 | 423 | |
439 | - // const [errorCode, result] = this.environment.call(gas, address, value, data) | |
440 | - // this.setMemory(resultOffset, resultLength, result) | |
441 | - // return errorCode | |
442 | - // | |
443 | - return 1 | |
424 | + const [errorCode, result] = this.environment.call(gas, address, value, data) | |
425 | + this.setMemory(resultOffset, resultLength, result) | |
426 | + return errorCode | |
444 | 427 | } |
445 | 428 | |
446 | 429 | /** |
447 | 430 | * Message-call into this account with an alternative account’s code. |
@@ -454,31 +437,18 @@ | ||
454 | 437 | * @param {integer} gas |
455 | 438 | * @return {integer} Returns 1 or 0 depending on if the VM trapped on the message or not |
456 | 439 | */ |
457 | 440 | callCode (gas, addressOffset, valueOffset, dataOffset, dataLength, resultOffset, resultLength) { |
441 | + // FIXME: count properly | |
442 | + this.takeGas(40) | |
443 | + | |
458 | 444 | // Load the params from mem |
459 | 445 | const address = Address.fromMemory(this.getMemory(addressOffset, ADDRESS_SIZE_BYTES)) |
460 | 446 | const value = U256.fromMemory(this.getMemory(valueOffset, U128_SIZE_BYTES)) |
461 | - | |
462 | - this.takeGas(40) | |
463 | - | |
464 | - // const data = this.getMemory(dataOffset, dataLength).slice(0) | |
465 | - | |
466 | - // // Special case for calling into empty account | |
467 | - // if (!this.environment.isAccountPresent(address)) { | |
468 | - // this.takeGas(25000) | |
469 | - // } | |
470 | - // // Special case for non-zero value | |
471 | - if (!value.isZero()) { | |
472 | - this.takeGas(9000 - 2300 + gas) | |
473 | - this.takeGas(-gas) | |
474 | - } | |
475 | - | |
476 | - // const [errorCode, result] = this.environment.call(gas, address, value, data) | |
477 | - // this.setMemory(resultOffset, resultLength, result) | |
478 | - // return errorCode | |
479 | - // | |
480 | - return 1 | |
447 | + const data = this.getMemory(dataOffset, dataLength).slice(0) | |
448 | + const [errorCode, result] = this.environment.callCode(gas, address, value, data) | |
449 | + this.setMemory(resultOffset, resultLength, result) | |
450 | + return errorCode | |
481 | 451 | } |
482 | 452 | |
483 | 453 | /** |
484 | 454 | * Message-call into this account with an alternative account’s code, but |
@@ -586,4 +556,18 @@ | ||
586 | 556 | } |
587 | 557 | this.environment.gasLeft -= amount |
588 | 558 | } |
589 | 559 | } |
560 | + | |
561 | +// converts a 64 bit number to a JS number | |
562 | +function from64bit (high, low) { | |
563 | + if (high < 0) { | |
564 | + // convert from a 32-bit two's compliment | |
565 | + high = 0x100000000 - high | |
566 | + } | |
567 | + if (low < 0) { | |
568 | + // convert from a 32-bit two's compliment | |
569 | + low = 0x100000000 - low | |
570 | + } | |
571 | + // JS only bitshift 32bits, so instead of high << 32 we have high * 2 ^ 32 | |
572 | + return (high * 4294967296) + low | |
573 | +} |
wasm/interface.wasm | ||
---|---|---|
@@ -1,2 +1,2 @@ | ||
1 | - asm type@ @ @ @ importF interfaceuseGas interfacegetGasLeftHigh interface getGasLeftLowfunctionexport useGas | |
2 | -getGasLeftcode& f� � � e �[ | |
1 | + asm type)@ @ @@ @ @importV interfaceuseGas interfacegetGasLeftHigh interface getGasLeftLow interfacecallfunctionexport useGas | |
2 | +getGasLeftcallcode@ f� � � e �[ f� � |
wasm/interface.wast | ||
---|---|---|
@@ -21,5 +21,25 @@ | ||
21 | 21 | (i64.shl (i64.extend_u/i32 (call_import $getGasLeftHigh)) (i64.const 32)) |
22 | 22 | (i64.extend_u/i32 (call_import $getGasLeftLow)))) |
23 | 23 | ) |
24 | 24 | (export "getGasLeft" $getGasLeft) |
25 | + | |
26 | + ;; call | |
27 | + ;; (import $call "ethereum" "call" (param i32 i32 i32 i32 i32 i32 i32) (result i32)) | |
28 | + (import $call "interface" "call" (param i32 i32 i32 i32 i32 i32 i32 i32) (result i32)) | |
29 | + (func $callShim | |
30 | + (param i64 i32 i32 i32 i32 i32 i32) | |
31 | + (result i32) | |
32 | + (call_import $call | |
33 | + (i32.wrap/i64 | |
34 | + (i64.shr_u (get_local 0) (i64.const 32))) | |
35 | + (i32.wrap/i64 (get_local 0)) | |
36 | + (get_local 1) | |
37 | + (get_local 2) | |
38 | + (get_local 3) | |
39 | + (get_local 4) | |
40 | + (get_local 5) | |
41 | + (get_local 6) | |
42 | + ) | |
43 | + ) | |
44 | + (export "call" $callShim) | |
25 | 45 | ) |
Built with git-ssb-web