feat(electrum): return JSON-RPC 2.0 error objects (port of mempool/electrs#103)#222
Open
EddieHouston wants to merge 1 commit into
Open
Conversation
…ectrs#103) Implements Blockstream#221. All Electrum RPC error replies are now JSON-RPC 2.0 error objects with codes instead of bare strings: -32601 unknown method, -32602 invalid params (via a new ErrorKind::InvalidParams raised by the param helpers), 1 for history-too-large, 2 for bitcoind RPC errors, -32603 otherwise. Malformed input now gets a reply instead of a dropped connection: -32700 for unparseable JSON (id null) and -32600 for valid JSON that is not a request object. A batch containing an unknown method answers its remaining entries instead of killing the connection. Deviations from the mempool port: replies always include 'id' (null when undetectable, per spec) and error messages do not echo request params back. Oversized batches and non-protocol bytes still close the connection. Co-authored-by: junderw <jonathan.underwood4649@gmail.com>
Randy808
approved these changes
Jun 12, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Resolves #221 by porting mempool/electrs#103 onto
new-indexand extending it witherror categorization. Supersedes #220 — this includes that unknown-method fix in
error-object form.
What changes on the wire
All Electrum RPC error replies are now JSON-RPC 2.0 error objects instead of bare
strings, and malformed input gets a reply instead of a dropped connection:
{"code": -32601, "message": "unknown method <name>"}"error": "missing height"{"code": -32602, "message": "missing height"}TooPopular){"code": 1, ...}(ElectrumX-style bad request){"code": 2, ...}(ElectrumX-style daemon error){"code": -32603, ...}{"code": -32700, "message": "parse error"},"id": null{"code": -32600, "message": "invalid request"},"id"echoed ornullSuccess replies are unchanged. Oversized batches, invalid UTF-8, and TLS bytes on the
plaintext port still close the connection (peers not speaking the protocol).
Implementation
JsonRpcV2Errorenum andjson_rpc_error()helper ported from Fix JSON-RPC v2 errors mempool/electrs#103(mempool/electrs@277b05b), with two deviations:
the reply always includes
"id"(nullwhen undetectable, per spec — mempool omitsit) and error messages don't echo request params back.
-32700/-32600) is re-implemented rather than picked,since mempool's hunk targets their refactored connection layer;
handle_valuenowreturns
Valueand keeps this repo's RPC event logging.ErrorKind::InvalidParamscarries param-validation failures from the*_from_valuehelpers to the dispatcher, wherejsonrpc_code()maps error kinds tocodes (
InvalidParams→ -32602,TooPopular→ 1,RpcError(daemon) → 2,everything else → -32603). A few call sites stop wrapping param errors with
chain_err, which previously erased the kind.Testing
New integration test
test_electrum_jsonrpc_errorscovers: -32601 with the connectionleft open, -32602 on a missing param, -32600 on a method-less request (id echoed),
-32700 on unparseable JSON (null id), a batch where an unknown method no longer kills
the other entries, and that the connection survives all of the above.
cargo testpasses (lib + electrum + REST integration tests);cargo checkis cleanon
--features liquidand--features electrum-discovery.