Skip to content

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

Merged
merged 2 commits into from
Jun 19, 2022

Conversation

kanishk779
Copy link
Contributor

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

@kanishk779 kanishk779 force-pushed the aman-integration-tests branch 3 times, most recently from 09ed81b to 210376a Compare May 22, 2022 20:24
} else {
return Err(Error::Unexpected(String::from(
"Computing spend data on a valid Tree should always succeed",
)));
Copy link
Member

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!!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. 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 function insert, which returns a TaprootBuilderError.
  2. For which function do you want me to add the unit tests?
  3. Ok I will split it into two commits.

Copy link
Member

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!

Copy link
Member

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.

@@ -31,6 +31,7 @@ sleep 3

RPC_URL=http://localhost:12348 \
RPC_COOKIE=${TESTDIR}/1/regtest/.cookie \
RUST_BACKTRACE=1 \
Copy link
Member

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.

// 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 {
Copy link
Member

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

Copy link
Member

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.

  1. Rename this type to DescTestError
  2. 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.

} else {
return Err(Error::Unexpected(String::from(
"Computing spend data on a valid Tree should always succeed",
)));
Copy link
Member

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.

@kanishk779 kanishk779 force-pushed the aman-integration-tests branch from 210376a to 58ccb93 Compare May 29, 2022 14:22
@kanishk779 kanishk779 force-pushed the aman-integration-tests branch 2 times, most recently from 987545b to be10ec1 Compare June 11, 2022 06:59
Copy link
Member

@sanket1729 sanket1729 left a 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

mod setup;
// use crate::test_util::{self, TestData};
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove the comment

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In ac639f6

@@ -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;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use map_err instead

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In ac639f6

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) {
Copy link
Member

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

Copy link
Member

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();
Copy link
Member

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

Copy link
Member

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

@@ -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)?),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove this commit

@kanishk779 kanishk779 force-pushed the aman-integration-tests branch from be10ec1 to 9ef6607 Compare June 13, 2022 16:56
@sanket1729
Copy link
Member

@kanishk779 , the history is still unclean. The first commit still adds a bunch of files.

@kanishk779 kanishk779 force-pushed the aman-integration-tests branch from 9ef6607 to ce0220d Compare June 13, 2022 17:35
@kanishk779 kanishk779 force-pushed the aman-integration-tests branch from ce0220d to ab79593 Compare June 18, 2022 10:05
@kanishk779
Copy link
Contributor Author

@sanket1729 I have made the required changes.

Copy link
Member

@sanket1729 sanket1729 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ACK ab79593

@sanket1729 sanket1729 merged commit e0650ab into rust-bitcoin:master Jun 19, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants