test: Fix authproxy named args debug logging #31955

pull maflcko wants to merge 1 commits into bitcoin:master from maflcko:2502-test-fix-auth changing 1 files +10 −5
  1. maflcko commented at 9:46 pm on February 25, 2025: member

    In Python the meaning of args or argsn is that argsn is fully ignored when args is a list with at least one element. However, the RPC server accepts mixed positional and named args in the same RPC.

    Fix the debug log by always printing both. Also, add a new _json_dumps helper to avoid bloated code.

    Can be tested via --tracerpc on a call that uses named args mixed with positional args.

  2. test: Fix authproxy named args debug logging fac1dd9dff
  3. DrahtBot commented at 9:46 pm on February 25, 2025: contributor

    The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.

    Code Coverage & Benchmarks

    For details see: https://corecheck.dev/bitcoin/bitcoin/pulls/31955.

    Reviews

    See the guideline for information on the review process.

    Type Reviewers
    ACK i-am-yuvi, rkrux, musaHaruna, ryanofsky

    If your review is incorrectly listed, please react with 👎 to this comment and the bot will ignore it on the next update.

  4. DrahtBot added the label Tests on Feb 25, 2025
  5. i-am-yuvi commented at 7:20 pm on February 27, 2025: contributor

    Approach ACK

    Correct me if I misunderstood:

    In Python, when a function accepts both positional(*args) and keyword(**argsn) arguments, for which args is handled as a tuple and argsn as dictionary where Python doesn’t merge them automatically into a combined list.

    The issue

    The RPC server accepts a single call which includes both positional as well as keyword. That means if a caller provides both, the server expects to receive both types together. In that case, the original code(logging part) ignores the argsn when there were args present - hiding the fact that both kinds were sent. This leads to debugging ambiguity because the debug log might not show the complete set of parameters the RPC server receives.

  6. in test/functional/test_framework/authproxy.py:108 in fac1dd9dff
    104@@ -105,14 +105,19 @@ def _request(self, method, path, postdata):
    105         self.__conn.request(method, path, postdata, headers)
    106         return self._get_response()
    107 
    108+    def _json_dumps(self, obj):
    


    i-am-yuvi commented at 7:23 pm on February 27, 2025:
    This helper function makes sense that instead of using repetitive code which might cause inconsistencies this will wrap json.dumps so that every part of code uses uniform serialization.
  7. i-am-yuvi commented at 7:35 pm on February 27, 2025: contributor

    Tested ACK fac1dd9dffba1033245c283bc0468e801c14e910

    I could see the difference: -1-> getblockcount {} - without changes

    -1-> getblockcount [] {} - with changes

  8. maflcko assigned ryanofsky on Feb 28, 2025
  9. rkrux commented at 12:26 pm on February 28, 2025: contributor

    In Python the meaning of args or argsn is that argsn is fully ignored when args is a list with at least one element.

    The following code prints argsn fine in this scenario.

    Code

    0def f(name, *args, **argsn):
    1    print(name)
    2    print(args)
    3    print(argsn)
    4
    5f("titlehere", [1,2], a=10, b=30)
    

    Output

    0titlehere
    1([1, 2],)
    2{'a': 10, 'b': 30}
    

    I don’t think it’s a generic Python thing. I believe argsn not being logged was due to the following older line, if args is there (not empty list notably), argsn is ignored in the log.

    0json.dumps(args or argsn, default=serialization_fallback, ensure_ascii=self.ensure_ascii),
    
  10. maflcko commented at 1:07 pm on February 28, 2025: member
    @rkrux Not sure if I understand your question, but if you used args or argsn in your print: print(args or argsn), then argsn is omitted from the print if the args list has at least one element. This should be true in general for all versions of Python that are supported. The changes in this pull request are fixing the problem, as explained in the pull request description.
  11. rkrux commented at 1:19 pm on February 28, 2025: contributor

    Yes, correct, I agree. The PR description statement I highlighted in my comment led me to believe that argsn is ignored always in any function if args is a list with at least 1 element, which I found to be confusing to read and hence verified it with an example. The issue as I understand is with the usage of args or argsn as the first argument to the json.dumps function, which this PR correctly fixes.

    The only concern is with the first statement in the PR description, which is too generic.

    In Python the meaning of args or argsn is that argsn is fully ignored when args is a list with at least one element.

  12. maflcko commented at 1:27 pm on February 28, 2025: member

    There may be a formatting issue. For me the pull description says (with code blocks):

    In Python the meaning of args or argsn is that argsn is fully ignored when args is a list with at least one element.

    However, for you it seems to say:

    In Python the meaning of args or argsn is that argsn is fully ignored when args is a list with at least one element.

    Obviously, the two have different meaning (and the second doesn’t really make sense). I am not sure how to fix this. What client are you using to access the description?

  13. rkrux commented at 1:56 pm on February 28, 2025: contributor
    Oh interesting, I see where the confusion is. I do see the same description as you but I didn’t read that particular part as a code expression instead read it as an English phrase (TFW Python does read like English), which made it sound too generic to me. Never mind.
  14. in test/functional/test_framework/authproxy.py:204 in fac1dd9dff
    201@@ -197,7 +202,7 @@ def _get_response(self):
    202         response = json.loads(responsedata, parse_float=decimal.Decimal)
    203         elapsed = time.time() - req_start_time
    204         if "error" in response and response["error"] is None:
    


    rkrux commented at 1:17 pm on March 3, 2025:
    Older code and doesn’t need to be fixed in this PR but this line seems buggy to me, Idk how these 2 conditions can be true at the same time.

    ryanofsky commented at 10:06 pm on March 10, 2025:

    re: #31955 (review)

    Older code and doesn’t need to be fixed in this PR but this line seems buggy to me, Idk how these 2 conditions can be true at the same time.

    The code and behavior here could probably be improved, but this is just trying to reduce noise in log output and avoid showing "error": null in JSONRPC v1 responses when the error field is present and null.

  15. in test/functional/test_framework/authproxy.py:207 in fac1dd9dff
    201@@ -197,7 +202,7 @@ def _get_response(self):
    202         response = json.loads(responsedata, parse_float=decimal.Decimal)
    203         elapsed = time.time() - req_start_time
    204         if "error" in response and response["error"] is None:
    205-            log.debug("<-%s- [%.6f] %s" % (response["id"], elapsed, json.dumps(response["result"], default=serialization_fallback, ensure_ascii=self.ensure_ascii)))
    206+            log.debug("<-%s- [%.6f] %s" % (response["id"], elapsed, self._json_dumps(response["result"])))
    207         else:
    208             log.debug("<-- [%.6f] %s" % (elapsed, responsedata))
    


    rkrux commented at 1:21 pm on March 3, 2025:

    The RPC request seems to be logged with the AuthServiceProxy.__id_count but the corresponding non erroneous response doesn’t seem to be. @maflcko Do you know the reason for it and should we improve it later?

    0-30-> getreceivedbyaddress
    1<-- [0.000553] {"jsonrpc":"2.0"...
    

    rkrux commented at 1:25 pm on March 3, 2025:
    Also it would be easier on the eyes to log the suffix s after the elapsed time but something maybe for another PR.

    ryanofsky commented at 10:11 pm on March 10, 2025:

    re: #31955 (review)

    The RPC request seems to be logged with the AuthServiceProxy.__id_count but the corresponding non erroneous response doesn’t seem to be. @maflcko Do you know the reason for it and should we improve it later?

    It looks like response["id"] is explicitly logged in the v1 case and responsedata which includes the "id" field is logged in the v2/error case, so the id should be present in both cases. Probably output could be improved and make more consistent though.

  16. rkrux approved
  17. rkrux commented at 1:28 pm on March 3, 2025: contributor

    tACK fac1dd9dffba1033245c283bc0468e801c14e910

    I ran the wallet_timelock test by passing both positional and named arguments in the first listunspent RPC.

    0coin_before = node.listunspent(1, maxconf=1, include_unsafe=True)
    

    I can see both kinds of arguments being logged.

    0-28-> listunspent [1] {"maxconf": 1, "include_unsafe": true}
    1<-- [0.000553] {"jsonrpc":"2.0","result":[{"txid":"c0bbea0efd29a1c72e264efeb4eb53096741ebf3d309cabcec343b96e21a59ed","vout":0,"address":"bcrt1q8npfane52r5fn45yp8murqrhce7vdff24a25mf","label":"timelock⌛🔓"...
    

    Mentioned few points that can be addressed in other PRs.

  18. musaHaruna commented at 8:34 am on March 8, 2025: none

    Tested ACK fac1dd9

    with change: ``DEBUG:BitcoinRPC:-1-> getblockhash [] {} { “jsonrpc”: “2.0”, “method”: “getblockhash”, “params”: {}, “id”: 1 }

    without change: DEBUG:RPC Test:Testing Positional Arguments Only DEBUG:BitcoinRPC:-1-> getblockhash {} { "jsonrpc": "2.0", "method": "getblockhash", "params": {}, "id": 1 } Also the new helper function helped makes the json.dumps reusable, and avoid repetition, it made the funtionality easier to graps

  19. ryanofsky approved
  20. ryanofsky commented at 10:21 pm on March 10, 2025: contributor
    Code review ACK fac1dd9dffba1033245c283bc0468e801c14e910. Thanks for logging fix. This change should have been included in #19762
  21. fanquake merged this on Mar 12, 2025
  22. fanquake closed this on Mar 12, 2025

  23. maflcko deleted the branch on Mar 12, 2025

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: 2025-03-29 06:12 UTC

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