mirror of
https://github.com/bitcoin/bitcoin
synced 2026-12-04 10:38:37 +00:00
test: Better test coverage for ParseHDKeypath() #35170
No reviewers
Labels
No labels
Android
Backport
Block storage
Brainstorming
Bug
Build system
CI failed
Consensus
Data corruption
Descriptors
Docs
DrahtBot Guix build requested
Feature
Fuzzing
GUI
Good First Review
IPC
Insufficient Review
Kernel
Linux/Unix
Mempool
Mining
Needs Backport (31.x)
Needs Backport (32.x)
Needs Benchmark
Needs Conceptual Review
Needs backport (30.x)
Needs rebase
Needs release note
Net
P2P
PSBT
Privacy
Private Broadcast
Questions and Help
RPC/REST/ZMQ
Refactoring
Resource usage
Review club
Scripts and tools
Settings
TX fees and policy
Tests
Tracking Issue
UTXO Db and Indexes
Up for grabs
Upstream
Utils/log/libs
Validation
Waiting for author
Wallet
Windows
good first issue
interfaces
macOS
No milestone
No project
No assignees
2 participants
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
bitcoin/bitcoin!35170
Loading…
Reference in a new issue
No description provided.
Delete branch "2604-parse-keypath-legacy"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Background: The
ParseHDKeypathmethod (src/util/bip32.cpp) is used directly only in a limited way: when loading legacy wallets. However, it shares internals with parsing within descriptors (ParseKeyPath,ParseKeyPathNuminsrc/script/descriptor.cpp, c.f. #35069).Deficiencies: The method has a unit test, but it checks only the bool result, and not the parsed keypath. This can be demonstrated by breaking the implementation by adding a
keypath.clear();statement just before the finalreturn true;. With this obviously broken implementation the tests pass.Solution proposed: Extend the tests with checks (without modifying the behavior).
Changes:
Extend the unit tests with checks for the returned parsed keypath. Add the expected parsed numerical keypath as test data. Note: breaking the production code as described above now results in test failure.
In the unit test also perform round-trip conversion: convert to string, then parse again, and convert to string again. In the last two steps the results should match the input before the round-trip (note: this is not true for the first roundtrip). Additionally, add negative tests for the unsupported 'h' hardened delimiter.
The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.
External sites
Reviews
See the guideline and AI policy for information on the review process.
If your review is incorrectly listed, please copy-paste
<!--meta-tag:bot-skip-->into the comment that the bot should ignore.Conflicts
No conflicts as of last run.
Concept ACK, only reviewed lightly to understand the extent of the change
Can you make this a scripted diff for simplicity?
we don't want to correlate the two calls for the fuzzer:
could we swap the order to store the value first and the validity next to follow the order in the usage, i.e.
BOOST_CHECK_EQUAL(ParseHDKeypath(keypath_str, keypath), is_valid);ACK 2bd25fc3a4ae663b0da77a9a4d9fecac50260183
Reviewed the test refactor and added coverage, the
ParseHDKeypathLegacyrename, and the fuzz target change. The new assertions cover returned path elements and round-trip behavior without changing the legacy wallet load behavior.Ran locally on macOS arm64:
build/bin/test_bitcoin --run_test=psbt_wallet_tests/parse_hd_keypathctest --test-dir build -R 'psbt_wallet|ipc_tests|walletdb_tests' --output-on-failure -j 9\n-ctest --test-dir build -j 9 --output-on-failure(0 failed out of 348;script_assets_testsskipped)\n-git diff --check upstream/master...HEADThis type conversion is unnecessary (and wrong).
Thanks for the reviews! Pushed changes for the suggestions from the first review. Also extended the PR description with 'Potential future steps'.
git diff 2bd25fc3a4ae663b0da77a9a4d9fecac50260183..a548fac787a89e2e69ec89a6f9450935b3a9e08fThanks for insisting, done.
Done.
That's how it was done first, but to keep the formatting so that items align to the same columns, it produced a lot of extra white space. The length of the string input varies a lot, while the bool only a little (4 or 5 chars), so it was put first for that reason.
Done
Rebased to current master (to refresh)
Concept ACK
2d6efe8414aeb75675769ca3ca71b0113ebf7169: nit: These comments "num -> str2" and "str2 -> num2" seem unnecessary, the code is already clear.
94536aba8f0f374953e765b9b76772765a18d2aa: Nice catch! The apostrophe param was added in
WriteHDKeypathinbd13dc2f46(3 years ago). I'm just wondering how effective is fuzzingFormatHDKeypathifWriteHDKeypathcan exactly callFormatHDKeypathagain with same values.code review ACK cf76d345c85283e64a37e850b92b748c5ee29d2b
Nothing blocker from my comments but happy to re-ack if any of them (or any other thing) is addressed.
🐙 This pull request conflicts with the target branch and needs rebase.
PR #32784 has been merged recently, and it overlaps with the changes of this PR (resulted in a conflict). Rebasing had to be done manually, including changes:
git range-diff cf76d345c85283e64a37e850b92b748c5ee29d2b...b3e4cd2eef85a55b2a734f5f9817993ff943e852@brunoerg, thanks for the review. I've applied the nit, as the PR had to be changed (due to merge conflict).
Note: this is irrelevant now, as #32784 had solved this (in a different way).
Applied (as I had to touch due to merge conflict). Note that this code has been moved to
src/test/bip32_tests.cppnow.Concept ACK
🐙 This pull request conflicts with the target branch and needs rebase.
Reworked, due to new conflict. #35069 has been merged, whereby the implementation of
ParseHDKeypathandParsePubkeyhas been mostly deduplicated, and test data changed.Updated this PR accordingly, it still keeps the test improvements (test data as data, check expected results, add round-trip tests), and the renaming.
Ready for review.
reACK
56b1a59b47Base:
aa76bd266df9742a266d52e0e5da8dbf033658f6Head:
3ce364124c09157722781720ee60aabb5a99e133Alternatives
Round-trip through WriteHDKeypath instead of FormatHDKeypath plus stripping the leading slash. In the round-trip block, use WriteHDKeypath(keypath_num, /apostrophe=/true). It returns "m" + FormatHDKeypath(...), so ParseHDKeypath accepts its output directly, including "m" for an empty path. That removes keypath_str2_adjusted, the starts_with('/') erase, and its comment. Optionally, compare the vectors with a single BOOST_CHECK(keypath_num2 == keypath_num) instead of the per-element loop. Benefit: Simpler test code. The round trip then uses the same formatter as the existing fuzz target (src/test/fuzz/parse_hd_keypath.cpp:26-28) and needs no special handling for the leading '/'. ParseHDKeypath rejects "/1" only because getline yields an empty first element, and that detail would no longer matter. Trade-off: The test would no longer call FormatHDKeypath directly. FormatHDKeypath is already exercised through WriteHDKeypath, and it is used directly by descriptor code. Also, the fuzz target already asserts round trips for both apostrophe modes, so the unit round trip adds only fixed-case coverage either way. Unresolved: Whether the author wants FormatHDKeypath tested on its own here, without the "m" prefix.
Previously reported concerns remain unresolved: 1 suggestion.
Previous findings
[Suggestion · still present] Include the input path in assertion context
Wrap each iteration’s assertions in a test context containing
keypath_str(and, if useful, the element index). When an assertion fails, the shared table location and reported values do not identify which input case failed; context would make these failures easier to diagnose without changing the checks.Location:
src/test/bip32_tests.cpp:340(head).First reported at
dd665be15b553ef4aa5913c03f4b5e3a53751ce3. Last checked at3ce364124c09157722781720ee60aabb5a99e133.Resolved findings
[Suggestion · resolved] Clarify what “Legacy” means for this parser
The legacy wallet database loader is one caller, but ParsePathBIP32 also routes the derivehdkey RPC’s user-supplied path through this parser; that RPC derives keys associated with active or unused(KEY) descriptors (see src/wallet/rpc/wallet.cpp:994, 1015–1048). The stated legacy-wallet-only rationale and future-removal inference therefore do not match the call graph: removing this parser with legacy-wallet loading would also break the current RPC. Consider a neutral BIP32-specific name, or clarify that “Legacy” refers to the parser’s grammar or implementation rather than its callers or lifecycle. This rename does not change runtime behavior.
Location:
src/rpc/util.cpp:1386(head).Fixed. First reported at
56b1a59b47bf7eb80bad5cbaf115e524a4192d57. Last checked at3ce364124c09157722781720ee60aabb5a99e133. The prior finding objected to renaming the parser with a "Legacy" name even though the RPC path also calls it. At head, the function is still named ParseHDKeypath in both the header and the implementation, and the RPC caller still uses that name. The patch touches only src/test/bip32_tests.cpp. Dropping the rename removed the change the finding was about. The author plans to address this separately. Author reply 2026-10-06T21:02:16ZReview tier: routine
Full review report
ACK
56b1a59b47In commit
56b1a59b47:I'm not convinced this rename makes sense after the addition of
derivehdkey. I'd prefer keeping it as is.I don't fully get the "after the addition of
derivehdkey" part. Do you meant that after #35069 the two implementations share the same internals? So althoughParseHDKeypathis used only in the legacy wallet case, it's internals are also used by the descriptor parsing (ParseKeyPathfromdescriptor.cpp)? If so, I can agree and will consider it.Agree with keeping the original naming. There is nothing legacy about this function. That the legacy wallet is currently the only caller should not make a difference for its naming, there may be other unrelated callers in the future.
With #35069 the two implementations has been brought together (share the same internal implementation), so the rename to 'Legacy' makes not much sense. Dropped the rename, and updated the OD accordingly. This change is a pure test-only change now. Thanks for the comments!
Re-reviews are welcome -- the rename was the very last commit, it was dropped, but the other 4 commits are unmodified (current changeset is a subset of the previous version).
reACK
dd665be15bConcept ACK
ACK
dd665be15bIn
41bba5e1b0test: Refactor in bip32_tests: extract test data in an array:IIUC this temporarily reduces test coverage. The commit message could mention it, or leave it out of the array initially:
Applied a minor change following review -- thanks!
git range-diff dd665be15b553ef4aa5913c03f4b5e3a53751ce3...3ce364124c09157722781720ee60aabb5a99e133Indeed! There was a single test case with result check, the first commit removed this check, while the second added it to all test cases. This check was removed temporarily.
For clarity, I simply kept this test case separately in the first commit (integrated into the array in the second). Also mentioned it in the commit message.
re-ACK
3ce3641ACK
3ce364124c