---
eip: 7843
label: EIP-7843
title: SLOTNUM opcode
status: Last Call
type: Standards Track
category: Core
created: 2024-12-06
official: https://eips.ethereum.org/EIPS/eip-7843
source: https://github.com/ethereum/EIPs/blob/master/EIPS/eip-7843.md
canonical: https://eips.huatuo.xyz/zh/eip-7843/
lang: zh
tldr: 新增 SLOTNUM 操作码（0x4b），返回当前区块的信标链 slot 编号，使合约不再需要硬编码 12 秒的 slot 时长。
tags: [evm, opcode, consensus, slots, core]
---

# EIP-7843: SLOTNUM opcode

> Opcode to get the current slot number

**TL;DR:** 新增 SLOTNUM 操作码（0x4b），返回当前区块的信标链 slot 编号，使合约不再需要硬编码 12 秒的 slot 时长。

## 它解决什么问题？

自 The Merge 以来，每个以太坊区块都属于一个信标链 slot。有些合约关心 slot 编号：例如任何需要与验证者职责、预确认或信标链证明对齐的场景。目前它们有两种选择：

1. **根据 `block.timestamp` 计算**，使用硬编码的创世时间和 slot 时长。成本低，但一旦 slot 时长改变就会悄无声息地失效，而缩短 slot 正在被积极讨论。
2. **通过 calldata 传入 slot**，并针对 [EIP-4788](/eip-4788/) 暴露的信标区块根进行证明。稳健，但成本高，所以人们往往还是选择方案 1。

EIP-7843 通过直接暴露 slot 编号，消除了这一两难。

## 它如何运作

- 新操作码 **`SLOTNUM`，位于 `0x4b`**。它没有输入，压入一个字：当前区块的 slot 编号，类型为 `uint64`。
- gas 成本固定为 **2**，与其他简单的区块上下文操作码（`TIMESTAMP`、`NUMBER` 等）相同。
- 共识层本来就知道 slot，它通过 Engine API 把 slot 传给执行层：新增带有 `slotNumber` 字段的 `ExecutionPayloadV4` 和 `PayloadAttributesV4` 对象，供 `engine_newPayloadV5`、`engine_getPayloadV6` 和 `engine_forkchoiceUpdatedV4` 使用。
- 执行区块头新增 `slotNumber` 字段，因此每个区块都是自包含的。轻客户端和 ZK-EVM 证明者不需要额外输入，就像 `TIMESTAMP` 一样。

在共识层计算该值，可以避免在执行客户端中重复 slot/时间戳逻辑，并为日后更改 slot 时序留出空间。

## 示例代码

一个只返回 slot 编号的函数的原始 EVM 字节码（无需编译器支持即可看清其语义）：

```text
4b        SLOTNUM          // stack: [slot]
5f        PUSH0            // stack: [0, slot]
52        MSTORE           // memory[0..32] = slot
6020      PUSH1 0x20
5f        PUSH0
f3        RETURN           // return 32 bytes
```

执行客户端伪代码：

```python
def slotnum(evm):
    charge_gas(evm, 2)                       # same tier as TIMESTAMP
    evm.stack.push(U256(evm.block_env.slot_number))  # from the block header
```

它所取代的脆弱写法：

```solidity
// Breaks if slot duration changes.
uint256 constant GENESIS = 1606824023;
uint256 constant SLOT_SECONDS = 12;
uint256 slot = (block.timestamp - GENESIS) / SLOT_SECONDS;
```

## 需要注意

- 状态：**Last Call**，规范中的截止日期为 2026-11-01。它尚未 Final，在网络升级将其激活之前，该操作码不会生效。
- 规范中的 Engine API 方法和对象版本号取决于与之同时发布的其他升级，可能会重新编号。
- Solidity 等编译器需要先添加支持，合约才能在不手写字节码的情况下使用它。
- 区块头扩展会影响任何直接对区块头做 RLP 解码的人。


---

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