@@ -1368,6 +1368,88 @@ impl<Signer: ChannelSigner> ChannelContext<Signer> {
1368
1368
pub fn counterparty_forwarding_info ( & self ) -> Option < CounterpartyForwardingInfo > {
1369
1369
self . counterparty_forwarding_info . clone ( )
1370
1370
}
1371
+
1372
+ /// Returns a HTLCStats about inbound pending htlcs
1373
+ fn get_inbound_pending_htlc_stats ( & self , outbound_feerate_update : Option < u32 > ) -> HTLCStats {
1374
+ let context = self ;
1375
+ let mut stats = HTLCStats {
1376
+ pending_htlcs : context. pending_inbound_htlcs . len ( ) as u32 ,
1377
+ pending_htlcs_value_msat : 0 ,
1378
+ on_counterparty_tx_dust_exposure_msat : 0 ,
1379
+ on_holder_tx_dust_exposure_msat : 0 ,
1380
+ holding_cell_msat : 0 ,
1381
+ on_holder_tx_holding_cell_htlcs_count : 0 ,
1382
+ } ;
1383
+
1384
+ let ( htlc_timeout_dust_limit, htlc_success_dust_limit) = if context. opt_anchors ( ) {
1385
+ ( 0 , 0 )
1386
+ } else {
1387
+ let dust_buffer_feerate = context. get_dust_buffer_feerate ( outbound_feerate_update) as u64 ;
1388
+ ( dust_buffer_feerate * htlc_timeout_tx_weight ( false ) / 1000 ,
1389
+ dust_buffer_feerate * htlc_success_tx_weight ( false ) / 1000 )
1390
+ } ;
1391
+ let counterparty_dust_limit_timeout_sat = htlc_timeout_dust_limit + context. counterparty_dust_limit_satoshis ;
1392
+ let holder_dust_limit_success_sat = htlc_success_dust_limit + context. holder_dust_limit_satoshis ;
1393
+ for ref htlc in context. pending_inbound_htlcs . iter ( ) {
1394
+ stats. pending_htlcs_value_msat += htlc. amount_msat ;
1395
+ if htlc. amount_msat / 1000 < counterparty_dust_limit_timeout_sat {
1396
+ stats. on_counterparty_tx_dust_exposure_msat += htlc. amount_msat ;
1397
+ }
1398
+ if htlc. amount_msat / 1000 < holder_dust_limit_success_sat {
1399
+ stats. on_holder_tx_dust_exposure_msat += htlc. amount_msat ;
1400
+ }
1401
+ }
1402
+ stats
1403
+ }
1404
+
1405
+ /// Returns a HTLCStats about pending outbound htlcs, *including* pending adds in our holding cell.
1406
+ fn get_outbound_pending_htlc_stats ( & self , outbound_feerate_update : Option < u32 > ) -> HTLCStats {
1407
+ let context = self ;
1408
+ let mut stats = HTLCStats {
1409
+ pending_htlcs : context. pending_outbound_htlcs . len ( ) as u32 ,
1410
+ pending_htlcs_value_msat : 0 ,
1411
+ on_counterparty_tx_dust_exposure_msat : 0 ,
1412
+ on_holder_tx_dust_exposure_msat : 0 ,
1413
+ holding_cell_msat : 0 ,
1414
+ on_holder_tx_holding_cell_htlcs_count : 0 ,
1415
+ } ;
1416
+
1417
+ let ( htlc_timeout_dust_limit, htlc_success_dust_limit) = if context. opt_anchors ( ) {
1418
+ ( 0 , 0 )
1419
+ } else {
1420
+ let dust_buffer_feerate = context. get_dust_buffer_feerate ( outbound_feerate_update) as u64 ;
1421
+ ( dust_buffer_feerate * htlc_timeout_tx_weight ( false ) / 1000 ,
1422
+ dust_buffer_feerate * htlc_success_tx_weight ( false ) / 1000 )
1423
+ } ;
1424
+ let counterparty_dust_limit_success_sat = htlc_success_dust_limit + context. counterparty_dust_limit_satoshis ;
1425
+ let holder_dust_limit_timeout_sat = htlc_timeout_dust_limit + context. holder_dust_limit_satoshis ;
1426
+ for ref htlc in context. pending_outbound_htlcs . iter ( ) {
1427
+ stats. pending_htlcs_value_msat += htlc. amount_msat ;
1428
+ if htlc. amount_msat / 1000 < counterparty_dust_limit_success_sat {
1429
+ stats. on_counterparty_tx_dust_exposure_msat += htlc. amount_msat ;
1430
+ }
1431
+ if htlc. amount_msat / 1000 < holder_dust_limit_timeout_sat {
1432
+ stats. on_holder_tx_dust_exposure_msat += htlc. amount_msat ;
1433
+ }
1434
+ }
1435
+
1436
+ for update in context. holding_cell_htlc_updates . iter ( ) {
1437
+ if let & HTLCUpdateAwaitingACK :: AddHTLC { ref amount_msat, .. } = update {
1438
+ stats. pending_htlcs += 1 ;
1439
+ stats. pending_htlcs_value_msat += amount_msat;
1440
+ stats. holding_cell_msat += amount_msat;
1441
+ if * amount_msat / 1000 < counterparty_dust_limit_success_sat {
1442
+ stats. on_counterparty_tx_dust_exposure_msat += amount_msat;
1443
+ }
1444
+ if * amount_msat / 1000 < holder_dust_limit_timeout_sat {
1445
+ stats. on_holder_tx_dust_exposure_msat += amount_msat;
1446
+ } else {
1447
+ stats. on_holder_tx_holding_cell_htlcs_count += 1 ;
1448
+ }
1449
+ }
1450
+ }
1451
+ stats
1452
+ }
1371
1453
}
1372
1454
1373
1455
// Internal utility functions for channels
@@ -2933,97 +3015,15 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
2933
3015
}
2934
3016
}
2935
3017
2936
- /// Returns a HTLCStats about inbound pending htlcs
2937
- fn get_inbound_pending_htlc_stats ( & self , outbound_feerate_update : Option < u32 > ) -> HTLCStats {
2938
- let context = & self . context ;
2939
- let mut stats = HTLCStats {
2940
- pending_htlcs : context. pending_inbound_htlcs . len ( ) as u32 ,
2941
- pending_htlcs_value_msat : 0 ,
2942
- on_counterparty_tx_dust_exposure_msat : 0 ,
2943
- on_holder_tx_dust_exposure_msat : 0 ,
2944
- holding_cell_msat : 0 ,
2945
- on_holder_tx_holding_cell_htlcs_count : 0 ,
2946
- } ;
2947
-
2948
- let ( htlc_timeout_dust_limit, htlc_success_dust_limit) = if context. opt_anchors ( ) {
2949
- ( 0 , 0 )
2950
- } else {
2951
- let dust_buffer_feerate = context. get_dust_buffer_feerate ( outbound_feerate_update) as u64 ;
2952
- ( dust_buffer_feerate * htlc_timeout_tx_weight ( false ) / 1000 ,
2953
- dust_buffer_feerate * htlc_success_tx_weight ( false ) / 1000 )
2954
- } ;
2955
- let counterparty_dust_limit_timeout_sat = htlc_timeout_dust_limit + context. counterparty_dust_limit_satoshis ;
2956
- let holder_dust_limit_success_sat = htlc_success_dust_limit + context. holder_dust_limit_satoshis ;
2957
- for ref htlc in context. pending_inbound_htlcs . iter ( ) {
2958
- stats. pending_htlcs_value_msat += htlc. amount_msat ;
2959
- if htlc. amount_msat / 1000 < counterparty_dust_limit_timeout_sat {
2960
- stats. on_counterparty_tx_dust_exposure_msat += htlc. amount_msat ;
2961
- }
2962
- if htlc. amount_msat / 1000 < holder_dust_limit_success_sat {
2963
- stats. on_holder_tx_dust_exposure_msat += htlc. amount_msat ;
2964
- }
2965
- }
2966
- stats
2967
- }
2968
-
2969
- /// Returns a HTLCStats about pending outbound htlcs, *including* pending adds in our holding cell.
2970
- fn get_outbound_pending_htlc_stats ( & self , outbound_feerate_update : Option < u32 > ) -> HTLCStats {
2971
- let context = & self . context ;
2972
- let mut stats = HTLCStats {
2973
- pending_htlcs : context. pending_outbound_htlcs . len ( ) as u32 ,
2974
- pending_htlcs_value_msat : 0 ,
2975
- on_counterparty_tx_dust_exposure_msat : 0 ,
2976
- on_holder_tx_dust_exposure_msat : 0 ,
2977
- holding_cell_msat : 0 ,
2978
- on_holder_tx_holding_cell_htlcs_count : 0 ,
2979
- } ;
2980
-
2981
- let ( htlc_timeout_dust_limit, htlc_success_dust_limit) = if context. opt_anchors ( ) {
2982
- ( 0 , 0 )
2983
- } else {
2984
- let dust_buffer_feerate = context. get_dust_buffer_feerate ( outbound_feerate_update) as u64 ;
2985
- ( dust_buffer_feerate * htlc_timeout_tx_weight ( false ) / 1000 ,
2986
- dust_buffer_feerate * htlc_success_tx_weight ( false ) / 1000 )
2987
- } ;
2988
- let counterparty_dust_limit_success_sat = htlc_success_dust_limit + context. counterparty_dust_limit_satoshis ;
2989
- let holder_dust_limit_timeout_sat = htlc_timeout_dust_limit + context. holder_dust_limit_satoshis ;
2990
- for ref htlc in context. pending_outbound_htlcs . iter ( ) {
2991
- stats. pending_htlcs_value_msat += htlc. amount_msat ;
2992
- if htlc. amount_msat / 1000 < counterparty_dust_limit_success_sat {
2993
- stats. on_counterparty_tx_dust_exposure_msat += htlc. amount_msat ;
2994
- }
2995
- if htlc. amount_msat / 1000 < holder_dust_limit_timeout_sat {
2996
- stats. on_holder_tx_dust_exposure_msat += htlc. amount_msat ;
2997
- }
2998
- }
2999
-
3000
- for update in context. holding_cell_htlc_updates . iter ( ) {
3001
- if let & HTLCUpdateAwaitingACK :: AddHTLC { ref amount_msat, .. } = update {
3002
- stats. pending_htlcs += 1 ;
3003
- stats. pending_htlcs_value_msat += amount_msat;
3004
- stats. holding_cell_msat += amount_msat;
3005
- if * amount_msat / 1000 < counterparty_dust_limit_success_sat {
3006
- stats. on_counterparty_tx_dust_exposure_msat += amount_msat;
3007
- }
3008
- if * amount_msat / 1000 < holder_dust_limit_timeout_sat {
3009
- stats. on_holder_tx_dust_exposure_msat += amount_msat;
3010
- } else {
3011
- stats. on_holder_tx_holding_cell_htlcs_count += 1 ;
3012
- }
3013
- }
3014
- }
3015
- stats
3016
- }
3017
-
3018
3018
/// Get the available balances, see [`AvailableBalances`]'s fields for more info.
3019
3019
/// Doesn't bother handling the
3020
3020
/// if-we-removed-it-already-but-haven't-fully-resolved-they-can-still-send-an-inbound-HTLC
3021
3021
/// corner case properly.
3022
3022
pub fn get_available_balances ( & self ) -> AvailableBalances {
3023
3023
let context = & self . context ;
3024
3024
// Note that we have to handle overflow due to the above case.
3025
- let inbound_stats = self . get_inbound_pending_htlc_stats ( None ) ;
3026
- let outbound_stats = self . get_outbound_pending_htlc_stats ( None ) ;
3025
+ let inbound_stats = context . get_inbound_pending_htlc_stats ( None ) ;
3026
+ let outbound_stats = context . get_outbound_pending_htlc_stats ( None ) ;
3027
3027
3028
3028
let mut balance_msat = context. value_to_self_msat ;
3029
3029
for ref htlc in context. pending_inbound_htlcs . iter ( ) {
@@ -3142,7 +3142,7 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
3142
3142
AvailableBalances {
3143
3143
inbound_capacity_msat : cmp:: max ( context. channel_value_satoshis as i64 * 1000
3144
3144
- context. value_to_self_msat as i64
3145
- - self . get_inbound_pending_htlc_stats ( None ) . pending_htlcs_value_msat as i64
3145
+ - context . get_inbound_pending_htlc_stats ( None ) . pending_htlcs_value_msat as i64
3146
3146
- context. holder_selected_channel_reserve_satoshis as i64 * 1000 ,
3147
3147
0 ) as u64 ,
3148
3148
outbound_capacity_msat,
@@ -3384,8 +3384,8 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
3384
3384
return Err ( ChannelError :: Close ( format ! ( "Remote side tried to send less than our minimum HTLC value. Lower limit: ({}). Actual: ({})" , self . context. holder_htlc_minimum_msat, msg. amount_msat) ) ) ;
3385
3385
}
3386
3386
3387
- let inbound_stats = self . get_inbound_pending_htlc_stats ( None ) ;
3388
- let outbound_stats = self . get_outbound_pending_htlc_stats ( None ) ;
3387
+ let inbound_stats = self . context . get_inbound_pending_htlc_stats ( None ) ;
3388
+ let outbound_stats = self . context . get_outbound_pending_htlc_stats ( None ) ;
3389
3389
if inbound_stats. pending_htlcs + 1 > self . context . holder_max_accepted_htlcs as u32 {
3390
3390
return Err ( ChannelError :: Close ( format ! ( "Remote tried to push more than our max accepted HTLCs ({})" , self . context. holder_max_accepted_htlcs) ) ) ;
3391
3391
}
@@ -4183,8 +4183,8 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
4183
4183
}
4184
4184
4185
4185
// Before proposing a feerate update, check that we can actually afford the new fee.
4186
- let inbound_stats = self . get_inbound_pending_htlc_stats ( Some ( feerate_per_kw) ) ;
4187
- let outbound_stats = self . get_outbound_pending_htlc_stats ( Some ( feerate_per_kw) ) ;
4186
+ let inbound_stats = self . context . get_inbound_pending_htlc_stats ( Some ( feerate_per_kw) ) ;
4187
+ let outbound_stats = self . context . get_outbound_pending_htlc_stats ( Some ( feerate_per_kw) ) ;
4188
4188
let keys = self . context . build_holder_transaction_keys ( self . context . cur_holder_commitment_transaction_number ) ;
4189
4189
let commitment_stats = self . context . build_commitment_transaction ( self . context . cur_holder_commitment_transaction_number , & keys, true , true , logger) ;
4190
4190
let buffer_fee_msat = commit_tx_fee_sat ( feerate_per_kw, commitment_stats. num_nondust_htlcs + outbound_stats. on_holder_tx_holding_cell_htlcs_count as usize + CONCURRENT_INBOUND_HTLC_FEE_BUFFER as usize , self . context . opt_anchors ( ) ) * 1000 ;
@@ -4435,8 +4435,8 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
4435
4435
// `get_dust_buffer_feerate` considers the `pending_update_fee` status), check that we
4436
4436
// won't be pushed over our dust exposure limit by the feerate increase.
4437
4437
if feerate_over_dust_buffer {
4438
- let inbound_stats = self . get_inbound_pending_htlc_stats ( None ) ;
4439
- let outbound_stats = self . get_outbound_pending_htlc_stats ( None ) ;
4438
+ let inbound_stats = self . context . get_inbound_pending_htlc_stats ( None ) ;
4439
+ let outbound_stats = self . context . get_outbound_pending_htlc_stats ( None ) ;
4440
4440
let holder_tx_dust_exposure = inbound_stats. on_holder_tx_dust_exposure_msat + outbound_stats. on_holder_tx_dust_exposure_msat ;
4441
4441
let counterparty_tx_dust_exposure = inbound_stats. on_counterparty_tx_dust_exposure_msat + outbound_stats. on_counterparty_tx_dust_exposure_msat ;
4442
4442
if holder_tx_dust_exposure > self . context . get_max_dust_htlc_exposure_msat ( ) {
0 commit comments