2

"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. getting this error how to solve

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

1 Answer 1

6

This is likely due to the recent introduction of a new opcode, PUSH0, as of solc version 0.8.20.

For a full list, see "When did each opcode get added to the EVM?".

Essentially, your version of the solidity compiler is "ahead" of the network that you are attempting to deploy to. In other words, solc outputs bytecode that contains an opcode, but the network does not yet.

You have 3 potential solutions:

  • Wait for your target network to support the new opcode, or use a different network.
    • Since your truffle config indicates that you are connecting to 127.0.0.1:8545, this means that you can upgrade to the latest version of your network running locally (e.g. Ganache) and perhaps that 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 your truffle config file: version: "0.8.19"
    • If the underlying cause of your error was indeed the PUSH0 opcode, this will solve your problem as solc version 0.8.19 does not output this.
  • Continue using the latest solc version, but specify a non-latest target EVM version
    • Update the solc section in your truffle config file to add a new property: settings: { evmVersion: 'london' }
    • Note that 0.8.20 targets evmVersion: 'shanghai' by default, which means it can output PUSH0
    • But if you override it to target evmVersion: 'london' instead, which is the 2nd most recent target EVM version (as of June 2023), you're essentially telling solc to avoid outputting PUSH0.
    • If the underlying cause of your error was indeed the PUSH0 opcode, this will solve your problem as solc has been told not output this.

References:

Assembler: Use push0 for placing 0 on the stack for EVM versions starting from "Shanghai". This decreases the deployment and runtime costs.

solc --evm-version <VERSION> contract.sol

evmVersion: <string> // Default: "istanbul"

Sign up to request clarification or add additional context in comments.

1 Comment

I think this one's correct, before I used solidity version as ^0.8.0. After I changed it as 0.8.19, and it worked

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.