Skip to content

util,test: fix roundtrip asymmetries in parser and serializer code#127

Draft
l0rinc wants to merge 4 commits intomasterfrom
detached494
Draft

util,test: fix roundtrip asymmetries in parser and serializer code#127
l0rinc wants to merge 4 commits intomasterfrom
detached494

Conversation

@l0rinc
Copy link
Copy Markdown
Owner

@l0rinc l0rinc commented Mar 16, 2026

The PR bundles four logically independent changes under one title. The actual roundtrip fix (commit 4) is the only one that matches the PR title; the other three are preparatory refactors. The description should make that clear without repeating what the commit messages already say.

Several parser/serializer pairs in the codebase had asymmetries that prevented a successful roundtrip: ParseHDKeypath() rejected the h hardened suffix that WriteHDKeypath() can produce, AmountCompression::Unser() could materialize CAmount values above MAX_MONEY without error, and fuzz targets for PSBTs, transactions, scripts, net addresses, net permissions, and money strings exercised only one direction of the parse/serialize cycle. The three preparatory commits reduce transitive include weight by moving uint256 hex parsing out of the header, dropping chainparams.h from key_io.h, and extracting SetupServerArgs into its own translation unit.

The fix accepts h as a hardened-path suffix in ParseHDKeypath() alongside the existing ', adds a bounds check in AmountCompression::Unser() that throws on values exceeding MAX_MONEY, and extends fuzz targets and unit tests to assert that serializing a successfully parsed value and parsing it again yields an identical result for PartiallySignedTransaction, CTxOut/TxOutCompression, Coin, CTxUndo, CBlockUndo, CNetAddr/CService/CSubNet, NetWhitebindPermissions/NetWhitelistPermissions, HD keypaths, and money strings.

l0rinc added 4 commits March 4, 2026 14:46
Define `uint160::FromHex`, `uint256::FromHex`, and `uint256::FromUserHex` in `uint256.cpp` and keep only declarations in `uint256.h`.

Drop heavy `util/strencodings.h` and `util/string.h` includes from `uint256.h` by using a local consteval hex helper for literal construction and non-inline parsing helpers in the `.cpp` file.

Add direct includes in `musig.cpp` and `outputtype.cpp` for symbols that were previously provided transitively via `uint256.h`.
Drop the heavy chainparams dependency from `key_io.h` and include it directly in the implementation and test files that use `Params()` or `SelectParams()`.
Move the SetupServerArgs implementation out of `init.cpp` into a dedicated translation unit and add it to the build.

This keeps `init.cpp` smaller and isolates server argument registration code in one place.
Make ParseHDKeypath() accept h as a hardened suffix so it can parse the default output of WriteHDKeypath() again, and cover both ' and h in the wallet test and fuzz target.

Add roundtrip checks for the text and serialization paths that were most likely to hide asymmetries, including network permissions, net addresses, scripts, transactions, PSBTs, money strings, and compressed txout/coin deserialization.

Reject out-of-range values in AmountCompression::Unser() instead of materializing invalid CAmounts, and add a minimal unit reproducer plus focused deserialize fuzz assertions for TxOutCompression, Coin, CTxUndo, and CBlockUndo.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant