diff --git a/src/commands/mod.rs b/src/commands/mod.rs index 5d83ba3b..08ee7b90 100644 --- a/src/commands/mod.rs +++ b/src/commands/mod.rs @@ -9,8 +9,8 @@ use crate::{ database::{Coin, DatabaseConnection, DatabaseInterface}, descriptors, spend::{ - check_output_value, create_spend, unsigned_tx_max_vbytes, AddrInfo, CandidateCoin, - CreateSpendRes, SpendCreationError, SpendOutputAddress, TxGetter, + check_output_value, create_spend, AddrInfo, CandidateCoin, CreateSpendRes, + SpendCreationError, SpendOutputAddress, TxGetter, }, DaemonControl, VERSION, }; @@ -29,7 +29,10 @@ use std::{ }; use miniscript::{ - bitcoin::{self, address, bip32, psbt::PartiallySignedTransaction as Psbt}, + bitcoin::{ + self, address, bip32, constants::WITNESS_SCALE_FACTOR, + psbt::PartiallySignedTransaction as Psbt, + }, psbt::PsbtExt, }; use serde::{Deserialize, Serialize}; @@ -176,6 +179,29 @@ impl<'a> TxGetter for BitcoindTxGetter<'a> { } } +/// An unsigned transaction's maximum possible size in vbytes after satisfaction. +/// +/// This assumes all inputs are internal (or have the same `max_sat_weight` value). +/// +/// `tx` is the unsigned transaction. +/// +/// `max_sat_weight` is the maximum weight difference of an input in the +/// transaction before and after satisfaction. Must be in weight units. +fn unsigned_tx_max_vbytes(tx: &bitcoin::Transaction, max_sat_weight: u64) -> u64 { + let witness_factor: u64 = WITNESS_SCALE_FACTOR.try_into().unwrap(); + let num_inputs: u64 = tx.input.len().try_into().unwrap(); + let tx_wu: u64 = tx + .weight() + .to_wu() + .checked_add(max_sat_weight.checked_mul(num_inputs).unwrap()) + .unwrap(); + tx_wu + .checked_add(witness_factor.checked_sub(1).unwrap()) + .unwrap() + .checked_div(witness_factor) + .unwrap() +} + impl DaemonControl { // Get the derived descriptor for this coin fn derived_desc(&self, coin: &Coin) -> descriptors::DerivedSinglePathLianaDesc { diff --git a/src/spend.rs b/src/spend.rs index 1b11a339..a57f6f45 100644 --- a/src/spend.rs +++ b/src/spend.rs @@ -138,9 +138,9 @@ fn sanity_check_psbt( // 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) + .checked_add(WITNESS_SCALE_FACTOR as u64 - 1) .unwrap() - .checked_div(descriptors::WITNESS_FACTOR as u64) + .checked_div(WITNESS_SCALE_FACTOR as u64) .unwrap(); let feerate_sats_vb = abs_fee .checked_div(tx_vb) @@ -361,29 +361,6 @@ fn derived_desc( desc.derive(coin.derivation_index, secp) } -/// An unsigned transaction's maximum possible size in vbytes after satisfaction. -/// -/// This assumes all inputs are internal (or have the same `max_sat_weight` value). -/// -/// `tx` is the unsigned transaction. -/// -/// `max_sat_weight` is the maximum weight difference of an input in the -/// transaction before and after satisfaction. Must be in weight units. -pub fn unsigned_tx_max_vbytes(tx: &bitcoin::Transaction, max_sat_weight: u64) -> u64 { - let witness_factor: u64 = WITNESS_SCALE_FACTOR.try_into().unwrap(); - let num_inputs: u64 = tx.input.len().try_into().unwrap(); - let tx_wu: u64 = tx - .weight() - .to_wu() - .checked_add(max_sat_weight.checked_mul(num_inputs).unwrap()) - .unwrap(); - tx_wu - .checked_add(witness_factor.checked_sub(1).unwrap()) - .unwrap() - .checked_div(witness_factor) - .unwrap() -} - #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub struct AddrInfo { pub index: bip32::ChildNumber,