Skip to content

Commit 36dc8a3

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 c6f6815 commit 36dc8a3

File tree

1 file changed

+23
-25
lines changed

1 file changed

+23
-25
lines changed

lightning/src/ln/channelmanager.rs

Lines changed: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4450,6 +4450,29 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
44504450
},
44514451
}
44524452
} else {
4453+
let preimage_update = ChannelMonitorUpdate {
4454+
update_id: CLOSED_CHANNEL_UPDATE_ID,
4455+
updates: vec![ChannelMonitorUpdateStep::PaymentPreimage {
4456+
payment_preimage,
4457+
}],
4458+
};
4459+
// We update the ChannelMonitor on the backward link, after
4460+
// receiving an offchain preimage event from the forward link (the
4461+
// event being update_fulfill_htlc).
4462+
let update_res = self.chain_monitor.update_channel(prev_hop.outpoint, preimage_update);
4463+
if update_res != ChannelMonitorUpdateStatus::Completed {
4464+
// TODO: This needs to be handled somehow - if we receive a monitor update
4465+
// with a preimage we *must* somehow manage to propagate it to the upstream
4466+
// channel, or we must have an ability to receive the same event and try
4467+
// again on restart.
4468+
log_error!(self.logger, "Critical error: failed to update channel monitor with preimage {:?}: {:?}",
4469+
payment_preimage, update_res);
4470+
}
4471+
// Note that we do process the completion action here. This totally could be a
4472+
// duplicate claim, but we have no way of knowing without interrogating the
4473+
// `ChannelMonitor` we've provided the above update to. Instead, note that `Event`s are
4474+
// generally always allowed to be duplicative (and it's specifically noted in
4475+
// `PaymentForwarded`)..
44534476
self.handle_monitor_update_completion_actions(completion_action(None));
44544477
return ClaimFundsFromHop::PrevHopForceClosed
44554478
}
@@ -4543,31 +4566,6 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
45434566
}})
45444567
} else { None }
45454568
});
4546-
if let ClaimFundsFromHop::PrevHopForceClosed = res {
4547-
let preimage_update = ChannelMonitorUpdate {
4548-
update_id: CLOSED_CHANNEL_UPDATE_ID,
4549-
updates: vec![ChannelMonitorUpdateStep::PaymentPreimage {
4550-
payment_preimage: payment_preimage.clone(),
4551-
}],
4552-
};
4553-
// We update the ChannelMonitor on the backward link, after
4554-
// receiving an offchain preimage event from the forward link (the
4555-
// event being update_fulfill_htlc).
4556-
let update_res = self.chain_monitor.update_channel(prev_outpoint, preimage_update);
4557-
if update_res != ChannelMonitorUpdateStatus::Completed {
4558-
// TODO: This needs to be handled somehow - if we receive a monitor update
4559-
// with a preimage we *must* somehow manage to propagate it to the upstream
4560-
// channel, or we must have an ability to receive the same event and try
4561-
// again on restart.
4562-
log_error!(self.logger, "Critical error: failed to update channel monitor with preimage {:?}: {:?}",
4563-
payment_preimage, update_res);
4564-
}
4565-
// Note that we do *not* set `claimed_htlc` to false here. In fact, this
4566-
// totally could be a duplicate claim, but we have no way of knowing
4567-
// without interrogating the `ChannelMonitor` we've provided the above
4568-
// update to. Instead, we simply document in `PaymentForwarded` that this
4569-
// can happen.
4570-
}
45714569
mem::drop(channel_state_lock);
45724570
if let ClaimFundsFromHop::MonitorUpdateFail(pk, err, _) = res {
45734571
let result: Result<(), _> = Err(err);

0 commit comments

Comments
 (0)