教程区块链区块链技术ch077.5 Gas 计费机制与 EIP-1559

本页目录

Gas 在以太坊中不是燃料的隐喻,而是去中心化计算的通用定价机制。它解决了两个根本问题:(1) 防止无限循环和 DoS 攻击——每个操作都有可量化成本;(2) 在资源稀缺时提供公平分配的市场机制。EIP-1559 将这套市场从"一价拍卖"改造为"双因素定价",是区块链经济设计史上最重要的实验之一。


7.5.1 从指令成本到交易定价

EVM 中每个操作码消耗特定量的 Gas。一笔交易的总成本:

Total CostETH=GasUsed×GasPrice\text{Total Cost}_{\text{ETH}} = \text{GasUsed} \times \text{GasPrice}

其中 GasUsed 是该交易实际执行过程中所有操作码的累计成本。GasPrice 由发送者在交易签名时自行设定。

常见 Gas 基准值(伦敦升级后)

操作Gas 消耗说明
交易基础开销 TXBASE21,000每笔交易最低成本
SSTORE(首次写入非零)20,000最昂贵操作,因为写磁盘
SSTORE(修改已有)5,000覆盖写入成本
SLOAD100(热)/ 2,100(冷)状态访问成本,分冷热状态
CREATE32,000 + bytecost(200/gas)部署新合约
ECRECOVER (预编译)3,000签名恢复
内存扩展mem = 3 × words + words² / 512非线性二次增长

7.5.2 Pre-EIP-1559:首价拍卖的缺陷

在 EIP-1559 之前,以太坊采用首价拍卖(First-Price Auction)

Fee=GasUsed×GasPricebid\text{Fee} = \text{GasUsed} \times \text{GasPrice}_{\text{bid}}

用户需要猜测当前网络的"合理价格"。这种机制的问题:

  1. 价格波动极大:高峰时 1 Gwei,拥堵时飙升至 200+ Gwei;
  2. 过高出价浪费:用户为保险出高价,实际只需出最低价即可纳入;
  3. 矿工可操纵:矿工可将自有交易(MEV)优先,甚至留空区块迫使价格下降;
  4. 用户体验差:钱包无法准确估计"当前合理价格"。

7.5.3 EIP-1559:双因素定价模型

EIP-1559(伦敦升级,2021 年 8 月 5 日)引入了一套全新的交易格式和定价机制。

三个新参数

参数说明控方
BaseFee协议自动计算的基础费用,被销毁协议/算法
MaxPriorityFee用户给矿工/验证者的"小费"用户
MaxFeePerGas用户愿意支付的最高单价上限用户

实际支付单价

GasPriceeffective=min(BaseFee+MaxPriorityFee, MaxFeePerGas)\text{GasPrice}_{effective} = \min\left(\text{BaseFee} + \text{MaxPriorityFee}, \ \text{MaxFeePerGas}\right)

总费用拆分

Total=GasUsed×BaseFee被销毁(通缩压力)+GasUsed×PriorityFee给验证者(激励)\text{Total} = \underbrace{\text{GasUsed} \times \text{BaseFee}}_{\text{被销毁(通缩压力)}} + \underbrace{\text{GasUsed} \times \text{PriorityFee}}_{\text{给验证者(激励)}}

BaseFee 自动调节算法

协议设定每区块 15M Gas 的目标使用量,最大上限 30M。

BaseFeen+1=BaseFeen×(1+GasUsednGasTargetGasTarget×18)\text{BaseFee}_{n+1} = \text{BaseFee}_n \times \left(1 + \frac{\text{GasUsed}_n - \text{GasTarget}}{\text{GasTarget}} \times \frac{1}{8}\right)
  • 若区块使用 15M(目标值)→ BaseFee 不变;
  • 若区块使用 30M(满)→ BaseFee 上升 12.5%;
  • 若区块使用 0M(空)→ BaseFee 下降 12.5%。
