Skip to content

Commit 6c8bf07

Browse files
committed
f - move logic to be after on-chain claim checks
1 parent bd6c3dd commit 6c8bf07

File tree

1 file changed

+57
-57
lines changed

1 file changed

+57
-57
lines changed

lightning/src/chain/channelmonitor.rs

+57-57
Original file line numberDiff line numberDiff line change
@@ -3522,63 +3522,6 @@ impl<Signer: WriteableEcdsaChannelSigner> ChannelMonitorImpl<Signer> {
35223522
}
35233523
}
35243524

3525-
// Fail back HTLCs on backwards channels if they expire within `LATENCY_GRACE_PERIOD_BLOCKS`
3526-
// blocks. If we haven't seen the preimage for an HTLC by the time the previous hop's
3527-
// timeout expires, we've lost that HTLC, so we might as well fail it back instead of having our
3528-
// counterparty force-close the channel.
3529-
let current_holder_htlcs = self.current_holder_commitment_tx.htlc_outputs.iter()
3530-
.map(|&(ref a, _, ref b)| (a, b.as_ref()));
3531-
3532-
let current_counterparty_htlcs = if let Some(txid) = self.current_counterparty_commitment_txid {
3533-
if let Some(htlc_outputs) = self.counterparty_claimable_outpoints.get(&txid) {
3534-
Some(htlc_outputs.iter().map(|&(ref a, ref b)| (a, b.as_ref().map(|boxed| &**boxed))))
3535-
} else { None }
3536-
} else { None }.into_iter().flatten();
3537-
3538-
let prev_counterparty_htlcs = if let Some(txid) = self.prev_counterparty_commitment_txid {
3539-
if let Some(htlc_outputs) = self.counterparty_claimable_outpoints.get(&txid) {
3540-
Some(htlc_outputs.iter().map(|&(ref a, ref b)| (a, b.as_ref().map(|boxed| &**boxed))))
3541-
} else { None }
3542-
} else { None }.into_iter().flatten();
3543-
3544-
let htlcs = current_holder_htlcs
3545-
.chain(current_counterparty_htlcs)
3546-
.chain(prev_counterparty_htlcs);
3547-
3548-
let height = self.best_block.height();
3549-
for (htlc, source_opt) in htlcs {
3550-
// Only check forwarded HTLCs' previous hops
3551-
let source = match source_opt {
3552-
Some(source) => source,
3553-
None => continue,
3554-
};
3555-
let (cltv_expiry, htlc_id) = match source {
3556-
HTLCSource::PreviousHopData(HTLCPreviousHopData {
3557-
htlc_id, cltv_expiry: Some(cltv_expiry), ..
3558-
}) if !self.failed_back_htlc_ids.contains(htlc_id) => (*cltv_expiry, *htlc_id),
3559-
_ => continue,
3560-
};
3561-
if cltv_expiry <= height + LATENCY_GRACE_PERIOD_BLOCKS {
3562-
let duplicate_event = self.pending_monitor_events.iter().any(
3563-
|update| if let &MonitorEvent::HTLCEvent(ref upd) = update {
3564-
upd.source == *source
3565-
} else { false });
3566-
if !duplicate_event {
3567-
log_debug!(logger, "Failing back HTLC {} upstream to preserve the \
3568-
channel as the forward HTLC hasn't resolved and our backward HTLC \
3569-
expires soon at {}", log_bytes!(htlc.payment_hash.0), cltv_expiry);
3570-
self.pending_monitor_events.push(MonitorEvent::HTLCEvent(HTLCUpdate {
3571-
source: source.clone(),
3572-
payment_preimage: None,
3573-
payment_hash: htlc.payment_hash,
3574-
htlc_value_satoshis: Some(htlc.amount_msat / 1000),
3575-
awaiting_downstream_confirmation: true,
3576-
}));
3577-
self.failed_back_htlc_ids.push(htlc_id);
3578-
}
3579-
}
3580-
}
3581-
35823525
// Find which on-chain events have reached their confirmation threshold.
35833526
let onchain_events_awaiting_threshold_conf =
35843527
self.onchain_events_awaiting_threshold_conf.drain(..).collect::<Vec<_>>();
@@ -3662,6 +3605,63 @@ impl<Signer: WriteableEcdsaChannelSigner> ChannelMonitorImpl<Signer> {
36623605
}
36633606
}
36643607

