Truffle: VM Exception while processing transaction: out of gas

Created on 24 Jun 2019  路  5Comments  路  Source: trufflesuite/truffle

  • [x ] I've asked for help in the Truffle Gitter before filing this issue.

Issue

I have a contract with the following function:

function emitDebug() external onlyValidator {
    require(shouldEmitDebug());
    require(candidatesLength() > 0);
    emit Debug(blockhash(block.number - 1), candidates());
    _setShouldEmitDebug(false);
  }

When I use truffle test I get Error: Returned error: VM Exception while processing transaction: out of gas but when I remove the line emit Debug(blockhash(block.number - 1), candidates()); everything seems to work.
I initiate my contract using await MyContract.new()

Steps to Reproduce

See above.

Expected Behavior

I expect it to run my tests.

Actual Results

Error: Returned error: VM Exception while processing transaction: out of gas

Environment

  • Operating System: MacOS
  • Ethereum client: Ganache CLI v6.4.3 (ganache-core: 2.5.5)
  • Truffle version (truffle version): Truffle v5.0.24 (core: 5.0.24)
  • node version (node --version): Node v10.16.0
  • npm version (npm --version): 6.9.0

Most helpful comment

Can you check the length of your contract with:

const myContractInstance = await MyContract.new();
console.log(myContractInstance.constructor._json.deployedBytecode.length);

Also truffle have a default gas limit gas: Gas limit used for deploys. Default is 4712388.
and you need enable the solidity optimizer in the truffle config

     ...
     settings: {
        optimizer: {
          enabled: true,
          runs: 200
        },
     },
     ...

look in truffle configuration

All 5 comments

Adding --allowUnlimitedContractSize to ganache-cli solves the issue.
And when I deploy to my private network using truffle migrate --reset --network local everything works perfect.

Presumably your contract is too large; networks generally do not allow contracts with machine code longer than 24 KiB, as per EIP 170. For some reason, EIP-170 specifies that this should yield an out-of-gas error, rather than a more descriptive error specifying that the contract is too long. Quite possibly Truffle should wrap this and return a more descriptive error message in this case -- this has been brought up before as issue #1655, which I'm afraid is still open. Still, hopefully this is helpful.

@haltman-at thanks for the reply.
I just don't understand how that single line of code is the one which makes me go over the limit.
How do I know the "length" of my contract?
Thanks

Can you check the length of your contract with:

const myContractInstance = await MyContract.new();
console.log(myContractInstance.constructor._json.deployedBytecode.length);

Also truffle have a default gas limit gas: Gas limit used for deploys. Default is 4712388.
and you need enable the solidity optimizer in the truffle config

     ...
     settings: {
        optimizer: {
          enabled: true,
          runs: 200
        },
     },
     ...

look in truffle configuration

Thanks @rotcivegaf.
I have the optimizer enabled and my contract was indeed too long.

Was this page helpful?
0 / 5 - 0 ratings