tests: Add regtest for JSON-RPC batch calls #14777

pull domob1812 wants to merge 1 commits into bitcoin:master from domob1812:batch-rpc changing 2 files +47 −0
  1. domob1812 commented at 10:59 AM on November 21, 2018: contributor

    This adds a new regtest file interface_rpc.py, containing a test for batch JSON-RPC requests. Those were previously not tested at all. Tests for basic requests are not really necessary, as those are used anyway in lots of other regtests.

    The existing interface_http.py file is more about the underlying HTTP connection, so adding a new interface file for the JSON-RPC specific things makes sense.

  2. in test/functional/interface_rpc.py:26 in 0c1597beb2 outdated
      29 | +            {"method": "getblockcount", "id": 1},
      30 | +            {"method": "getbestblockhash", "id": 2},
      31 | +            # Request that will fail.  The whole batch request should still
      32 | +            # work fine.
      33 | +            {"method": "invalidmethod", "id": 3},
      34 | +        ]
    


    promag commented at 11:11 AM on November 21, 2018:

    nit, add another call here or move one of the above?


    domob1812 commented at 2:14 PM on November 21, 2018:

    What other call do you mean here? Or is this comment obsolete as well with @laanwj's remark below?


    promag commented at 2:56 PM on November 21, 2018:

    I was suggesting to put the invalid method between valid methods.


    domob1812 commented at 3:07 PM on November 21, 2018:

    I see, that makes sense - changed.

  3. promag commented at 11:13 AM on November 21, 2018: member

    Have you considered adding batch support to AuthServiceProxy?

    Edit: nervermind, see @laanwj comment below #14777#pullrequestreview-177211024.

  4. fanquake added the label Tests on Nov 21, 2018
  5. laanwj commented at 12:18 PM on November 21, 2018: member

    There's a test for batching added in 4526d21, but it's part of the wallet tests so adding a general one is probably good.

  6. in test/functional/interface_rpc.py:35 in 0c1597beb2 outdated
      30 | +            {"method": "getbestblockhash", "id": 2},
      31 | +            # Request that will fail.  The whole batch request should still
      32 | +            # work fine.
      33 | +            {"method": "invalidmethod", "id": 3},
      34 | +        ]
      35 | +        results = self.send_request(request)
    


    laanwj commented at 12:19 PM on November 21, 2018:

    you're not using the .batch() functionality of the RPC proxy?


    domob1812 commented at 2:13 PM on November 21, 2018:

    No, as I didn't know it exists. Will update the code later.


    domob1812 commented at 3:08 PM on November 21, 2018:

    Changed - that's indeed much simpler!

  7. domob1812 force-pushed on Nov 21, 2018
  8. domob1812 force-pushed on Nov 21, 2018
  9. in test/functional/interface_rpc.py:44 in 41970315e4 outdated
      39 | +        assert_equal(result_by_id[3]['error'], None)
      40 | +        assert_equal(result_by_id[3]['result'], self.node.getbestblockhash())
      41 | +
      42 | +    def run_test(self):
      43 | +        self.node = self.nodes[0]
      44 | +        self.node.generate(10)
    


    promag commented at 3:12 PM on November 21, 2018:

    Remove this (unnecessary for the test and also deprecated) and update accordingly?


    domob1812 commented at 3:22 PM on November 21, 2018:

    Done

  10. domob1812 force-pushed on Nov 21, 2018
  11. laanwj commented at 3:29 PM on November 21, 2018: member

    utACK 06ffc4b19b8ca8a8173caf5d3e90d0651e22d72d

  12. in test/functional/interface_rpc.py:43 in 06ffc4b19b outdated
      38 | +
      39 | +        assert_equal(result_by_id[3]['error'], None)
      40 | +        assert_equal(result_by_id[3]['result'], self.node.getbestblockhash())
      41 | +
      42 | +    def run_test(self):
      43 | +        self.node = self.nodes[0]
    


    promag commented at 3:34 PM on November 21, 2018:

    nit, could just use self.nodes[0] above.


    domob1812 commented at 3:48 PM on November 21, 2018:

    I chose to use self.node to make it more explicit in the code that there is only one and no index is needed, but I guess it is clear without that as well. Changed.

  13. in test/functional/interface_rpc.py:18 in 06ffc4b19b outdated
      13 | +        self.setup_clean_chain = True
      14 | +
      15 | +    def test_batch_request(self):
      16 | +        self.log.info("Testing basic JSON-RPC batch request...")
      17 | +
      18 | +        request = [
    


    promag commented at 3:34 PM on November 21, 2018:

    nit, could inline.


    domob1812 commented at 3:48 PM on November 21, 2018:

    Done

  14. in test/functional/interface_rpc.py:25 in 06ffc4b19b outdated
      20 | +            {"method": "getblockcount", "id": 1},
      21 | +            # Request that will fail.  The whole batch request should still
      22 | +            # work fine.
      23 | +            {"method": "invalidmethod", "id": 2},
      24 | +            # Another call that should succeed.
      25 | +            {"method": "getbestblockhash", "id": 3},
    


    promag commented at 3:36 PM on November 21, 2018:

    nit, could use getblockchaininfo to avoid getbestblockhash below.


    domob1812 commented at 3:49 PM on November 21, 2018:

    Why would that be better / why should we "avoid" getbestblockhash? A function that just returns a string seems simpler to me than one that returns an object (that's why I chose it), but it doesn't really matter. So I'm happy to change it, but I don't see why that would be better?


    promag commented at 4:19 PM on November 21, 2018:

    Just to save another call when it's irrelevant for this test.


    domob1812 commented at 4:26 PM on November 21, 2018:

    I see - but for that, getblockchaininfo won't help, either, as I would also have to call that on the node again to get the "expected" data. I could just call getblockcount again - would you prefer that?

    Or alternatively just check that there is some result, without checking what it is.


    promag commented at 4:32 PM on November 21, 2018:

    Exactly, doesn't matter full expected result.


    domob1812 commented at 4:41 PM on November 21, 2018:

    Ok, I've changed the code now to just check that result is not None without checking what it actually is (so that no second RPC is needed for that).

  15. promag commented at 3:37 PM on November 21, 2018: member

    utACK 06ffc4b, just some nits.

  16. domob1812 force-pushed on Nov 21, 2018
  17. promag commented at 4:26 PM on November 21, 2018: member

    Where is Travis?

  18. domob1812 force-pushed on Nov 21, 2018
  19. in test/functional/interface_rpc.py:28 in 4fd7c4b389 outdated
      23 | +            {"method": "invalidmethod", "id": 2},
      24 | +            # Another call that should succeed.
      25 | +            {"method": "getbestblockhash", "id": 3},
      26 | +        ])
      27 | +
      28 | +        result_by_id = {}
    


    promag commented at 4:58 PM on November 21, 2018:

    What is the reason to index by id? Could do something like:

    assert_equal(result[0]["id"], 1)
    assert_equal(result[0]["error"], None)
    assert_equal(result[0]["result"], 0)
    ...
    

    since the response order matters.


    domob1812 commented at 6:42 PM on November 21, 2018:

    One (minor) reason is that this verifies indirectly that the server handles the id field correctly. But the main reason is to make sure that we match up the right responses to requests. According to my understanding of the spec, the order is not guaranteed to be the same as in the request. (It is for Bitcoin Core's implementation, but that may change in the future.)


    promag commented at 6:54 PM on November 21, 2018:

    Indeed! From the specs:

    The Server MAY process a batch rpc call as a set of concurrent tasks, processing them in any order and with any width of parallelism.

    The Response objects being returned from a batch call MAY be returned in any order within the Array. The Client SHOULD match contexts between the set of Request objects and the resulting set of Response objects based on the id member within each Object.

    This thread can be resolved then.

  20. in test/functional/interface_rpc.py:19 in 4fd7c4b389 outdated
      14 | +
      15 | +    def test_batch_request(self):
      16 | +        self.log.info("Testing basic JSON-RPC batch request...")
      17 | +
      18 | +        results = self.nodes[0].batch([
      19 | +            # A basic requests that will work fine.
    


    promag commented at 4:59 PM on November 21, 2018:

    Bad grammar?


    domob1812 commented at 6:43 PM on November 21, 2018:

    Good catch, that is a left over from before the order was changed. Updated.

  21. promag commented at 5:00 PM on November 21, 2018: member

    Thanks for the quick fixes @domob1812, looks great! just left 2 more comments.

  22. Add regtest for JSON-RPC batch calls.
    This adds a new regtest file 'interface_rpc.py', containing a test for
    batch JSON-RPC requests.  Those were previously not tested at all.  Tests
    for basic requests are not really necessary, as those are used anyway
    in lots of other regtests.
    
    The existing interface_http.py file is more about the underlying HTTP
    connection, so adding a new interface file for the JSON-RPC specific
    things makes sense.
    3d2c7d6f94
  23. domob1812 force-pushed on Nov 21, 2018
  24. promag commented at 6:56 PM on November 21, 2018: member

    utACK 3d2c7d6.

  25. Empact commented at 2:26 PM on November 22, 2018: member

    utACK 3d2c7d6

  26. MarcoFalke merged this on Nov 22, 2018
  27. MarcoFalke closed this on Nov 22, 2018

  28. MarcoFalke referenced this in commit 3dda4c5f03 on Nov 22, 2018
  29. domob1812 deleted the branch on Nov 22, 2018
  30. Munkybooty referenced this in commit 3ed8288e7e on Jul 29, 2021
  31. Munkybooty referenced this in commit 4e82f0bc7f on Jul 29, 2021
  32. Munkybooty referenced this in commit af3bb18bbe on Aug 3, 2021
  33. Munkybooty referenced this in commit 7c8f0e5397 on Aug 5, 2021
  34. Munkybooty referenced this in commit cdad567faa on Aug 8, 2021
  35. Munkybooty referenced this in commit b781865e58 on Aug 9, 2021
  36. Munkybooty referenced this in commit b45c353c5c on Aug 11, 2021
  37. 5tefan referenced this in commit 2aea05bd26 on Aug 12, 2021
  38. MarcoFalke locked this on Sep 8, 2021

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 18:15 UTC

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