[Ethereum] Running out of gas during a deploy due to a large number of require() statements

errorgasout-of-gassoliditytruffle-migration

I have a smart contract that has a fair number of functions, but a large number of require() statements because I carefully check every incoming parameter and some intermediate calculation results too. I've been fighting an out-of-gas error during my Truffle 5.0 beta migrations for a long time now with this contract.

On a hunch, I did a search-and-replace throughout the contract that commented out every require() statement. The out-of-gas error went away and I could deploy successfully.

If I use the solc optimizer I can deploy successfully with the require() statements intact. But then I can't use the Truffle debugger since currently it can't coexist with optimized code, and I really need the debugger.

I thought it might be due to the long strings I was using as my "revert messages" for the require() statements, so I radically cut down their size, but the error still occurred. Then I tried changing them all to empty strings but still got the error. It really appears to be the sheer number of require() statements that is causing the out-of-gas error but only when I don't use the optimizer.

Has anyone experience this and can suggest a technique to get the gas low enough that I can deploy. Note I am sending an extremly high amount of gas with my migration, just below the Block Limit for the Ganache client.

2_deploy_ebb.js
===============

   Replacing 'EtherBandBattlesManager'
   -----------------------------------
Error:  *** Deployment Failed ***

"EtherBandBattlesManager" ran out of gas (using a value you set in your network config or deployment parameters.)
   * Block limit:  100000000
   * Gas sent:     99999999

    at ./.nvm/versions/node/v8.11.1/lib/node_modules/truffle/build/webpack:/packages/truffle-deployer/src/deployment.js:364:1
    at <anonymous>
    at process._tickCallback (internal/process/next_tick.js:188:7)
Truffle v5.0.0-beta.2 (core: 5.0.0-beta.2)
Solidity v0.5.0 (solc-js)
Node v8.11.1

Best Answer

I was made aware of a flag in the Ganache command line client that allows unlimited contract sizes during development. By using this flag, I can now deploy/migrate to Ganache without getting an out-of-gas error (thanks to Jerry on Loom Network Telegram channel for telling me about this flag).

Here is the command line I use to launch the Ganache CLI client:

ganache-cli -d 100000000 --allowUnlimitedContractSize

Note the additional -d flag that allows you to specify a higher maximum block gas limit than the default value. You can see the complete list of Ganache CLI flags on the GitHub repo readme page for that project:

https://github.com/trufflesuite/ganache-core

So, when I develop I use the Ganache CLI client with the --allowUnlimitedContractSize flag and without using the solc optimizer. When I deploy to Rinkeby or Main Net (or any other non-local network), to avoid out-of-gas errors, I do enable the solc optimizer by adding the following top level section to my truffle.js configuration file :

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