我看过其他“没油”的帖子,但他们没有解决我的问题。我正在使用 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)
错误信息是正确的。您没有为合约创建发送足够的气体。
部署合约时,将在 3 个不同的部署阶段消耗 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上有一篇很棒的文章,其中详细介绍了每个计算的示例。
| 归档时间: |
|
| 查看次数: |
2567 次 |
| 最近记录: |