Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deployed bytecode error #6

Closed
LayneHaber opened this issue Jun 10, 2020 · 9 comments
Closed

Deployed bytecode error #6

LayneHaber opened this issue Jun 10, 2020 · 9 comments

Comments

@LayneHaber
Copy link

LayneHaber commented Jun 10, 2020

Trying to use this package to run view functions, and running into some strange issues.

When I try to call execute with the bytecode retrieved from eth_getCode (the runtime/deployed bytecode), I get a rust panic error:

RuntimeError: unreachable
        at __rust_start_panic (wasm-function[330]:0x3a810)
        at rust_panic (wasm-function[213]:0x39f81)

      313 |
      314 |       // Execute on the raw bytes.
    > 315 |       const output = pure_evm.exec(
          |                               ^
      316 |         Uint8Array.from(Buffer.from(bytecode.replace("0x", ""), "hex")),
      317 |         encodedData,
      318 |       );

      at std::panicking::rust_panic_with_hook::h5e7c2dc110ae79d4 (wasm-function[95]:0x36e5a)
      at std::panicking::begin_panic::h6da1813f9eea9b6a (wasm-function[200]:0x39c59)
      at <pure_evm::pure_ext::PureExt as vm::ext::Ext>::depth::he3b3a85b57bdb8e8 (wasm-function[273]:0x3a540)
      at evm::interpreter::Interpreter<Cost>::exec_instruction::h8137cbc088506585 (wasm-function[1]:0xc988)
      at <evm::interpreter::Interpreter<Cost> as vm::Exec>::exec::h41e00997af783aac (wasm-function[5]:0x1df67)
      at pure_evm::exec::h1eb45dc630e9c525 (wasm-function[10]:0x24982)
          at exec (wasm-function[92]:0x36a29)
      at Object.module.exports.exec (../../node_modules/pure-evm/pure-evm_main.js:38:22)
      at AppInstance.computeStateTransition (src/models/app-instance.ts:315:31)
          at runMicrotasks (<anonymous>)

Interestingly enough, when using the init/creation bytecode, the error disappears and the output of the execute function once converted to a hex string is the same as the deployed bytecode returned from eth_getCode. Any idea on what might be causing these errors?

Here is a code snippet (running in node env, not browser):

const appArtifact = require(`@connext/contracts/artifacts/${pureAppName}.json`);
const iface = new Interface(appArtifact.abi);

// applyAction is a view function, using solidity 0.6
const data = iface.encodeFunctionData("applyAction", [
        this.encodedLatestState,
        this.encodeAction(action),
]);
const encodedData = Uint8Array.from(Buffer.from(data.replace("0x", ""), "hex"));

const bytecode = await provider.send("eth_getCode", [this.appDefinition, "latest"]);

// Works IFF using init bytecode, but returns deployed bytecode hex string. Fails otherwise.
const output = pure_evm.exec(
        Uint8Array.from(Buffer.from(bytecode.replace("0x", ""), "hex")),
        encodedData,
);

// Taken from the ttt test
const bytes = defaultAbiCoder.decode(["bytes"], output)[0];
const computedNextState = defaultAbiCoder.decode([this.abiEncodings.stateEncoding], bytes)[0];
@armaniferrante
Copy link
Owner

pure-evm can only execute evm bytecode that computes pure functions, so the code being executed should never read from or modify state.

Are you trying to read contract storage (considering it's a view function)?

@LayneHaber
Copy link
Author

No, the contract storage isn't being read or modified. It could be a pure function, but it would break the interface we are inheriting.

However, this problem persisted even when I used a pure function. Using ethers v5 and solidity 0.6.4 if that helps

@armaniferrante
Copy link
Owner

Please attach a minimum reproducible example and I'll take a look.

@armaniferrante
Copy link
Owner

armaniferrante commented Jun 10, 2020

Without looking at your code, it seems the problem is that the depth() externality is being called by your code (which would result in the given error, as expected).

I took a quick look at the parity-ethereum/open-ethereum codebase and it looks like it's only called on contract creation or cross contract calls.

So I would guess your bytecode has at least one of those opcodes in it, i.e.,instructions::CREATE | instructions::CREATE2, instructions::CALL | instructions::CALLCODE | instructions::DELEGATECALL | instructions::STATICCALL.

@LayneHaber
Copy link
Author

LayneHaber commented Jun 11, 2020

Sure, here is a simple reproduction. Just install, build, and test and you should see the same failure.

That's interesting. Was chatting with @HeikoFisch about it on our side, and we were thinking it might be due to the usage of sha256 or recover within the pure or view function

@armaniferrante
Copy link
Owner

If I recall correctly, all Ethereum "builtin" functions like sha256 and recover execute as cross contract calls at builtin addresses, which would explain the error you're receiving.

@LayneHaber
Copy link
Author

Yeah, they would appear as contract calls in the compiled code.

So even though this is accepted as pure by the EVM it doesn't look like it will work within this library, bummer! Do you have any plans on supporting these precompile calls in the future?

@armaniferrante
Copy link
Owner

There's currently no plans for feature development on this project, but I filed a new issue to track this feature request. See #7.

@LayneHaber
Copy link
Author

Awesome, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants