RPC: Strict JSON-RPC 2.0 compliance (gated behind flag) #12435

pull esotericnonsense wants to merge 1 commits into bitcoin:master from esotericnonsense:2018-02-jsonrpc-2.0 changing 11 files +198 −45
  1. esotericnonsense commented at 11:00 PM on February 14, 2018: contributor

    This patch adds a -strictjsonrpcspec flag.

    If the flag is used, bitcoind enters JSON-RPC 2.0 mode, which allows it to be fully spec-compliant (and thus work with libraries like libjson-rpc-cpp without modification).

    I've added a functional test for the specific bits of the spec that I've changed.

    univalue changes are included in this commit for ease of review but I can pull those out (see https://github.com/bitcoin-core/univalue/pull/12)

  2. fanquake added the label RPC/REST/ZMQ on Feb 14, 2018
  3. in src/univalue/include/univalue.h:183 in 6fe7afa92d outdated
     179 | @@ -180,7 +180,7 @@ class UniValue {
     180 |      bool push_back(std::pair<std::string,UniValue> pear) {
     181 |          return pushKV(pear.first, pear.second);
     182 |      }
     183 | -    friend const UniValue& find_value( const UniValue& obj, const std::string& name);
    


    meshcollider commented at 5:31 AM on February 15, 2018:

    univalue changes need to be submitted upstream, not here :)


    laanwj commented at 8:40 AM on February 15, 2018:

    He did that (see OP).

  4. laanwj commented at 8:40 AM on February 15, 2018: member

    Concept ACK

  5. in src/rpc/server.cpp:417 in 6fe7afa92d outdated
     411 | @@ -396,9 +412,15 @@ static UniValue JSONRPCExecOne(JSONRPCRequest jreq, const UniValue& req)
     412 |  
     413 |          UniValue result = tableRPC.execute(jreq);
     414 |          rpc_result = JSONRPCReplyObj(result, NullUniValue, jreq.id);
     415 | -    }
     416 | -    catch (const UniValue& objError)
     417 | -    {
     418 | +    } catch (const int x) {
     419 | +        if (x != -1) {
     420 | +            throw;
    


    conscott commented at 1:18 PM on February 15, 2018:

    I commented in the univalue PR that this catch should probably be for a derived class of std::exception, not int. If this is the case, there will be no need to re-throw, as the std:exception handler will catch all else below.


    laanwj commented at 3:08 PM on February 15, 2018:

    Yes, throwing an int is really weird.

  6. in src/httprpc.cpp:194 in 6fe7afa92d outdated
     196 |  
     197 | -            // Send reply
     198 | -            strReply = JSONRPCReply(result, NullUniValue, jreq.id);
     199 | +        if (ret.isNull()) {
     200 | +            // JSON-RPC 2.0: return nothing at all
     201 | +            return false;
    


    jamesob commented at 6:38 PM on February 15, 2018:

    Does this create different behavior for degenerate returns when not passing -strictjsonrpcspec?

  7. in src/httprpc.cpp:198 in 6fe7afa92d outdated
     205 | -        } else if (valRequest.isArray())
     206 | -            strReply = JSONRPCExecBatch(jreq, valRequest.get_array());
     207 | -        else
     208 | -            throw JSONRPCError(RPC_PARSE_ERROR, "Top-level object parse error");
     209 | +        std::string strReply;
     210 | +        strReply = ret.write() + "\n";
    


    jamesob commented at 6:39 PM on February 15, 2018:

    Could just do std::string strReply(ret.write() + "\n");

  8. in src/rpc/protocol.cpp:29 in 6fe7afa92d outdated
      20 | @@ -21,11 +21,18 @@
      21 |   *
      22 |   * 1.0 spec: http://json-rpc.org/wiki/specification
      23 |   * 1.2 spec: http://jsonrpc.org/historical/json-rpc-over-http.html
      24 | + *
      25 | + * By passing -strictjsonrpcspec one can force Bitcoin to speak strict 2.0.
      26 | + *
      27 |   */
      28 |  
      29 | +bool fStrictJSONRPCSpec = false;
    


    jamesob commented at 6:41 PM on February 15, 2018:

    Should this have some kind of g prefix given it's a global? Also, may want to use snake_case.


    laanwj commented at 5:19 PM on April 23, 2018:

    Yes, should be g_strict_jsonrpc_spec or such w/ the new guidelines.

  9. jamesob commented at 6:48 PM on February 15, 2018: contributor

    Concept ACK https://github.com/bitcoin/bitcoin/pull/12435/commits/6fe7afa92d06b22b5e7447b51d64fa048c19d7a5

    Just a few small comments. As others have noted, should definitely figure out something to throw that isn't an int.

  10. in src/univalue/include/univalue.h:311 in a655c30cfd outdated
     305 | @@ -304,4 +306,9 @@ extern const UniValue NullUniValue;
     306 |  
     307 |  const UniValue& find_value(const UniValue& obj, const std::string& name, const bool throw_if_not_present = false);
     308 |  
     309 | +class KeyNotPresentError: public std::runtime_error {
     310 | +    public:
     311 | +        KeyNotPresentError(): std::runtime_error("KeyNotPresentError") {}
    


    conscott commented at 7:23 PM on February 15, 2018:

    These exception constructors are usually made explicit to prevent implicit conversion. You can see uint_error as an example.

  11. esotericnonsense commented at 9:35 PM on February 16, 2018: contributor

    Tests are failing on the latest commit. Looking in to it.

  12. esotericnonsense commented at 9:45 PM on February 16, 2018: contributor

    doh!

  13. jamesob commented at 9:51 PM on February 16, 2018: contributor

    Squash needed at some point.

  14. jonasschnelli commented at 11:20 AM on February 17, 2018: contributor

    Nice. Concept ACK. Travis is failing because of the subtree change that is separately opened in https://github.com/bitcoin-core/univalue/pull/12

  15. esotericnonsense force-pushed on Feb 19, 2018
  16. esotericnonsense force-pushed on Feb 19, 2018
  17. esotericnonsense force-pushed on Feb 19, 2018
  18. esotericnonsense commented at 12:05 AM on February 19, 2018: contributor

    My attempt to squash this seems to have gone awfully wrong. Trying to fix it up...

    edit: should be fine now.

  19. RPC: Strict JSON-RPC 2.0 compliance (gated behind flag) d87d60aaa9
  20. esotericnonsense force-pushed on Feb 19, 2018
  21. meshcollider commented at 2:52 PM on March 7, 2018: contributor
  22. in src/univalue/include/univalue.h:308 in d87d60aaa9
     304 | @@ -302,6 +305,6 @@ static inline bool json_isspace(int ch)
     305 |  
     306 |  extern const UniValue NullUniValue;
     307 |  
     308 | -const UniValue& find_value( const UniValue& obj, const std::string& name);
     309 | +const UniValue& find_value(const UniValue& obj, const std::string& name, enum UniValue::keystatus *status=NULL);
    


    laanwj commented at 5:13 PM on April 23, 2018:

    Are you sure this univalue API change is really necessary? You could find out if the key is there with an additional call to Univalue::exists? (or am I missing something)

  23. laanwj commented at 9:22 AM on April 24, 2018: member

    (travis failure is due to subtree check for univalue)

  24. DrahtBot closed this on Jul 20, 2018

  25. DrahtBot commented at 8:29 PM on July 20, 2018: contributor

    <!--5d09a71f8925f3f132321140b44b946d-->The last travis run for this pull request was 151 days ago and is thus outdated. To trigger a fresh travis build, this pull request should be closed and re-opened.

  26. DrahtBot reopened this on Jul 20, 2018

  27. DrahtBot added the label Needs rebase on Sep 10, 2018
  28. DrahtBot commented at 6:23 PM on September 10, 2018: contributor

    <!--cf906140f33d8803c4a75a2196329ecb-->Needs rebase

  29. laanwj commented at 2:25 PM on September 12, 2018: member

    Closing this for now, and adding "up for grabs" tag. Let me know if you want to start work on this again and I'll reopen…

  30. laanwj closed this on Sep 12, 2018

  31. laanwj added the label Up for grabs on Sep 12, 2018
  32. laanwj removed the label Needs rebase on Oct 24, 2019
  33. bitcoin locked this on Dec 16, 2021
  34. fanquake removed the label Up for grabs on Jul 23, 2025
  35. fanquake commented at 10:46 AM on July 23, 2025: member

    Removing "Up for grabs" as JSON 2.0 was done in #27101. cc @pinheadmz


github-metadata-mirror

This is a metadata mirror of the GitHub repository bitcoin/bitcoin. This site is not affiliated with GitHub. Content is generated from a GitHub metadata backup.
generated: 2026-04-21 21:15 UTC

This site is hosted by @0xB10C
More mirrored repositories can be found on mirror.b10c.me