Skip to content

Commit 9e6bf86

Browse files
committed
f - refactor to remove macro, chain htlcs instead
1 parent 34c48d9 commit 9e6bf86

File tree

1 file changed

+47
-46
lines changed

1 file changed

+47
-46
lines changed

lightning/src/chain/channelmonitor.rs

+47-46
Original file line numberDiff line numberDiff line change
@@ -3526,57 +3526,58 @@ impl<Signer: WriteableEcdsaChannelSigner> ChannelMonitorImpl<Signer> {
35263526
// blocks. If we haven't seen the preimage for an HTLC by the time the previous hop's
35273527
// timeout expires, we've lost that HTLC, so we might as well fail it back instead of having our
35283528
// counterparty force-close the channel.
3529-
let height = self.best_block.height();
3530-
macro_rules! fail_soon_to_expire_htlcs {
3531-
($htlcs: expr) => {{
3532-
for (htlc, source_opt) in $htlcs {
3533-
// Only check forwarded HTLCs' previous hops
3534-
let source = match source_opt {
3535-
Some(source) => source,
3536-
None => continue,
3537-
};
3538-
let (cltv_expiry, htlc_id) = match source {
3539-
HTLCSource::PreviousHopData(HTLCPreviousHopData { htlc_id, cltv_expiry: Some(cltv_expiry), .. }) if !self.failed_back_htlc_ids.contains(htlc_id) => (*cltv_expiry, *htlc_id),
3540-
_ => continue,
3541-
};
3542-
if cltv_expiry <= height + LATENCY_GRACE_PERIOD_BLOCKS {
3543-
let duplicate_event = self.pending_monitor_events.iter().any(
3544-
|update| if let &MonitorEvent::HTLCEvent(ref upd) = update {
3545-
upd.source == *source
3546-
} else { false });
3547-
if !duplicate_event {
3548-
log_debug!(logger, "Failing back HTLC {} upstream to preserve the \
3549-
channel as the forward HTLC hasn't resolved and our backward HTLC \
3550-
expires soon at {}", log_bytes!(htlc.payment_hash.0), cltv_expiry);
3551-
self.pending_monitor_events.push(MonitorEvent::HTLCEvent(HTLCUpdate {
3552-
source: source.clone(),
3553-
payment_preimage: None,
3554-
payment_hash: htlc.payment_hash,
3555-
htlc_value_satoshis: Some(htlc.amount_msat / 1000),
3556-
awaiting_downstream_confirmation: true,
3557-
}));
3558-
self.failed_back_htlc_ids.insert(htlc_id);
3559-
}
3560-
}
3561-
}
3562-
}}
3563-
}
3564-
35653529
let current_holder_htlcs = self.current_holder_commitment_tx.htlc_outputs.iter()
35663530
.map(|&(ref a, _, ref b)| (a, b.as_ref()));
3567-
fail_soon_to_expire_htlcs!(current_holder_htlcs);
35683531

3569-
if let Some(ref txid) = self.current_counterparty_commitment_txid {
3570-
if let Some(ref htlc_outputs) = self.counterparty_claimable_outpoints.get(txid) {
3571-
fail_soon_to_expire_htlcs!(htlc_outputs.iter().map(|&(ref a, ref b)| (a, (b.as_ref().clone()).map(|boxed| &**boxed))));
3572-
}
3573-
};
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();
35743537

3575-
if let Some(ref txid) = self.prev_counterparty_commitment_txid {
3576-
if let Some(ref htlc_outputs) = self.counterparty_claimable_outpoints.get(txid) {
3577-
fail_soon_to_expire_htlcs!(htlc_outputs.iter().map(|&(ref a, ref b)| (a, (b.as_ref().clone()).map(|boxed| &**boxed))));
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.insert(htlc_id);
3578+
}
35783579
}
3579-
};
3580+
}
35803581

35813582
// Find which on-chain events have reached their confirmation threshold.
35823583
let onchain_events_awaiting_threshold_conf =

0 commit comments

Comments
 (0)