cryptoadvance / specter-desktop

A desktop GUI for Bitcoin Core optimised to work with hardware wallets
MIT License
808 stars 242 forks source link

KeyError: 'error' in getblockchaininfo when the 'error' key is missing in the RPC response #2473

Open 0xA1-devops opened 3 weeks ago

0xA1-devops commented 3 weeks ago

When using the getblockchaininfo RPC command, a KeyError: 'error' occurs in rpc.py if the server response does not include the error key. Even if there is no error in the response, the code expects this key to be present, which causes a crash when attempting to access it.

Steps to Reproduce:

  1. Configure Specter to connect to an RPC server on port 8332.
  2. Ensure the server responds successfully to getblockchaininfo but without an explicit error key in the response.
  3. Observe that Specter throws a KeyError: 'error' in this case.

Environment: Bitcoin Core version: v28.0.0 (release build) Specter from master 0ac4640

Example RPC Response: The following response from a Bitcoin node using curl lacks the error key: curl --user client:secret --data-binary '{"jsonrpc": "1.0", "id": "curltest", "method": "getblockchaininfo", "params": []}' -H 'content-type: text/plain;' http://127.0.0.1:8332/ Response:

{
  "result": {
    "chain": "main",
    "blocks": 868689,
    "headers": 868689,
    "bestblockhash": "0000000000000000000177b015f5337d1f4f11b7c3f661f36eb1ae44d0bddffc",
    "difficulty": 95672703408223.94,
    "time": 1730637153,
    "mediantime": 1730633139,
    "verificationprogress": 0.9999955761319607,
    "initialblockdownload": false,
    "chainwork": "000000000000000000000000000000000000000097e8942c8bce909bcbfa99a4",
    "size_on_disk": 637103765,
    "pruned": true,
    "pruneheight": 868395,
    "automatic_pruning": true,
    "prune_target_size": 576716800,
    "warnings": []
  },
  "error": null,
  "id": "curltest"
}

As shown, the error key is present but set to null, which can also cause a KeyError if not handled properly.

Error Log:

[11-03-2024 01:16:02 PM] ERROR in node: 'error'
Traceback (most recent call last):
  File "/home/user/specter/src/cryptoadvance/specter/node.py", line 431, in _get_rpc
    res = rpc.getblockchaininfo()
  File "/home/user/specter/src/cryptoadvance/specter/rpc.py", line 508, in fn
    if r["error"] is not None:
KeyError: 'error'

Temp Solution: In rpc.py, on line 508, replace the line: if r["error"] is not None: with if r.get("error") is not None:

This change ensures that if the error key is missing in the response, it defaults to None, preventing the KeyError and allowing the code to handle responses without an error key gracefully.

Proof of Fix: After applying this change, the issue was resolved, and Specter no longer crashes when the error key is missing in the RPC response.

nmfretz commented 3 weeks ago

@0xA1-devops nice work. Seeing the same thing with Specter Desktop running on umbrelOS.

It looks like Specter has been sending JSON-RPC 2.0 requests for a while: https://github.com/cryptoadvance/specter-desktop/blob/0ac4640a3fcbbb9fd734d01b6eb9cffa5ad9634c/src/cryptoadvance/specter/rpc.py#L399-L404

However, Core is only just now recognizing 2.0 requests as of v28 and sending back 2.0 responses that strictly adhere to the specs: https://www.jsonrpc.org/specification#conventions

Prior to v28, Core would have been responding to Specter's requests with 1.0 responses that always included an error field, so this issue was never encountered. But in RPC-JSON 2.0, the error attribute is not included if there was no error:

error
This member is REQUIRED on error.
This member MUST NOT exist if there was no error triggered during invocation.
The value for this member MUST be an Object as defined in section 5.1.

So as @0xA1-devops has shown above, the solution is to handle the case when no error field is present.

VicVeq commented 3 weeks ago

In attempting to fix this on my Umbrel (RaspPi4), I had uninstalled Specter and so now cannot reinstall it to try the workaround (removed from app store). When is it likely that a fixed Specter Desktop app for Umbrel will be available in the Umbrel 'Store' for installation?

Funman2 commented 2 weeks ago

Unfortunate even the hack fix does not work, when will there be a solution?

https://github.com/cryptoadvance/specter-desktop/issues/2472#issuecomment-2467858958