19

I'm trying to deploy a sample contract (following this guide) but I'm receiving an error of not enough gas. I changed the numbers a little but could not fix it. I saw this question in stack exchange but there are few things I don't understand in the answer suggested there:

  • What does it mean to modify the genesis block as follows?

    "You can modify your genesis block to have a gas limit of 0x2fefd8 which is the same as the main network (3,141,592 gas). You will also need to reset that test chain since you'll be changing the genesis block."

  • In the js files I print below there are two "gas" parameters: one in 2_deploy_contracts.js and the other in truffle.js. What each of them means (how do they affect the deployment of a contract)? Are there any other places that relate to amount of gas issues?

My error and file contents:

~/voting$ truffle deploy
Using network 'development'.

Running migration: 1_initial_migration.js
  Deploying Migrations...
Error encountered, bailing. Network state unknown. Review successful transactions manually.
Error: exceeds block gas limit
    at Object.InvalidResponse (/usr/lib/node_modules/truffle/build/cli.bundled.js:37022:16)
    at /usr/lib/node_modules/truffle/build/cli.bundled.js:209743:36
    at XMLHttpRequest.request.onreadystatechange (/usr/lib/node_modules/truffle/build/cli.bundled.js:208522:13)
    at XMLHttpRequestEventTarget.dispatchEvent (/usr/lib/node_modules/truffle/build/cli.bundled.js:210395:18)
    at XMLHttpRequest._setReadyState (/usr/lib/node_modules/truffle/build/cli.bundled.js:210685:12)
    at XMLHttpRequest._onHttpResponseEnd (/usr/lib/node_modules/truffle/build/cli.bundled.js:210840:12)
    at IncomingMessage.<anonymous> (/usr/lib/node_modules/truffle/build/cli.bundled.js:210800:24)
    at emitNone (events.js:91:20)
    at IncomingMessage.emit (events.js:185:7)
    at endReadableNT (_stream_readable.js:974:12)

migrations/1_initial_migration.js

var Migrations = artifacts.require("./Migrations.sol");
module.exports = function(deployer) {
  deployer.deploy(Migrations);
};

migrations/2_deploy_contracts.js

var Voting = artifacts.require("./Voting.sol");
module.exports = function(deployer) {
  deployer.deploy(Voting, ['Rama', 'Nick', 'Jose'], {gas: 5000000});
};

truffle.js require('babel-register')

module.exports = {
  networks: {
    development: {
      host: 'localhost',
      port: 8545,
      network_id: '*', // Match any network id
      // gas:400000000000,
      gas:1000258612000000000,
      from: "0xf212bb926f7a831ff745e4236fc704a9947de77c"
    }
  }
}
25

Inside truffle.js, add

,gas: 4600000

Don't forget the little "," so it looks like

  networks: {
    development: {
      host: "localhost",
      port: 8545,
      network_id: "*",
      gas: 4600000
    }
  • 2
    This should be accepted as correct answer – wavekiter Feb 9 '18 at 13:51
2

Either your contract is entirely too large, or your truffle configuration is bunk.

gas:1000258612000000000

and

{gas: 5000000}

These are invalid gas values. The max is around 4m, so there's just no way that is a valid value.

Go back to your commented out one, and if you still see the error, it's likely that your contract is too large and should probably be broken up into libraries. A good way to check is to look at the "creation" gas cost with solc --gas path/to/contract.sol.

What does it mean to modify the genesis block as follows?

That is only if you're running a test network, so ignore it unless your project exists solely on a private chain.

In the js files I print below there are two "gas" parameters: one in 2_deploy_contracts.js and the other in truffle.js. What each of them means (how do they affect the deployment of a contract)? Are there any other places that relate to amount of gas issues?

The first one in deployer.deploy is just the maximum gas value you are allowing for the transaction. This is also above the block limit of 4m, so fix that too.

The second one, looks like a description of the network(I'm not completely sure with truffle). This tells truffle what the network's maximum gas value is. not sure where to find the most current limit, but I've been running my tests with a 4,000,000 limit. According to ethstats.net that might be up to about 6m now.

2

In my case I used a genesis block with a very low gas limit:

"gasLimit": "2100000"

By specifying a gas amount slightly below this amount in truffle.js solved the problem for me. Here is my network definition in truffle.js:

"private": {
    network_id: 15,
    host: "localhost",
    port: 8545,
    gas: 2000000
},
1

I also faced the similar issue. The version of truffle I was using was Truffle v4.0.1 (core: 4.0.1). If you downgrade to beta version things will work just fine. 1. npm uninstall -g truffle 2. truffle version(to check if its uninstalled) 3. npm install -g truffle@beta

You are good to go. This should fix the problem.

  • I just downgraded and I still can't get Truffle to listen to my "gasPrice" field - is there something beyond just changing that field and running truffle compile I should be doing? – Tommy Nicholas Nov 13 '17 at 19:13
  • I hope you are running truffle migrate after running truffle compile. Also you can try putting the gas value as the maximum value which is 4million. – Neha Kumari Nov 14 '17 at 8:07
  • It turned out this was only a problem with --dry-run – Tommy Nicholas Nov 19 '17 at 21:11
0

I am also following the same guide and I had the same issue. In the truffle.js, I gave 4700000 for the gas. Also in the migration file(the second file), I changed gas to 4700000 like this:

var Voting = artifacts.require("./Voting.sol");
module.exports = function(deployer) {
  deployer.deploy(Voting, ['Rama', 'Nick', 'Jose'], {gas: 4700000});
};

I run with truffle migrate --reset and my problem has been solved.

0

For those of you that non of these helped, I deleted everything and set it up again (following the mentioned guide), set up gas in truffle config and it worked. I used three nodes.

Thing is that my gasLimit was stuck to 5003 (regardless of starting node with --targetgaslimit after removing complete chain data) and I guess it is because of dynamic gas limit calculation the nodes use so it decreased because of network inactivity. It doesn't make sense because once the whole blockchain is deleted, this should reset. But for some reason it was stuck at that value.

After this, you just need to check current gas limit

geth attach <path_to_node_dir>/geth.ipc

and, once connected, check it via

web3.eth.getBlock("pending").gasLimit

and then make sure to set gas param in your truffle config file. After this, migration worked.

protected by Community Jul 22 '18 at 18:58

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

Not the answer you're looking for? Browse other questions tagged or ask your own question.