Skip to content

Commit aae02f4

Browse files
committed
Handle closed-chan HTLC claims in claim_funds_from_hop
Currently `claim_funds` does all HTLC claims in one `channel_state` lock, ensuring that we always make claims from channels which are open. It can thus avoid ever having to generate a `ChannelMonitorUpdate` containing a preimage for a closed channel, which we only do in `claim_funds_internal` (for forwarded payments). In the next commit we'll change the locking of `claim_funds_from_hop` so that `claim_funds` is no longer under a single lock but takes a lock for each claim. This allows us to be more flexible with locks going forward, and ultimately isn't a huge change - if our counterparty intends to force-close a channel, us choosing to ignore it by holding the `channel_state` lock for the duration of the claim isn't going to result in a commitment update, it will just result in the preimage already being in the `ChannelMonitor`.
1 parent e558164 commit aae02f4

File tree

1 file changed

+20
-19
lines changed

1 file changed

+20
-19
lines changed

lightning/src/ln/channelmanager.rs

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4452,7 +4452,26 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
44524452
return ClaimFundsFromHop::MonitorUpdateFail(counterparty_node_id, res, None);
44534453
},
44544454
}
4455-
} else { return ClaimFundsFromHop::PrevHopForceClosed }
4455+
} else {
4456+
let preimage_update = ChannelMonitorUpdate {
4457+
update_id: CLOSED_CHANNEL_UPDATE_ID,
4458+
updates: vec![ChannelMonitorUpdateStep::PaymentPreimage {
4459+
payment_preimage,
4460+
}],
4461+
};
4462+
// We update the ChannelMonitor on the backward link, after
4463+
// receiving an `update_fulfill_htlc` from the forward link.
4464+
let update_res = self.chain_monitor.update_channel(prev_hop.outpoint, preimage_update);
4465+
if update_res != ChannelMonitorUpdateStatus::Completed {
4466+
// TODO: This needs to be handled somehow - if we receive a monitor update
4467+
// with a preimage we *must* somehow manage to propagate it to the upstream
4468+
// channel, or we must have an ability to receive the same event and try
4469+
// again on restart.
4470+
log_error!(self.logger, "Critical error: failed to update channel monitor with preimage {:?}: {:?}",
4471+
payment_preimage, update_res);
4472+
}
4473+
return ClaimFundsFromHop::PrevHopForceClosed
4474+
}
44564475
}
44574476

44584477
fn finalize_claims(&self, mut sources: Vec<HTLCSource>) {
@@ -4533,24 +4552,6 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
45334552
_ => None,
45344553
};
45354554
if let ClaimFundsFromHop::PrevHopForceClosed = res {
4536-
let preimage_update = ChannelMonitorUpdate {
4537-
update_id: CLOSED_CHANNEL_UPDATE_ID,
4538-
updates: vec![ChannelMonitorUpdateStep::PaymentPreimage {
4539-
payment_preimage: payment_preimage.clone(),
4540-
}],
4541-
};
4542-
// We update the ChannelMonitor on the backward link, after
4543-
// receiving an offchain preimage event from the forward link (the
4544-
// event being update_fulfill_htlc).
4545-
let update_res = self.chain_monitor.update_channel(prev_outpoint, preimage_update);
4546-
if update_res != ChannelMonitorUpdateStatus::Completed {
4547-
// TODO: This needs to be handled somehow - if we receive a monitor update
4548-
// with a preimage we *must* somehow manage to propagate it to the upstream
4549-
// channel, or we must have an ability to receive the same event and try
4550-
// again on restart.
4551-
log_error!(self.logger, "Critical error: failed to update channel monitor with preimage {:?}: {:?}",
4552-
payment_preimage, update_res);
4553-
}
45544555
// Note that we do *not* set `claimed_htlc` to false here. In fact, this
45554556
// totally could be a duplicate claim, but we have no way of knowing
45564557
// without interrogating the `ChannelMonitor` we've provided the above

0 commit comments

Comments
 (0)