From 176859ed926febfa16055d9e8e8f57ddc72cb9ba Mon Sep 17 00:00:00 2001 From: jp1ac4 <121959000+jp1ac4@users.noreply.github.com> Date: Fri, 20 Oct 2023 09:50:43 +0100 Subject: [PATCH] commands: add weights before converting to vbytes Converting to vbytes first can lead to a larger result and therefore a lower feerate, which could fall below 1 and fail the sanity check. --- src/commands/mod.rs | 9 ++++++++- src/descriptors/mod.rs | 2 +- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/src/commands/mod.rs b/src/commands/mod.rs index 0bdf09e8..705ce698 100644 --- a/src/commands/mod.rs +++ b/src/commands/mod.rs @@ -202,7 +202,14 @@ fn sanity_check_psbt( } // Check the feerate isn't insane. - let tx_vb = (tx.vsize() + spent_desc.max_sat_vbytes() * tx.input.len()) as u64; + // Add weights together before converting to vbytes to avoid rounding up multiple times + // and increasing the result, which could lead to the feerate in sats/vb falling below 1. + let tx_wu = tx.weight().to_wu() + (spent_desc.max_sat_weight() * tx.input.len()) as u64; + let tx_vb = tx_wu + .checked_add(descriptors::WITNESS_FACTOR as u64 - 1) + .unwrap() + .checked_div(descriptors::WITNESS_FACTOR as u64) + .unwrap(); let feerate_sats_vb = abs_fee .checked_div(tx_vb) .ok_or(CommandError::InsaneFees(InsaneFeeInfo::InvalidFeerate))?; diff --git a/src/descriptors/mod.rs b/src/descriptors/mod.rs index 72f1a919..f5b1564e 100644 --- a/src/descriptors/mod.rs +++ b/src/descriptors/mod.rs @@ -17,7 +17,7 @@ pub use keys::*; pub mod analysis; pub use analysis::*; -const WITNESS_FACTOR: usize = 4; +pub const WITNESS_FACTOR: usize = 4; #[derive(Debug)] pub enum LianaDescError {