Skip to content

Conversation

maflcko
Copy link
Member

@maflcko maflcko commented Feb 7, 2024

struct.pack has many issues:

  • The format string consists of characters that may be confusing and may need to be looked up in the documentation, as opposed to using easy to understand self-documenting code.

This lead to many test bugs, which weren't hit, which is fine, but still confusing. Ref: #29400, #29399, #29363, fa3886b, ...

Fix all issues by using the built-in int helpers to_bytes via a scripted diff.

Review notes:

  • For struct.pack and int.to_bytes the error behavior is the same, although the error messages are not identical.
  • Two struct.pack remain. One for float serialization in a C++ code comment, and one for native serialization.

@DrahtBot
Copy link
Contributor

DrahtBot commented Feb 7, 2024

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

Code Coverage

For detailed information about the code coverage, see the test coverage report.

Reviews

See the guideline for information on the review process.

Type Reviewers
ACK rkrux, theStack, achow101
Concept ACK epiccurious

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

Conflicts

Reviewers, this pull request conflicts with the following ones:

  • #29325 (consensus: Store transaction nVersion as uint32_t by achow101)
  • #27865 (wallet: Track no-longer-spendable TXOs separately by achow101)
  • #27286 (wallet: Keep track of the wallet's own transaction outputs in memory by achow101)

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.

@DrahtBot DrahtBot changed the title test: Remove struct.pack from almost all places test: Remove struct.pack from almost all places Feb 7, 2024
@DrahtBot DrahtBot added the Tests label Feb 7, 2024
@maflcko maflcko marked this pull request as draft February 7, 2024 12:07
@DrahtBot
Copy link
Contributor

DrahtBot commented Feb 7, 2024

🚧 At least one of the CI tasks failed. Make sure to run all tests locally, according to the
documentation.

Possibly this is due to a silent merge conflict (the changes in this pull request being
incompatible with the current code in the target branch). If so, make sure to rebase on the latest
commit of the target branch.

Leave a comment here, if you need help tracking down a confusing failure.

Debug: https://github.com/bitcoin/bitcoin/runs/21315900160

@epiccurious
Copy link
Contributor

Concept ACK. Is this ready for testing?

@maflcko maflcko force-pushed the 2402-nuke-struct-pack- branch from ceb63b3 to 830e908 Compare February 12, 2024 12:22
@maflcko maflcko marked this pull request as ready for review February 22, 2024 07:47
@maflcko maflcko force-pushed the 2402-nuke-struct-pack- branch from 830e908 to fa855d8 Compare February 22, 2024 07:48
MarcoFalke added 4 commits May 7, 2024 15:40
* Add () around some int values
* Remove b-prefix from strings

This is needed for the scripted diff to work.
This is done in prepration for the scripted diff, which can not deal
with those lines.
-BEGIN VERIFY SCRIPT-
 sed -i --regexp-extended 's!struct.pack\(.<?B., (.*)\)!\1.to_bytes(1, "little")!g'             $( git grep -l struct.pack )
 sed -i --regexp-extended 's!struct.pack\(.<I., (.*)\)!\1.to_bytes(4, "little")!g'              $( git grep -l struct.pack )
 sed -i --regexp-extended 's!struct.pack\(.<H., (.*)\)!\1.to_bytes(2, "little")!g'              $( git grep -l struct.pack )
 sed -i --regexp-extended 's!struct.pack\(.<i., (.*)\)!\1.to_bytes(4, "little", signed=True)!g' $( git grep -l struct.pack )
 sed -i --regexp-extended 's!struct.pack\(.<q., (.*)\)!\1.to_bytes(8, "little", signed=True)!g' $( git grep -l struct.pack )
 sed -i --regexp-extended 's!struct.pack\(.>H., (.*)\)!\1.to_bytes(2, "big")!g'                 $( git grep -l struct.pack )
-END VERIFY SCRIPT-
@maflcko maflcko force-pushed the 2402-nuke-struct-pack- branch from fa855d8 to fa52e13 Compare May 7, 2024 14:25
Copy link
Contributor

@rkrux rkrux left a comment

Choose a reason for hiding this comment

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

tACK fa52e13

Make successful, so are all the functional tests.

Overall I agree with the intent to get rid of struct.pack usage, which requires looking up the documentation most of the time and might be prone to errors. Using the alternate approach of to_bytes is more self-documenting and preferable to me.

@theStack
Copy link
Contributor

theStack commented Jun 2, 2024

Concept ACK

Copy link
Contributor

@theStack theStack 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 fa52e13

@@ -117,11 +116,11 @@ def ser_compact_size(l):
if l < 253:
r = l.to_bytes(1, "little")
elif l < 0x10000:
r = struct.pack("<BH", 253, l)
r = (253).to_bytes(1, "little") + l.to_bytes(2, "little")
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: for serializing single bytes I personally prefer bytes([value]) over the .to_bytes method, e.g.

Suggested change
r = (253).to_bytes(1, "little") + l.to_bytes(2, "little")
r = bytes([253]) + l.to_bytes(2, "little")

(but no blocker, feel free to ignore)

Copy link
Member Author

Choose a reason for hiding this comment

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

I agree, but for consistency with the other def ser_compact_size and the other code, I'll leave as-is for now.

@achow101
Copy link
Member

achow101 commented Jun 6, 2024

ACK fa52e13

@achow101 achow101 merged commit 4a020ca into bitcoin:master Jun 6, 2024
@maflcko maflcko deleted the 2402-nuke-struct-pack- branch June 7, 2024 07:32
@bitcoin bitcoin locked and limited conversation to collaborators Jun 7, 2025
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.

6 participants