Ethereum 在迁移合同时气体耗尽

Ethereum 在迁移合同时气体耗尽,ethereum,Ethereum,我已经看过了其他“没油”的帖子,但它们并没有解决我的问题。 我使用的是从开始的ganache cli ganache-cli --account="0xce2ddf7d4509856c2b7256d002c004db6e34eeb19b37cee04f7b493d2b89306d, 2000000000000000000000000000000" 然后我执行 truffle migrate --reset 它返回一个错误 Error encountered, bailing. Networ

我已经看过了其他“没油”的帖子,但它们并没有解决我的问题。 我使用的是从开始的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
(完全错误在末尾) 这些是涉及的文件

块菌

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

}
投票权。索尔

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;
   }
}
pragma-solidity^0.4.18;
合同投票{
映射(bytes32=>uint8)公共votesReceived;
bytes32[]公共候选人名单;
功能投票(BYTES3[]候选名称)公共{
候选名单=候选名单;
}
函数totalVotesFor(字节32候选)查看公共返回(uint8){
要求(有效期(候选人));
已收到[候选人]的回信;
}
函数voteForCandidate(bytes32候选)公共{
要求(有效期(候选人));
votesReceived[候选者]+=1;
}
函数validCandidate(bytes32候选)查看公共回报(bool){
对于(uint i=0;i
完全错误

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)
正在替换迁移。。。
... 0xaf3b7d40ac17f297a4970b75e1cc55659e86dea3ba7bcf13dd9f82e2b6cf0086
迁移:0x1ea6ea9d7528a8ac4b378ae799d2c38fe006b9b6
正在保存成功迁移到网络。。。
... 0xa8400e873da3cb15719c2c31804ec558e73aa9bfa91c4dc48e922c0ed0db736f
正在保存工件。。。
正在运行迁移:2_deploy_contracts.js
正在部署投票。。。
... 0x72947eda435cf854abeeeb5483c9625efad45b664f3bcc7c2085f8aabdbb1076
遇到错误,正在清除。网络状态未知。手动查看成功的交易记录。
错误:处理事务时VM异常:气体不足
在Object.InvalidResponse(C:\Users\Paul\AppData\Roaming\npm\node\u modules\truffle\build\webpack:\~\web3\lib\web3\errors.js:38:1)
在C:\Users\Paul\AppData\Roaming\npm\node\u modules\truffle\build\webpack:\~\web3\lib\web3\requestmanager.js:86:1
在C:\Users\Paul\AppData\Roaming\npm\node\u modules\truffle\build\webpack:\~\truffle migrate\index.js:225:1
在C:\Users\Paul\AppData\Roaming\npm\node\u modules\truffle\build\webpack:\~\truffle provider\wrapper.js:134:1
在XMLHttpRequest.request.onreadystatechange(C:\Users\Paul\AppData\Roaming\npm\node\U modules\truffle\build\webpack:\~\web3\lib\web3\httpprovider.js:128:1)
在XMLHttpRequestEventTarget.dispatchEvent(C:\Users\Paul\AppData\Roaming\npm\node\U modules\truffle\build\webpack:\~\xhr2\lib\xhr2.js:64:1)
在XMLHttpRequest.\u setReadyState(C:\Users\Paul\AppData\Roaming\npm\node\u modules\truffle\build\webpack:\~\xhr2\lib\xhr2.js:354:1)
在XMLHttpRequest.\u onHttpResponseed(C:\Users\Paul\AppData\Roaming\npm\node\u modules\truffle\build\webpack:\~\xhr2\lib\xhr2.js:509:1)
在收到消息时。(C:\Users\Paul\AppData\Roaming\npm\node\u modules\truffle\build\webpack:\~\xhr2\lib\xhr2.js:469:1)
在emitNone(events.js:91:20)

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

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

  • 固有气体:这是任何交易中使用的基准金额。对于所有交易,初始成本为21000燃气。对于合同创建,还有32000个。因此,在实际部署之前,您已经准备好了53000汽油
  • 构造函数执行:这是用于构造函数执行的操作码的gas。我在Rinkeby上部署了这个契约,您可以看到构造函数执行的所有操作码,以及它们的成本。这部分消耗了81040的汽油
  • 合同代码存储:最后,您需要支付存储合同代码的费用。如果您查看气体估算工具,这被称为“代码存款”。存储的运行时合同代码的每个字节需要200 gas。要获得合同代码的大小,请运行
    solc--optimize Voting.sol--bin runtime-o.
    并查看结果文件的大小。您的合同是1116字节(我使用的是solc版本0.4.19,因此您在.18上的大小可能略有不同),这导致消耗223200个气体
总的来说,这是357240汽油,所以你的290000限制太低了(在Rinkeby上运行的实际契约消耗了351640个gas。同样,我认为小的差异是由于编译器版本输出的细微差异。我不是100%确定这一点,但差异足够小——实际上是28字节的契约代码——我没有深入挖掘以找到根本原因)


这里有一篇很棒的文章,用一个例子详细介绍了每一次计算。

如果有人发现错误:

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

我也有同样的问题,现在已经解决了

运行时:VM耗尽气体可能出现两种情况-

  • 使用
    truffle migrate--reset时

    • 确保启用solidity编译器的Optimizer
  • 加-

    在windows的
    truffle.js
    和mac的
    truffle config.js

    此外,在
    网络->开发中
    请给出较高的gasPrice和gasLimit,并确保将其与
    ganache cli
    匹配(如果使用)

  • 发送交易时或与solidity连接时-
  • 这是最常见的错误,当contract在
    remix
    上正常运行,但在
    truffle

    当使用
    web3
    时,它使用的默认气体是90000,有些调用失败。因此,无论何时发送事务,请记住提供足够的气体

    样本

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

    您的回复质量非常好。感谢您添加此评论。我完全忘记了在启用优化器后我必须删除构建文件夹,您为我节省了一个头疼的lol+1
    await this.state.instance.methods.sendingTransactionFunction().send({from : this.state.account, gas : 1000000})