资讯动态

go-ethereum EIP-7918 回归测试详解:用 t8n 验证 Osaka 分叉 Blob Gas 储备价格计算

发布时间:2026/9/18 23:57:07 来源:尧图企业网站定制
go-ethereum EIP-7918 回归测试详解用 t8n 验证 Osaka 分叉 Blob Gas 储备价格计算【免费下载链接】go-ethereumGo implementation of the Ethereum protocol项目地址: https://gitcode.com/gh_mirrors/go/go-ethereum在 go-ethereum 中cmd/evm/testdata/34是evm t8n状态过渡工具的一组测试数据用于验证 Osaka 分叉下 EIP-7918 储备价格reserve price计算在提供了parentBaseFee时能否正确执行——它同时是一则回归测试防止“计算 Blob Gas 时父块头BaseFee为 nil 导致空指针解引用”的缺陷复发。读完本文你可以独立运行这组 t8n 测试、逐字段理解其输入/输出 JSON并对照 EIP-4844 实现 推导出预期 Blob Gas 的完整计算过程。一、测试目标与测试用例入口该测试用例的原始说明位于 cmd/evm/testdata/34/README.md其全部内容为This test verifies that Osaka fork blob gas calculation works correctly when parentBaseFee is provided. It tests the EIP-7918 reserve price calculation which requires parent.BaseFee to be properly set.Regression test for: nil pointer dereference when parent.BaseFee was not included in the parent header during Osaka fork blob gas calculations.归纳起来有两个测试目的功能验证Osaka 分叉激活时若环境输入中带有parentBaseFeet8n 工具必须能正确执行 Blob Gas 计算包括 EIP-7918 引入的储备价格分支回归防护修复前t8n 在构造父块头时漏掉BaseFee字段CalcExcessBlobGas内部访问parent.BaseFee时触发 nil 指针解引用 panic。该用例把这条易错路径固化进持续测试。用例在 cmd/evm/t8n_test.go 的TestT8n中注册约第 299–306 行{ // Osaka test, EIP-7918 blob gas with parent base fee base: ./testdata/34, input: t8nInput{ alloc.json, txs.json, env.json, Osaka, , }, output: t8nOutput{alloc: true, result: true}, expOut: exp.json, },含义是以testdata/34目录下的三个 JSON 文件为输入--state.fork取Osaka将alloc与result两项输出到 stdout并与exp.json做 JSON 级比对。测试执行方式是把编译后的evm以子命令t8n重新执行一遍等价的手动命令为go run ./cmd/evm t8n \ --input.alloc cmd/evm/testdata/34/alloc.json \ --input.txs cmd/evm/testdata/34/txs.json \ --input.env cmd/evm/testdata/34/env.json \ --state.fork Osaka \ --output.body \ --output.alloc stdout \ --output.result stdout二、测试目录结构与各文件解读目录内容如下cmd/evm/testdata/34/ ├── README.md # 用例说明见上文原文 ├── alloc.json # 执行前状态prestate ├── env.json # 块执行环境 ├── txs.json # 交易列表 └── exp.json # 期望输出执行后 alloc 执行结果 result2.1 env.json驱动 Blob Gas 计算的执行环境cmd/evm/testdata/34/env.json 完整内容{ currentCoinbase: 0x0000000000000000000000000000000000000000, currentDifficulty: 0x0, currentRandom: 0x0000000000000000000000000000000000000000000000000000000000000000, currentGasLimit: 0x5f5e100, currentNumber: 0x1, currentTimestamp: 0x1000, parentTimestamp: 0x0, currentBaseFee: 0x10, parentBaseFee: 0x0a, parentGasUsed: 0x0, parentGasLimit: 0x5f5e100, currentExcessBlobGas: 0x0, parentExcessBlobGas: 0x0, parentBlobGasUsed: 0x20000, parentBeaconBlockRoot: 0x0000000000000000000000000000000000000000000000000000000000000000, withdrawals: [] }字段取值与来源结构定义见 stEnv 的 JSON tag字段本用例取值作用currentCoinbase/currentGasLimit/currentNumber/currentTimestamp零地址 /0x5f5e100100M/ 1 /0x1000块上下文t8n 必填项currentDifficulty/currentRandom0x0/ 全零 hash满足合并后post-merge校验difficulty 必须为 0、random 必须提供currentBaseFee0x1016 wei显式给定当前块 base fee给定后 applyLondonChecks 不再用parentBaseFee反推base fee 优先于 parent base feeparentBaseFee0x0a10 wei本用例的关键输入供 EIP-7918 储备价格公式使用parentGasUsed/parentGasLimit0x0/0x5f5e100仅在未显式给出currentBaseFee时参与 EIP-1559 反推currentExcessBlobGas0x0显式给定当前块 excess blob gas因此 BlobBaseFee 直接由它计算1 wei不再走“由父块值推导”的分支parentExcessBlobGas/parentBlobGasUsed0x0/0x20000131072父块 excess 为 0、消耗 2 个 blob 的 gas是 EIP-7918 公式的自变量parentBeaconBlockRoot全零 hash满足 Cancun 之后parentBeaconBlockRoot必填校验withdrawals[]满足 Shanghai 之后withdrawals必须存在的校验可以看到这份 env 一次性覆盖了 London/Shanghai/Merge/Cancun 四道前置检查对应 transition.go 中applyLondonChecks、applyShanghaiChecks、applyMergeChecks、applyCancunChecks使执行顺利推进到 Osaka 的 Blob Gas 计算环节。2.2 alloc.json 与 txs.json最简化的状态与空交易集cmd/evm/testdata/34/alloc.json 定义执行前状态共 3 个账户0xa94f5374fce5edbc8e2a8697c15331677e6ebf0b1 ETH 余额、nonce 0仅作为资金账户0x00000961...7002、0x0000bbdd...7251各携带一段部署好的合约字节码EIP-7702 风格授权码模式与本用例前一编号 testdata/33 相同余额 0、nonce 1。cmd/evm/testdata/34/txs.json 是空数组[]。交易集为空是本用例的设计要点不需要执行任何交易就能强制走通 Blob Gas 计算路径——excessBlobGas/BlobBaseFee在进入交易循环之前就已计算若parent.BaseFee为 nilpanic 会在执行交易前发生。同时空交易集使执行后的状态与执行前完全一致便于核对stateRoot。2.3 exp.json期望输出cmd/evm/testdata/34/exp.json 包含alloc执行后状态与result执行结果两部分。关键期望值result: { stateRoot: 0x483ed12ebbb83ddc8d5c6de80d5cb352580800285a5a141697bd922cac1d5a49, txRoot: 0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421, receiptsRoot: 0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421, gasUsed: 0x0, currentBaseFee: 0x10, withdrawalsRoot: 0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421, currentExcessBlobGas: 0x0, blobGasUsed: 0x0, requestsHash: 0xe3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855, requests: [] }stateRoot与输入 alloc 计算出的根一致印证空交易集不改变状态txRoot/receiptsRoot/withdrawalsRoot均为空集合哈希currentExcessBlobGas为0x0env 已显式给出该字段工具按给定值回显不重新推导requestsHash为 requests 空列表的哈希对应 Prague 之后 EIP-7002 的请求处理路径。三、EIP-7918 储备价格公式与数值推演Osaka 分支下的核心实现在 consensus/misc/eip4844/eip4844.go 的calcExcessBlobGas约第 138–169 行// EIP-7918 (post-Osaka) introduces a different formula for computing excess, // in cases where the price is lower than a reserve price. if isOsaka { var ( baseCost big.NewInt(params.BlobBaseCost) reservePrice baseCost.Mul(baseCost, parent.BaseFee) // (BlobBaseCost * parentBaseFee)^2 blobPrice bcfg.blobPrice(parentExcessBlobGas) ) if reservePrice.Cmp(blobPrice) 0 { scaledExcess : parentBlobGasUsed * uint64(bcfg.Max-bcfg.Target) / uint64(bcfg.Max) return parentExcessBlobGas scaledExcess } } // Original EIP-4844 formula. return excessBlobGas - targetGasreservePrice的计算直接解引用parent.BaseFee*big.Int这就是回归测试所指的空指针来源。相关常量定义在 params/protocol_params.go约第 200–206 行BlobTxBlobGasPerBlob 1 1765536单个 blob 的 gas 与字节数、BlobTxMinBlobGasprice 1、BlobBaseCost 1 138192单个 blob 的基础执行 gas 成本。用本用例的 env 数值代入Osaka fork 的 blob 调度取当前已配置的 Prague 档Target 6、Max 9见 tests/init.go 第 413–438 行的 Osaka 链配置parentExcessBlobGas 0parentBlobGasUsed 0x20000 1310722 个 blobtargetGas 6 × 65536 393216excessBlobGas 0 131072 393216不满足下限继续走 EIP-7918 分支reservePrice (8192 × 10)² 82_944_000_000weiparentBaseFee 0x0a 10blobPrice blobBaseFee(0) × 65536 1 × 65536 65_536weiexcess 为 0 时fakeExponential输出BlobTxMinBlobGasprice 1reservePrice blobPrice成立进入新公式scaledExcess 131072 × (9 − 6) / 9 43690返回0 43690 43690。即 Osaka 规则下父块若已低于目标水位excess 不再简单清零而是按“超配比例Max−Target / Max”保留一部分压力使 blob 价格维持在储备价格附近。同一公式在 consensus/misc/eip4844/eip4844_test.go 的TestCalcExcessBlobGasEIP7918第 190 行起还有两组参数化单测低于储备价格时parentBaseFee 1 gwei期望blobGasTarget * 3 / 9高于储备价格时parentBaseFee 1期望回落为 0与 testdata/34 的用例互为印证。四、t8n 工具侧调用链parentBaseFee 如何抵达 EIP-7918 公式从源码结构看parentBaseFee的传递链为解析 envTransition 读取--input.env反序列化为stEnvJSON 映射由 gencodec 生成见 gen_stenv.go其中ParentBaseFee *math.HexOrDecimal256对应parentBaseFee,omitempty随后按--state.fork构造链配置本用例为 tests.GetChainConfig(Osaka)OsakaTime 0即从块 0 起全部激活构造父块头在 Apply约第 209–238 行中当 env 未显式给出currentExcessBlobGas时会构造如下父块头再调用计算parent : types.Header{ Time: pre.Env.ParentTimestamp, ExcessBlobGas: pre.Env.ParentExcessBlobGas, BlobGasUsed: pre.Env.ParentBlobGasUsed, BaseFee: pre.Env.ParentBaseFee, // 回归修复点必须带上 parentBaseFee SlotNumber: pre.Env.SlotNumber, } ... excessBlobGas eip4844.CalcExcessBlobGas(chainConfig, parent, header.Time) vmContext.BlobBaseFee eip4844.CalcBlobFee(chainConfig, header)若此处遗漏BaseFeecalcExcessBlobGas中baseCost.Mul(baseCost, parent.BaseFee)即触发 nil 指针解引用——这正是 testdata/34 README 所描述的回归缺陷进入 EIP-7918 分支CalcExcessBlobGas 先用config.IsOsaka(config.LondonBlock, headTimestamp)判定分叉定义见 params/config.go 第 809–812 行基于OsakaTime时间戳判断再按上文第三节的公式计算结果回写计算出的excessBlobGas与BlobBaseFee写入 EVM 上下文参与交易执行并回填到 ExecutionResult 的currentExcessBlobGas/blobGasUsed字段本用例分别为0x0因 env 显式给定了 excess。此外Osaka fork 在主网/测试网的切换时间戳定义于 params/config.go如 mainnetOsakaTime: 1764798551后续 BPO1 起的 blob 参数升级DefaultBPO1BlobConfig等第 349–354 行Target 10、Max 15通过BlobScheduleConfig在latestBlobConfig中按时间选取说明该测试覆盖的是调度中“当前档”参数的通用逻辑而非某次参数升级的特例。五、小结这组回归测试守护了什么一条易错路径t8n 工具构造父块头时必须携带BaseFee否则 Osaka 分叉的 Blob Gas 计算会 panic。TestT8n以 JSON 深度比对的方式持续验证该路径见 cmd/evm/t8n_test.go一个共识公式EIP-7918 的储备价格与scaledExcess计算在 consensus/misc/eip4844/eip4844.go 中实现testdata/34 的用例数值parentBaseFee10使reservePrice远高于blobPrice恰好覆盖新公式分支一份可复现的契约alloc.jsonenv.jsontxs.json→exp.json的完整输入输出对使任何修改 Blob Gas 逻辑、EIP-4844 调度参数或 Osaka 分支条件的开发者都能用一条evm t8n命令快速回归验证无需启动完整节点。【免费下载链接】go-ethereumGo implementation of the Ethereum protocol项目地址: https://gitcode.com/gh_mirrors/go/go-ethereum创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考

读完文章,也想定制专属网站?

尧图设计师 24 小时内与您沟通定制方案

免费获取报价