@@ -524,10 +524,11 @@ pub(super) struct Channel<Signer: Sign> {
524
524
pub ( super ) counterparty_max_htlc_value_in_flight_msat : u64 ,
525
525
#[ cfg( not( test) ) ]
526
526
counterparty_max_htlc_value_in_flight_msat : u64 ,
527
- //get_holder_max_htlc_value_in_flight_msat(): u64,
527
+ holder_max_htlc_value_in_flight_msat : u64 ,
528
+
528
529
/// minimum channel reserve for self to maintain - set by them.
529
530
counterparty_selected_channel_reserve_satoshis : Option < u64 > ,
530
- // get_holder_selected_channel_reserve_satoshis(channel_value_sats : u64): u64
531
+ holder_selected_channel_reserve_satoshis : u64 ,
531
532
counterparty_htlc_minimum_msat : u64 ,
532
533
holder_htlc_minimum_msat : u64 ,
533
534
#[ cfg( test) ]
@@ -679,6 +680,10 @@ impl<Signer: Sign> Channel<Signer> {
679
680
/// required by us.
680
681
///
681
682
/// Guaranteed to return a value no larger than channel_value_satoshis
683
+ ///
684
+ /// This is used both for new channels and to figure out what reserve value we sent to peers
685
+ /// for channels serialized before we included our selected reserve value in the serialized
686
+ /// data explicitly.
682
687
pub ( crate ) fn get_holder_selected_channel_reserve_satoshis ( channel_value_satoshis : u64 ) -> u64 {
683
688
let ( q, _) = channel_value_satoshis. overflowing_div ( 100 ) ;
684
689
cmp:: min ( channel_value_satoshis, cmp:: max ( q, 1000 ) ) //TODO
@@ -792,7 +797,9 @@ impl<Signer: Sign> Channel<Signer> {
792
797
counterparty_dust_limit_satoshis : 0 ,
793
798
holder_dust_limit_satoshis : MIN_CHAN_DUST_LIMIT_SATOSHIS ,
794
799
counterparty_max_htlc_value_in_flight_msat : 0 ,
800
+ holder_max_htlc_value_in_flight_msat : Self :: get_holder_max_htlc_value_in_flight_msat ( channel_value_satoshis) ,
795
801
counterparty_selected_channel_reserve_satoshis : None , // Filled in in accept_channel
802
+ holder_selected_channel_reserve_satoshis,
796
803
counterparty_htlc_minimum_msat : 0 ,
797
804
holder_htlc_minimum_msat : if config. own_channel_config . our_htlc_minimum_msat == 0 { 1 } else { config. own_channel_config . our_htlc_minimum_msat } ,
798
805
counterparty_max_accepted_htlcs : 0 ,
@@ -1083,7 +1090,9 @@ impl<Signer: Sign> Channel<Signer> {
1083
1090
counterparty_dust_limit_satoshis : msg. dust_limit_satoshis ,
1084
1091
holder_dust_limit_satoshis : MIN_CHAN_DUST_LIMIT_SATOSHIS ,
1085
1092
counterparty_max_htlc_value_in_flight_msat : cmp:: min ( msg. max_htlc_value_in_flight_msat , msg. funding_satoshis * 1000 ) ,
1093
+ holder_max_htlc_value_in_flight_msat : Self :: get_holder_max_htlc_value_in_flight_msat ( msg. funding_satoshis ) ,
1086
1094
counterparty_selected_channel_reserve_satoshis : Some ( msg. channel_reserve_satoshis ) ,
1095
+ holder_selected_channel_reserve_satoshis,
1087
1096
counterparty_htlc_minimum_msat : msg. htlc_minimum_msat ,
1088
1097
holder_htlc_minimum_msat : if config. own_channel_config . our_htlc_minimum_msat == 0 { 1 } else { config. own_channel_config . our_htlc_minimum_msat } ,
1089
1098
counterparty_max_accepted_htlcs : msg. max_accepted_htlcs ,
@@ -1288,7 +1297,7 @@ impl<Signer: Sign> Channel<Signer> {
1288
1297
} ;
1289
1298
debug_assert ! ( broadcaster_max_commitment_tx_output. 0 <= value_to_self_msat as u64 || value_to_self_msat / 1000 >= self . counterparty_selected_channel_reserve_satoshis. unwrap( ) as i64 ) ;
1290
1299
broadcaster_max_commitment_tx_output. 0 = cmp:: max ( broadcaster_max_commitment_tx_output. 0 , value_to_self_msat as u64 ) ;
1291
- debug_assert ! ( broadcaster_max_commitment_tx_output. 1 <= value_to_remote_msat as u64 || value_to_remote_msat / 1000 >= Channel :: < Signer > :: get_holder_selected_channel_reserve_satoshis ( self . channel_value_satoshis ) as i64 ) ;
1300
+ debug_assert ! ( broadcaster_max_commitment_tx_output. 1 <= value_to_remote_msat as u64 || value_to_remote_msat / 1000 >= self . holder_selected_channel_reserve_satoshis as i64 ) ;
1292
1301
broadcaster_max_commitment_tx_output. 1 = cmp:: max ( broadcaster_max_commitment_tx_output. 1 , value_to_remote_msat as u64 ) ;
1293
1302
}
1294
1303
@@ -1706,9 +1715,8 @@ impl<Signer: Sign> Channel<Signer> {
1706
1715
if msg. channel_reserve_satoshis < self . holder_dust_limit_satoshis {
1707
1716
return Err ( ChannelError :: Close ( format ! ( "Peer never wants payout outputs? channel_reserve_satoshis was ({}). dust_limit is ({})" , msg. channel_reserve_satoshis, self . holder_dust_limit_satoshis) ) ) ;
1708
1717
}
1709
- let remote_reserve = Channel :: < Signer > :: get_holder_selected_channel_reserve_satoshis ( self . channel_value_satoshis ) ;
1710
- if msg. dust_limit_satoshis > remote_reserve {
1711
- return Err ( ChannelError :: Close ( format ! ( "Dust limit ({}) is bigger than our channel reserve ({})" , msg. dust_limit_satoshis, remote_reserve) ) ) ;
1718
+ if msg. dust_limit_satoshis > self . holder_selected_channel_reserve_satoshis {
1719
+ return Err ( ChannelError :: Close ( format ! ( "Dust limit ({}) is bigger than our channel reserve ({})" , msg. dust_limit_satoshis, self . holder_selected_channel_reserve_satoshis) ) ) ;
1712
1720
}
1713
1721
let full_channel_value_msat = ( self . channel_value_satoshis - msg. channel_reserve_satoshis ) * 1000 ;
1714
1722
if msg. htlc_minimum_msat >= full_channel_value_msat {
@@ -2109,7 +2117,7 @@ impl<Signer: Sign> Channel<Signer> {
2109
2117
cmp:: max ( self . channel_value_satoshis as i64 * 1000
2110
2118
- self . value_to_self_msat as i64
2111
2119
- self . get_inbound_pending_htlc_stats ( None ) . pending_htlcs_value_msat as i64
2112
- - Self :: get_holder_selected_channel_reserve_satoshis ( self . channel_value_satoshis ) as i64 * 1000 ,
2120
+ - self . holder_selected_channel_reserve_satoshis as i64 * 1000 ,
2113
2121
0 ) as u64 ,
2114
2122
cmp:: max ( self . value_to_self_msat as i64
2115
2123
- self . get_outbound_pending_htlc_stats ( None ) . pending_htlcs_value_msat as i64
@@ -2119,8 +2127,7 @@ impl<Signer: Sign> Channel<Signer> {
2119
2127
}
2120
2128
2121
2129
pub fn get_holder_counterparty_selected_channel_reserve_satoshis ( & self ) -> ( u64 , Option < u64 > ) {
2122
- ( Self :: get_holder_selected_channel_reserve_satoshis ( self . channel_value_satoshis ) ,
2123
- self . counterparty_selected_channel_reserve_satoshis )
2130
+ ( self . holder_selected_channel_reserve_satoshis , self . counterparty_selected_channel_reserve_satoshis )
2124
2131
}
2125
2132
2126
2133
// Get the fee cost in MSATS of a commitment tx with a given number of HTLC outputs.
@@ -2336,9 +2343,8 @@ impl<Signer: Sign> Channel<Signer> {
2336
2343
if inbound_stats. pending_htlcs + 1 > OUR_MAX_HTLCS as u32 {
2337
2344
return Err ( ChannelError :: Close ( format ! ( "Remote tried to push more than our max accepted HTLCs ({})" , OUR_MAX_HTLCS ) ) ) ;
2338
2345
}
2339
- let holder_max_htlc_value_in_flight_msat = Channel :: < Signer > :: get_holder_max_htlc_value_in_flight_msat ( self . channel_value_satoshis ) ;
2340
- if inbound_stats. pending_htlcs_value_msat + msg. amount_msat > holder_max_htlc_value_in_flight_msat {
2341
- return Err ( ChannelError :: Close ( format ! ( "Remote HTLC add would put them over our max HTLC value ({})" , holder_max_htlc_value_in_flight_msat) ) ) ;
2346
+ if inbound_stats. pending_htlcs_value_msat + msg. amount_msat > self . holder_max_htlc_value_in_flight_msat {
2347
+ return Err ( ChannelError :: Close ( format ! ( "Remote HTLC add would put them over our max HTLC value ({})" , self . holder_max_htlc_value_in_flight_msat) ) ) ;
2342
2348
}
2343
2349
// Check holder_selected_channel_reserve_satoshis (we're getting paid, so they have to at least meet
2344
2350
// the reserve_satoshis we told them to always have as direct payment so that they lose
@@ -2399,9 +2405,7 @@ impl<Signer: Sign> Channel<Signer> {
2399
2405
return Err ( ChannelError :: Close ( "Remote HTLC add would not leave enough to pay for fees" . to_owned ( ) ) ) ;
2400
2406
} ;
2401
2407
2402
- let chan_reserve_msat =
2403
- Channel :: < Signer > :: get_holder_selected_channel_reserve_satoshis ( self . channel_value_satoshis ) * 1000 ;
2404
- if pending_remote_value_msat - msg. amount_msat - remote_commit_tx_fee_msat < chan_reserve_msat {
2408
+ if pending_remote_value_msat - msg. amount_msat - remote_commit_tx_fee_msat < self . holder_selected_channel_reserve_satoshis * 1000 {
2405
2409
return Err ( ChannelError :: Close ( "Remote HTLC add would put them under remote reserve value" . to_owned ( ) ) ) ;
2406
2410
}
2407
2411
@@ -2416,7 +2420,7 @@ impl<Signer: Sign> Channel<Signer> {
2416
2420
// sensitive to fee spikes.
2417
2421
let htlc_candidate = HTLCCandidate :: new ( msg. amount_msat , HTLCInitiator :: RemoteOffered ) ;
2418
2422
let remote_fee_cost_incl_stuck_buffer_msat = 2 * self . next_remote_commit_tx_fee_msat ( htlc_candidate, Some ( ( ) ) ) ;
2419
- if pending_remote_value_msat - msg. amount_msat - chan_reserve_msat < remote_fee_cost_incl_stuck_buffer_msat {
2423
+ if pending_remote_value_msat - msg. amount_msat - self . holder_selected_channel_reserve_satoshis * 1000 < remote_fee_cost_incl_stuck_buffer_msat {
2420
2424
// Note that if the pending_forward_status is not updated here, then it's because we're already failing
2421
2425
// the HTLC, i.e. its status is already set to failing.
2422
2426
log_info ! ( logger, "Attempting to fail HTLC due to fee spike buffer violation in channel {}. Rebalancing is required." , log_bytes!( self . channel_id( ) ) ) ;
@@ -2559,7 +2563,7 @@ impl<Signer: Sign> Channel<Signer> {
2559
2563
} else { false } ;
2560
2564
if update_fee {
2561
2565
debug_assert ! ( !self . is_outbound( ) ) ;
2562
- let counterparty_reserve_we_require_msat = Channel :: < Signer > :: get_holder_selected_channel_reserve_satoshis ( self . channel_value_satoshis ) * 1000 ;
2566
+ let counterparty_reserve_we_require_msat = self . holder_selected_channel_reserve_satoshis * 1000 ;
2563
2567
if commitment_stats. remote_balance_msat < commitment_stats. total_fee_sat * 1000 + counterparty_reserve_we_require_msat {
2564
2568
return Err ( ( None , ChannelError :: Close ( "Funding remote cannot afford proposed new fee" . to_owned ( ) ) ) ) ;
2565
2569
}
@@ -4000,7 +4004,7 @@ impl<Signer: Sign> Channel<Signer> {
4000
4004
// channel might have been used to route very small values (either by honest users or as DoS).
4001
4005
self . channel_value_satoshis * 1000 * 9 / 10 ,
4002
4006
4003
- Channel :: < Signer > :: get_holder_max_htlc_value_in_flight_msat ( self . channel_value_satoshis )
4007
+ self . holder_max_htlc_value_in_flight_msat
4004
4008
) ;
4005
4009
}
4006
4010
@@ -4400,8 +4404,8 @@ impl<Signer: Sign> Channel<Signer> {
4400
4404
funding_satoshis : self . channel_value_satoshis ,
4401
4405
push_msat : self . channel_value_satoshis * 1000 - self . value_to_self_msat ,
4402
4406
dust_limit_satoshis : self . holder_dust_limit_satoshis ,
4403
- max_htlc_value_in_flight_msat : Channel :: < Signer > :: get_holder_max_htlc_value_in_flight_msat ( self . channel_value_satoshis ) ,
4404
- channel_reserve_satoshis : Channel :: < Signer > :: get_holder_selected_channel_reserve_satoshis ( self . channel_value_satoshis ) ,
4407
+ max_htlc_value_in_flight_msat : self . holder_max_htlc_value_in_flight_msat ,
4408
+ channel_reserve_satoshis : self . holder_selected_channel_reserve_satoshis ,
4405
4409
htlc_minimum_msat : self . holder_htlc_minimum_msat ,
4406
4410
feerate_per_kw : self . feerate_per_kw as u32 ,
4407
4411
to_self_delay : self . get_holder_selected_contest_delay ( ) ,
@@ -4438,8 +4442,8 @@ impl<Signer: Sign> Channel<Signer> {
4438
4442
msgs:: AcceptChannel {
4439
4443
temporary_channel_id : self . channel_id ,
4440
4444
dust_limit_satoshis : self . holder_dust_limit_satoshis ,
4441
- max_htlc_value_in_flight_msat : Channel :: < Signer > :: get_holder_max_htlc_value_in_flight_msat ( self . channel_value_satoshis ) ,
4442
- channel_reserve_satoshis : Channel :: < Signer > :: get_holder_selected_channel_reserve_satoshis ( self . channel_value_satoshis ) ,
4445
+ max_htlc_value_in_flight_msat : self . holder_max_htlc_value_in_flight_msat ,
4446
+ channel_reserve_satoshis : self . holder_selected_channel_reserve_satoshis ,
4443
4447
htlc_minimum_msat : self . holder_htlc_minimum_msat ,
4444
4448
minimum_depth : self . minimum_depth . unwrap ( ) ,
4445
4449
to_self_delay : self . get_holder_selected_contest_delay ( ) ,
@@ -4722,7 +4726,7 @@ impl<Signer: Sign> Channel<Signer> {
4722
4726
// Check that we won't violate the remote channel reserve by adding this HTLC.
4723
4727
let htlc_candidate = HTLCCandidate :: new ( amount_msat, HTLCInitiator :: LocalOffered ) ;
4724
4728
let counterparty_commit_tx_fee_msat = self . next_remote_commit_tx_fee_msat ( htlc_candidate, None ) ;
4725
- let holder_selected_chan_reserve_msat = Channel :: < Signer > :: get_holder_selected_channel_reserve_satoshis ( self . channel_value_satoshis ) * 1000 ;
4729
+ let holder_selected_chan_reserve_msat = self . holder_selected_channel_reserve_satoshis * 1000 ;
4726
4730
if commitment_stats. remote_balance_msat < counterparty_commit_tx_fee_msat + holder_selected_chan_reserve_msat {
4727
4731
return Err ( ChannelError :: Ignore ( "Cannot send value that would put counterparty balance under holder-announced channel reserve value" . to_owned ( ) ) ) ;
4728
4732
}
@@ -5368,6 +5372,15 @@ impl<Signer: Sign> Writeable for Channel<Signer> {
5368
5372
let chan_type = if self . channel_type != ChannelTypeFeatures :: only_static_remote_key ( ) {
5369
5373
Some ( & self . channel_type ) } else { None } ;
5370
5374
5375
+ // The same logic applies for `holder_selected_channel_reserve_satoshis` and
5376
+ // `holder_max_htlc_value_in_flight_msat` values other than the defaults.
5377
+ let serialized_holder_selected_reserve =
5378
+ if self . holder_selected_channel_reserve_satoshis != Self :: get_holder_selected_channel_reserve_satoshis ( self . channel_value_satoshis )
5379
+ { Some ( self . holder_selected_channel_reserve_satoshis ) } else { None } ;
5380
+ let serialized_holder_htlc_max_in_flight =
5381
+ if self . holder_max_htlc_value_in_flight_msat != Self :: get_holder_max_htlc_value_in_flight_msat ( self . channel_value_satoshis )
5382
+ { Some ( self . holder_max_htlc_value_in_flight_msat ) } else { None } ;
5383
+
5371
5384
write_tlv_fields ! ( writer, {
5372
5385
( 0 , self . announcement_sigs, option) ,
5373
5386
// minimum_depth and counterparty_selected_channel_reserve_satoshis used to have a
@@ -5379,7 +5392,9 @@ impl<Signer: Sign> Writeable for Channel<Signer> {
5379
5392
( 1 , self . minimum_depth, option) ,
5380
5393
( 2 , chan_type, option) ,
5381
5394
( 3 , self . counterparty_selected_channel_reserve_satoshis, option) ,
5395
+ ( 4 , serialized_holder_selected_reserve, option) ,
5382
5396
( 5 , self . config, required) ,
5397
+ ( 6 , serialized_holder_htlc_max_in_flight, option) ,
5383
5398
( 7 , self . shutdown_scriptpubkey, option) ,
5384
5399
( 9 , self . target_closing_feerate_sats_per_kw, option) ,
5385
5400
( 11 , self . monitor_pending_finalized_fulfills, vec_type) ,
@@ -5619,6 +5634,8 @@ impl<'a, Signer: Sign, K: Deref> ReadableArgs<(&'a K, u32)> for Channel<Signer>
5619
5634
let mut announcement_sigs = None ;
5620
5635
let mut target_closing_feerate_sats_per_kw = None ;
5621
5636
let mut monitor_pending_finalized_fulfills = Some ( Vec :: new ( ) ) ;
5637
+ let mut holder_selected_channel_reserve_satoshis = Some ( Self :: get_holder_selected_channel_reserve_satoshis ( channel_value_satoshis) ) ;
5638
+ let mut holder_max_htlc_value_in_flight_msat = Some ( Self :: get_holder_max_htlc_value_in_flight_msat ( channel_value_satoshis) ) ;
5622
5639
// Prior to supporting channel type negotiation, all of our channels were static_remotekey
5623
5640
// only, so we default to that if none was written.
5624
5641
let mut channel_type = Some ( ChannelTypeFeatures :: only_static_remote_key ( ) ) ;
@@ -5628,7 +5645,9 @@ impl<'a, Signer: Sign, K: Deref> ReadableArgs<(&'a K, u32)> for Channel<Signer>
5628
5645
( 1 , minimum_depth, option) ,
5629
5646
( 2 , channel_type, option) ,
5630
5647
( 3 , counterparty_selected_channel_reserve_satoshis, option) ,
5648
+ ( 4 , holder_selected_channel_reserve_satoshis, option) ,
5631
5649
( 5 , config, option) , // Note that if none is provided we will *not* overwrite the existing one.
5650
+ ( 6 , holder_max_htlc_value_in_flight_msat, option) ,
5632
5651
( 7 , shutdown_scriptpubkey, option) ,
5633
5652
( 9 , target_closing_feerate_sats_per_kw, option) ,
5634
5653
( 11 , monitor_pending_finalized_fulfills, vec_type) ,
@@ -5707,7 +5726,9 @@ impl<'a, Signer: Sign, K: Deref> ReadableArgs<(&'a K, u32)> for Channel<Signer>
5707
5726
counterparty_dust_limit_satoshis,
5708
5727
holder_dust_limit_satoshis,
5709
5728
counterparty_max_htlc_value_in_flight_msat,
5729
+ holder_max_htlc_value_in_flight_msat : holder_max_htlc_value_in_flight_msat. unwrap ( ) ,
5710
5730
counterparty_selected_channel_reserve_satoshis,
5731
+ holder_selected_channel_reserve_satoshis : holder_selected_channel_reserve_satoshis. unwrap ( ) ,
5711
5732
counterparty_htlc_minimum_msat,
5712
5733
holder_htlc_minimum_msat,
5713
5734
counterparty_max_accepted_htlcs,
0 commit comments