Web3 Ethereum RPC gateway : Parse Error and crash on id value

To add an other issue about the CloudFlare Web3 service, it replies with an unexpected error ‘Parse Error’ message when interacting with some contracts.

The issue can be seen using the following Web3 call :
Contract “BalanceScanner” at 0x571C62a1c863aEAD01c1d34D8cB3Ee2c6f938800.
eth_call of the method tokensBalance(address,address[]) = e5da1b68
Using the FTX account wallet 0x2faf487a4414fe77e2327f0bf4ae2a264a776ad2,
and the USDT token 0xdac17f958d2ee523a2206206994597c13d831ec7.
eth_call → tokensBalance(0x2faf487a4414fe77e2327f0bf4ae2a264a776ad2, [0xdac17f958d2ee523a2206206994597c13d831ec7])

Which translates after ABI/HexSerial encoding to :
0xe5da1b680000000000000000000000002faf487a4414fe77e2327f0bf4ae2a264a776ad200000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000001000000000000000000000000dac17f958d2ee523a2206206994597c13d831ec7 for data.


Full request and response

Request :arrow_right:
https://cloudflare-eth.com/
Headers :

Content-length: 392
Host: cloudflare-eth.com
User-agent: Mozilla/5.0
Content-type: application/json

Data :
{"jsonrpc":"2.0","id":0,"method":"eth_call","params":[{"to":"0x571c62a1c863aead01c1d34d8cb3ee2c6f938800","data":"0xe5da1b680000000000000000000000002faf487a4414fe77e2327f0bf4ae2a264a776ad200000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000001000000000000000000000000dac17f958d2ee523a2206206994597c13d831ec7"},"latest"]}

Response :arrow_left:
raw headers

Content-Type: application/json
Content-Length: 73
Connection: close
Access-Control-Allow-Origin: *
Access-Control-Allow-Headers: Content-Type
Access-Control-Allow-Methods: POST, OPTIONS
Expect-CT: max-age=604800, report-uri="https://report-uri.cloudflare.com/cdn-cgi/beacon/expect-ct"
Vary: Accept-Encoding
Server: cloudflare
CF-RAY: 6d3281a45dbc32c2-CDG

Data
{"jsonrpc":"2.0","error":{"code":-32700,"message":"Parse Error"},"id":-1}

Our software trace at higher level :
Exception: Error in JSON-RPC response: {'code': -32700, 'message': 'Parse Error'}


Using an alternative RPC service :
Response
{"jsonrpc":"2.0","id":0,"result":"0x000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000d65e40b9db26"}\n

Which can be translated back after JSON-RPC and EVM/ABI decoding :
[235700301191974] = 235,700,301.191974 USDT token units.


Do you maintain high quality about this free service, as it were a paid one? Or you just provide this service as a promotional marketing free but technically unreliable service?

Searching even deeper about the error, I found the EIP1474 which lists the error codes and message. It tells the Parse Error message is because of a invalid JSON. So I played with the JSON encoding, then changing the JSON-RPC id value.
Turns out the error is triggered when the JSON-RPC id is 0. When a strictly positive number is used, the error vanishes.

About the JSON-RPC id value.
Nothing prevents the use of the value 0. The JSON RPC standard states :
An identifier established by the Client that MUST contain a String, Number, or NULL value if included. If it is not included it is assumed to be a notification. The value SHOULD normally not be Null and Numbers SHOULD NOT contain fractional parts.
The Server MUST reply with the same value in the Response object if included. This member is used to correlate the context between the two objects.
The use of Null as a value for the id member in a Request object is discouraged, because this specification uses a value of Null for Responses with an unknown id. Also, because JSON-RPC 1.0 uses an id value of Null for Notifications this could cause confusion in handling.
Fractional parts may be problematic, since many decimal fractions cannot be represented exactly as binary fractions.

0 is not null. null is not a value, per se : it is a value outside the domain of the variable indicating missing or unknown data. 0 is a value, like a float having the value of 0.

About the error code/message returned
Parse Error means :
acc. JSON-RPC : Invalid JSON was received by the server. An error occurred on the server while parsing the JSON text.
acc. EIP1 : Invalid JSON

The JSON sent is 100% valid. What is not valid is the id value (invalid value for CloudFlare, it is actually a valid value). The error is not triggered when the JSON is parsed.

I think the following error might be more appropriate for this case.
-32600 Invalid Request The JSON sent is not a valid Request object.

Overall, I think that you JSONRPC implementation may have a code like if (request.id) which branches to the wrong path when id value is 0. So it has different behaviour depending if value is 0 or not.

Keep investigating on the matter, the CloudFlare RPC worker is crashing when the id is a negative integer number. In this case, the RPC API endpoint provides an HTML page stating :

Error 1101 Ray ID: 6d32cb3c3f4c692a • 2022-01-25 16:14:19 UTC
Worker threw exception

What happened?
You've requested a page on a website (cloudflare-eth.com) that is on
the Cloudflare network. An unknown error occurred while rendering
the page.

On top of the behavior with value 0, there’s an unhanded exception when the id value is negative.