refactor: Replace remaining binascii method calls #22633

pull Zero-1729 wants to merge 1 commits into bitcoin:master from Zero-1729:follow-ups-to-22605 changing 7 files +14 −19
  1. Zero-1729 commented at 11:18 AM on August 5, 2021: contributor

    This PR removes the remaining binascii method calls outside test/functional and test_framework, as pointed out here #22619#pullrequestreview-722153458.

    Follow-up to #22593 and #22619 Closes #22605

  2. DrahtBot added the label Refactoring on Aug 5, 2021
  3. DrahtBot added the label Scripts and tools on Aug 5, 2021
  4. Zero-1729 force-pushed on Aug 8, 2021
  5. Zero-1729 commented at 12:19 PM on August 8, 2021: contributor

    Diff from 3a2ad3b to 33b8a31: Updated Python2 code sample to Python3 and removed binascii.hexlify call.

    Now git grep binascii no longer yields any result.

  6. in contrib/zmq/zmq_sub.py:60 in 33b8a3107f outdated
      56 | @@ -58,18 +57,18 @@ async def handle(self) :
      57 |              sequence = str(struct.unpack('<I', seq)[-1])
      58 |          if topic == b"hashblock":
      59 |              print('- HASH BLOCK ('+sequence+') -')
      60 | -            print(binascii.hexlify(body))
      61 | +            print(body.hex().encode())
    


    theStack commented at 10:40 PM on August 8, 2021:

    nit: Though this is right from a refactoring perspective, I don't think the encode() part is needed here (and in the substitutions below). This leads to displaying an example hex-string '1234abcd' as

    b'1234abcd'
    

    instead of

    1234abcd
    

    The latter seems more natural and was also the behaviour of print(binascii.hexlify(...)) on Python 2.7 (which was used when this file was first introduced, see commit e6a14b64d665eb1fafd03a6bbc8d14597ce1c83c), when there was no 'bytes' type yet.

                print(body.hex())
    

    Zero-1729 commented at 1:34 AM on August 9, 2021:

    Ahh, makes sense now, the issue was wrongly assuming it was Py3 code to begin with :)

    Fixed to replicate the original Py2 behavior (i.e. printing the hex-strings).

  7. theStack approved
  8. theStack commented at 10:53 PM on August 8, 2021: member

    Code-review ACK 33b8a3107fc2d647b2c575f4c081db51f6041e16

    It's nice to see that with this PR, there is now only one method left for hex-string/bytes conversions in the functional tests and contrib scripts. Special kudos for also replacing the Python code comment in serfloat_tests.cpp. I tested this directly in the Python interpreter and it works as expected, i.e. the statement yields True.

  9. Zero-1729 force-pushed on Aug 9, 2021
  10. Zero-1729 commented at 1:35 AM on August 9, 2021: contributor

    Diff from 33b8a31 to b95d5ad: addressed comment above.

    Thanks @theStack for the background reference regarding contrib/zmq/zmq_sub.py!

  11. theStack approved
  12. theStack commented at 8:51 AM on August 9, 2021: member

    re-ACK b95d5ad3676a8cc42165ab4d51a2aae7c7b059fb 💯

  13. josibake commented at 2:40 PM on August 12, 2021: member

    ACK https://github.com/bitcoin/bitcoin/pull/22633/commits/b95d5ad3676a8cc42165ab4d51a2aae7c7b059fb

    +1 for consistency! did a code review and everything looks good. also ran the standalone scripts where possible and tested a few individual functions (e.g python3 -c 'from rpcauth import generate_salt; print(generate_salt(10))'). copied the python snippet from the comment and ran it, everything working as expected

  14. josibake approved
  15. laanwj renamed this:
    refactor: follow-ups to 22619
    refactor: Replace remaining binascii method calls
    on Aug 16, 2021
  16. laanwj commented at 3:21 PM on August 16, 2021: member

    Concept ACK. Let's be sure that this covers all and is the last PR in the series, it's been dragging on for a while.

  17. Zero-1729 commented at 3:53 PM on August 16, 2021: contributor

    I think this is the last PR in this series. No other instances of binascii remain. Unless @theStack has any other suggestions that need to be added?

  18. theStack commented at 5:17 PM on August 16, 2021: member

    Let's be sure that this covers all and is the last PR in the series, it's been dragging on for a while.

    Agreed!

    I think this is the last PR in this series. No other instances of binascii remain. Unless @theStack has any other suggestions that need to be added?

    Doing a search in test/function via "git grep -i hex", I unfortunately found yet another variant of converting bin-to-hex that should be fixed (I verified via git grep hex_codec that this is the only place it is used):

    diff --git a/test/functional/test_framework/messages.py b/test/functional/test_framework/messages.py
    index 6e57107f8..65d90f844 100755
    --- a/test/functional/test_framework/messages.py
    +++ b/test/functional/test_framework/messages.py
    @@ -19,7 +19,6 @@ Classes use __slots__ to ensure extraneous attributes aren't accidentally added
     by tests, compromising their intended effect.
     """
     from base64 import b32decode, b32encode
    -from codecs import encode
     import copy
     import hashlib
     from io import BytesIO
    @@ -681,7 +680,7 @@ class CBlockHeader:
                 r += struct.pack("<I", self.nBits)
                 r += struct.pack("<I", self.nNonce)
                 self.sha256 = uint256_from_str(hash256(r))
    -            self.hash = encode(hash256(r)[::-1], 'hex_codec').decode('ascii')
    +            self.hash = hash256(r)[::-1].hex()
    
         def rehash(self):
             self.sha256 = None
    
    

    This one is not using binascii (thus not matching the PR title), but should still be tackled in this PR for the reasons laanwj mentioned. After this I'm pretty sure we have them all and #22605 can be closed.

  19. refactor: replace remaining binascii method calls 021daedfa1
  20. Zero-1729 force-pushed on Aug 16, 2021
  21. Zero-1729 commented at 6:38 PM on August 16, 2021: contributor

    Nice catch! Updated.

    Diff b95d5ad -> 021daed:

    a/test/functional/test_framework/messages.py

        - from codecs import encode
        ...
    
        -            self.hash = encode(hash256(r)[::-1], 'hex_codec').decode('ascii')
        +            self.hash = hash256(r)[::-1].hex()
    
        ...
    
  22. theStack approved
  23. theStack commented at 7:38 PM on August 16, 2021: member

    re-ACK 021daedfa100defad553d69cd3d628aaa5bc7caf

  24. fanquake commented at 3:57 AM on August 17, 2021: member

    cc @practicalswift @tryphe given you both reviewed the parent PR.

  25. MarcoFalke merged this on Aug 21, 2021
  26. MarcoFalke closed this on Aug 21, 2021

  27. jonatack commented at 3:52 PM on August 21, 2021: member

    Posthumous ACK, modulo could have updated the Python example in src/test/serfloat_tests.cpp -- git grep binascii takes you there.

    Edit: oops, I ran grep from /src instead of root, there are some other ones, idem for git grep hexlify. Maybe a follow-up if it matters.

  28. Zero-1729 commented at 4:40 PM on August 21, 2021: contributor

    Posthumous ACK, modulo could have updated the Python example in src/test/serfloat_tests.cpp -- git grep binascii takes you there.

    This PR also tackled the Python code sample in src/test/serfloat_tests.cpp.

    Edit: oops, I ran grep from /src instead of root, there are some other ones, idem for git grep hexlify. Maybe a follow-up if it matters.

    Just pulled master locally, running both git grep binascii and git grep hexlify in the root shows nothing for me. Also tried it in src and got no results. @jonatack are you still getting any results after f5a406f003a060325128db47552089b3254044e3?

  29. jonatack commented at 5:15 PM on August 21, 2021: member

    @Zero-1729 You're right! I grepped from the PR branch (edit: nope, another PR branch :sweat_smile:) sorry for the noise.

  30. Zero-1729 commented at 5:22 PM on August 21, 2021: contributor

    @jonatack No worries, happy to help! 🙏🏾

  31. sidhujag referenced this in commit ab83d376bc on Aug 22, 2021
  32. DrahtBot locked this on Aug 21, 2022

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-14 21:14 UTC

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