1cb416397bpsbt: avoid duplicate taproot leaf script keys when merging (Shuvam Pandey) Pull request description: Follow-up to #35665, which fixed the same combiner defect for `PSBT_GLOBAL_XPUB`. thomasbuilds and winterrdog asked for this one as its own PR when I reported it there. `m_tap_scripts` maps a leaf script to a set of control blocks, but is serialized as one record per control block, keyed by the control block (`SerializeToVector(s, PSBT_IN_TAP_LEAF_SCRIPT, std::span{control_block})`). `PSBTInput::Merge` unions it by the map key, so two PSBTs that map the same control block to different leaf scripts merge into an input that serializes the `0x15` key twice. Duplicate keys make a PSBT invalid, so it is the same `combinepsbt` then `decodepsbt` failure as the xpub case, at the input level. Present since #22558 (v24.0). Both decode on their own, and differ only in the leaf script the control block maps to, `OP_1` against `OP_1 OP_1`: ``` $ A=cHNidP8BADwCAAAAAaqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqAAAAAAAAAAAAAQAAAAAAAAAAAAAAAAAAIhXAUJKbdMGgSVS3i0tgNel6XgeKWg8o7JbVR7/ums6AOsACUcAAAA== $ B=cHNidP8BADwCAAAAAaqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqAAAAAAAAAAAAAQAAAAAAAAAAAAAAAAAAIhXAUJKbdMGgSVS3i0tgNel6XgeKWg8o7JbVR7/ums6AOsADUVHAAAA= $ bitcoin-cli -regtest decodepsbt "$(bitcoin-cli -regtest combinepsbt "[\"$A\",\"$B\"]")" error code: -22 error message: TX decode failed Duplicate Key, input key "15c050929b74c1a04954b78b4b6035e97a5e078a5a0f28ec96d547bfee9ace803ac0" already provided: unspecified iostream_category error ``` winterrdog reproduced it on the #35665 thread with another pair. Merge the records rather than the map entries, keeping the leaf script already there. BIP 174 lets the combiner "pick arbitrarily when conflicts occur", and unknown and proprietary records already resolve that way. Refusing to combine is the BIP's other option, but that would fail `combinepsbt` on input it accepts today. Merging by map key drops records as well. `std::map::insert` leaves existing keys alone, so when both PSBTs carry the same leaf script with different control blocks, the incoming set was dropped. Those keys do not conflict, so merging per record keeps them. The control blocks already present are collected once per merge rather than searched for per incoming record, which would be quadratic in the size of the two PSBTs `combinepsbt` takes from the caller. Since this is the second field with this shape I checked the rest. `m_xpubs` (#35665) and `m_tap_scripts` are the only two whose record key comes from the value, so two map entries can serialize the same key. `partial_sigs` is keyed by `CKeyID` and serialized under the pubkey, but the pubkey determines the `CKeyID`, so those records stay distinct. The others key the record by the map key, `m_proprietary` included, and `PSBTOutput` has no such field. The test fails on master on both counts, and covers the merges that do not conflict as well. I found this with a local assertion in the psbt fuzz target that a combined PSBT must roundtrip. That assertion can go in a follow-up. Tested: ``` ./build/bin/test_bitcoin --run_test=psbt_tests ./build/bin/test_bitcoin --run_test=psbt_wallet_tests ./build/test/functional/test_runner.py rpc_psbt.py rpc_rawtransaction.py wallet_taproot.py wallet_signer.py feature_taproot.py wallet_basic.py ``` ACKs for top commit: achow101: ACK1cb416397bwinterrdog: re-ACK1cb416397bTree-SHA512: 2599beffe701ba9b672e8dcc3d43844f3853ceeb3d86fc53798a428d3288aa8edd2e42f338b32a1046fa558d6cc5cfa5d55b3a0aaf960278b0f3002158381623
Bitcoin Core integration/staging tree
For an immediately usable, binary version of the Bitcoin Core software, see https://bitcoincore.org/en/download/.
What is Bitcoin Core?
Bitcoin Core connects to the Bitcoin peer-to-peer network to download and fully validate blocks and transactions. It also includes a wallet and graphical user interface, which can be optionally built.
Further information about Bitcoin Core is available in the doc folder.
License
Bitcoin Core is released under the terms of the MIT license. See COPYING for more information or see https://opensource.org/license/MIT.
Development Process
The master branch is regularly built (see doc/build-*.md for instructions) and tested, but it is not guaranteed to be
completely stable. Tags are created
regularly from release branches to indicate new official, stable release versions of Bitcoin Core.
The https://github.com/bitcoin-core/gui repository is used exclusively for the development of the GUI. Its master branch is identical in all monotree repositories. Release branches and tags do not exist, so please do not fork that repository unless it is for development reasons.
The contribution workflow is described in CONTRIBUTING.md and useful hints for developers can be found in doc/developer-notes.md.
Testing
Testing and code review is the bottleneck for development; we get more pull requests than we can review and test on short notice. Please be patient and help out by testing other people's pull requests, and remember this is a security-critical project where any mistake might cost people lots of money.
Automated Testing
Developers are strongly encouraged to write unit tests for new code, and to
submit new unit tests for old code. Unit tests can be compiled and run
(assuming they weren't disabled during the generation of the build system) with: ctest. Further details on running
and extending unit tests can be found in /src/test/README.md.
There are also regression and integration tests, written
in Python.
These tests can be run (if the test dependencies are installed) with: build/test/functional/test_runner.py
(assuming build is your build directory).
The CI (Continuous Integration) systems make sure that every pull request is tested on Windows, Linux, and macOS. The CI must pass on all commits before merge to avoid unrelated CI failures on new pull requests.
Manual Quality Assurance (QA) Testing
Changes should be tested by somebody other than the developer who wrote the code. This is especially important for large or high-risk changes. It is useful to add a test plan to the pull request description if testing the changes is not straightforward.
Translations
Changes to translations as well as new translations can be submitted to Bitcoin Core's Transifex page.
Translations are periodically pulled from Transifex and merged into the git repository. See the translation process for details on how this works.
Important: We do not accept translation changes as GitHub pull requests because the next pull from Transifex would automatically overwrite them again.