How to fix "Migration encountered an invalid opcode while deploying" error in Solidity?
P粉523625080
P粉523625080 2024-03-27 00:03:27
0
1
362

'migrates' encountered an invalid opcode while deploying. try:

  • Verify that your constructor parameters satisfy all assertion conditions.
  • Verify that your constructor code does not access the array out of bounds.
  • Add the reason string to your assertion statement. How to solve this error

My migration.sol code

// 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;
      }
    }

My truffle config.js file

    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,
      },
    },
  },
   };

P粉523625080
P粉523625080

reply all(1)
P粉924915787

This may be due to the recent introduction of the new opcode PUSH0 starting with solc version 0.8.20.

For a complete list, see "When was each opcode added to the EVM?" . p>

Essentially, your version of the Solidity compiler is "ahead" of the network you are trying to deploy to. In other words, solc outputs bytecode that contains the opcode, but the network doesn't have it yet.

You have 3 potential solutions:

  • Wait for your target network to support the new opcode, or use a different network.
    • Since your truffle configuration indicates that you are connecting to 127.0.0.1:8545, this means that you can upgrade to the latest version of the network you are running locally (e.g. Ganache), maybe this will solve the problem
    • Downgrade to an earlier version of solc.
    • Change the solc version in your Solidity files: pragma Solidity 0.8.19;
  • Change the solc version in the truffle configuration file: version: "0.8.19"
    • If the root cause of the error is indeed the PUSH0 opcode, this will resolve your issue, as solc version 0.8.19 does not output this.
  • Continue to use the latest solc version, but specify a non-latest target EVM version
    • Update the solc section in the truffle configuration file to add the new property: settings: { evmVersion: 'london' }
    • Note that 0.8.20 targets evmVersion: 'shanghai' by default, which means it can output PUSH0
    • However, if you override it to target evmVersion: 'london', which is the second latest target EVM version (as of June 2023), then you are actually telling solc Avoid output PUSH0.
    • If the root cause of the error is indeed the PUSH0 opcode, this will solve your problem since solc has been told not to output this.

references:

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!