Skip to content

test: refactor: various (de)serialization helpers cleanups/improvements#22257

Merged
maflcko merged 4 commits intobitcoin:masterfrom
theStack:202106-test-refactor-replace_manual_deser_with_fromtohex
Jun 24, 2021
Merged

test: refactor: various (de)serialization helpers cleanups/improvements#22257
maflcko merged 4 commits intobitcoin:masterfrom
theStack:202106-test-refactor-replace_manual_deser_with_fromtohex

Conversation

@theStack
Copy link
Contributor

@theStack theStack commented Jun 15, 2021

There are still many functional tests that perform conversions from a hex-string to a message object (deserialization) manually. This PR identifies all those instances and replaces them with a newly introduced helper tx_from_hex.

Instances were found via

  • git grep "deserialize.*BytesIO"

and some of them manually, when it were not one-liners.

Further, the helper ToHex was removed and simply replaced by .serialize().hex(), since now both variants are in use (sometimes even within the same test) and using the helper doesn't really have an advantage in readability. (see discussion #22257 (comment))

@theStack theStack force-pushed the 202106-test-refactor-replace_manual_deser_with_fromtohex branch 2 times, most recently from 3749742 to 82449a5 Compare June 15, 2021 23:07
@DrahtBot DrahtBot added the Tests label Jun 16, 2021
Copy link
Contributor

@klementtan klementtan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code review ACK 82449a55f16b7a2897ccef999c3f807ec1a1e591

Verified that .serialize().hex() to ToHex(obj) and obj.deserialize(BytesIO(hex_str_to_bytes(hex_string))) to FromHex(obj, hex_string) are the only changes made.

Also manually checked with rg '.deserialize' -g '*.py' and rg '\.hex\(\)' -g '*.py that there are no other places to refactor.

@fanquake
Copy link
Member

Concept ACK

@practicalswift
Copy link
Contributor

Concept ACK: nice cleanup!

Copy link
Member

@maflcko maflcko left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure about the long term goal of the ToHex replacement.

@DrahtBot
Copy link
Contributor

DrahtBot commented Jun 16, 2021

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

Conflicts

Reviewers, this pull request conflicts with the following ones:

If you consider this pull request important, please also help to review the conflicting pull requests. Ideally, start with the one that should be merged first.

@theStack theStack force-pushed the 202106-test-refactor-replace_manual_deser_with_fromtohex branch from 82449a5 to 6665fec Compare June 16, 2021 14:14
@theStack theStack changed the title test: refactor: use {From,To}Hex helpers for msg (de)serialization from/to hex test: refactor: use FromHex helper for msg serialization from hex, remove ToHex helper Jun 16, 2021
@theStack
Copy link
Contributor Author

Force-pushed with a change in the second commit: ToHex is removed and all instances are replaced with .serialize().hex(), as suggested in this discussion by MarcoFalke. Also adapted PR title and description accordingly.

Copy link
Contributor

@brunoerg brunoerg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tACK 6665fec39e8e42e05a7b602a24f0f6d312cf3991

Nice improv!

Ran test runner and every changed test several times on MacOS 11.3.

@theStack theStack force-pushed the 202106-test-refactor-replace_manual_deser_with_fromtohex branch 2 times, most recently from 02d2f96 to 18b780b Compare June 17, 2021 15:55
@theStack
Copy link
Contributor Author

theStack commented Jun 17, 2021

Added a commit 18b780b to improve the documentation of FromHex by mentioning the recommended reverse operation (as suggested by practicalswift) and changing to docstring format.

@practicalswift
Copy link
Contributor

cr ACK 18b780b6ce2b42245f067e922af5552bd8931be3

@theStack theStack force-pushed the 202106-test-refactor-replace_manual_deser_with_fromtohex branch from 18b780b to c43d96e Compare June 19, 2021 11:02
@theStack
Copy link
Contributor Author

Rebased on master.

@theStack
Copy link
Contributor Author

theStack commented Jun 19, 2021

