0

I am writing a proof of concept using truffle and testrpc and I have to spend hours optimizing the code to avoid out of gas errors. Is there a way to bypass this error or have a very high value. I have tried setting high limit on testrpc

testrpc -l 4500000000000

Tried specifying high gas in truffle.js

networks: {
    development: {
      host: "127.0.0.1",
      port: 8545,
      network_id: "*" // Match any network id
      ,gas: 500000000
    }
  }

But still, encounter out of gas when running my tests.

Transaction: 0x0981ab4c4ebdcb851e138d7a18344d256489ef01259f6b714edefdfe23ab824c
  Contract created: 0xccb7c4bd42c1e8e809d9f4573a2c2e0590b27b78
  Gas usage: 500000000
  Block Number: 23
  Block Time: Tue Jan 16 2018 08:35:30 GMT-0800 (PST)
  Runtime Error: out of gas

If I comment out 5 lines of the code triggering events (for validation failures), it runs without any issues with the default 6.7M gas. Here are my events, I have switched to bytes32 from string.

// Used for error handling.
  event AnyException(bytes32 m);
  event AnyException(bytes32 m1, bytes32 m2);
  event AnyException(bytes32 m1, bytes32 m2, bytes32 m3);

Here is a successful transaction after commenting 5 lines generating events using default gas in truffle.js.

Transaction: 0xb9f0f703c7f7e1ae5d51fc8170c00d2f12de146c6c2d5b48a47bd2a1e2a14203
  Contract created: 0x87d84cf9d012a181658ce76fa03ab1a7bc0ad538
  Gas usage: 6227032
  Block Number: 28
  Block Time: Tue Jan 16 2018 08:51:03 GMT-0800 (PST)

How can gas usage jump so high with 5 lines of event logs?

0

The gas limit you set up in testrpc is different than the gas limit for transactions from your client. The testrpc configuration is used to specify a limit for blocks on the blockchain. However, the gas limit used when creating a contract is going to be either the value set in the gas field in your transaction object, or if you don't provide one, it will make a call to estimateGas(). I have seen cases in testrpc where estimateGas() returns a bad estimate resulting in a low gas limit used for that transaction causing out of gas errors. Make sure you're specifying a gas limit in your client when sending the transaction and you should (hopefully) see this error go away.

  • Thanks, Adam. I understand that truffle uses fixed gas for testrpc. From the error message, I can see it uses 500000000 gas which is specified in truffle.js. – Titu Jan 22 '18 at 16:22
0

It helped me when I added solc optimizer to truffle.js and ran

truffle compile --all

truffle.js

module.exports = {
  // See <http://truffleframework.com/docs/advanced/configuration>
  // for more about customizing your Truffle configuration!
  networks: {
    development: {
      host: "127.0.0.1",
      port: 8545,
      network_id: "*" // Match any network id
    }
  },
  solc: { optimizer: { enabled: true, runs: 200 } }
};
0

https://github.com/trufflesuite/ganache-cli/releases/tag/v3.0.0

TestRPC 3.0.0 Breaking Changes: Default gas limit for transactions is now 90000 gas instead of the full block gas limit.

To avoid these new out of gas errors, you can now pass a higher gas limit as a parameter to web3:

web3.eth.sendTransaction({..., gas: 3141592}) // choose your own gas limit suitable for you

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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