question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

6.4.2: `TypeError: Cannot read property '0' of undefined`

See original GitHub issue

Expected Behavior

ganache-cli not crashing

Current Behavior

After upgrading to v6.4.2 (from v6.2.5), the testchain will randomly crash in the middle of our tests with

eth_blockNumber
eth_blockNumber
eth_blockNumber
TypeError: Cannot read property '0' of undefined
    at e (/Users/Tyler/dai.js/testchain/node_modules/ganache-cli/build/ganache-core.node.cli.js:69:5393)
    at u (/Users/Tyler/dai.js/testchain/node_modules/ganache-cli/build/ganache-core.node.cli.js:69:5429)
    at u (/Users/Tyler/dai.js/testchain/node_modules/ganache-cli/build/ganache-core.node.cli.js:69:5955)
    at u (/Users/Tyler/dai.js/testchain/node_modules/ganache-cli/build/ganache-core.node.cli.js:69:5770)
    at /Users/Tyler/dai.js/testchain/node_modules/ganache-cli/build/ganache-core.node.cli.js:69:4169
    at /Users/Tyler/dai.js/testchain/node_modules/ganache-cli/build/ganache-core.node.cli.js:61:1861552
    at /Users/Tyler/dai.js/testchain/node_modules/ganache-cli/build/ganache-core.node.cli.js:2:26124
    at i (/Users/Tyler/dai.js/testchain/node_modules/ganache-cli/build/ganache-core.node.cli.js:2:41179)

Steps to Reproduce

checkout branch sdk-688 here: https://github.com/makerdao/dai.js/tree/sdk-688 follow steps under Developing in Readme run tests with yarn test

Your Environment

  • Version used: 6.4.2
  • NodeJS Version: v10.13.0
  • Operating System and version: macOS High Sierra, version 10.13.4 Beta
  • Link to your project or repro gist: https://github.com/makerdao/dai.js/tree/sdk-688
  • Commit hash to use with above link for reproduction: 7a246d18b91f259bb9e3f9ec1a064fba96ab9619

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:4
  • Comments:13 (6 by maintainers)

github_iconTop GitHub Comments

4reactions
ndeshevcommented, Apr 19, 2019

David, yes, I tested it with the verbose option on. Also the contracts passed about 200 unit tests in Truffle where I never estimate gas usage and the only time the ganache-cli breaks with TypeError: Cannot read property '0' of undefined error is when a link to a contract function is clicked and the MetaMask transaction sign window has to pop up with the gas price and the total cost of the transaction.

The estimated gas for the transaction that the MetaMask is displaying is almost the maximum I set for allowed for block gas limit. If I leave the MetaMask transaction window open and restart the ganache-cli it sends it successfully.

Also, the problem did not exist in versions lower than 6.4.2 which is another thing that led me to think is gas estimation problem related as this was one of the few changes in versions 6.4.2 and 6.4.3.

The estimated gas for the transactions that is displayed in the logs below are incorrect - it is almost the maximum allowed for the block, as mentioned above.

I did some more testing, there are functions that do not break the ganache client when MetaMask is invoked, but they are very simple short ones, on calls to more complicated functions the problem persists.


Update - More testing and information related to use cases with web3.js: After executing only the web3.js gas estimation method contract.methods.myMethod().estimateGas() on a function call (that crashes ganache when called via MetaMask), the ganache-cli does NOT break, but it estimates super incorrect gas required for the call - more precisely 46k for a function transaction that requires 650k, meaning that the gas is very underestimated, as opposed to the MetaMask case above.

However, when I tried to run the same web3.js .estimateGas() method on a ganache-cli prior to v.6.4.2 it doesn’t work at all throwing the following error:

RPC Error: Internal JSON-RPC error. Object { code: -32603, message: “Internal JSON-RPC error.” }


In summary, that might be a tricky one…

OS: Windows 10, web3.js version: 1.00-beta.52 | Node - 10.14.1 | ganache-cli version used for the logs is 6.4.3 but when I tried 6.4.2 before a few days the outcome was the same.

