执行truffle deploy时Migrations部署遇无效操作码错误求助
解决Truffle部署Migrations合约时的无效操作码错误
问题详情
执行truffle deploy时触发错误:"Migrations" hit an invalid opcode while deploying,迁移文件未做任何修改,已将Ganache和Truffle更新至最新版本,但问题仍未解决。
完整错误日志
⠋ Fetching solc version list from solc-bin. Attempt #1 Starting migrations... > Network name: 'development' Network id: 5777 Block gas limit: 6721975 (0x6691b7) 1_initial_migration.js ⠙ Fetching solc version list from solc-bin. Attempt #1 Deploying 'Migrations' *** Deployment Failed ***st from solc-bin. Attempt #1 "Migrations" hit an invalid opcode while deploying. Try: - Verifying that your constructor params satisfy all assert conditions. - Verifying your constructor code doesn't access an array out of bounds. - Adding reason strings to your assert statements. Exiting: Review successful transactions manually by checking the transaction hashes above on Etherscan. Error: *** Deployment Failed *** "Migrations" hit an invalid opcode while deploying. Try: - Verifying that your constructor params satisfy all assert conditions. - Verifying your constructor code doesn't access an array out of bounds. - Adding reason strings to your assert statements. at /usr/local/lib/node_modules/truffle/build/webpack:/packages/deployer/src/deployment.js:330:1
环境参数:
- Truffle v5.9.2 (core: 5.9.2)
- Node v18.7.0
涉及代码
Migrations.sol
// SPDX-License-Identifier: MIT pragma solidity ^0.8.9; contract Migrations { address public owner; uint256 public lastCompletedMigration; constructor() { owner = msg.sender; } modifier restricted() { require(msg.sender == owner, "Restricted to contract owner"); _; } function setCompleted(uint256 completed) public restricted { lastCompletedMigration = completed; } function upgrade(address new_address) public restricted { Migrations upgraded = Migrations(new_address); upgraded.setCompleted(lastCompletedMigration); } }
1_initial_migration.js
const Migrations = artifacts.require("Migrations"); module.exports = function(deployer) { deployer.deploy(Migrations); };
排查与修复方案
验证Ganache网络一致性
确认truffle-config.js中development网络的network_id为5777,与Ganache当前网络ID完全匹配;重启Ganache并创建新工作区清除旧链数据,排除链状态异常。对齐Solc编译器版本
在truffle-config.js的compilers字段中明确指定与合约pragma solidity ^0.8.9兼容的版本(如0.8.19):compilers: { solc: { version: "0.8.19" } }执行
truffle clean清除编译缓存后,重新执行truffle compile && truffle deploy。切换Node LTS版本
当前使用的Node v18.7.0为非LTS版本,存在兼容性风险,切换至v18.17.0或v16.x等LTS版本后重试部署。手动指定部署Gas上限
修改迁移脚本,为部署操作指定足够的Gas:module.exports = function(deployer) { deployer.deploy(Migrations, { gas: 6000000 }); };同时确认Ganache的区块Gas限制(当前为6721975)不低于指定值。
重装Truffle
执行npm uninstall -g truffle卸载现有版本,再通过npm install -g truffle重新安装,避免损坏的安装文件导致异常。
内容的提问来源于stack exchange,提问作者Joe Thompson
相关产品推荐
相关产品推荐

