---
eip: 2003
label: EIP-2003
title: EVMC modules for implementations of precompiled contracts
status: Stagnant
type: Standards Track
category: Interface
created: 2019-05-09
requires: 1352
official: https://eips.ethereum.org/EIPS/eip-2003
source: https://github.com/ethereum/EIPs/blob/master/EIPS/eip-2003.md
canonical: https://eips.huatuo.xyz/zh/eip-2003/
lang: zh
tldr: 规定如何将以太坊预编译合约打包为 EVMC 模块，让客户端可以通过与 EVM 引擎相同的插件 API 加载预编译实现。
tags: [evmc, precompiles, clients, interface]
---

# EIP-2003: EVMC modules for implementations of precompiled contracts

> [EVMC] specifies a generic API for Ethereum execution engines. This EIP specifies a way of providing implementations of Ethereum precompiled contracts using the [EVMC VM API].

**TL;DR:** 规定如何将以太坊预编译合约打包为 EVMC 模块，让客户端可以通过与 EVM 引擎相同的插件 API 加载预编译实现。

## 它解决什么问题？

[EVMC](https://github.com/ethereum/evmc) 是一套 C API，允许以太坊客户端把执行引擎（例如 evmone）作为独立模块接入。预编译合约，即运行哈希、椭圆曲线运算等原生代码的特殊地址，却仍然硬编码在各个客户端中。

EIP-2003 定义了如何把预编译实现也作为 **EVMC 模块**发布。这样，已经支持 EVMC 的客户端无需修改自身代码，就可以换用另一套（或更快的）预编译实现，或者测试 [EIP-1962](/eip-1962/) 等新的预编译。

## 它如何运作

预编译模块是一个普通的 EVMC VM（ABI 版本 6），并遵循几条规则：

1. 在 `get_capabilities()` 中声明 `EVMC_CAPABILITY_PRECOMPILES` 能力。
2. 其 `execute()` 首先校验请求：
   - 消息类型是普通调用（`EVMC_CALL`）；
   - 目标地址位于 [EIP-1352](/eip-1352/) 定义的预编译地址范围内；
   - 不传入代码（`code == NULL`，`code_size == 0`）。
   任何一项检查失败，都返回 `EVMC_REJECTED`。
3. 如果在请求的 EVM 版本下该地址存在预编译，就根据输入计算 gas 成本。若 gas 上限过低，返回 `EVMC_OUT_OF_GAS`；否则执行并返回 `EVMC_SUCCESS`、输出和剩余 gas。
4. 如果该地址没有预编译，则表现得像空代码：返回 `EVMC_SUCCESS`，并退回全部 gas。

它也可以返回 `EVMC_FAILURE`（例如输入无效），或在预编译不应消耗全部 gas 时返回 `EVMC_REVERT`。

重要的是，客户端**无需提供 EVMC Host 接口**：context 参数为 `NULL`，模块不得访问它。预编译从不需要读取或修改状态，因此客户端可以省去完整 EVMC 集成的大部分工作。

## 示例代码

模块 `execute` 的示意骨架（字段名为 EVMC ABI 6 中的名称，后续 EVMC 版本重命名了部分字段）：

```c
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;
}
```

## 需要注意

- **状态：Stagnant。** 它是客户端软件组件之间的接口约定，不改变共识规则，也不改变链上任何可见内容。
- 它绑定于 EVMC ABI 版本 6。较新的 EVMC 版本更改了部分字段名和签名，请确认你所构建的版本。
- 它所依赖的预编译地址范围由 [EIP-1352](/eip-1352/) 定义，而该 EIP 本身也是 Stagnant 状态。
- EVMC 附带 `evmc-vmtester` 工具，可用于检查模块是否符合 API 规范。


---

This is an unofficial plain-language summary. Read the full specification: https://eips.ethereum.org/EIPS/eip-2003
