-
Notifications
You must be signed in to change notification settings - Fork 175
Append witness script for P2WSH in Plan::satisfy()
#897
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Append witness script for P2WSH in Plan::satisfy()
#897
Conversation
66692de to
962bcc2
Compare
|
The other option is to update |
|
cc @tcharding |
|
In 962bcc2: It looks like you have two identical branches that only differ in their Thanks for the LLM disclosure! And nice job finding this bug. |
34a5f89 to
8d9a61a
Compare
| use miniscript::{ | ||
| bitcoin, hash256, Descriptor, DescriptorPublicKey, Error, Miniscript, ScriptContext, | ||
| Translator, | ||
| bitcoin, hash256, Descriptor, DescriptorPublicKey, Error, Miniscript, ScriptContext, Translator, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: If you feel like it can you throw a patch at the front of this PR that runs the formatter in bitcoind-tests please mate so that these fromatting changes aren't mixed in with your changes.
bitcoind-tests/tests/test_desc.rs
Outdated
| /// Test that Plan::satisfy() correctly constructs witness for P2WSH descriptors. | ||
| /// This is a regression test for https://github.com/rust-bitcoin/rust-miniscript/issues/896 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I rekon we can trim a bit more LLM noise out of this patch. I see no value in these comments.
| assert!(num_conf > 0); | ||
|
|
||
| Ok(unsigned_tx.input[0].witness.clone()) | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This repo is not my baby so defering to @apoelstra.
Do we want to have code like this in the repo? It may do whats needed but its pretty far from clean code.
(Not a dig at you @evanlinjin more a philosophical / high level comment on do we want to merge LLM generated test code since users will look at it and copy it and think we wrote it.)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The idea of this is to test what we have against Bitcoin core - can we broadcast a tx with wsh spend(s)?
In terms of "cleanliness", I would argue it's quite easy to read as it is? Could you expand on this please, thanks.
bitcoind-tests/tests/test_desc.rs
Outdated
| println!("Testing wsh(multi(2,K1,K2,K3)) with Plan::satisfy()"); | ||
| test_plan_satisfy_wsh(cl, &testdata, "wsh(multi(2,K1,K2,K3))").unwrap(); | ||
|
|
||
| // Test P2SH-wrapped P2WSH with Plan::satisfy() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd prefer to see two tests instead of these separator style comments.
bitcoind-tests/tests/test_desc.rs
Outdated
| println!("Testing sh(wsh(pk(K))) with Plan::satisfy()"); | ||
| test_plan_satisfy_wsh(cl, &testdata, "sh(wsh(pk(K)))").unwrap(); | ||
|
|
||
| println!("Testing sh(wsh(multi(2,K1,K2,K3))) with Plan::satisfy()"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
All these println statements are unneeded IMO.
| | DescriptorType::ShWshSortedMulti => { | ||
| let mut stack = stack; | ||
| let witness_script = self | ||
| .descriptor | ||
| .explicit_script() | ||
| .expect("wsh descriptors have explicit script"); | ||
| stack.push(witness_script.into_bytes()); | ||
| let script_sig = self.descriptor.unsigned_script_sig(); | ||
| (stack, script_sig) | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| | DescriptorType::ShWshSortedMulti => { | |
| let mut stack = stack; | |
| let witness_script = self | |
| .descriptor | |
| .explicit_script() | |
| .expect("wsh descriptors have explicit script"); | |
| stack.push(witness_script.into_bytes()); | |
| let script_sig = self.descriptor.unsigned_script_sig(); | |
| (stack, script_sig) | |
| } | |
| | DescriptorType::ShWshSortedMulti => { | |
| let mut stack = stack; | |
| // We need to append the witness script as per BIP-141 | |
| let witness_script = self | |
| .descriptor | |
| .explicit_script() | |
| .expect("wsh descriptors have explicit script"); | |
| stack.push(witness_script.into_bytes()); | |
| (stack, self.descriptor.unsigned_script_sig()) | |
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't hack on this repo much so am not super familiar with the style but I'd write it like that. Specifically making the return statement mirror the other one that is the same while highlighting the difference.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @evanlinjin for the bug find and the fix. I appreciate that you have other work to do so I'm hesitant to ask too much of you but from my personal view point this PR has way too much test code for this fix. The bare minimum test code to prove that the bug exits would be better IMO.
EDIT: hmm, re-reading my review I see I suggested cleaning up the test code then said "there is too much test code" ... I think you will get where I'm coming from.
1c841fa to
026943a
Compare
|
@tcharding I've cleaned things up as much as I can. I think the integration test is worth keeping as it checks the logic against Bitcoin Core - however, I'm happy to remove them as well. |
|
Thanks @evanlinjin. I did a more thorough read of the test code. Its not perfect but I retract my statement that there is just too much test code. Thanks for iterating. My review doesn't hold too much weight in this repo but FWIW this LGTM. One nit: can we format with the nightly toolchain like we do for the rest of the repo? Reviewed: 026943a |
🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
Fix Plan::satisfy() to correctly append the witnessScript as the final witness stack element for P2WSH descriptor types (Wsh, WshSortedMulti, ShWsh, ShWshSortedMulti). Previously, these types were incorrectly grouped with Wpkh and Tr, which don't require a trailing witness script. This caused transactions built using Plan::satisfy() to fail validation with "Witness program hash mismatch" when broadcast. The fix separates the descriptor type handling: - Wpkh/Tr: return stack as-is (no witness script needed) - Wsh/WshSortedMulti: append witness script, empty script_sig - ShWpkh: return stack with unsigned_script_sig (no witness script) - ShWsh/ShWshSortedMulti: append witness script and unsigned_script_sig Closes rust-bitcoin#896 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
Add test_plan_satisfy_wsh() and test_plan_satisfy_sh_wsh() which verify that Plan::satisfy() correctly constructs witness and script_sig for P2WSH descriptors by calling plan.satisfy() directly and broadcasting the resulting transaction to Bitcoin Core. This is a regression test for rust-bitcoin#896. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
026943a to
871e953
Compare
Done! |
tcharding
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ACK 871e953
Fix Plan::satisfy() to correctly append the witnessScript as the final witness stack element for P2WSH descriptor types (Wsh, WshSortedMulti, ShWsh, ShWshSortedMulti).
Previously, these types were incorrectly grouped with Wpkh and Tr, which don't require a trailing witness script. This caused transactions built using Plan::satisfy() to fail validation with "Witness program hash mismatch" when broadcast.
The fix separates the descriptor type handling:
Closes #896
🤖 Generated with Claude Code