“Migrations”在部署过程中触发了无效的操作码。

huangapple go评论112阅读模式
英文:

"Migrations" hit an invalid opcode while deploying

问题

I have suddenly started getting "Migrations" hit an invalid opcode while deploying error when I do 'truffle deploy' command.

My migrations file has not changed so I'm not sure why I am suddenly getting this error. Everyone who's posted for the same error just suggests downloading the latest versions of ganache/truffle, but I have updated my truffle and ganache to the most up-to-date versions and still getting the error.

This is the full error:
⠋ 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);
};

<details>
<summary>英文:</summary>

I have suddenly started getting &#39;&quot;Migrations&quot; hit an invalid opcode while deploying&#39; error when I do &#39;truffle deploy&#39; command.

My migrations file has not changed so I&#39;m not sure why I am suddenly getting this error. Everyone who&#39;s posted for the same error just suggests downloading latest versions of ganache/truffle but I have updated my truffle and ganache to the most up to date versions and still getting the error.

This is the full error:
⠋ Fetching solc version list from solc-bin. Attempt #1
Starting migrations...
======================
&gt; Network name:    &#39;development&#39;
&gt; Network id:      5777
&gt; Block gas limit: 6721975 (0x6691b7)


1_initial_migration.js
======================
⠙ Fetching solc version list from solc-bin. Attempt #1
   Deploying &#39;Migrations&#39;
   ----------------------
 *** Deployment Failed ***st from solc-bin. Attempt #1

&quot;Migrations&quot; hit an invalid opcode while deploying. Try:
   * Verifying that your constructor params satisfy all assert conditions.
   * Verifying your constructor code doesn&#39;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 ***

&quot;Migrations&quot; hit an invalid opcode while deploying. Try:
   * Verifying that your constructor params satisfy all assert conditions.
   * Verifying your constructor code doesn&#39;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, &quot;Restricted to contract owner&quot;);
    _;
}

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



</details>


# 答案1
**得分**: 5

尝试更改您的 pragma 和 solc 版本

Migrations.sol

```solidity
// SPDX-License-Identifier: MIT
pragma solidity >=0.4.22 <0.9.0;

truffle-config.js

compilers: {
    solc: {
        version: "0.5.1"
    }
}

有时,solc 对我来说也可以使用 0.8.20 或 0.7.3。

英文:

Try change your pragma and solc version

Migrations.sol

// SPDX-License-Identifier: MIT
pragma solidity &gt;=0.4.22 &lt;0.9.0;

truffle-config.js

compilers: {
    solc: {
      version: &quot;0.5.1&quot;
}

Sometimes, solc works for me with 0.8.20 or 0.7.3

答案2

得分: 1

将solc版本从0.8.20降级到0.8.13解决了我的问题。

我通过下载metacoin truffle box来获得那个版本,确认迁移正常运作,然后从项目的truffle-config.js中获取solc的版本。

值得做同样的操作,而不是在这个时间点使用特定解决我的问题的solc版本,因为Truffle可能会更新metacoin box,以保持与其余工具的同步,这个版本会变得过时。

下载metacoin truffle box 就像这样简单:

truffle unbox metacoin [PATH/TO/DIRECTORY]
英文:

Downgrading solc from 0.8.20 to 0.8.13 resolved the problem for me.

I got to that version by downloading the metacoin truffle box, verifying that migration works and then getting the version of solc from the truffle-config.js of the project.

It is worth doing the same, rather than using the specific solc version that resolved this for me at this point in time, as Truffle will probably update the metacoin box eventually to keep up with the rest of their tools and this number will get out of date.

Downloading the metacoin truffle box is as easy as this:

truffle unbox metacoin [PATH/TO/DIRECTORY]

答案3

得分: 0

更改truffle-config.js文件中的solc版本如下。

compilers: {
    solc: {
        version: "0.8.13"
    }
}
英文:

Change solc version in the truffle-config.js file as below.

compilers: {
    solc: {
      version: &quot;0.8.13&quot;
    }
  }

答案4

得分: 0

确保你正在使用最新版本的Ganache。尝试使用 npm install -g ganache 全局安装Ganache,这应该会起作用。

英文:

make sure you are using the latest version of ganache. Try installing ganache globally with npm install -g ganche this should work

答案5

得分: 0

将 truffle-config.js 文件中的编译器版本从 0.8.21 更改为 0.8.17 或更低版本。

英文:

Change compiler version 0.8.21 to 0.8.17 or lower in truffle-config.js file.

答案6

得分: 0

我将所有.sol合约版本更改为固定版本,然后错误消失。

pragma solidity ^0.8.20;

英文:

I changed all .sol contract version to fixed version, then the error disappeared.

pragma solidity ^0.8.20;

huangapple
  • 本文由 发表于 2023年6月1日 04:43:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/76377136.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定