作者:Henry 系列:《深入理解区块链 Gas 机制》 · 第 6 篇 受众:Web3 开发者 / Solidity 工程师 / 区块链学习者
一、交易失败的常见原因分类类别描述
gas 设置不足
设置的 gasLimit 不足或 baseFee + tip 不足
调用逻辑失败
外部合约 revert、require 检查失败
权限或数据异常
msg.sender 不匹配、无权限、参数非法
合约缺陷
重入攻击、整数溢出、错误分支逻辑等
兼容性问题
ABI 解码失败、EVM 版本不兼容
二、错误信息该怎么看?(以 Etherscan 为例)示例:
Fail with error 'Ownable: caller is not the owner'
说明调用了 OpenZeppelin Ownable 模块的 onlyOwner 修饰器,当前账户无权限。
提示:三、Hardhat 本地重现失败交易步骤:获取交易哈希(txHash)使用 hardhat_fork 功能 fork 对应区块高度编写脚本模拟相同调用用 console.log 或 debug 调试示例代码:
// hardhat.config.ts
forking: {
url: "https://eth-mainnet.alchemyapi.io/v2/xxx",
blockNumber: 19119119
}
// 调试脚本
const contract = await ethers.getContractAt("MyContract", address)
await contract.connect(attacker).doSomething(params)
四、使用 Tenderly 回溯调用栈
Tenderly 可以做到:
五、Gas 设置失败的场景排查场景排查建议
gas limit 太低
提高限制或使用 estimateGas()
maxFee 太低被淘汰
动态设置 baseFee + tip
simulate 成功但上链失败
检查链上下文是否一致(nonce、状态)
fallback 调用失败
fallback/receive 函数异常
六、Gas 优化建议实践七、结语:调试 ≠ 编程辅助,是链上信任基础
掌握失败交易调试技巧,能帮助你: