@@ -319,6 +319,13 @@ pub(super) struct Channel {
319
319
channel_update_count : u32 ,
320
320
feerate_per_kw : u64 ,
321
321
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
+
322
329
#[ cfg( test) ]
323
330
// Used in ChannelManager's tests to send a revoked transaction
324
331
pub last_local_commitment_txn : Vec < Transaction > ,
@@ -491,6 +498,11 @@ impl Channel {
491
498
next_remote_htlc_id : 0 ,
492
499
channel_update_count : 1 ,
493
500
501
+ #[ cfg( debug_assertions) ]
502
+ max_commitment_tx_output_local : :: std:: sync:: Mutex :: new ( ( channel_value_satoshis * 1000 - push_msat, push_msat) ) ,
503
+ #[ cfg( debug_assertions) ]
504
+ max_commitment_tx_output_remote : :: std:: sync:: Mutex :: new ( ( channel_value_satoshis * 1000 - push_msat, push_msat) ) ,
505
+
494
506
last_local_commitment_txn : Vec :: new ( ) ,
495
507
496
508
last_sent_closing_fee : None ,
@@ -641,6 +653,11 @@ impl Channel {
641
653
next_remote_htlc_id : 0 ,
642
654
channel_update_count : 1 ,
643
655
656
+ #[ cfg( debug_assertions) ]
657
+ max_commitment_tx_output_local : :: std:: sync:: Mutex :: new ( ( msg. push_msat , msg. funding_satoshis * 1000 - msg. push_msat ) ) ,
658
+ #[ cfg( debug_assertions) ]
659
+ max_commitment_tx_output_remote : :: std:: sync:: Mutex :: new ( ( msg. push_msat , msg. funding_satoshis * 1000 - msg. push_msat ) ) ,
660
+
644
661
last_local_commitment_txn : Vec :: new ( ) ,
645
662
646
663
last_sent_closing_fee : None ,
@@ -826,9 +843,32 @@ impl Channel {
826
843
}
827
844
828
845
846
+ let value_to_self_msat: i64 = ( self . value_to_self_msat - local_htlc_total_msat) as i64 + value_to_self_msat_offset;
847
+ 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;
848
+
849
+ #[ cfg( debug_assertions) ]
850
+ {
851
+ // Make sure that the to_self/to_remote is always either past the appropriate
852
+ // channel_reserve *or* it is making progress towards it.
853
+ // TODO: This should happen after fee calculation, but we don't handle that correctly
854
+ // yet!
855
+ let mut max_commitment_tx_output = if generated_by_local {
856
+ self . max_commitment_tx_output_local . lock ( ) . unwrap ( )
857
+ } else {
858
+ self . max_commitment_tx_output_remote . lock ( ) . unwrap ( )
859
+ } ;
860
+ 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 ) ;
861
+ max_commitment_tx_output. 0 = cmp:: max ( max_commitment_tx_output. 0 , value_to_self_msat as u64 ) ;
862
+ 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 ) ;
863
+ max_commitment_tx_output. 1 = cmp:: max ( max_commitment_tx_output. 1 , value_to_remote_msat as u64 ) ;
864
+ }
865
+
829
866
let total_fee: u64 = feerate_per_kw * ( COMMITMENT_TX_BASE_WEIGHT + ( txouts. len ( ) as u64 ) * COMMITMENT_TX_WEIGHT_PER_HTLC ) / 1000 ;
830
- 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 } ;
831
- 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 } ;
867
+ let ( value_to_self, value_to_remote) = if self . channel_outbound {
868
+ ( value_to_self_msat / 1000 - total_fee as i64 , value_to_remote_msat / 1000 )
869
+ } else {
870
+ ( value_to_self_msat / 1000 , value_to_remote_msat / 1000 - total_fee as i64 )
871
+ } ;
832
872
833
873
let value_to_a = if local { value_to_self } else { value_to_remote } ;
834
874
let value_to_b = if local { value_to_remote } else { value_to_self } ;
0 commit comments