28

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

7 Answers 7

29

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
    }
1
  • 3
    This should be accepted as correct answer
    – wavekiter
    Feb 9, 2018 at 13:51
3

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.

3

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.

3
  • 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? Nov 13, 2017 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. Nov 14, 2017 at 8:07
  • It turned out this was only a problem with --dry-run Nov 19, 2017 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.

0

It can be a problem with gas limit in Ganache.

Set the gas limit on Ganache:

  • Press the settings icon on the top right;
  • Go to 'chain';
  • Set the gas limit to 7721975 (or more).

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