Skip to content

Commit ff6ea71

Browse files
TheBlueMattyuntai
authored andcommitted
Check that reserves are met or progress is made on commitment tx gen
1 parent 8dc6c4e commit ff6ea71

File tree

1 file changed

+42
-2
lines changed

1 file changed

+42
-2
lines changed

src/ln/channel.rs

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,13 @@ pub(super) struct Channel {
319319
channel_update_count: u32,
320320
feerate_per_kw: u64,
321321

322+
#[cfg(debug_assertions)]
323+
/// Max to_local and to_remote outputs in a locally-generated commitment transaction
324+
max_commitment_tx_output_local: ::std::sync::Mutex<(u64, u64)>,
325+
#[cfg(debug_assertions)]
326+
/// Max to_local and to_remote outputs in a remote-generated commitment transaction
327+
max_commitment_tx_output_remote: ::std::sync::Mutex<(u64, u64)>,
328+
322329
#[cfg(test)]
323330
// Used in ChannelManager's tests to send a revoked transaction
324331
pub last_local_commitment_txn: Vec<Transaction>,
@@ -483,6 +490,11 @@ impl Channel {
483490
next_remote_htlc_id: 0,
484491
channel_update_count: 1,
485492

493+
#[cfg(debug_assertions)]
494+
max_commitment_tx_output_local: ::std::sync::Mutex::new((channel_value_satoshis * 1000 - push_msat, push_msat)),
495+
#[cfg(debug_assertions)]
496+
max_commitment_tx_output_remote: ::std::sync::Mutex::new((channel_value_satoshis * 1000 - push_msat, push_msat)),
497+
486498
last_local_commitment_txn: Vec::new(),
487499

488500
last_sent_closing_fee: None,
@@ -643,6 +655,11 @@ impl Channel {
643655
next_remote_htlc_id: 0,
644656
channel_update_count: 1,
645657

658+
#[cfg(debug_assertions)]
659+
max_commitment_tx_output_local: ::std::sync::Mutex::new((msg.push_msat, msg.funding_satoshis * 1000 - msg.push_msat)),
660+
#[cfg(debug_assertions)]
661+
max_commitment_tx_output_remote: ::std::sync::Mutex::new((msg.push_msat, msg.funding_satoshis * 1000 - msg.push_msat)),
662+
646663
last_local_commitment_txn: Vec::new(),
647664

648665
last_sent_closing_fee: None,
@@ -828,9 +845,32 @@ impl Channel {
828845
}
829846

830847

848+
let value_to_self_msat: i64 = (self.value_to_self_msat - local_htlc_total_msat) as i64 + value_to_self_msat_offset;
849+
let value_to_remote_msat: i64 = (self.channel_value_satoshis * 1000 - self.value_to_self_msat - remote_htlc_total_msat) as i64 - value_to_self_msat_offset;
850+
851+
#[cfg(debug_assertions)]
852+
{
853+
// Make sure that the to_self/to_remote is always either past the appropriate
854+
// channel_reserve *or* it is making progress towards it.
855+
// TODO: This should happen after fee calculation, but we don't handle that correctly
856+
// yet!
857+
let mut max_commitment_tx_output = if generated_by_local {
858+
self.max_commitment_tx_output_local.lock().unwrap()
859+
} else {
860+
self.max_commitment_tx_output_remote.lock().unwrap()
861+
};
862+
debug_assert!(max_commitment_tx_output.0 <= value_to_self_msat as u64 || value_to_self_msat / 1000 >= self.their_channel_reserve_satoshis as i64);
863+
max_commitment_tx_output.0 = cmp::max(max_commitment_tx_output.0, value_to_self_msat as u64);
864+
debug_assert!(max_commitment_tx_output.1 <= value_to_remote_msat as u64 || value_to_remote_msat / 1000 >= Channel::get_our_channel_reserve_satoshis(self.channel_value_satoshis) as i64);
865+
max_commitment_tx_output.1 = cmp::max(max_commitment_tx_output.1, value_to_remote_msat as u64);
866+
}
867+
831868
let total_fee: u64 = feerate_per_kw * (COMMITMENT_TX_BASE_WEIGHT + (txouts.len() as u64) * COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000;
832-
let value_to_self: i64 = ((self.value_to_self_msat - local_htlc_total_msat) as i64 + value_to_self_msat_offset) / 1000 - if self.channel_outbound { total_fee as i64 } else { 0 };
833-
let value_to_remote: i64 = (((self.channel_value_satoshis * 1000 - self.value_to_self_msat - remote_htlc_total_msat) as i64 - value_to_self_msat_offset) / 1000) - if self.channel_outbound { 0 } else { total_fee as i64 };
869+
let (value_to_self, value_to_remote) = if self.channel_outbound {
870+
(value_to_self_msat / 1000 - total_fee as i64, value_to_remote_msat / 1000)
871+
} else {
872+
(value_to_self_msat / 1000, value_to_remote_msat / 1000 - total_fee as i64)
873+
};
834874

835875
let value_to_a = if local { value_to_self } else { value_to_remote };
836876
let value_to_b = if local { value_to_remote } else { value_to_self };

0 commit comments

Comments
 (0)