-
Notifications
You must be signed in to change notification settings - Fork 155
Add support for writing integration tests that fail #417
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
Add support for writing integration tests that fail #417
Conversation
kanishk779
commented
May 22, 2022
- Earlier we did not have the ability to write integration tests that fail.
- The default behaviour was to panic and stop the execution of the program.
- But in addition to verifying that the code works in the correct cases, we must also ensure that it should generate an appropriate error in incorrect cases.
- Some functions are modified to return Result<T, E> to support this feature.
- Four failing tests have also been added (tests 10-13) to demonstrate this.
09ed81b
to
210376a
Compare
src/descriptor/tr.rs
Outdated
} else { | ||
return Err(Error::Unexpected(String::from( | ||
"Computing spend data on a valid Tree should always succeed", | ||
))); |
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.
How do you actually hit this case? How do you obtain a Tr
with an invalid tree? Can you add a unit test?
Relatedly, can you separate out the changes to src/
into a separate commit from the changes to the integration test?
Thanks for your contribution!!
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 case is hit when the height of the tree is more than 128. In the Impl for
TaprootBuilder
in file taproot.rs of the rust-bitcoin project, there is a functioninsert
, which returns aTaprootBuilderError
. - For which function do you want me to add the unit tests?
- Ok I will split it into two commits.
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.
Can you add a unit test for Taproot::spend_info
which causes this new error path to be taken?
I think that would answer my "how do you hit this case" question, which I'm still confused about, sorry.
And thanks for splitting up the commits!
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.
Yep, I am also curious when this gets hit. This seems like a big bug if we need to change this.
integration_test/run.sh
Outdated
@@ -31,6 +31,7 @@ sleep 3 | |||
|
|||
RPC_URL=http://localhost:12348 \ | |||
RPC_COOKIE=${TESTDIR}/1/regtest/.cookie \ | |||
RUST_BACKTRACE=1 \ |
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.
It is odd to supply this in RPC_URL.
You can supply this via your own shell environment.
integration_test/src/test_desc.rs
Outdated
// Currently this is not being used, since miniscript::Error is being propagated | ||
// But if the types of errors grow in future, we can extend this enum and use it. | ||
#[derive(Debug, PartialEq)] | ||
pub enum MalleableDescError { |
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 Error should be used as the return type
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.
Here is a suggestion for how to use this enum.
- Rename this type to DescTestError
- Change the return type from Error to DescTestError for the test function.
pub enum DesctestError {
DescParseError(Error),
PsbtFinalizeError(Error),
AddressComputationError(Error),
...
}
In the test function then, you would change the lines to make sure that correct variant is retuned.
src/descriptor/tr.rs
Outdated
} else { | ||
return Err(Error::Unexpected(String::from( | ||
"Computing spend data on a valid Tree should always succeed", | ||
))); |
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.
Yep, I am also curious when this gets hit. This seems like a big bug if we need to change this.
210376a
to
58ccb93
Compare
987545b
to
be10ec1
Compare
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.
Left a few comments
tests/test_desc.rs
Outdated
mod setup; | ||
// use crate::test_util::{self, TestData}; |
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.
remove the 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.
In ac639f6
tests/test_desc.rs
Outdated
@@ -55,12 +84,30 @@ pub fn test_desc_satisfy(cl: &Client, testdata: &TestData, desc: &str) -> Witnes | |||
.unwrap(); | |||
assert_eq!(blocks.len(), 1); | |||
|
|||
let desc = test_util::parse_test_desc(&desc, &testdata.pubdata); | |||
let mut desc; |
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.
use map_err instead
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.
In ac639f6
tests/test_desc.rs
Outdated
let derived_desc = desc.derived_descriptor(&secp, 0).unwrap(); | ||
// Next send some btc to each address corresponding to the miniscript | ||
let mut rest; | ||
match derived_desc.address(bitcoin::Network::Regtest) { |
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.
same as above: map_err
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.
In ac639f6
assert!(wit.len() == 3); // control block, script and one signatures | ||
|
||
// Test 5: When everything is available, we should select the key spend path | ||
let wit = test_desc_satisfy(cl, testdata, "tr(X,{pk(X1),and_v(v:pk(X2),pk(X3!))})"); | ||
let wit = test_desc_satisfy(cl, testdata, "tr(X,{pk(X1),and_v(v:pk(X2),pk(X3!))})").unwrap(); |
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.
Fix other warnings requiring the same unwrap
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.
In ac639f6, you should fix all the unwraps
src/descriptor/mod.rs
Outdated
@@ -332,7 +332,7 @@ impl<Pk: MiniscriptKey + ToPublicKey> Descriptor<Pk> { | |||
Descriptor::Wpkh(ref wpkh) => Ok(wpkh.address(network)), | |||
Descriptor::Wsh(ref wsh) => Ok(wsh.address(network)), | |||
Descriptor::Sh(ref sh) => Ok(sh.address(network)), | |||
Descriptor::Tr(ref tr) => Ok(tr.address(network)), | |||
Descriptor::Tr(ref tr) => Ok(tr.address(network)?), |
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.
Remove this commit
be10ec1
to
9ef6607
Compare
@kanishk779 , the history is still unclean. The first commit still adds a bunch of files. |
9ef6607
to
ce0220d
Compare
ce0220d
to
ab79593
Compare
@sanket1729 I have made the required changes. |
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 ab79593