“迁移”在部署时遇到无效的操作码。尝试:
我的migration.sol代码
// SPDX-License-Identifier: UNLICENSED
    //the version of solidity that is compatible
    pragma solidity ^0.8.0;
    contract Migrations {
      address public owner = msg.sender;
      uint public last_completed_migration;
      modifier restricted() {
        require(
          msg.sender == owner,
          "This function is restricted to the contract's owner"
        );
        _;
      }
      function setCompleted(uint completed) public restricted {
        last_completed_migration = completed;
      }
    }
我的松露 config.js 文件
    const path = require("path");
    module.exports = {
      // See <http://truffleframework.com/docs/advanced/configuration>
      // to customize your Truffle configuration!
      contracts_build_directory: path.join(__dirname, "/build"),
  
   
      networks: {
        development: {
        host: "127.0.0.1",
      port: 8545,
      network_id: "*" //Match any network id
     }
    },
    
    plugins: ["truffle-contract-size"],
    compilers: {
    solc: {
      version: "^0.8.0"
    }
    },
    solidity: {
    version: "0.8.3",
    settings: {
      optimizer: {
        enabled: true,
        runs: 1000,
      },
    },
  },
   };
            Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
这可能是由于最近从
solc版本 0.8.20 开始引入了新的操作码PUSH0。有关完整列表,请参阅“每个操作码何时添加到 EVM 中?”。 p>
本质上,您的 Solidity 编译器版本“领先于”您尝试部署到的网络。换句话说,
solc输出包含操作码的字节码,但网络还没有。您有 3 个潜在的解决方案:
127.0.0.1:8545,这意味着您可以升级到本地运行的网络的最新版本(例如 Ganache),也许这可以解决问题solc的早期版本。solc版本:pragma Solidity 0.8.19;solc版本:version: "0.8.19"PUSH0操作码,这将解决您的问题,因为solc版本 0.8.19 不会输出此内容。solc版本,但指定非最新的目标 EVM 版本solc部分以添加新属性:settings: { evmVersion: 'london' }evmVersion: 'shanghai',这意味着它可以输出PUSH0evmVersion: 'london',这是第二个最新的目标 EVM 版本(截至 2023 年 6 月),那么您实际上是在告诉solc避免输出PUSH0。PUSH0操作码,这将解决您的问题,因为solc已被告知不要输出此内容。参考文献: