pure-evm icon indicating copy to clipboard operation
pure-evm copied to clipboard

Add ability to execute "builtin" ethereum functions

Open armaniferrante opened this issue 5 years ago • 12 comments

Builtin functions like sha256 and recover execute as cross contract calls to fixed account addresses on Ethereum.

Currently, pure-evm doesn't allow any cross contract calls whatsoever.

It might be desireable to special case "builtin" functions as requested by https://github.com/armaniferrante/pure-evm/issues/6.

armaniferrante avatar Jun 11 '20 01:06 armaniferrante

If we were to implement this ourselves and PR it, how would this work?

ArjunBhuptani avatar Jun 11 '20 09:06 ArjunBhuptani

So all we should need to do is to implement the following I think?

call(...) {
    fn call(
    &mut self,
    _gas: &U256,
    _sender_address: &Address,
    _receive_address: &Address,
    _value: Option<U256>,
    _data: &[u8],
    _code_address: &Address,
    _call_type: CallType,
    _trap: bool,
) -> ::std::result::Result<MessageCallResult, TrapKind> {
    if _receive_address == Address::hex!("0x1") {
        let d = // do ecrecover here
        Ok(MessageCallResult::Success(*gas, ReturnData::&*d))
    } else {
        unimplemented();
    }
}

ArjunBhuptani avatar Jun 11 '20 15:06 ArjunBhuptani

@ArjunBhuptani that seems like a good place to start. You also need to keep track of the callstack depth and implement the depth externality--though you can probably just return a fake constant since it's only used on calls and creates.

armaniferrante avatar Jun 11 '20 17:06 armaniferrante

Ah good tip, thanks!

ArjunBhuptani avatar Jun 11 '20 17:06 ArjunBhuptani

@ArjunBhuptani I updated the repo to use the latest openethereum packages, since everything moved over from paritytech to a new org. You'll want to pull the latest changes.

armaniferrante avatar Jun 11 '20 20:06 armaniferrante

Alright, so working on creating a test that will emulate this properly within our fork. Having a bit of trouble getting the regular node tests passing, are there specific instructions for running those?

LayneHaber avatar Jun 12 '20 02:06 LayneHaber

Right now the workflow is:

  1. cargo build in root and in /wasm
  2. make build in /wasm
  3. yarn install && yarn test in /examples/node

but I seem to be messing things up along the way

LayneHaber avatar Jun 12 '20 02:06 LayneHaber

I seem to be getting this error when trying to merge the browser and node packages: sed: pkg-node/pure-evm_bg.js: No such file or directory

LayneHaber avatar Jun 12 '20 02:06 LayneHaber

Yeah, as soon as I use the imports from ../pkg-node it starts to behave

LayneHaber avatar Jun 12 '20 02:06 LayneHaber

You should only need steps 2 and 3, as listed above.

Maybe you don't have wasm-pack installed (or something else)? pkg-node shouldn't be there if the build was successful.

(To install wasm-pack, you can use the docker container with all the required deps or install it directly with this line https://github.com/armaniferrante/pure-evm/blob/master/.circleci/docker/Dockerfile#L18)

armaniferrante avatar Jun 12 '20 03:06 armaniferrante

yeah there was a different error when I didn't have wasm installed. not sure what's off with my build step quite yet, but found a workaround so i'll probably come back to that later

LayneHaber avatar Jun 12 '20 05:06 LayneHaber

PR: #13

ArjunBhuptani avatar Jun 15 '20 15:06 ArjunBhuptani