"Large" solidity tests fail during the truffle test operation, with the message
The contract code couldn't be stored, please check your gas amount.
This is similar to https://github.com/trufflesuite/truffle/issues/825 and I mentioned this there, but there are some differences. In particular, this is happening during test. I had poked around when I first started seeing these errors, and I found for instance that commenting out functions in Assert.sol let me include more in my test file before hitting that error.
But it turns out that bandaid only went so far. I still had to split my test into multiple files to get each to deploy, and in the end, even my simplest test (no steps I could split off to a separate file as I navigated through various calls to test a path) hit this error.
Interestingly, in the most recent case, I could deploy the test file if I changed x= new X(args); to x = X(DeployedAddresses.X()) as in my other files. The problem was that I needed to invoke the contract differently for this test than for the others. If I invoke it differently in the migration file, this test works but others now break because I've changed the behavior.
Unfortunately, I don't know a way to distill this to something reproducible without exposing proprietary code.
Test contract will run.
1) "before all" hook: prepare suite
0 passing
1 failing
1) TestX "before all" hook: prepare suite:
Error: VM Exception while processing transaction: out of gas
at Object.InvalidResponse (/usr/local/lib/node_modules/truffle/build/cli.bundled.js:41484:16)
at /usr/local/lib/node_modules/truffle/build/cli.bundled.js:329540:36
at /usr/local/lib/node_modules/truffle/build/cli.bundled.js:325210:9
at XMLHttpRequest.request.onreadystatechange (/usr/local/lib/node_modules/truffle/build/cli.bundled.js:328239:7)
at XMLHttpRequestEventTarget.dispatchEvent (/usr/local/lib/node_modules/truffle/build/cli.bundled.js:176416:18)
at XMLHttpRequest._setReadyState (/usr/local/lib/node_modules/truffle/build/cli.bundled.js:176706:12)
at XMLHttpRequest._onHttpResponseEnd (/usr/local/lib/node_modules/truffle/build/cli.bundled.js:176861:12)
at IncomingMessage.<anonymous> (/usr/local/lib/node_modules/truffle/build/cli.bundled.js:176821:24)
truffle version):Also true of the master version as of early this week, which I rebuilt from sources.
node version (node --version): v6.11.0
npm version (npm --version): 3.10.10
Same for me. Some tests fail to run the first time, but they pass the next time I run the test, without changing anything. I麓m using Ganache as the network:
Error: The contract code couldn't be stored, please check your gas amount.
at Object.callback (/mnt/c/Users/shaka/AppData/Roaming/npm/node_modules/truffle/build/webpack:/~/web3/lib/web3/contract.js:147:1)
at /mnt/c/Users/shaka/AppData/Roaming/npm/node_modules/truffle/build/webpack:/~/web3/lib/web3/method.js:142:1
at /mnt/c/Users/shaka/AppData/Roaming/npm/node_modules/truffle/build/webpack:/~/web3/lib/web3/requestmanager.js:89:1
at /mnt/c/Users/shaka/AppData/Roaming/npm/node_modules/truffle/build/webpack:/~/truffle-provider/wrapper.js:134:1
at XMLHttpRequest.request.onreadystatechange (/mnt/c/Users/shaka/AppData/Roaming/npm/node_modules/truffle/build/webpack:/~/web3/lib/web3/httpprovider.js:128:1)
at XMLHttpRequestEventTarget.dispatchEvent (/mnt/c/Users/shaka/AppData/Roaming/npm/node_modules/truffle/build/webpack:/~/xhr2/lib/xhr2.js:64:1)
at XMLHttpRequest._setReadyState (/mnt/c/Users/shaka/AppData/Roaming/npm/node_modules/truffle/build/webpack:/~/xhr2/lib/xhr2.js:354:1)
at XMLHttpRequest._onHttpResponseEnd (/mnt/c/Users/shaka/AppData/Roaming/npm/node_modules/truffle/build/webpack:/~/xhr2/lib/xhr2.js:509:1)
at IncomingMessage.<anonymous> (/mnt/c/Users/shaka/AppData/Roaming/npm/node_modules/truffle/build/webpack:/~/xhr2/lib/xhr2.js:469:1)
at endReadableNT (_stream_readable.js:1101:12)
at process._tickCallback (internal/process/next_tick.js:114:19)
You can set the --gasLimit option.
From https://github.com/trufflesuite/ganache-cli :
-l or --gasLimit: Use a custom Gas Limit (defaults to 90000)
PS :
Interestingly, in the most recent case, I could deploy the test file if I changed x= new X(args); to x = X(DeployedAddresses.X()) as in my other files.
That's because with new X() you are using gas in the contract deployment. When using x = X(address) there are two distinct deployments.
If the limit is 90,000 gas, then using 85,000 to deploy A + 85,000 to deploy B result in a fail when deploying B from A's constructor and no error if B is deployed separately.
Right, I shouldn't have said it was interesting, I should have just said it was frustrating. I have set the gaslimit to be huuuuge, but it still doesn't allow it.
Hi, I've been looking into this a bit more and found this:
https://github.com/ethereum/EIPs/issues/170
So pretty much even if you override the gas limit, ethereumjs-vm still caps the size of the contract bytecode to 24576 (i.e. any contract 24577 bytes or larger will trigger an out-of-gas error).
The check happens here:
FWIW, that commit introduces the allowUnlimitedContractSize option for the VM. So I guess all that's needed is a way to pass in that option via ganache-cli.
@attente Nice! That makes sense.
Cool, somebody is actually already working on doing that:
Hey everyone! The code that supports the allowUnlimitedContractSize flag in Ganache was merged in https://github.com/trufflesuite/ganache-core/pull/126. This should be in the next release of ganache-cli (I'll add something to the readme and link the PR to this issue).
If you're having issues deploying contracts without this flag, it's likely contracts aren't being optimized during compilation. Perhaps this issue really needs to see if that's the root cause for this instance. The flag should only be used knowingly as it deviates ganache-cli's behavior from production (i.e. geth, etc.)
I'll let @cgewecke judge on whether or not this issue stays open or closed.
The flag was added to ganache-cli in https://github.com/trufflesuite/ganache-cli/pull/550
Hey, Mike,
Hesitant to use the flag - what are some ways to improve compilation before resorting to this? I'm at a point where commenting out some logic or even modifiers will allow the specs to run successfully.
Hey @asoong,
I'm sorry for not initially suggesting this; I didn't quite do my homework (I'm knew to the Truffle team).
Looking at the docs, I highly suggest that you use the compiler optimizer since your contracts shouldn't deploy on main and test nets without it! You can add this in your truffle.js configuration: https://truffleframework.com/docs/advanced/configuration#solc
solc: {
optimizer: {
enabled: true,
runs: 200
}
}
You can increase the number of runs to optimize more.
I'm going to go ahead and close this. This comment seems to be the answer to this issue
Please let us know if you still have issues after that!
Most helpful comment
The flag was added to ganache-cli in https://github.com/trufflesuite/ganache-cli/pull/550