]> git.bitcoin.ninja Git - rust-lightning/commitdiff
Introduce new helper commit_tx_fee_sat
authorAntoine Riard <dev@ariard.me>
Tue, 19 Oct 2021 17:29:50 +0000 (13:29 -0400)
committerAntoine Riard <dev@ariard.me>
Sun, 24 Oct 2021 21:16:39 +0000 (17:16 -0400)
lightning/src/ln/channel.rs

index 7883d7b29e4806643c16513fcfc3aa751fffd709..5bd47c329422b1e895a5e7f7df21bafa9d6a5282 100644 (file)
@@ -1183,11 +1183,11 @@ impl<Signer: Sign> Channel<Signer> {
                        broadcaster_max_commitment_tx_output.1 = cmp::max(broadcaster_max_commitment_tx_output.1, value_to_remote_msat as u64);
                }
 
-               let total_fee = feerate_per_kw as u64 * (COMMITMENT_TX_BASE_WEIGHT + (included_non_dust_htlcs.len() as u64) * COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000;
+               let total_fee_sat = Channel::<Signer>::commit_tx_fee_sat(feerate_per_kw, included_non_dust_htlcs.len());
                let (value_to_self, value_to_remote) = if self.is_outbound() {
-                       (value_to_self_msat / 1000 - total_fee as i64, value_to_remote_msat / 1000)
+                       (value_to_self_msat / 1000 - total_fee_sat as i64, value_to_remote_msat / 1000)
                } else {
-                       (value_to_self_msat / 1000, value_to_remote_msat / 1000 - total_fee as i64)
+                       (value_to_self_msat / 1000, value_to_remote_msat / 1000 - total_fee_sat as i64)
                };
 
                let mut value_to_a = if local { value_to_self } else { value_to_remote };
@@ -1995,7 +1995,7 @@ impl<Signer: Sign> Channel<Signer> {
                self.counterparty_selected_channel_reserve_satoshis)
        }
 
-       // Get the fee cost of a commitment tx with a given number of HTLC outputs.
+       // Get the fee cost in MSATS of a commitment tx with a given number of HTLC outputs.
        // Note that num_htlcs should not include dust HTLCs.
        fn commit_tx_fee_msat(&self, num_htlcs: usize) -> u64 {
                // Note that we need to divide before multiplying to round properly,
@@ -2003,6 +2003,13 @@ impl<Signer: Sign> Channel<Signer> {
                (COMMITMENT_TX_BASE_WEIGHT + num_htlcs as u64 * COMMITMENT_TX_WEIGHT_PER_HTLC) * self.feerate_per_kw as u64 / 1000 * 1000
        }
 
+       // Get the fee cost in SATS of a commitment tx with a given number of HTLC outputs.
+       // Note that num_htlcs should not include dust HTLCs.
+       #[inline]
+       fn commit_tx_fee_sat(feerate_per_kw: u32, num_htlcs: usize) -> u64 {
+               feerate_per_kw as u64 * (COMMITMENT_TX_BASE_WEIGHT + num_htlcs as u64 * COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000
+       }
+
        // Get the commitment tx fee for the local's (i.e. our) next commitment transaction based on the
        // number of pending HTLCs that are on track to be in our next commitment tx, plus an additional
        // HTLC if `fee_spike_buffer_htlc` is Some, plus a new HTLC given by `new_htlc_amount`. Dust HTLCs
@@ -2426,10 +2433,10 @@ impl<Signer: Sign> Channel<Signer> {
                        update_state == FeeUpdateState::RemoteAnnounced
                } else { false };
                if update_fee { debug_assert!(!self.is_outbound()); }
-               let total_fee = feerate_per_kw as u64 * (COMMITMENT_TX_BASE_WEIGHT + (num_htlcs as u64) * COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000;
+               let total_fee_sat = Channel::<Signer>::commit_tx_fee_sat(feerate_per_kw, num_htlcs);
                if update_fee {
                        let counterparty_reserve_we_require = Channel::<Signer>::get_holder_selected_channel_reserve_satoshis(self.channel_value_satoshis);
-                       if self.channel_value_satoshis - self.value_to_self_msat / 1000 < total_fee + counterparty_reserve_we_require {
+                       if self.channel_value_satoshis - self.value_to_self_msat / 1000 < total_fee_sat + counterparty_reserve_we_require {
                                return Err((None, ChannelError::Close("Funding remote cannot afford proposed new fee".to_owned())));
                        }
                }
@@ -2445,7 +2452,7 @@ impl<Signer: Sign> Channel<Signer> {
                                                && info.next_holder_htlc_id == self.next_holder_htlc_id
                                                && info.next_counterparty_htlc_id == self.next_counterparty_htlc_id
                                                && info.feerate == self.feerate_per_kw {
-                                                       assert_eq!(total_fee, info.fee / 1000);
+                                                       assert_eq!(total_fee_sat, info.fee / 1000);
                                                }
                                }
                        }