Added another commit to introduce a tx_from_hex helper, as suggested by MarcoFalke (#22257 (comment)).
Tempted to squash this with the first commit to minimize reviewing burden, OTOH both replacing code with an existing helper and introducing a new one seems a bit much for a single commit. Opinions?

@theStack theStack force-pushed the 202106-test-refactor-replace_manual_deser_with_fromtohex branch 2 times, most recently from 8eb2389 to 65bf7b0 Compare June 19, 2021 20:33
@maflcko
Copy link
Member

maflcko commented Jun 20, 2021

I have a slight preference to squash to avoid repeatedly touching the same lines

@theStack theStack force-pushed the 202106-test-refactor-replace_manual_deser_with_fromtohex branch from 65bf7b0 to 446fd17 Compare June 20, 2021 12:05
@theStack theStack changed the title test: refactor: use FromHex helper for msg serialization from hex, remove ToHex helper test: refactor: various (de)serialization helpers cleanups/improvements Jun 20, 2021
@theStack
Copy link
Contributor Author

Restructured everything a bit (sorry for confusing to all who already reviewed!):

  • the first commit immediately introduces a helper tx_from_hex, which is used for instances where deserialization was done manually, and also for instances which used FromHex already
  • second commit gets rid of ToHex
  • third commit renames FromHex to from_hex via scripted diff (to follow coding guidelines / PEP8 naming)
  • fourth commit adds from_hex documentation

@maflcko
Copy link
Member

maflcko commented Jun 21, 2021

I've rewritten the scripted diff and it seems there is a silent merge conflict?

$ grd 8ed7dbd386562368e864fce23722676cd3637854 HEAD
1:  8c127a86be = 1:  8c127a86be test: introduce `tx_from_hex` helper for tx deserialization
2:  4223f18a8c = 2:  4223f18a8c test: remove `ToHex` helper, use .serialize().hex() instead
3:  8ed7dbd386 ! 3:  662223f4ba scripted-diff: test: rename `FromHex` to `from_hex`
    @@ Commit message
         scripted-diff: test: rename `FromHex` to `from_hex`
     
         -BEGIN VERIFY SCRIPT-
    -    git ls-files -- test/functional | xargs sed -i 's/FromHex/from_hex/g'
    +    sed -i 's/\<FromHex\>/from_hex/g' $(git grep -l FromHex)
         -END VERIFY SCRIPT-
     
      ## test/functional/feature_utxo_set_hash.py ##

@maflcko
Copy link
Member

maflcko commented Jun 21, 2021

review ACK 446fd1792ae3e7d0917df771bc0ccfa7c3c3b8f6 🏵

Show signature and timestamp

Signature:

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA512

review ACK 446fd1792ae3e7d0917df771bc0ccfa7c3c3b8f6 🏵
-----BEGIN PGP SIGNATURE-----

iQGzBAEBCgAdFiEE+rVPoUahrI9sLGYTzit1aX5ppUgFAlwqrYAACgkQzit1aX5p
pUiqPAv8DOhtbQZoGRJyrizrhOHso5pM2dbArc/E6N3UEPT/FpHLBHuZiEvgbKeA
CmPU4f9QQKrW4pLMIdF7i/R94Lwo7pNulA0697LyZMSypRSMZiLIq6WBSaY7U6+o
9Ao7AOf9gzBBhqs9qWeFrk0dSK8uaEcPMhe+poDOvzr+8BGyf49amhySCUFo9CRQ
gsjhPVt2/cEfX7Chg+iUVotW35t5lLid8NUBpelumapeeflZ0943cguv6QSy6IZ8
shtdjptvn/VFMi8nhqeBPTspKrQfY29cF3h0DBjPxFqWdERgdBhdNetG3wIGHTzQ
Hg/yf1FfsBs7Sk9MFDE+HW+u1i8LhmzCFkP3rRXI8qhY7RGCWKe4NFSJUJLi+4JB
CpPtsPSjJkomj6rqec34GqWDuGzkxOqpWM5j++xpCp46pzA4zkdtICecnp3VCcYv
8vGpeU2K6x2tKX7ZiTMSKZ3m7XqoF2x9RcP/NP2kWDVzdA4f184VXoJVT0DHRF5Z
bpeDtV39
=U71S
-----END PGP SIGNATURE-----

Timestamp of file with hash a3b9802ea42e8e0ba75b10e75c2b16701bc96227df1740b76ceb4b2ee5a11dda -

theStack and others added 4 commits June 21, 2021 14:28
`FromHex` is mostly used for transactions, so we introduce a
shortcut `tx_from_hex` for `FromHex(CTransaction, hex_str)`.
-BEGIN VERIFY SCRIPT-
sed -i 's/\<FromHex\>/from_hex/g' $(git grep -l FromHex)
-END VERIFY SCRIPT-

Co-authored-by: MarcoFalke <[email protected]>
@theStack theStack force-pushed the 202106-test-refactor-replace_manual_deser_with_fromtohex branch from 446fd17 to bdb8b9a Compare June 21, 2021 12:50
@theStack
Copy link
Contributor Author

I've rewritten the scripted diff and it seems there is a silent merge conflict?

$ grd 8ed7dbd386562368e864fce23722676cd3637854 HEAD
1:  8c127a86be = 1:  8c127a86be test: introduce `tx_from_hex` helper for tx deserialization
2:  4223f18a8c = 2:  4223f18a8c test: remove `ToHex` helper, use .serialize().hex() instead
3:  8ed7dbd386 ! 3:  662223f4ba scripted-diff: test: rename `FromHex` to `from_hex`
    @@ Commit message
         scripted-diff: test: rename `FromHex` to `from_hex`
     
         -BEGIN VERIFY SCRIPT-
    -    git ls-files -- test/functional | xargs sed -i 's/FromHex/from_hex/g'
    +    sed -i 's/\<FromHex\>/from_hex/g' $(git grep -l FromHex)
         -END VERIFY SCRIPT-
     
      ## test/functional/feature_utxo_set_hash.py ##

Oops, I completely forgot that we also have Python scripts outside of test/functional that use helpers from the test framework and are thus affected by the changes (namely contrib/signet/miner).

Force-pushed with changes including it (affects all but the last commit), used your scripted-diff and add you as co-author.

(Not directly connected to this PR, but it would probably make sense to call the signet mining tool in one of the functional tests, to let it run through CI?)

@maflcko
Copy link
Member

maflcko commented Jun 23, 2021

review re-ACK bdb8b9a 😁

Show signature and timestamp

Signature:

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA512

review re-ACK bdb8b9a347e68f80a2e8d44ce5590a2e8214b6bb 😁
-----BEGIN PGP SIGNATURE-----

iQGzBAEBCgAdFiEE+rVPoUahrI9sLGYTzit1aX5ppUgFAlwqrYAACgkQzit1aX5p
pUjrswv/bCHcA6Zpk9Gv9fkRzBtmSfbuLE2ifDg+RGN2prKClfrlim2Kx9LhjTK+
BoeGGWwMAUdg+SHdNi5RMhmiRrSB+4JMtxw3TY6COBjP/QYdmzyySle/xNGwAoov
0CGZsEZaqiC5QPFJe6pLZJMRJET8iJR9gLaNNkWbEkUllqkjdvaIuGCJjKC4k1ld
epdUxh+yxg5U/FPMgSR5jPew1GBMpb6XPk0YSJ2yPimCBlSeaIH9X1XlYNix1WPv
tn6rCN4ZUkufPqb8x0R//y/x3T0lMOgjOsnODR5BZtUdV0P/GK7ZTRa9zBtz/Xa4
D7somqqh5TKOBvNJounyLS1FO3zXBoE6Kbr5xZQinob7fpTlEf9nvS9yWNtE+tyZ
em+74lyfdHMiBBWmlEIIiHaOrrG6pPOXQ6zn/GSEkrlHjQLtLJ2a+YoAzO6hc6Af
RUA/NKPHz8NPHcOzpbtPTs+sYloLrtbp8O5dUsgyZ7JWTT0hAEwIpahQsEoi9j4b
lnHvly/x
=rKMS
-----END PGP SIGNATURE-----

Timestamp of file with hash 1cd7510533208f2380d21b0afee7343c273d929ac88f1e044e3c4c7724daab3b -

@maflcko
Copy link
Member

maflcko commented Jun 23, 2021

(Not directly connected to this PR, but it would probably make sense to call the signet mining tool in one of the functional tests, to let it run through CI?)

Agree

@maflcko maflcko merged commit d6a5916 into bitcoin:master Jun 24, 2021
sidhujag pushed a commit to syscoin/syscoin that referenced this pull request Jun 24, 2021
@theStack theStack deleted the 202106-test-refactor-replace_manual_deser_with_fromtohex branch July 31, 2021 20:06
gwillen pushed a commit to ElementsProject/elements that referenced this pull request Jun 1, 2022
Just as mechanical and annoying as it looks.
@bitcoin bitcoin locked as resolved and limited conversation to collaborators Aug 16, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants