web3 / web3.js

Collection of comprehensive TypeScript libraries for Interaction with the Ethereum JSON RPC API and utility functions.
https://web3js.org/
Other
19.18k stars 4.91k forks source link

Web3JS throws InvalidResponse without Ganache core ever receiving a request #2135

Closed roschler closed 5 years ago

roschler commented 5 years ago

Versions:

Ubuntu 18.04 LTS Node.JS: 8.3.0 WebJS: web3: ^1.0.0-beta.37 web3-utils: ^1.0.0-beta.36 npm: 5.6.0 Truffle version 5.x (latest)

I have traced through the HttpProvider code in the Web3 package and what I see is quite strange. The provider's onreadystatechange gets a DONE/4 event even though Ganache does not receive a request.

To test this, I initiate a sendSignedTransaction() call from my Node.JS app with a breakpoint set right on the actual low-level send() method the provider uses to contact Ganache with the actual RPC request. When the breakpoint is hit, I shut down Metamask to stop any other entity from making requests to Ganache. I switch to another instance of my development IDE that has the ganache-cli repo loaded. This repo has ganache-core resident as one of its packages. I set breakpoints on the Ganache core server's on-data and on-end event handlers for the request object to make sure I trap any RPC request activity during the test.

With everything in place for a clean test, I resume execution of my Node.JS app and discover that the Web3JS code for the HttpProvider that made the request already processing a result and as you can see in the screenshot below, the result variable is an empty string:

screenshot from 2018-12-19 00-18-59

This results in that code throwing an InvalidResponse error. I have run this test several times and I always see the exact same behavior. That is, the exact same sequence of code paths taken. What could cause the Web3 provider's XMLHttpRequest to believe it already has received a response from Ganache when at least from Ganache's point of view, WebJS never even sent the request?

The last time I saw anomalies like this one was when I learned the hard way that you have to BigNumber everything related to gas values. Assuming I haven't done something wrong and that's always strong possibility, it really feels like some other code in Web3JS is causing the event emitter code to misfire or suffer perhaps some data structure corruption. I say this because except for the one particular smart contract method that results in this error when I call it, all the other method calls work perfectly and there's quite a few of those. I believe there's something about the arguments I am passing with this specific smart contract method and the way Web3JS is trying to parse the arguments against, or match them up to the Solidity ABI is resulting in an unexpected error.

Any tips you can give for debugging this would be appreciated. Otherwise I have a long debug hunt in front of me.

Solidity method being called:

    function finalizeGame(
            address payable _paymentAndEscrowWarehouseAddr,
            uint256 _clientReferenceTimestamp,
            uint256 _gameId
        )

Node.JS code calling that method:

    return ebbDetails.contractInstanceObj.methods.finalizeGame(
    paymentsAndEscrowContractAddr,
    dataBag.paymentStartTimestamp,
    dataBag.idInSmartContract);
nivida commented 5 years ago

I'm currently refactoring the providers and will test it after I finished the refactoring.

The HttpProvider currently in the working branch (PR #2000): https://github.com/ethereum/web3.js/blob/ethereumProvider/packages/web3-providers/src/providers/HttpProvider.js

But it looks more like a bug from ganache or something else to me.

roschler commented 5 years ago

ADDITIONAL INFORMATION

I ran Wireshark to see what was happening at the RPC request level, setting it to only display packets associated with local traffic on port 8545, since that is the location of Ganache on my system. I ran it once with a success case and then again on the finalizeGame() smart contract method that is failing for me. I am including the results below.

In the success case I triggered a Metamask call that pays for a transaction sent to my smart contract. As you can see from the Wireshark capture, the transaction executes without error. There is an RST response later in the stream but that is in response to some TCP request unrelated to the successful RPC query involved with transaction number 12:

screenshot from 2018-12-20 01-00-52

However, after I shut down Metamask to eliminate any interference with Ganache, when I try to call my finalizeGame() transaction using eth_sendRawTransaction Ganache immediately responds with an RST packet, indicating it rejects the request or perhaps due to an internal problem, is no longer available to service the request and the connection, as you can see below:

screenshot from 2018-12-20 01-01-36

If anybody could take a look at this soon I'd be very grateful. The dApp I've been working on for over 8 long months now is stalled because of this. I want to take advantage of the end of the year holiday period to finally get my first deployment up on Rinkeby, and I'll be extremely sad if this problem prevents me from succeeding at that goal.

nivida commented 5 years ago

Thanks for this detailed informations! Could you tell me which version of ganache you have installed? Did you also test it with the latest node version?

roschler commented 5 years ago

@nivida Here are the version numbers, updated compared those shown in my original post to show the fact that before running the test you see the screenshots from, I updated Node.JS to the latest stable release:

Versions:

ganache-cli: Ganache CLI v6.2.3 (ganache-core: 2.3.2) Node.JS: 10.14.2 npm: 5.6.0 solc: 0.5.1 Truffle version 5.x (latest) Ubuntu 18.04 LTS WebJS: web3: ^1.0.0-beta.37 web3-utils: ^1.0.0-beta.36

roschler commented 5 years ago

@nivida Hello Samuel. I have just finished a private repo and added you and David as collaborators. Let me know if you got the invite. Thanks in advance for your help.

roschler commented 5 years ago

@nivida Did you ever get a chance to look at the private repo I made for you? My dApp development is on hold because I can't get past this error. I really want to finish the dApp. It's a really fun game and helps raise money for musicians so I believe the Ethereum community will really like it.

roschler commented 5 years ago

For those that run into that error and run into problems trying to update to the latest Web3 package like I did, see this post:

https://github.com/ethereum/web3.js/issues/2181

roschler commented 5 years ago

I'm cross-posting this solution to this problem because it involves several important fundamental Ethereum/Ganache development issues, and I want to make sure other developers can benefit from the information that solved the issue for me, along with @seesemichaelj Mike's tremendous help:

https://github.com/trufflesuite/ganache/issues/1057#issuecomment-453770649