---
eip: 7519
label: EIP-7519
title: Atomic Storage Operations SCREDIT and SDEBIT
status: Stagnant
type: Standards Track
category: Core
created: 2023-09-16
requires: 2200, 2929
official: https://eips.ethereum.org/EIPS/eip-7519
source: https://github.com/ethereum/EIPs/blob/master/EIPS/eip-7519.md
canonical: https://eips.huatuo.xyz/zh/eip-7519/
lang: zh
tldr: 提议新增两个 EVM 操作码 SCREDIT 和 SDEBIT，一步完成对存储槽的加法或减法并带溢出检查，为并行 EVM 提供用于余额和计数器的安全原语。
tags: [evm, opcodes, storage, parallel-execution, core]
---

# EIP-7519: Atomic Storage Operations SCREDIT and SDEBIT

> Add atomic operations for incrementing and decrementing storage slots

**TL;DR:** 提议新增两个 EVM 操作码 SCREDIT 和 SDEBIT，一步完成对存储槽的加法或减法并带溢出检查，为并行 EVM 提供用于余额和计数器的安全原语。

## 它解决什么问题？

许多链，尤其是 Layer 2，都在尝试并行执行交易。常见做法是乐观并发控制：让交易并排运行，再重新运行那些读取被其他交易写入所破坏的交易。但对于最常见的写操作，即更新余额或计数器，这种做法就失效了。两笔触及同一余额的转账总是冲突，尽管"加 5"和"减 3"可以按任意顺序应用。

EIP-7519 新增了直接表达"向该槽加值"和"从该槽减值"的操作码。由于 EVM 知道意图，并行引擎只需检查在任何排序下该槽都不会上溢或下溢，而不必检查每次读取是否保持不变。

## 它如何运作

两个操作码，每个都从栈中弹出两项（`slot`、`value`），不压入任何内容。操作码编号在规范中仍为 `TBD`。

| 操作码 | 效果 | 失败条件 |
| --- | --- | --- |
| `SCREDIT` | `storage[slot] += value` | 结果将 ≥ 2^256 |
| `SDEBIT` | `storage[slot] -= value` | 结果将 < 0 |

发生上溢或下溢时，该调用帧会**异常终止**（该帧中的全部 gas 被消耗），不像 `ADD`/`SUB` 那样悄悄回绕。

Gas 的收取方式与 **`SSTORE` 完全相同**，包括 [EIP-2929](/eip-2929/) 的 warm/cold 槽规则和 [EIP-2200](/eip-2200/) 的净计量，未来对 `SSTORE` 的任何重新定价也会自动适用。规范认为，额外的工作量（一次加法和一次比较）与存储 I/O 相比可以忽略不计。

只涵盖持久存储。瞬态存储（[EIP-1153](/eip-1153/)）不在其内，因为单笔交易内部不存在并行性。

## 示例代码

该操作码替代了"加载 / 检查 / 存储"这一序列。今天在 Solidity 中是这样：

```solidity
// What SDEBIT(slot, amount) and SCREDIT(slot, amount) do, written with today's opcodes
mapping(address => uint256) internal balanceOf;

function _transfer(address from, address to, uint256 amount) internal {
    balanceOf[from] -= amount;   // SLOAD, SUB with underflow check, SSTORE  -> SDEBIT
    balanceOf[to]   += amount;   // SLOAD, ADD with overflow check,  SSTORE  -> SCREDIT
}
```

规范中的执行伪代码，经过精简：

```python
def scredit(slot, value):
    new = storage[slot] + value
    if new >= 2**256:
        raise ExceptionalHalt("scredit overflow")
    storage[slot] = new

def sdebit(slot, value):
    new = storage[slot] - value
    if new < 0:
        raise ExceptionalHalt("sdebit underflow")
    storage[slot] = new
```

## 需要注意

- 状态为 **Stagnant**。这些操作码尚未分配字节值，也没有被排入任何以太坊升级。
- 规范声明通用的并发原语（锁、信号量）不在范围内；目标是代币余额及类似的计数器。
- 异常终止会消耗当前调用帧中的全部 gas，比 Solidity 带检查算术的 `revert` 更严厉。调用方应当考虑这一点。
- 这个新增的失败点是审计人员在推理重入问题时需要考虑的内容。
- 曾考虑过主要面向 L2，以系统合约而非操作码的方式实现这些操作，但因每次调用所需的代码体积而被否决。


---

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