test: Descriptor roundtrip and raw()/ addr() coverage #36013

Open
pablomartin4btc wants to merge 2 commits from descriptor/roundtrip-raw-addr-tests into master AGit
pablomartin4btc commented 2026-08-18 19:29:54 +00:00 (Migrated from github.com)

Part 1 of 2, reviving #24361.

Ports the two test-only commits from the original PR above, re-applied against the current descriptor_tests.cpp — the original no longer applies cleanly, since DoCheck()'s signature has changed substantially since the original PR was created back in 2022.

norm_prv was dropped, and op_desc_id, spender_nlocktime, spender_nsequence, preimages, expected_prv/expected_pub, and desc_index were all added - so the original diff no longer applies...

diff -u <(git show 15220ec903:src/test/descriptor_tests.cpp | sed -n '/^void DoCheck/,/^{/p') \
        <(git show HEAD:src/test/descriptor_tests.cpp | sed -n '/^void DoCheck/,/^{/p')
@@ -1,3 +1,7 @@
-void DoCheck(const std::string& prv, const std::string& pub, const std::string& norm_prv, const std::string& norm_pub, int flags, const std::vector<std::vector<std::string>>& scripts, const std::optional<OutputType>& type, const std::set<std::vector<uint32_t>>& paths = ONLY_EMPTY,
-    bool replace_apostrophe_with_h_in_prv=false, bool replace_apostrophe_with_h_in_pub=false)
+void DoCheck(std::string prv, std::string pub, const std::string& norm_pub, int flags,
+             const std::vector<std::vector<std::string>>& scripts, const std::optional<OutputType>& type, std::optional<uint256> op_desc_id = std::nullopt,
+             const std::set<std::vector<uint32_t>>& paths = ONLY_EMPTY, bool replace_apostrophe_with_h_in_prv=false,
+             bool replace_apostrophe_with_h_in_pub=false, uint32_t spender_nlocktime=0, uint32_t spender_nsequence=CTxIn::SEQUENCE_FINAL,
+             std::map<std::vector<uint8_t>, std::vector<uint8_t>> preimages={},
+             std::optional<std::string> expected_prv = std::nullopt, std::optional<std::string> expected_pub = std::nullopt, int desc_index = 0)
 {
 
  • Roundtrip testing that inferring a descriptor from a generated scriptPubKey reproduces the original descriptor's structure (keys stripped from both sides for comparison). Added extension for MuSig key expressions (#31244) and BIP389 multipath (#22838), both added since 2022, and skipping for multi-branch taproot trees, where sibling order isn't meaningful.
    (For more details check 1st commit body)
  • Check() coverage for valid raw() and addr() descriptors, previously exercised only for invalid inputs and only in the script-to-descriptor direction. Added a new NO_KEYS flag, since these descriptors have zero keys and several existing checks assumed at least one.
    (For more details check 2nd commit body)

No behavior change, test-only.


Part 2 (not yet opened): the ToString()/ToPrivateString() merge from #24361, independent of this PR — needs more design work before it's ready.

Part 1 of 2, reviving #24361. <details> <summary>Ports the two test-only commits from the original PR above, re-applied against the current <code>descriptor_tests.cpp</code> — the original no longer applies cleanly, since <code>DoCheck()</code>'s signature has changed substantially since the original PR was created back in 2022.</summary> <br> `norm_prv` was dropped, and `op_desc_id`, `spender_nlocktime`, `spender_nsequence`, `preimages`, `expected_prv`/`expected_pub`, and `desc_index` were all added - so the original diff no longer applies... ``` diff -u <(git show 15220ec903:src/test/descriptor_tests.cpp | sed -n '/^void DoCheck/,/^{/p') \ <(git show HEAD:src/test/descriptor_tests.cpp | sed -n '/^void DoCheck/,/^{/p') ``` ```diff @@ -1,3 +1,7 @@ -void DoCheck(const std::string& prv, const std::string& pub, const std::string& norm_prv, const std::string& norm_pub, int flags, const std::vector<std::vector<std::string>>& scripts, const std::optional<OutputType>& type, const std::set<std::vector<uint32_t>>& paths = ONLY_EMPTY, - bool replace_apostrophe_with_h_in_prv=false, bool replace_apostrophe_with_h_in_pub=false) +void DoCheck(std::string prv, std::string pub, const std::string& norm_pub, int flags, + const std::vector<std::vector<std::string>>& scripts, const std::optional<OutputType>& type, std::optional<uint256> op_desc_id = std::nullopt, + const std::set<std::vector<uint32_t>>& paths = ONLY_EMPTY, bool replace_apostrophe_with_h_in_prv=false, + bool replace_apostrophe_with_h_in_pub=false, uint32_t spender_nlocktime=0, uint32_t spender_nsequence=CTxIn::SEQUENCE_FINAL, + std::map<std::vector<uint8_t>, std::vector<uint8_t>> preimages={}, + std::optional<std::string> expected_prv = std::nullopt, std::optional<std::string> expected_pub = std::nullopt, int desc_index = 0) { ``` </details> - Roundtrip testing that inferring a descriptor from a generated scriptPubKey reproduces the original descriptor's structure (keys stripped from both sides for comparison). Added extension for MuSig key expressions ([#31244](https://github.com/bitcoin/bitcoin/pull/31244)) and BIP389 multipath ([#22838](https://github.com/bitcoin/bitcoin/pull/22838)), both added since 2022, and skipping for multi-branch taproot trees, where sibling order isn't meaningful. (For more details check 1st commit body) - `Check()` coverage for valid `raw()` and `addr()` descriptors, previously exercised only for invalid inputs and only in the script-to-descriptor direction. Added a new `NO_KEYS` flag, since these descriptors have zero keys and several existing checks assumed at least one. (For more details check 2nd commit body) No behavior change, test-only. --- Part 2 (not yet opened): the `ToString()`/`ToPrivateString()` merge from #24361, independent of this PR — needs more design work before it's ready.
DrahtBot commented 2026-08-18 19:30:03 +00:00 (Migrated from github.com)

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.

Type Reviewers
ACK fametrano
Concept ACK w0xlt, jeanpablojp

If your review is incorrectly listed, please copy-paste <!--meta-tag:bot-skip--> into the comment that the bot should ignore.

Conflicts

Reviewers, this pull request conflicts with the following ones:

  • #36772 (test: Remove BOOST_FAIL and add info in descriptor_tests by rustaceanrob)
  • #36154 (wallet: fill PSBT_GLOBAL_XPUB by jeanpablojp)

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.

<!--e57a25ab6845829454e8d69fc972939a--> The following sections might be updated with supplementary metadata relevant to reviewers and maintainers. <!--006a51241073e994b41acfe9ec718e94--> ### External sites * Code coverage & benchmarks: https://corecheck.dev/bitcoin/bitcoin/pulls/36013 * Ralph review <img src=https://review.fish.foo/assets/bitcoin/bitcoin/36013.png alt="Ralph review status"> https://git.fish.foo/bitcoin/bitcoin/pulls/36013#author-ralph <!--021abf342d371248e50ceaed478a90ca--> ### Reviews See [the guideline](https://github.com/bitcoin/bitcoin/blob/master/CONTRIBUTING.md#code-review) and [AI policy](https://github.com/bitcoin/bitcoin/blob/master/doc/AI_POLICY.md) for information on the review process. | Type | Reviewers | | ---- | --------- | | ACK | [fametrano](https://github.com/bitcoin/bitcoin/pull/36013#pullrequestreview-5452859423) | | Concept ACK | [w0xlt](https://github.com/bitcoin/bitcoin/pull/36013#issuecomment-5334185063), [jeanpablojp](https://github.com/bitcoin/bitcoin/pull/36013#pullrequestreview-5104606928) | If your review is incorrectly listed, please copy-paste <code>&lt;!--meta-tag:bot-skip--&gt;</code> into the comment that the bot should ignore. <!--174a7506f384e20aa4161008e828411d--> ### Conflicts Reviewers, this pull request conflicts with the following ones: * [#36772](https://github.com/bitcoin/bitcoin/pull/36772) <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/36772.svg"></sub> (test: Remove `BOOST_FAIL` and add info in descriptor_tests by rustaceanrob) * [#36154](https://github.com/bitcoin/bitcoin/pull/36154) <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/36154.svg"></sub> (wallet: fill PSBT_GLOBAL_XPUB by jeanpablojp) 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. <!--5faf32d7da4f0f540f40219e4f7537a3-->
w0xlt commented 2026-08-18 21:17:54 +00:00 (Migrated from github.com)

Concept ACK

Concept ACK
rustaceanrob (Migrated from github.com) reviewed 2026-08-24 10:10:22 +00:00
rustaceanrob (Migrated from github.com) commented 2026-08-24 10:10:23 +00:00

Now that we have a few checks in the true part of the if branch below, perhaps we can change this to be BOOST_TEST_INFO_SCOPE so the descriptor is also printed if BOOST_CHECK_EQUAL(parse_pub->GetKeyCount(), 0U); fails.

Now that we have a few checks in the `true` part of the `if` branch below, perhaps we can change this to be `BOOST_TEST_INFO_SCOPE` so the descriptor is also printed if `BOOST_CHECK_EQUAL(parse_pub->GetKeyCount(), 0U);` fails.
rustaceanrob (Migrated from github.com) reviewed 2026-08-24 10:11:13 +00:00
rustaceanrob (Migrated from github.com) commented 2026-08-24 10:11:13 +00:00

Same feedback as the comment above, perhaps could be BOOST_TEST_INFO_SCOPE

Same feedback as the comment above, perhaps could be `BOOST_TEST_INFO_SCOPE`
rustaceanrob (Migrated from github.com) reviewed 2026-08-24 10:12:26 +00:00
rustaceanrob (Migrated from github.com) left a comment

Left a comment regarding BOOST_TEST_INFO, which only prints the value the next failing check, not any check in scope

Left a comment regarding `BOOST_TEST_INFO`, which only prints the value the next failing check, not any check in scope
pablomartin4btc (Migrated from github.com) reviewed 2026-08-27 02:01:33 +00:00
pablomartin4btc (Migrated from github.com) commented 2026-08-27 02:01:33 +00:00

Done in 2d230d8366, thanks!

Done in 2d230d8366d2149c71e69a2e2ec65561ca09cd0e, thanks!
pablomartin4btc (Migrated from github.com) reviewed 2026-08-27 02:01:49 +00:00
pablomartin4btc (Migrated from github.com) commented 2026-08-27 02:01:49 +00:00

Done in 2d230d8366, thanks!

Done in 2d230d8366d2149c71e69a2e2ec65561ca09cd0e, thanks!
pablomartin4btc commented 2026-08-27 02:06:21 +00:00 (Migrated from github.com)

-Updates:

-<ins>_**Updates**_</ins>: - Addressed @rustaceanrob's [feedback](https://github.com/bitcoin/bitcoin/pull/36013#pullrequestreview-5006717185): replaced `BOOST_TEST_INFO` with `BOOST_TEST_INFO_SCOPE` in https://github.com/bitcoin/bitcoin/commit/2d230d8366d2149c71e69a2e2ec65561ca09cd0e.
jeanpablojp (Migrated from github.com) reviewed 2026-09-03 16:56:41 +00:00
jeanpablojp (Migrated from github.com) left a comment

Concept ACK

Built at the head, ran the unit suite, and checked the six new addresses by decoding them outside of Core.

Concept ACK Built at the head, ran the unit suite, and checked the six new addresses by decoding them outside of Core.
jeanpablojp (Migrated from github.com) commented 2026-09-03 16:56:41 +00:00

The multipath alternative here seems avoidable. It only exists because the roundtrip comparison uses pub, the literal test string. In these checks, pub1 is expanded, and DoCheck() already compares it against pub, or the expanded expected_pub literal for multipath. With the old STR_KEYSTEP and the comparison on pub1, the suite passes. Both forms run the same 666 roundtrip checks and produce the same 60 failures under the tested InferScript() mutation. mark_count() is worth keeping either way. Is the literal intended to catch something pub1 would not?

The multipath alternative here seems avoidable. It only exists because the roundtrip comparison uses `pub`, the literal test string. In these checks, `pub1` is expanded, and `DoCheck()` already compares it against `pub`, or the expanded `expected_pub` literal for multipath. With the old `STR_KEYSTEP` and the comparison on `pub1`, the suite passes. Both forms run the same 666 roundtrip checks and produce the same 60 failures under the tested `InferScript()` mutation. `mark_count()` is worth keeping either way. Is the literal intended to catch something `pub1` would not?
jeanpablojp (Migrated from github.com) commented 2026-09-03 16:56:41 +00:00

nit: this matches sortedmulti( but not sortedmulti_a(, which InferMultiA() gives back as multi_a. No vector uses it today, so nothing breaks, but I flipped the existing 1-of-1 multi_a vector to sortedmulti_a, same script by construction, and only the roundtrip comparison failed, six times, multi_a versus sortedmulti_a. Allowing an optional _a here and keeping it in the replacement fixes it, ran that and the suite went back to green.

nit: this matches `sortedmulti(` but not `sortedmulti_a(`, which `InferMultiA()` gives back as `multi_a`. No vector uses it today, so nothing breaks, but I flipped the existing 1-of-1 `multi_a` vector to `sortedmulti_a`, same script by construction, and only the roundtrip comparison failed, six times, `multi_a` versus `sortedmulti_a`. Allowing an optional `_a` here and keeping it in the replacement fixes it, ran that and the suite went back to green.
jeanpablojp (Migrated from github.com) commented 2026-09-03 16:56:41 +00:00

Could we assert the ToPrivateString() contract for NO_KEYS here instead of only skipping the block? The header promises a false return when no private keys are available at all, which is exactly the raw() and addr() case. I changed both implementations to return true and the whole unit suite stayed green. An else if (flags & NO_KEYS) with the two negative checks closes it. I ran that, and the same mutation now fails 14 times, twice per new vector.

Could we assert the `ToPrivateString()` contract for `NO_KEYS` here instead of only skipping the block? The header promises a `false` return when no private keys are available at all, which is exactly the `raw()` and `addr()` case. I changed both implementations to return `true` and the whole unit suite stayed green. An `else if (flags & NO_KEYS)` with the two negative checks closes it. I ran that, and the same mutation now fails 14 times, twice per new vector.
First-time contributor

Base: 2034d3078ee1df99cdbfab738b1f2b54c30bd022
Head: cf9821aaa5ed971c9e3eb16bbf5daed0495a9252

Findings

Suggestions

Include <iterator> directly (src/test/descriptor_tests.cpp:217, head)

This code uses std::ostreambuf_iterator, but the file does not include <iterator>. Add the direct include rather than relying on a transitive include that may change.

Full review report

<!-- forgejo-review-bot:bitcoin/bitcoin --> <!-- ralph-engine:current:0c398e5fd579cb8b00b6b62f2903ad741ac3f867996353a69082cd6ac57373b5:9d9e25a7349f2e108262b6ed44577d47304e975faf1917f1972defa0a828ae3d --> Base: `2034d3078ee1df99cdbfab738b1f2b54c30bd022` Head: `cf9821aaa5ed971c9e3eb16bbf5daed0495a9252` ##### Findings **Suggestions** **Include `<iterator>` directly** (src/test/descriptor_tests.cpp:217, head) This code uses `std::ostreambuf_iterator`, but the file does not include `<iterator>`. Add the direct include rather than relying on a transitive include that may change. [Full review report](<https://review.fish.foo/traces/36013-cf9821aaa5ed971c9e3eb16bbf5daed0495a9252-1075-4.html>)
fametrano (Migrated from github.com) reviewed 2026-10-03 20:03:56 +00:00
fametrano (Migrated from github.com) left a comment

ACK 2d230d8366

Built test_bitcoin at the head and ran descriptor_tests, no errors. Recomputed the six new addr() vectors with btclib: the scriptPubKeys, the addresses back from them, the witness versions and programs, and the legacy/bech32/bech32m output types all match. One nit: the v1 vector cannot be a plain taproot address, since a P2TR script infers back as a solvable rawtr() and fails both the new structure compare and the existing solvability checks (tried addr(bc1padda8…) for 5120eb5b…); a short comment saying why it is a 40-byte program instead would spare the next reader the experiment.

ACK 2d230d8366d2149c71e69a2e2ec65561ca09cd0e Built `test_bitcoin` at the head and ran `descriptor_tests`, no errors. Recomputed the six new `addr()` vectors with btclib: the scriptPubKeys, the addresses back from them, the witness versions and programs, and the legacy/bech32/bech32m output types all match. One nit: the v1 vector cannot be a plain taproot address, since a P2TR script infers back as a solvable `rawtr()` and fails both the new structure compare and the existing solvability checks (tried `addr(bc1padda8…)` for `5120eb5b…`); a short comment saying why it is a 40-byte program instead would spare the next reader the experiment.
fametrano (Migrated from github.com) commented 2026-10-03 20:03:56 +00:00
    // 40-byte v1 program: a 32-byte one would infer as a solvable rawtr() and fail the checks
    Check("addr(bc1pw508d6qejxtdg4y5r3zarvary0c5xw7kw508d6qejxtdg4y5r3zarvary0c5xw7kt5nd6y)", "addr(bc1pw508d6qejxtdg4y5r3zarvary0c5xw7kw508d6qejxtdg4y5r3zarvary0c5xw7kt5nd6y)", "addr(bc1pw508d6qejxtdg4y5r3zarvary0c5xw7kw508d6qejxtdg4y5r3zarvary0c5xw7kt5nd6y)", NO_KEYS | UNSOLVABLE, {{"5128751e76e8199196d454941c45d1b3a323f1433bd6751e76e8199196d454941c45d1b3a323f1433bd6"}}, OutputType::BECH32M, /*op_desc_id=*/std::nullopt, NO_PATHS);
```suggestion // 40-byte v1 program: a 32-byte one would infer as a solvable rawtr() and fail the checks Check("addr(bc1pw508d6qejxtdg4y5r3zarvary0c5xw7kw508d6qejxtdg4y5r3zarvary0c5xw7kt5nd6y)", "addr(bc1pw508d6qejxtdg4y5r3zarvary0c5xw7kw508d6qejxtdg4y5r3zarvary0c5xw7kt5nd6y)", "addr(bc1pw508d6qejxtdg4y5r3zarvary0c5xw7kw508d6qejxtdg4y5r3zarvary0c5xw7kt5nd6y)", NO_KEYS | UNSOLVABLE, {{"5128751e76e8199196d454941c45d1b3a323f1433bd6751e76e8199196d454941c45d1b3a323f1433bd6"}}, OutputType::BECH32M, /*op_desc_id=*/std::nullopt, NO_PATHS); ```
pablomartin4btc (Migrated from github.com) reviewed 2026-10-08 00:58:19 +00:00
pablomartin4btc (Migrated from github.com) commented 2026-10-08 00:58:19 +00:00

Thanks! The multipath branch was only needed because the comparison at line ~531 used pub directly instead of pub1. I'll switch it to pub1, reverting this to the simpler form.

Thanks! The multipath branch was only needed because the comparison at line ~531 used `pub` directly instead of `pub1`. I'll switch it to `pub1`, reverting this to the simpler form.
pablomartin4btc (Migrated from github.com) reviewed 2026-10-08 00:59:49 +00:00
pablomartin4btc (Migrated from github.com) commented 2026-10-08 00:59:49 +00:00

Thanks! I'll include a short comment explaining the 40-byte choice.

Thanks! I'll include a short comment explaining the 40-byte choice.
DrahtBot commented 2026-10-08 03:10:41 +00:00 (Migrated from github.com)

🚧 At least one of the CI tasks failed.
Task tidy: https://github.com/bitcoin/bitcoin/actions/runs/37714644244/job/113108232448
LLM reason (✨ experimental): CI failed because clang-tidy flagged /*op_desc_id=*/ comments in descriptor_tests.cpp as mismatched with the parameter name op_compat_desc_hash (warnings treated as errors).

Hints

Try to run the tests locally, according to the documentation. However, a CI failure may still
happen due to a number of reasons, for example:

  • Possibly 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.

  • A sanitizer issue, which can only be found by compiling with the sanitizer and running the
    affected test.

  • An intermittent issue.

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

<!--85328a0da195eb286784d51f73fa0af9--> 🚧 At least one of the CI tasks failed. <sub>Task `tidy`: https://github.com/bitcoin/bitcoin/actions/runs/37714644244/job/113108232448</sub> <sub>LLM reason (✨ experimental): CI failed because clang-tidy flagged `/*op_desc_id=*/` comments in `descriptor_tests.cpp` as mismatched with the parameter name `op_compat_desc_hash` (warnings treated as errors).</sub> <details><summary>Hints</summary> Try to run the tests locally, according to the documentation. However, a CI failure may still happen due to a number of reasons, for example: * Possibly 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. * A sanitizer issue, which can only be found by compiling with the sanitizer and running the affected test. * An intermittent issue. Leave a comment here, if you need help tracking down a confusing failure. </details>
pablomartin4btc (Migrated from github.com) reviewed 2026-10-08 04:19:20 +00:00
pablomartin4btc (Migrated from github.com) commented 2026-10-08 04:19:20 +00:00

Done.

Done.
pablomartin4btc (Migrated from github.com) reviewed 2026-10-08 04:19:44 +00:00
pablomartin4btc (Migrated from github.com) commented 2026-10-08 04:19:44 +00:00

Done, thanks!

Done, thanks!
pablomartin4btc (Migrated from github.com) reviewed 2026-10-08 04:19:53 +00:00
pablomartin4btc (Migrated from github.com) commented 2026-10-08 04:19:53 +00:00

Done.

Done.
pablomartin4btc (Migrated from github.com) reviewed 2026-10-08 04:20:06 +00:00
pablomartin4btc (Migrated from github.com) commented 2026-10-08 04:20:06 +00:00

Done, thanks!

Done, thanks!
pablomartin4btc commented 2026-10-08 04:27:13 +00:00 (Migrated from github.com)

-Updates:

  • Addressed @jeanpablojp's and @fametrano's feedback, thanks both!
  • Rebased — #35445 renamed DescriptorID/ op_desc_id to CompatDescriptorHash/ op_compat_desc_hash, leaving a few comments out of sync and failing tidy CI. Fixed.
-<ins>_**Updates**_</ins>: - Addressed @jeanpablojp's and @fametrano's feedback, thanks both! - Rebased — [#35445](https://github.com/bitcoin/bitcoin/pull/35445) renamed `DescriptorID`/ `op_desc_id` to `CompatDescriptorHash`/ `op_compat_desc_hash`, leaving a few comments out of sync and [failing tidy CI](https://github.com/bitcoin/bitcoin/actions/runs/37714644244/job/113108232448). Fixed.
fametrano (Migrated from github.com) reviewed 2026-10-08 07:00:04 +00:00
fametrano (Migrated from github.com) left a comment

reACK cf9821aaa5

descriptor_tests passes at the new head, and the raw()/addr() vectors are unchanged since 2d230d8, so my earlier check of them still holds. Thanks for the comment on the 40-byte v1 program.

reACK cf9821aaa5ed971c9e3eb16bbf5daed0495a9252 `descriptor_tests` passes at the new head, and the `raw()`/`addr()` vectors are unchanged since 2d230d8, so my earlier check of them still holds. Thanks for the comment on the 40-byte v1 program.
rustaceanrob (Migrated from github.com) reviewed 2026-10-17 23:30:04 +00:00
rustaceanrob (Migrated from github.com) commented 2026-10-17 23:10:42 +00:00

Not sure if I am missing something, but it seems like this function can be simplified using regex_replace and std::regex:

diff --git a/src/test/descriptor_tests.cpp b/src/test/descriptor_tests.cpp
index 70c7d5e8b5..622157b2f6 100644
--- a/src/test/descriptor_tests.cpp
+++ b/src/test/descriptor_tests.cpp
@@ -14,7 +14,6 @@

 #include <optional>
 #include <regex>
-#include <sstream>
 #include <string>
 #include <vector>

@@ -188,46 +187,22 @@ const std::string STR_BIP32KEY = "(" + STR_XPUBPRV + "(/" + STR_KEYSTEP + ")*(/[
 /** ERE string matching a descriptor key expression. */
 const std::string STR_KEYEXPR = "(" + STR_MAYBE_ORIGIN + "(" + STR_PUBKEY + "|" + STR_PUBKEYHASH + "|" + STR_BIP32KEY + "))";

-/** Regular expression matching a descriptor key expression. */
-const std::regex KEY_RE("([(,])" + STR_KEYEXPR + "([),])", std::regex::extended);
-/** Regular expression matching "sortedmulti" or "sortedmulti_a", capturing the optional "_a". */
-const std::regex SORTEDMULTI_RE("(^|[(,])sortedmulti(_a)?[(]", std::regex::extended);
+/** Regular expression matching a descriptor key expression, with the trailing separator as lookahead. */
+const std::regex KEY_RE("([(,])" + STR_KEYEXPR + "(?=[),])");
 /** Regular expression collapsing a whole musig() key expression, including any trailing
  *  derivation path, to a single unit. musig()'s participants are plain keys with no
  *  parentheses of their own, and musig() cannot be nested or carry origin info (see
  *  ParsePubkey() in script/descriptor.cpp), so a match up to the first ')' is sufficient;
  *  KEY_RE above doesn't need to understand musig()'s internal structure as a result. */
-const std::regex MUSIG_RE("musig[(][^()]*[)][^(),]*", std::regex::extended);
-/** Replacement string for KEY_RE: keeps the separators captured in groups 1 and mark_count(),
- *  replacing the key expression itself with "<KEY>". The trailing group number is computed from
- *  KEY_RE's actual capture count rather than hardcoded, so it can't silently drift out of sync
- *  if STR_KEYEXPR's structure changes again in the future. */
-const std::string KEY_RE_REPLACEMENT = "$1<KEY>$" + util::ToString(KEY_RE.mark_count());
+const std::regex MUSIG_RE("musig[(][^()]*[)][^(),]*");

 /** Replace all key expressions in desc with "<KEY>", "sortedmulti" with "multi", and drop checksum. */
 std::string DropKeys(std::string desc)
 {
-    if (desc.size() >= 9 && *(desc.end() - 9) == '#') {
-        // Drop checksum
-        desc = desc.substr(0, desc.size() - 9);
-    }
-    {
-        // Collapse musig() expressions first; KEY_RE below only understands plain keys.
-        std::ostringstream ostr;
-        std::regex_replace(std::ostreambuf_iterator<char>(ostr), desc.begin(), desc.end(), MUSIG_RE, "<KEY>");
-        desc = ostr.str();
-    }
-    while (true) {
-        std::ostringstream ostr;
-        std::regex_replace(std::ostreambuf_iterator<char>(ostr), desc.begin(), desc.end(), KEY_RE, KEY_RE_REPLACEMENT);
-        auto newdesc = ostr.str();
-        ostr.str("");
-        std::regex_replace(std::ostreambuf_iterator<char>(ostr), newdesc.begin(), newdesc.end(), SORTEDMULTI_RE, "$1multi$2(");
-        auto newdesc2 = ostr.str();
-        std::swap(desc, newdesc2);
-        if (newdesc2 == desc) break;
-    }
-    return desc;
+    desc = desc.substr(0, desc.find('#'));
+    desc = std::regex_replace(desc, MUSIG_RE, "<KEY>");
+    desc = std::regex_replace(desc, KEY_RE, "$1<KEY>");
+    return std::regex_replace(desc, std::regex("sortedmulti"), "multi");
 }
Not sure if I am missing something, but it seems like this function can be simplified using `regex_replace` and `std::regex`: ```diff diff --git a/src/test/descriptor_tests.cpp b/src/test/descriptor_tests.cpp index 70c7d5e8b5..622157b2f6 100644 --- a/src/test/descriptor_tests.cpp +++ b/src/test/descriptor_tests.cpp @@ -14,7 +14,6 @@ #include <optional> #include <regex> -#include <sstream> #include <string> #include <vector> @@ -188,46 +187,22 @@ const std::string STR_BIP32KEY = "(" + STR_XPUBPRV + "(/" + STR_KEYSTEP + ")*(/[ /** ERE string matching a descriptor key expression. */ const std::string STR_KEYEXPR = "(" + STR_MAYBE_ORIGIN + "(" + STR_PUBKEY + "|" + STR_PUBKEYHASH + "|" + STR_BIP32KEY + "))"; -/** Regular expression matching a descriptor key expression. */ -const std::regex KEY_RE("([(,])" + STR_KEYEXPR + "([),])", std::regex::extended); -/** Regular expression matching "sortedmulti" or "sortedmulti_a", capturing the optional "_a". */ -const std::regex SORTEDMULTI_RE("(^|[(,])sortedmulti(_a)?[(]", std::regex::extended); +/** Regular expression matching a descriptor key expression, with the trailing separator as lookahead. */ +const std::regex KEY_RE("([(,])" + STR_KEYEXPR + "(?=[),])"); /** Regular expression collapsing a whole musig() key expression, including any trailing * derivation path, to a single unit. musig()'s participants are plain keys with no * parentheses of their own, and musig() cannot be nested or carry origin info (see * ParsePubkey() in script/descriptor.cpp), so a match up to the first ')' is sufficient; * KEY_RE above doesn't need to understand musig()'s internal structure as a result. */ -const std::regex MUSIG_RE("musig[(][^()]*[)][^(),]*", std::regex::extended); -/** Replacement string for KEY_RE: keeps the separators captured in groups 1 and mark_count(), - * replacing the key expression itself with "<KEY>". The trailing group number is computed from - * KEY_RE's actual capture count rather than hardcoded, so it can't silently drift out of sync - * if STR_KEYEXPR's structure changes again in the future. */ -const std::string KEY_RE_REPLACEMENT = "$1<KEY>$" + util::ToString(KEY_RE.mark_count()); +const std::regex MUSIG_RE("musig[(][^()]*[)][^(),]*"); /** Replace all key expressions in desc with "<KEY>", "sortedmulti" with "multi", and drop checksum. */ std::string DropKeys(std::string desc) { - if (desc.size() >= 9 && *(desc.end() - 9) == '#') { - // Drop checksum - desc = desc.substr(0, desc.size() - 9); - } - { - // Collapse musig() expressions first; KEY_RE below only understands plain keys. - std::ostringstream ostr; - std::regex_replace(std::ostreambuf_iterator<char>(ostr), desc.begin(), desc.end(), MUSIG_RE, "<KEY>"); - desc = ostr.str(); - } - while (true) { - std::ostringstream ostr; - std::regex_replace(std::ostreambuf_iterator<char>(ostr), desc.begin(), desc.end(), KEY_RE, KEY_RE_REPLACEMENT); - auto newdesc = ostr.str(); - ostr.str(""); - std::regex_replace(std::ostreambuf_iterator<char>(ostr), newdesc.begin(), newdesc.end(), SORTEDMULTI_RE, "$1multi$2("); - auto newdesc2 = ostr.str(); - std::swap(desc, newdesc2); - if (newdesc2 == desc) break; - } - return desc; + desc = desc.substr(0, desc.find('#')); + desc = std::regex_replace(desc, MUSIG_RE, "<KEY>"); + desc = std::regex_replace(desc, KEY_RE, "$1<KEY>"); + return std::regex_replace(desc, std::regex("sortedmulti"), "multi"); } ```
rustaceanrob (Migrated from github.com) commented 2026-10-17 23:29:18 +00:00

nit: Could use (flags & NO_KEYS) != 0 for legibility. Had to look up the !! case

nit: Could use `(flags & NO_KEYS) != 0` for legibility. Had to look up the `!!` case
DrahtBot commented 2026-11-06 18:33:56 +00:00 (Migrated from github.com)

🐙 This pull request conflicts with the target branch and needs rebase.

<!--cf906140f33d8803c4a75a2196329ecb--> 🐙 This pull request conflicts with the target branch and [needs rebase](https://github.com/bitcoin/bitcoin/blob/master/CONTRIBUTING.md#rebasing-changes).
rustaceanrob (Migrated from github.com) reviewed 2026-11-06 19:16:15 +00:00
rustaceanrob (Migrated from github.com) commented 2026-11-06 19:16:15 +00:00

You can remove this line as of #36772, the descriptor is added as context at the top of DoCheck now

You can remove this line as of #36772, the descriptor is added as context at the top of `DoCheck` now
Sign in to join this conversation.
No reviewers
No milestone
No project
No assignees
2 participants
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
bitcoin/bitcoin!36013
No description provided.