use crate::ln::features::ChannelTypeFeatures;
use crate::ln::PaymentPreimage;
use crate::prelude::*;
-use crate::sign::{EcdsaChannelSigner, SignerProvider, WriteableEcdsaChannelSigner};
+use crate::sign::{EcdsaChannelSigner, SignerProvider, WriteableEcdsaChannelSigner, P2WPKH_WITNESS_WEIGHT};
use crate::sync::Mutex;
use crate::util::logger::Logger;
}
impl Utxo {
- const P2WPKH_WITNESS_WEIGHT: u64 = 1 /* num stack items */ +
- 1 /* sig length */ +
- 73 /* sig including sighash flag */ +
- 1 /* pubkey length */ +
- 33 /* pubkey */;
-
/// Returns a `Utxo` with the `satisfaction_weight` estimate for a legacy P2PKH output.
pub fn new_p2pkh(outpoint: OutPoint, value: u64, pubkey_hash: &PubkeyHash) -> Self {
let script_sig_size = 1 /* script_sig length */ +
value,
script_pubkey: Script::new_p2sh(&Script::new_v0_p2wpkh(pubkey_hash).script_hash()),
},
- satisfaction_weight: script_sig_size * WITNESS_SCALE_FACTOR as u64 + Self::P2WPKH_WITNESS_WEIGHT,
+ satisfaction_weight: script_sig_size * WITNESS_SCALE_FACTOR as u64 + P2WPKH_WITNESS_WEIGHT,
}
}
value,
script_pubkey: Script::new_v0_p2wpkh(pubkey_hash),
},
- satisfaction_weight: EMPTY_SCRIPT_SIG_WEIGHT + Self::P2WPKH_WITNESS_WEIGHT,
+ satisfaction_weight: EMPTY_SCRIPT_SIG_WEIGHT + P2WPKH_WITNESS_WEIGHT,
}
}
}
previous_utxo: anchor_utxo,
satisfaction_weight: commitment_tx.weight() as u64 + ANCHOR_INPUT_WITNESS_WEIGHT + EMPTY_SCRIPT_SIG_WEIGHT,
}];
+ #[cfg(debug_assertions)]
+ let must_spend_amount = must_spend.iter().map(|input| input.previous_utxo.value).sum::<u64>();
log_debug!(self.logger, "Peforming coin selection for commitment package (commitment and anchor transaction) targeting {} sat/kW",
package_target_feerate_sat_per_1000_weight);
input: vec![anchor_descriptor.unsigned_tx_input()],
output: vec![],
};
+
#[cfg(debug_assertions)]
- let total_satisfaction_weight =
- coin_selection.confirmed_utxos.iter().map(|utxo| utxo.satisfaction_weight).sum::<u64>() +
- ANCHOR_INPUT_WITNESS_WEIGHT + EMPTY_SCRIPT_SIG_WEIGHT;
+ let total_satisfaction_weight = ANCHOR_INPUT_WITNESS_WEIGHT + EMPTY_SCRIPT_SIG_WEIGHT +
+ coin_selection.confirmed_utxos.iter().map(|utxo| utxo.satisfaction_weight).sum::<u64>();
+ #[cfg(debug_assertions)]
+ let total_input_amount = must_spend_amount +
+ coin_selection.confirmed_utxos.iter().map(|utxo| utxo.output.value).sum::<u64>();
self.process_coin_selection(&mut anchor_tx, coin_selection);
let anchor_txid = anchor_tx.txid();
// never underestimate.
assert!(expected_signed_tx_weight >= signed_tx_weight &&
expected_signed_tx_weight - (expected_signed_tx_weight / 100) <= signed_tx_weight);
+
+ let expected_package_fee = fee_for_weight(package_target_feerate_sat_per_1000_weight,
+ signed_tx_weight + commitment_tx.weight() as u64);
+ let package_fee = total_input_amount -
+ anchor_tx.output.iter().map(|output| output.value).sum::<u64>();
+ // Our fee should be within a 5% error margin of the expected fee based on the
+ // feerate and transaction weight and we should never pay less than required.
+ let fee_error_margin = expected_package_fee * 5 / 100;
+ assert!(package_fee >= expected_package_fee &&
+ package_fee - fee_error_margin <= expected_package_fee);
}
log_info!(self.logger, "Broadcasting anchor transaction {} to bump channel close with txid {}",
log_debug!(self.logger, "Peforming coin selection for HTLC transaction targeting {} sat/kW",
target_feerate_sat_per_1000_weight);
+
#[cfg(debug_assertions)]
let must_spend_satisfaction_weight =
must_spend.iter().map(|input| input.satisfaction_weight).sum::<u64>();
+ #[cfg(debug_assertions)]
+ let must_spend_amount = must_spend.iter().map(|input| input.previous_utxo.value).sum::<u64>();
+
let coin_selection = self.utxo_source.select_confirmed_utxos(
claim_id, must_spend, &htlc_tx.output, target_feerate_sat_per_1000_weight,
)?;
+
#[cfg(debug_assertions)]
- let total_satisfaction_weight =
- coin_selection.confirmed_utxos.iter().map(|utxo| utxo.satisfaction_weight).sum::<u64>() +
- must_spend_satisfaction_weight;
+ let total_satisfaction_weight = must_spend_satisfaction_weight +
+ coin_selection.confirmed_utxos.iter().map(|utxo| utxo.satisfaction_weight).sum::<u64>();
+ #[cfg(debug_assertions)]
+ let total_input_amount = must_spend_amount +
+ coin_selection.confirmed_utxos.iter().map(|utxo| utxo.output.value).sum::<u64>();
+
self.process_coin_selection(&mut htlc_tx, coin_selection);
#[cfg(debug_assertions)]
// never underestimate.
assert!(expected_signed_tx_weight >= signed_tx_weight &&
expected_signed_tx_weight - (expected_signed_tx_weight / 100) <= signed_tx_weight);
+
+ let expected_signed_tx_fee = fee_for_weight(target_feerate_sat_per_1000_weight, signed_tx_weight);
+ let signed_tx_fee = total_input_amount -
+ htlc_tx.output.iter().map(|output| output.value).sum::<u64>();
+ // Our fee should be within a 5% error margin of the expected fee based on the
+ // feerate and transaction weight and we should never pay less than required.
+ let fee_error_margin = expected_signed_tx_fee * 5 / 100;
+ assert!(signed_tx_fee >= expected_signed_tx_fee &&
+ signed_tx_fee - fee_error_margin <= expected_signed_tx_fee);
}
log_info!(self.logger, "Broadcasting {}", log_tx!(htlc_tx));