ref-fvm
ref-fvm copied to clipboard
EVM runtime: handle Ethereum precompiles
Current situation
- Docs on precompiled contracts: https://www.evm.codes/precompiled
- evmodin (implementation that fvm-evm is currently based on) is agnostic of precompiles.
- Akula (Ethereum client using evmodin, built by the same dev) handles them at the client level: https://github.com/akula-bft/akula/blob/c5694cda658880898953187ba52d6249e37527ad/src/execution/evmglue.rs#L303-L330
- evmodin just relays the CALL to the client, who then realises that it’s a static address and handles the native call.
FEVM solution
In FEVM, we will need to handle the precompile inside the EVM actor by:
- Identifying that it's a call to a precompiled contract.
- Parsing the params, like revm does here for ecrecover (for example): https://github.com/bluealloy/revm/blob/main/crates/revm_precompiles/src/secp256k1.rs#L62.
- Calling the appropriate syscall (hash, ecrecover, etc.) -- or performing the work in Wasm if able (e.g. maybe for the identity precompile)
- Forming the return value
- Applying the right Ethereum gas charge
- Handling errors
Precompiled contracts to support
We don't need to honour the Ethereum precompile history. We can introduce all precompiles that exist today.
- [ ] Support Ethereum precompile
0x01: ecrecover- Requires #627
- [ ] Support Ethereum precompile
0x02: sha256- Requires extending the hash syscall to add support for 256
- [ ] Support Ethereum precompile
0x03: ripemd160- Requires the above + adding support for ripemd160 in multihash
- [ ] Support Ethereum precompile
0x04: identity- Can likely be resolved inside Wasm as we use bulk_memory?
- [ ] Support Ethereum precompile
0x05: modexp- TBD
- [ ] Support Ethereum precompile
0x06: ecAdd- TBD
- [ ] Support Ethereum precompile
0x07: ecMul- TBD
- [ ] Support Ethereum precompile
0x08: ecPairing- TBD
- [ ] Support Ethereum precompile
0x09: blake2f- Can likely use the existing hash syscall as-is.
https://github.com/bluealloy/revm has support for precompiles, although they live in a different crate, and we'd have to reimplement their logic so they call a syscall. We have two options:
- Implement all of this in our fork of evmodin
- Switch to revm (and possibly only implement the logic, but not the glue code?)
@Stebalien -- thoughts?
I think we're just going to have to implement them ourselves. We wouldn't get much from REVM because we usually want to lower to FVM syscalls.
Support Ethereum precompile 0x06: ecAdd
(and friends)
Will probably use something like https://github.com/paritytech/bn. At least for the pairing part.
Support Ethereum precompile 0x05: modexp
We'll have to benchmark this. We can use num, but the question is whether it will be faster inside wasm or outside.
I agree with steb
https://github.com/filecoin-project/builtin-actors/pull/564 implements all precompiles, unit tests are to be done before this tracking issue is closed however
Tracking unit tests in https://github.com/filecoin-project/ref-fvm/issues/842, so we can close this issue for the Selenium release.