3608+
// Fail back HTLCs on backwards channels if they expire within `LATENCY_GRACE_PERIOD_BLOCKS`
3609+
// blocks. If we haven't seen the preimage for an HTLC by the time the previous hop's
3610+
// timeout expires, we've lost that HTLC, so we might as well fail it back instead of having our
3611+
// counterparty force-close the channel.
3612+
let current_holder_htlcs = self.current_holder_commitment_tx.htlc_outputs.iter()
3613+
.map(|&(ref a, _, ref b)| (a, b.as_ref()));
3614+
3615+
let current_counterparty_htlcs = if let Some(txid) = self.current_counterparty_commitment_txid {
3616+
if let Some(htlc_outputs) = self.counterparty_claimable_outpoints.get(&txid) {
3617+
Some(htlc_outputs.iter().map(|&(ref a, ref b)| (a, b.as_ref().map(|boxed| &**boxed))))
3618+
} else { None }
3619+
} else { None }.into_iter().flatten();
3620+
3621+
let prev_counterparty_htlcs = if let Some(txid) = self.prev_counterparty_commitment_txid {
3622+
if let Some(htlc_outputs) = self.counterparty_claimable_outpoints.get(&txid) {
3623+
Some(htlc_outputs.iter().map(|&(ref a, ref b)| (a, b.as_ref().map(|boxed| &**boxed))))
3624+
} else { None }
3625+
} else { None }.into_iter().flatten();
3626+
3627+
let htlcs = current_holder_htlcs
3628+
.chain(current_counterparty_htlcs)
3629+
.chain(prev_counterparty_htlcs);
3630+
3631+
let height = self.best_block.height();
3632+
for (htlc, source_opt) in htlcs {
3633+
// Only check forwarded HTLCs' previous hops
3634+
let source = match source_opt {
3635+
Some(source) => source,
3636+
None => continue,
3637+
};
3638+
let (cltv_expiry, htlc_id) = match source {
3639+
HTLCSource::PreviousHopData(HTLCPreviousHopData {
3640+
htlc_id, cltv_expiry: Some(cltv_expiry), ..
3641+
}) if !self.failed_back_htlc_ids.contains(htlc_id) => (*cltv_expiry, *htlc_id),
3642+
_ => continue,
3643+
};
3644+
if cltv_expiry <= height + LATENCY_GRACE_PERIOD_BLOCKS {
3645+
let duplicate_event = self.pending_monitor_events.iter().any(
3646+
|update| if let &MonitorEvent::HTLCEvent(ref upd) = update {
3647+
upd.source == *source
3648+
} else { false });
3649+
if !duplicate_event {
3650+
log_debug!(logger, "Failing back HTLC {} upstream to preserve the \
3651+
channel as the forward HTLC hasn't resolved and our backward HTLC \
3652+
expires soon at {}", log_bytes!(htlc.payment_hash.0), cltv_expiry);
3653+
self.pending_monitor_events.push(MonitorEvent::HTLCEvent(HTLCUpdate {
3654+
source: source.clone(),
3655+
payment_preimage: None,
3656+
payment_hash: htlc.payment_hash,
3657+
htlc_value_satoshis: Some(htlc.amount_msat / 1000),
3658+
awaiting_downstream_confirmation: true,
3659+
}));
3660+
self.failed_back_htlc_ids.push(htlc_id);
3661+
}
3662+
}
3663+
}
3664+
36653665
self.onchain_tx_handler.update_claims_view_from_requests(claimable_outpoints, conf_height, self.best_block.height(), broadcaster, fee_estimator, logger);
36663666
self.onchain_tx_handler.update_claims_view_from_matched_txn(&txn_matched, conf_height, conf_hash, self.best_block.height(), broadcaster, fee_estimator, logger);
36673667

0 commit comments

Comments
 (0)