From 03a3230a67e9bf09e556d10550d9cdd08fb805d6 Mon Sep 17 00:00:00 2001 From: Michael Mallan Date: Fri, 8 Nov 2024 17:23:08 +0000 Subject: [PATCH] descriptors: add method to get max tx weight Errors in the weight calculation could be hidden when converting to vbytes. This separate method will make it easier to spot any such errors. --- liana/src/descriptors/mod.rs | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/liana/src/descriptors/mod.rs b/liana/src/descriptors/mod.rs index 2456f32e3..d59033787 100644 --- a/liana/src/descriptors/mod.rs +++ b/liana/src/descriptors/mod.rs @@ -545,20 +545,25 @@ impl LianaDescriptor { Ok(self.prune_bip32_derivs(psbt, path_info)) } - /// Maximum possible size in vbytes of an unsigned transaction, `tx`, + /// Maximum possible weight in weight units of an unsigned transaction, `tx`, /// after satisfaction, assuming all inputs of `tx` are from this /// descriptor. - pub fn unsigned_tx_max_vbytes(&self, tx: &bitcoin::Transaction, use_primary_path: bool) -> u64 { - let witness_factor: u64 = WITNESS_SCALE_FACTOR.try_into().unwrap(); + fn unsigned_tx_max_weight(&self, tx: &bitcoin::Transaction, use_primary_path: bool) -> u64 { let num_inputs: u64 = tx.input.len().try_into().unwrap(); let max_sat_weight: u64 = self.max_sat_weight(use_primary_path).try_into().unwrap(); // Add weights together before converting to vbytes to avoid rounding up multiple times. - let tx_wu = tx - .weight() + tx.weight() .to_wu() .checked_add(max_sat_weight.checked_mul(num_inputs).unwrap()) - .unwrap(); - tx_wu + .unwrap() + } + + /// Maximum possible size in vbytes of an unsigned transaction, `tx`, + /// after satisfaction, assuming all inputs of `tx` are from this + /// descriptor. + pub fn unsigned_tx_max_vbytes(&self, tx: &bitcoin::Transaction, use_primary_path: bool) -> u64 { + let witness_factor: u64 = WITNESS_SCALE_FACTOR.try_into().unwrap(); + self.unsigned_tx_max_weight(tx, use_primary_path) .checked_add(witness_factor.checked_sub(1).unwrap()) .unwrap() .checked_div(witness_factor)