graph TD
    A[GasUsed=10M<br>&lt;15M] -->|BaseFee × 0.875| B[BaseFee 下降 12.5%]
    C[GasUsed=15M<br>=目标] -->|BaseFee 不变| D[稳定]
    E[GasUsed=30M<br>满] -->|BaseFee × 1.125| F[BaseFee 上升 12.5%]
    D --> G[用户 MaxPriorityFee 小费竞争纳入]
    style B fill:#ccffcc
    style F fill:#ffcccc

7.5.4 "超声货币"叙事:销毁的通缩引擎

EIP-1559 的 BaseFee 销毁使得以太坊每天的销毁量可预测。在繁忙时期(如 NFT 发行、DeFi 热潮),销毁速度可超过区块奖励的发行速度:

Net Issuance=Block RewardsBaseFee Burn\text{Net Issuance} = \text{Block Rewards} - \text{BaseFee Burn}

BaseFee Burn>Block Rewards\text{BaseFee Burn} > \text{Block Rewards} 时,ETH 总供应量负增长,形成通缩。

ts
// eip1559-basefee-sim.ts
// 纯内置:模拟 EIP-1559 BaseFee 调节与销毁逻辑

function simulateBaseFee(
  initialBaseFee: bigint,
  blockGasUsed: bigint[],
  gasTarget: bigint
): { baseFees: bigint[]; totalBurn: bigint } {
  let baseFee = initialBaseFee;
  const baseFees: bigint[] = [baseFee];
  let totalBurn = 0n;

  for (const gas of blockGasUsed) {
    const delta = gas - gasTarget;
    // 每 1/8 的调整系数
    const adjustment = 1n + (delta * 1n) / (gasTarget * 8n);
    // 整数运算模拟
    baseFee = (baseFee * adjustment) / 1n;
    if (baseFee < 1n) baseFee = 1n;
    baseFees.push(baseFee);
    totalBurn += gas * baseFee;
  }
  return { baseFees, totalBurn };
}

// 模拟 5 个区块:15M → 20M → 30M → 15M → 5M
const gasUsed = [15_000_000n, 20_000_000n, 30_000_000n, 15_000_000n, 5_000_000n];
const result = simulateBaseFee(1_000_000_000n, gasUsed, 15_000_000n);
console.log('BaseFee 序列 (Gwei):');
result.baseFees.forEach((f, i) => console.log(`  Block i:{i}:{Number(f) / 1e9} Gwei`));
console.log(`总计销毁: ${result.totalBurn / 1e18n} ETH-equivalent units`);
// 输出:拥堵时上升,空闲时稳定/下降,验证 BaseFee 的动态调节

7.5.5 交易池选择与排序

在 EIP-1559 下,交易池按 有效 Gas 价格 排序:

sortKey=min(maxFeePerGas, baseFee+maxPriorityFeePerGas)\text{sortKey} = \min\left(\text{maxFeePerGas}, \ \text{baseFee} + \text{maxPriorityFeePerGas}\right)
graph LR
    A[用户提交交易] --> B[验证 MaxFeePerGas >= BaseFee]
    B --> C[加入 mempool]
    C --> D[按有效价格降序排序]
    D --> E[区块构建器选择前 N 笔<br>至 gas limit]
    E --> F[执行 + 纳入区块]
    style E fill:#ccffcc

这与 PoW/PoS 的区块构建者(Builder)角色紧密相关。在 PBS(Proposer-Builder Separation)下,Builder 专门优化交易排序以捕获 MEV(最大可提取价值),验证者仅选择最高价值区块头。


关键认知四:EIP-1559 不是简单的"降价改革",而是将交易定价权从矿工/验证者手中部分收归协议。BaseFee 的可预测性降低了用户猜测成本,销毁机制将使用价值回流到全体 ETH 持有者,而 PriorityFee 仍保留激励验证者的经济杠杆。


← 7.4 EVM 执行模型 | 前往 → 7.6 预编译合约与扩展

评论

0

评论加载中…

发表评论

0/2000