Skip to content

Commit ed6a5bb

Browse files
committed
Move Channel::get_*_pending_htlc_stats to ChannelContext impl
1 parent 2774aa2 commit ed6a5bb

File tree

1 file changed

+91
-91
lines changed

1 file changed

+91
-91
lines changed

lightning/src/ln/channel.rs

Lines changed: 91 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -1368,6 +1368,88 @@ impl<Signer: ChannelSigner> ChannelContext<Signer> {
13681368
pub fn counterparty_forwarding_info(&self) -> Option<CounterpartyForwardingInfo> {
13691369
self.counterparty_forwarding_info.clone()
13701370
}
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+
}
13711453
}
13721454

13731455
// Internal utility functions for channels
@@ -2933,97 +3015,15 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
29333015
}
29343016
}
29353017

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-
30183018
/// Get the available balances, see [`AvailableBalances`]'s fields for more info.
30193019
/// Doesn't bother handling the
30203020
/// if-we-removed-it-already-but-haven't-fully-resolved-they-can-still-send-an-inbound-HTLC
30213021
/// corner case properly.
30223022
pub fn get_available_balances(&self) -> AvailableBalances {
30233023
let context = &self.context;
30243024
// 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);
30273027

30283028
let mut balance_msat = context.value_to_self_msat;
30293029
for ref htlc in context.pending_inbound_htlcs.iter() {
@@ -3142,7 +3142,7 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
31423142
AvailableBalances {
31433143
inbound_capacity_msat: cmp::max(context.channel_value_satoshis as i64 * 1000
31443144
- 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
31463146
- context.holder_selected_channel_reserve_satoshis as i64 * 1000,
31473147
0) as u64,
31483148
outbound_capacity_msat,
@@ -3384,8 +3384,8 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
33843384
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)));
33853385
}
33863386

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);
33893389
if inbound_stats.pending_htlcs + 1 > self.context.holder_max_accepted_htlcs as u32 {
33903390
return Err(ChannelError::Close(format!("Remote tried to push more than our max accepted HTLCs ({})", self.context.holder_max_accepted_htlcs)));
33913391
}
@@ -4183,8 +4183,8 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
41834183
}
41844184

41854185
// 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));
41884188
let keys = self.context.build_holder_transaction_keys(self.context.cur_holder_commitment_transaction_number);
41894189
let commitment_stats = self.context.build_commitment_transaction(self.context.cur_holder_commitment_transaction_number, &keys, true, true, logger);
41904190
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> {
44354435
// `get_dust_buffer_feerate` considers the `pending_update_fee` status), check that we
44364436
// won't be pushed over our dust exposure limit by the feerate increase.
44374437
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);
44404440
let holder_tx_dust_exposure = inbound_stats.on_holder_tx_dust_exposure_msat + outbound_stats.on_holder_tx_dust_exposure_msat;
44414441
let counterparty_tx_dust_exposure = inbound_stats.on_counterparty_tx_dust_exposure_msat + outbound_stats.on_counterparty_tx_dust_exposure_msat;
44424442
if holder_tx_dust_exposure > self.context.get_max_dust_htlc_exposure_msat() {

0 commit comments

Comments
 (0)