Log 1:

eth_blockNumber { “id”: 1333707413168176, “jsonrpc”: “2.0”, “params”: [], “method”: “eth_blockNumber” } < { < “id”: 5050169228901374, < “jsonrpc”: “2.0”, < “result”: “0x1e43” < } < { < “id”: 1333707413168176, < “jsonrpc”: “2.0”, < “result”: “0x1e43” < } eth_estimateGas { “id”: 4525324431062, “jsonrpc”: “2.0”, “params”: [ { “from”: “0x2b235b23be383bfd75d87f865f6fb74536072f57”, “to”: “0x81e86b03500da6377478272f2dcde68e33ec360a”, “value”: “0x3bf3b91c95b00000”, “data”: “0xbaaa1ea8000000000000000000000000000000000000000000000000000000000000000500000000000000000000000018c6a286552899dc9d74b3dbd3dc26bf80d277a1”, “gasPrice”: “0x4a817c800”, “gas”: “0x657890” } ], “method”: “eth_estimateGas” } TypeError: Cannot read property ‘0’ of undefined at e (C:\Users\ndesh\AppData\Roaming\npm\node_modules\ganache-cli\build\ganache-core.node.cli.js:69:5393) at u (C:\Users\ndesh\AppData\Roaming\npm\node_modules\ganache-cli\build\ganache-core.node.cli.js:69:5429) at u (C:\Users\ndesh\AppData\Roaming\npm\node_modules\ganache-cli\build\ganache-core.node.cli.js:69:5955) at u (C:\Users\ndesh\AppData\Roaming\npm\node_modules\ganache-cli\build\ganache-core.node.cli.js:69:5770) at C:\Users\ndesh\AppData\Roaming\npm\node_modules\ganache-cli\build\ganache-core.node.cli.js:69:4169 at C:\Users\ndesh\AppData\Roaming\npm\node_modules\ganache-cli\build\ganache-core.node.cli.js:61:1853218 at C:\Users\ndesh\AppData\Roaming\npm\node_modules\ganache-cli\build\ganache-core.node.cli.js:2:26124 at i (C:\Users\ndesh\AppData\Roaming\npm\node_modules\ganache-cli\build\ganache-core.node.cli.js:2:41179) at C:\Users\ndesh\AppData\Roaming\npm\node_modules\ganache-cli\build\ganache-core.node.cli.js:61:1210468 at C:\Users\ndesh\AppData\Roaming\npm\node_modules\ganache-cli\build\ganache-core.node.cli.js:2:105466 at C:\Users\ndesh\AppData\Roaming\npm\node_modules\ganache-cli\build\ganache-core.node.cli.js:32:392 at c (C:\Users\ndesh\AppData\Roaming\npm\node_modules\ganache-cli\build\ganache-core.node.cli.js:32:5407) at C:\Users\ndesh\AppData\Roaming\npm\node_modules\ganache-cli\build\ganache-core.node.cli.js:32:317 at C:\Users\ndesh\AppData\Roaming\npm\node_modules\ganache-cli\build\ganache-core.node.cli.js:61:1871644 at C:\Users\ndesh\AppData\Roaming\npm\node_modules\ganache-cli\build\ganache-core.node.cli.js:2:23237 at o (C:\Users\ndesh\AppData\Roaming\npm\node_modules\ganache-cli\build\ganache-core.node.cli.js:2:26646) at C:\Users\ndesh\AppData\Roaming\npm\node_modules\ganache-cli\build\ganache-core.node.cli.js:2:26124 at C:\Users\ndesh\AppData\Roaming\npm\node_modules\ganache-cli\build\ganache-core.node.cli.js:61:1864681 at C:\Users\ndesh\AppData\Roaming\npm\node_modules\ganache-cli\build\ganache-core.node.cli.js:61:1862544 at C:\Users\ndesh\AppData\Roaming\npm\node_modules\ganache-cli\build\ganache-core.node.cli.js:61:1889757 at C:\Users\ndesh\AppData\Roaming\npm\node_modules\ganache-cli\build\ganache-core.node.cli.js:2:23237 at o (C:\Users\ndesh\AppData\Roaming\npm\node_modules\ganache-cli\build\ganache-core.node.cli.js:2:26646)

