在迁移合同时没有燃气

时间:2018-03-04 12:53:11

标签: ethereum

我看了另一个"没有气体" SO帖子,他们还没有解决我的问题。 我正在使用ganache-cli开始

ganache-cli  --account="0xce2ddf7d4509856c2b7256d002c004db6e34eeb19b37cee04f7b493d2b89306d, 2000000000000000000000000000000"
然后我执行

truffle migrate --reset

返回错误

Error encountered, bailing. Network state unknown. Review successful transactions manually.
Error: VM Exception while processing transaction: out of gas

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

truffle.js

module.exports = {
   networks: {
   development: {
   host: "localhost",
   port: 8545,
   network_id: "*",
   gas: 470000
  }
 }
};

1_initial_migration.js

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

module.exports = function(deployer) {
  deployer.deploy(Migrations,  {gas: 4500000});
};

2_deploy_contracts.js

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

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

}

Voting.sol

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

完整错误

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)

3 个答案:

答案 0 :(得分:3)

错误消息是正确的。你没有为合同创造发送足够的天然气。

部署合同时,将为部署的3个不同阶段消耗燃气:

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

总共有357,240气体,因此你的290,000限制太低(Rinkeby的实际合约消耗了351,640气体。再次,我认为小的差异是由于编译器版本输出的轻微差异。我'我不是100%肯定这一点,但差异足够小 - 实际上是28字节的合同代码 - 我没有深入挖掘以找到根本原因。)

关于HackerNoon的一篇很好的文章,通过一个例子详细介绍了每个计算。

答案 1 :(得分:2)

如果其他人遇到错误:

Error encountered, bailing. Network state unknown. Review successful transactions manually.
Error: VM Exception while processing transaction: out of gas

删除./build目录并在truffle.js中启用solc优化器:

module.exports = {
    networks: {
        development: {
            host: "localhost",
            port: 8545, // Using ganache as development network
            network_id: "*",
            gas: 4698712,
            gasPrice: 25000000000
        }
    },
    solc: {
        optimizer: {
            enabled: true,
            runs: 200
        }
    }
};

答案 2 :(得分:0)

我遇到了同样的问题,现在已经解决了。

运行时:VM出现故障时有两种情况-

  1. 使用truffle migrate --reset

    • 确保启用Solidity编译器优化程序。

添加-

solc: {
        optimizer: {
            enabled: true,
            runs: 200
        }
    }

在Windows中的truffle.js和Mac中的truffle-config.js

还请在networks->development中给gasPrice和gasLimit较高,并确保与之匹配 ganache-cli(如果使用)

  1. 在发送事务时或与实体连接时-

当合同在remix上运行正常,但在Runtime上运行truffle时,这是面临的最常见错误

使用web3时,它使用的默认气体为90000,并且某些呼叫失败。因此,每当您发送交易时,请记住要提供足够的电量。

样品

await this.state.instance.methods.sendingTransactionFunction().send({from : this.state.account, gas : 1000000})
相关问题