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()
See above.
I expect it to run my tests.
Error: Returned error: VM Exception while processing transaction: out of gas
MacOSGanache CLI v6.4.3 (ganache-core: 2.5.5)truffle version): Truffle v5.0.24 (core: 5.0.24)node --version): Node v10.16.0npm --version): 6.9.0Adding --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.
Most helpful comment
Can you check the length of your contract with:
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
look in truffle configuration