Sometimes ganache executes one or few more JSON RPC call before crashing, everything else is the same, the gas cost is again at the maximum allowed block gas limit.

eth_estimateGas { “id”: 4525324431077, “jsonrpc”: “2.0”, “params”: [ { “from”: “0x8f9e2539f358b13af290d323f7b9220983745a98”, “to”: “0x81e86b03500da6377478272f2dcde68e33ec360a”, “value”: “0x1df9dc8e4ad80000”, “data”: “0xbaaa1ea8000000000000000000000000000000000000000000000000000000000000000500000000000000000000000018c6a286552899dc9d74b3dbd3dc26bf80d277a1”, “gasPrice”: “0x4a817c800”, “gas”: “0x657890” } ], “method”: “eth_estimateGas” } eth_blockNumber { “id”: 1333707413168638, “jsonrpc”: “2.0”, “params”: [], “method”: “eth_blockNumber” } TypeError: Cannot read property ‘0’ of undefined at e (C:\Users\ndesh\AppData\Roaming\npm\node_modules\ganache-cli\build\ganache-core.node.cli.js:69:5393) at u (C:\Users\ndesh\AppData\Roaming\npm\node_modules\ganache-cli\build\ganache-core.node.cli.js:69:5429) at u (C:\Users\ndesh\AppData\Roaming\npm\node_modules\ganache-cli\build\ganache-core.node.cli.js:69:5955)…

2reactions
davidmurdochcommented, Apr 20, 2019

Thanks @ndeshev for following up. I wasn’t able to reproduce the crash with the contract examples you provided since they are incomplete contracts. I’m hesitant to release this patch without a test to ensure we don’t regress in the future. Is there any chance you can provide us with a complete reproduction contract.

The overestimate is definitely a separate bug. @nicholasjpaterno will likely be looking into this issue when he wraps up some of his current tasks. Changing the blockGasLimit having an effect on estimation isn’t too surprising (probably still a bug if we are returning fluctuating gas estimates), as many contract functions do require additional gas the more gas you provide (we use the blockGasLimit as the default gas amount in our gas estimate calculations). See EIP-150 (specifically the EIP-114 proposal it implements) for details.

There is one edge case we do know about and don’t try to handle: when a contract checks its gasLeft and behaves differently depending on this value. A contrived example being:

  if (gasLeft % 2 == 0) {
     expensive();  
  } else {
    cheap();
  }

A function like the above would cause gas estimations to vary wildly depending on the amount of gas initially supplied to the transaction.


Just to reiterate: the amount of gas required is often greater than the amount of gas used. Any chance you could provide a minimal reproduction of a fluctuating and wildly high gas estimate contracts?

Read more comments on GitHub >

github_iconTop Results From Across the Web

6.4.2: `TypeError: Cannot read property '0' of undefined ...
Expected Behavior ganache-cli not crashing Current Behavior After upgrading to v6.4.2 (from v6.2.5), the testchain will randomly crash in ...
Read more >
[SOLVED] Cannot Read Property '0' of Undefined in JavaScript
The “cannot read property '0' of undefined” error in JavaScript will occur when you access the 0 index of a variable that is...
Read more >
How to Prevent the Error: Cannot Read Property '0' of Undefined
If a property of an object is an array, it's possible to accidentally misspell the key or try to access the array through...
Read more >
Explaining and Solving "Cannot read property ... - YouTube
This video aims at answering questions of 형빈 to my video.Basic Web Development Environment- VS Code with Live Server Extension- ...
Read more >
TypeError: Cannot read Property '0' of Undefined in JS
The "Cannot read Property '0' of undefined" error occurs when accessing an undefined value at index 0 . To solve the error, initialize...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found