Skip to content

Commit

Permalink
Merge #734: commands: add weights before converting to vbytes
Browse files Browse the repository at this point in the history
176859e commands: add weights before converting to vbytes (jp1ac4)

Pull request description:

  The `sanity_check_psbt()` function checks, among other things, that the feerate in sats/vbytes is in the required range. When estimating the transaction size, converting each part to vbytes first can lead to a larger result due to rounding multiple times and therefore a lower feerate, which could fall below 1 and fail the sanity check.

  This issue has arisen in #560 when creating a transaction using coin selection.

ACKs for top commit:
  darosior:
    utACK 176859e -- great catch

Tree-SHA512: 5e4fc795c5977c71a0f02b21c09cd71d59c9412f94bcafd5a4adb429c4ae3a568a25c4174bd305e0be927f06ae4e9be9aef791ffbbee2a7ac1c40740f639ab10
  • Loading branch information
darosior committed Oct 20, 2023
2 parents 65cf5cc + 176859e commit a4183dd
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
9 changes: 8 additions & 1 deletion src/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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))?;
Expand Down
2 changes: 1 addition & 1 deletion src/descriptors/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit a4183dd

Please sign in to comment.