Skip to content

Commit 8817924

Browse files
committed
Add support for writing integration tests that fail : src folder
1 parent a79be4f commit 8817924

File tree

3 files changed

+17
-13
lines changed

3 files changed

+17
-13
lines changed

src/descriptor/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,7 @@ impl<Pk: MiniscriptKey + ToPublicKey> Descriptor<Pk> {
331331
Descriptor::Wpkh(ref wpkh) => Ok(wpkh.address(network)),
332332
Descriptor::Wsh(ref wsh) => Ok(wsh.address(network)),
333333
Descriptor::Sh(ref sh) => Ok(sh.address(network)),
334-
Descriptor::Tr(ref tr) => Ok(tr.address(network)),
334+
Descriptor::Tr(ref tr) => Ok(tr.address(network)?),
335335
}
336336
}
337337

src/descriptor/tr.rs

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -214,15 +214,15 @@ impl<Pk: MiniscriptKey> Tr<Pk> {
214214
/// If spend data is already computed (i.e it is not `None`), this does not recompute it.
215215
///
216216
/// [`TaprootSpendInfo`] is only required for spending via the script paths.
217-
pub fn spend_info(&self) -> Arc<TaprootSpendInfo>
217+
pub fn spend_info(&self) -> Result<Arc<TaprootSpendInfo>, Error>
218218
where
219219
Pk: ToPublicKey,
220220
{
221221
// If the value is already cache, read it
222222
// read only panics if the lock is poisoned (meaning other thread having a lock panicked)
223223
let read_lock = self.spend_info.lock().expect("Lock poisoned");
224224
if let Some(ref spend_info) = *read_lock {
225-
return Arc::clone(spend_info);
225+
return Ok(Arc::clone(spend_info));
226226
}
227227
drop(read_lock);
228228

@@ -236,9 +236,13 @@ impl<Pk: MiniscriptKey> Tr<Pk> {
236236
let mut builder = TaprootBuilder::new();
237237
for (depth, ms) in self.iter_scripts() {
238238
let script = ms.encode();
239-
builder = builder
240-
.add_leaf(depth, script)
241-
.expect("Computing spend data on a valid Tree should always succeed");
239+
if let Ok(taproot_builder) = builder.add_leaf(depth, script) {
240+
builder = taproot_builder;
241+
} else {
242+
return Err(Error::Unexpected(String::from(
243+
"Computing spend data on a valid Tree should always succeed",
244+
)));
245+
}
242246
}
243247
// Assert builder cannot error here because we have a well formed descriptor
244248
match builder.finalize(&secp, self.internal_key.to_x_only_pubkey()) {
@@ -267,7 +271,7 @@ impl<Pk: MiniscriptKey> Tr<Pk> {
267271
};
268272
let spend_info = Arc::new(data);
269273
*self.spend_info.lock().expect("Lock poisoned") = Some(Arc::clone(&spend_info));
270-
spend_info
274+
Ok(spend_info)
271275
}
272276

273277
/// Checks whether the descriptor is safe.
@@ -315,7 +319,7 @@ impl<Pk: MiniscriptKey> Tr<Pk> {
315319
impl<Pk: MiniscriptKey + ToPublicKey> Tr<Pk> {
316320
/// Obtains the corresponding script pubkey for this descriptor.
317321
pub fn script_pubkey(&self) -> Script {
318-
let output_key = self.spend_info().output_key();
322+
let output_key = self.spend_info().unwrap().output_key();
319323
let builder = bitcoin::blockdata::script::Builder::new();
320324
builder
321325
.push_opcode(opcodes::all::OP_PUSHNUM_1)
@@ -324,9 +328,9 @@ impl<Pk: MiniscriptKey + ToPublicKey> Tr<Pk> {
324328
}
325329

326330
/// Obtains the corresponding address for this descriptor.
327-
pub fn address(&self, network: Network) -> Address {
328-
let spend_info = self.spend_info();
329-
Address::p2tr_tweaked(spend_info.output_key(), network)
331+
pub fn address(&self, network: Network) -> Result<Address, Error> {
332+
let spend_info = self.spend_info()?;
333+
Ok(Address::p2tr_tweaked(spend_info.output_key(), network))
330334
}
331335

332336
/// Returns satisfying non-malleable witness and scriptSig with minimum
@@ -658,7 +662,7 @@ where
658662
Pk: ToPublicKey,
659663
S: Satisfier<Pk>,
660664
{
661-
let spend_info = desc.spend_info();
665+
let spend_info = desc.spend_info().unwrap();
662666
// First try the key spend path
663667
if let Some(sig) = satisfier.lookup_tap_key_spend_sig() {
664668
Ok((vec![sig.to_vec()], Script::new()))

src/psbt/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -959,7 +959,7 @@ fn update_input_with_descriptor_helper(
959959

960960
// NOTE: they will both always be Tr
961961
if let (Descriptor::Tr(tr_derived), Descriptor::Tr(tr_xpk)) = (&derived, descriptor) {
962-
let spend_info = tr_derived.spend_info();
962+
let spend_info = tr_derived.spend_info().unwrap();
963963
let ik_derived = spend_info.internal_key();
964964
let ik_xpk = tr_xpk.internal_key();
965965
input.tap_internal_key = Some(ik_derived);

0 commit comments

Comments
 (0)