@@ -308,6 +308,13 @@ pub(super) struct Channel {
308
308
channel_update_count : u32 ,
309
309
feerate_per_kw : u64 ,
310
310
311
+ #[ cfg( debug_assertions) ]
312
+ /// Max to_local and to_remote outputs in a locally-generated commitment transaction
313
+ max_commitment_tx_output_local : :: std:: sync:: Mutex < ( u64 , u64 ) > ,
314
+ #[ cfg( debug_assertions) ]
315
+ /// Max to_local and to_remote outputs in a remote-generated commitment transaction
316
+ max_commitment_tx_output_remote : :: std:: sync:: Mutex < ( u64 , u64 ) > ,
317
+
311
318
#[ cfg( test) ]
312
319
// Used in ChannelManager's tests to send a revoked transaction
313
320
pub last_local_commitment_txn : Vec < Transaction > ,
@@ -469,6 +476,11 @@ impl Channel {
469
476
next_remote_htlc_id : 0 ,
470
477
channel_update_count : 1 ,
471
478
479
+ #[ cfg( debug_assertions) ]
480
+ max_commitment_tx_output_local : :: std:: sync:: Mutex :: new ( ( channel_value_satoshis * 1000 - push_msat, push_msat) ) ,
481
+ #[ cfg( debug_assertions) ]
482
+ max_commitment_tx_output_remote : :: std:: sync:: Mutex :: new ( ( channel_value_satoshis * 1000 - push_msat, push_msat) ) ,
483
+
472
484
last_local_commitment_txn : Vec :: new ( ) ,
473
485
474
486
last_sent_closing_fee : None ,
@@ -629,6 +641,11 @@ impl Channel {
629
641
next_remote_htlc_id : 0 ,
630
642
channel_update_count : 1 ,
631
643
644
+ #[ cfg( debug_assertions) ]
645
+ max_commitment_tx_output_local : :: std:: sync:: Mutex :: new ( ( msg. push_msat , msg. funding_satoshis * 1000 - msg. push_msat ) ) ,
646
+ #[ cfg( debug_assertions) ]
647
+ max_commitment_tx_output_remote : :: std:: sync:: Mutex :: new ( ( msg. push_msat , msg. funding_satoshis * 1000 - msg. push_msat ) ) ,
648
+
632
649
last_local_commitment_txn : Vec :: new ( ) ,
633
650
634
651
last_sent_closing_fee : None ,
@@ -814,9 +831,32 @@ impl Channel {
814
831
}
815
832
816
833
834
+ let value_to_self_msat: i64 = ( self . value_to_self_msat - local_htlc_total_msat) as i64 + value_to_self_msat_offset;
835
+ 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;
836
+
837
+ #[ cfg( debug_assertions) ]
838
+ {
839
+ // Make sure that the to_self/to_remote is always either past the appropriate
840
+ // channel_reserve *or* it is making progress towards it.
841
+ // TODO: This should happen after fee calculation, but we don't handle that correctly
842
+ // yet!
843
+ let mut max_commitment_tx_output = if generated_by_local {
844
+ self . max_commitment_tx_output_local . lock ( ) . unwrap ( )
845
+ } else {
846
+ self . max_commitment_tx_output_remote . lock ( ) . unwrap ( )
847
+ } ;
848
+ 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 ) ;
849
+ max_commitment_tx_output. 0 = cmp:: max ( max_commitment_tx_output. 0 , value_to_self_msat as u64 ) ;
850
+ 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 ) ;
851
+ max_commitment_tx_output. 1 = cmp:: max ( max_commitment_tx_output. 1 , value_to_remote_msat as u64 ) ;
852
+ }
853
+
817
854
let total_fee: u64 = feerate_per_kw * ( COMMITMENT_TX_BASE_WEIGHT + ( txouts. len ( ) as u64 ) * COMMITMENT_TX_WEIGHT_PER_HTLC ) / 1000 ;
818
- 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 } ;
819
- 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 } ;
855
+ let ( value_to_self, value_to_remote) = if self . channel_outbound {
856
+ ( value_to_self_msat / 1000 - total_fee as i64 , value_to_remote_msat / 1000 )
857
+ } else {
858
+ ( value_to_self_msat / 1000 , value_to_remote_msat / 1000 - total_fee as i64 )
859
+ } ;
820
860
821
861
let value_to_a = if local { value_to_self } else { value_to_remote } ;
822
862
let value_to_b = if local { value_to_remote } else { value_to_self } ;
0 commit comments