迁移合约时天然气耗尽

P H*_*ans 3 ethereum

我看过其他“没油”的帖子,但他们没有解决我的问题。我正在使用 ganache-cli 开始

ganache-cli  --account="0xce2ddf7d4509856c2b7256d002c004db6e34eeb19b37cee04f7b493d2b89306d, 2000000000000000000000000000000"
Run Code Online (Sandbox Code Playgroud)

然后我执行

truffle migrate --reset
Run Code Online (Sandbox Code Playgroud)

它返回一个错误

Error encountered, bailing. Network state unknown. Review successful transactions manually.
Error: VM Exception while processing transaction: out of gas
Run Code Online (Sandbox Code Playgroud)

(完整错误在最后)这些是涉及的文件;

松露.js

module.exports = {
   networks: {
   development: {
   host: "localhost",
   port: 8545,
   network_id: "*",
   gas: 470000
  }
 }
};
Run Code Online (Sandbox Code Playgroud)

1_initial_migration.js

var Migrations = artifacts.require("./Migrations.sol");

module.exports = function(deployer) {
  deployer.deploy(Migrations,  {gas: 4500000});
};
Run Code Online (Sandbox Code Playgroud)

2_deploy_contracts.js

var Voting = artifacts.require("./Voting.sol");

module.exports = function(deployer){
    deployer.deploy(Voting, ['Rama', 'Nick', 'Jose'], {gas: 290000});

}
Run Code Online (Sandbox Code Playgroud)

投票文件

pragma solidity ^0.4.18;

contract Voting {

  mapping (bytes32 => uint8) public votesReceived;
  bytes32[] public candidateList;

  function Voting(bytes32[] candidateNames) public {
    candidateList = candidateNames;
  }

  function totalVotesFor(bytes32 candidate) view public returns (uint8) {
    require(validCandidate(candidate));
    return votesReceived[candidate];
  }

  function voteForCandidate(bytes32 candidate) public {
    require(validCandidate(candidate));
    votesReceived[candidate]  += 1;
  }

  function validCandidate(bytes32 candidate) view public returns (bool) {
    for(uint i = 0; i < candidateList.length; i++) {
      if (candidateList[i] == candidate) {
        return true;
      }
    }
    return false;
   }
}
Run Code Online (Sandbox Code Playgroud)

完全错误

Replacing Migrations...
... 0xaf3b7d40ac17f297a4970b75e1cc55659e86dea3ba7bcf13dd9f82e2b6cf0086
Migrations: 0x1ea6ea9d7528a8ac4b378ae799d2c38fe006b9b6
Saving successful migration to network...
... 0xa8400e873da3cb15719c2c31804ec558e73aa9bfa91c4dc48e922c0ed0db736f
Saving artifacts...
Running migration: 2_deploy_contracts.js
Deploying Voting...
... 0x72947eda435cf854abeeeb5483c9625efad45b664f3bcc7c2085f8aabdbb1076
Error encountered, bailing. Network state unknown. Review successful transactions manually.
Error: VM Exception while processing transaction: out of gas
at Object.InvalidResponse (C:\Users\Paul\AppData\Roaming\npm\node_modules\truffle\build\webpack:\~\web3\lib\web3\errors.js:38:1)
at C:\Users\Paul\AppData\Roaming\npm\node_modules\truffle\build\webpack:\~\web3\lib\web3\requestmanager.js:86:1
at C:\Users\Paul\AppData\Roaming\npm\node_modules\truffle\build\webpack:\~\truffle-migrate\index.js:225:1
at C:\Users\Paul\AppData\Roaming\npm\node_modules\truffle\build\webpack:\~\truffle-provider\wrapper.js:134:1
at XMLHttpRequest.request.onreadystatechange (C:\Users\Paul\AppData\Roaming\npm\node_modules\truffle\build\webpack:\~\web3\lib\web3\httpprovider.js:128:1)
at XMLHttpRequestEventTarget.dispatchEvent (C:\Users\Paul\AppData\Roaming\npm\node_modules\truffle\build\webpack:\~\xhr2\lib\xhr2.js:64:1)
at XMLHttpRequest._setReadyState (C:\Users\Paul\AppData\Roaming\npm\node_modules\truffle\build\webpack:\~\xhr2\lib\xhr2.js:354:1)
at XMLHttpRequest._onHttpResponseEnd (C:\Users\Paul\AppData\Roaming\npm\node_modules\truffle\build\webpack:\~\xhr2\lib\xhr2.js:509:1)
at IncomingMessage.<anonymous> (C:\Users\Paul\AppData\Roaming\npm\node_modules\truffle\build\webpack:\~\xhr2\lib\xhr2.js:469:1)
at emitNone (events.js:91:20)
Run Code Online (Sandbox Code Playgroud)

Ada*_*nis 5

错误信息是正确的。您没有为合约创建发送足够的气体。

部署合约时,将在 3 个不同的部署阶段消耗 gas:

  • 固有气体:这是任何交易中使用的基线数量。对于所有交易,初始成本为 21,000 gas。对于合约创建,还有额外的 32,000。因此,在实际进行任何部署之前,您已经需要 53,000 gas。
  • 构造函数执行:这是用于构造函数执行的操作码的气体。我部署在Rinkeby本合同,你可以看到所有的操作码来构造执行的,和他们的成本,在这里。这部分消耗了 81,040 的气体。
  • 合约代码存储:最后,你有存储合约代码的成本。如果您查看 gas 估算工具,这被称为“代码存款”。对于存储的每个运行时合约代码字节,这将花费 200 gas 。要获取合约代码的大小,请运行solc --optimize Voting.sol --bin-runtime -o .并查看生成的文件的大小。您的合约为 1116 字节(我使用的是 solc 版本 0.4.19,因此您在 .18 上的大小可能略有不同),这导致消耗了 223,200 气体。

总的来说,这是 357,240 gas,所以你的 290,000 限制太低(在 Rinkeby 上运行的实际合约消耗了 351,640 gas。同样,我相信小的差异是由于编译器版本输出的细微差异。我不是 100 % 肯定这一点,但差异足够小 - 实际上是 28 字节的合同代码 - 我没有深入挖掘以找到根本原因)。

HackerNoon上有一篇很棒的文章,其中详细介绍了每个计算的示例。