EIP-2003 · Stagnant · Interface · 2019-05-09
EIP-2003: EVMC modules for implementations of precompiled contracts
Specifies how to package Ethereum precompiled contracts as EVMC modules, so clients can load precompile implementations through the same plug-in API used for EVM engines.
What problem does it solve?
EVMC is a C API that lets an Ethereum client plug in an execution engine (for example evmone) as a separate module. Precompiled contracts, the special addresses that run native code for things like hashing and elliptic-curve math, were still hard-coded in each client.
EIP-2003 defines how a precompile implementation can be shipped as an EVMC module too. A client that already speaks EVMC can then swap in a different or faster set of precompiles, or test new ones such as EIP-1962, without changing its own code.
How it works
A precompiles module is an ordinary EVMC VM (ABI version 6) with a few rules:
- It advertises the
EVMC_CAPABILITY_PRECOMPILEScapability inget_capabilities(). - Its
execute()first validates the request:- the message kind is a plain call (
EVMC_CALL); - the destination is inside the precompile address range defined by EIP-1352;
- no code is passed (
code == NULL,code_size == 0). If any check fails it returnsEVMC_REJECTED.
- the message kind is a plain call (
- If a precompile exists at that address for the requested EVM revision, it computes the gas cost from the input. If the gas limit is too low it returns
EVMC_OUT_OF_GAS; otherwise it runs, returningEVMC_SUCCESS, the output and the gas left. - If no precompile exists there, it behaves like empty code:
EVMC_SUCCESSwith all gas returned.
It may also return EVMC_FAILURE (for example invalid input) or EVMC_REVERT if a precompile should not consume all gas.
Importantly, the client does not need to provide the EVMC Host interface: the context argument is NULL, and the module must not touch it. Precompiles never need to read or change state, so this saves clients most of the work of a full EVMC integration.
Sample code
Illustrative skeleton of a module's execute (field names as in EVMC ABI 6; later EVMC versions renamed some):
static struct evmc_result execute(struct evmc_vm* vm,
const struct evmc_host_interface* host,
struct evmc_host_context* ctx, /* NULL */
enum evmc_revision rev,
const struct evmc_message* msg,
const uint8_t* code, size_t code_size)
{
struct evmc_result res = {.status_code = EVMC_REJECTED};
if (msg->kind != EVMC_CALL || code != NULL || code_size != 0 ||
!in_precompile_range(&msg->destination))
return res;
const struct precompile* p = lookup(&msg->destination, rev);
if (p == NULL) { /* behave like empty code */
res.status_code = EVMC_SUCCESS;
res.gas_left = msg->gas;
return res;
}
int64_t cost = p->gas_cost(msg->input_data, msg->input_size);
if (cost > msg->gas) { res.status_code = EVMC_OUT_OF_GAS; return res; }
res = p->run(msg->input_data, msg->input_size); /* sets output */
res.gas_left = msg->gas - cost;
return res;
}
Things to know
- Status: Stagnant. It is an interface convention between client software components; it does not change consensus rules or anything visible on-chain.
- It is tied to EVMC ABI version 6. Newer EVMC releases changed some field names and signatures, so check the version you build against.
- EIP-1352, which defines the precompile address range it relies on, is itself Stagnant.
- EVMC ships a
evmc-vmtestertool that can check a module for API compliance.