Skip to content

Commit 6d82647

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 99ff94f commit 6d82647

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
@@ -4443,6 +4443,29 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
44434443
},
44444444
}
44454445
} else {
4446+
let preimage_update = ChannelMonitorUpdate {
4447+
update_id: CLOSED_CHANNEL_UPDATE_ID,
4448+
updates: vec![ChannelMonitorUpdateStep::PaymentPreimage {
4449+
payment_preimage,
4450+
}],
4451+
};
4452+
// We update the ChannelMonitor on the backward link, after
4453+
// receiving an offchain preimage event from the forward link (the
4454+
// event being update_fulfill_htlc).
4455+
let update_res = self.chain_monitor.update_channel(prev_hop.outpoint, preimage_update);
4456+
if update_res != ChannelMonitorUpdateStatus::Completed {
4457+
// TODO: This needs to be handled somehow - if we receive a monitor update
4458+
// with a preimage we *must* somehow manage to propagate it to the upstream
4459+
// channel, or we must have an ability to receive the same event and try
4460+
// again on restart.
4461+
log_error!(self.logger, "Critical error: failed to update channel monitor with preimage {:?}: {:?}",
4462+
payment_preimage, update_res);
4463+
}
4464+
// Note that we do process the completion action here. This totally could be a
4465+
// duplicate claim, but we have no way of knowing without interrogating the
4466+
// `ChannelMonitor` we've provided the above update to. Instead, note that `Event`s are
4467+
// generally always allowed to be duplicative (and it's specifically noted in
4468+
// `PaymentForwarded`)..
44464469
self.handle_monitor_update_completion_actions(completion_action(None));
44474470
return ClaimFundsFromHop::PrevHopForceClosed
44484471
}
@@ -4536,31 +4559,6 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
45364559
}})
45374560
} else { None }
45384561
});
4539-
if let ClaimFundsFromHop::PrevHopForceClosed = res {
4540-
let preimage_update = ChannelMonitorUpdate {
4541-
update_id: CLOSED_CHANNEL_UPDATE_ID,
4542-
updates: vec![ChannelMonitorUpdateStep::PaymentPreimage {
4543-
payment_preimage: payment_preimage.clone(),
4544-
}],
4545-
};
4546-
// We update the ChannelMonitor on the backward link, after
4547-
// receiving an offchain preimage event from the forward link (the
4548-
// event being update_fulfill_htlc).
4549-
let update_res = self.chain_monitor.update_channel(prev_outpoint, preimage_update);
4550-
if update_res != ChannelMonitorUpdateStatus::Completed {
4551-
// TODO: This needs to be handled somehow - if we receive a monitor update
4552-
// with a preimage we *must* somehow manage to propagate it to the upstream
4553-
// channel, or we must have an ability to receive the same event and try
4554-
// again on restart.
4555-
log_error!(self.logger, "Critical error: failed to update channel monitor with preimage {:?}: {:?}",
4556-
payment_preimage, update_res);
4557-
}
4558-
// Note that we do *not* set `claimed_htlc` to false here. In fact, this
4559-
// totally could be a duplicate claim, but we have no way of knowing
4560-
// without interrogating the `ChannelMonitor` we've provided the above
4561-
// update to. Instead, we simply document in `PaymentForwarded` that this
4562-
// can happen.
4563-
}
45644562
mem::drop(channel_state_lock);
45654563
if let ClaimFundsFromHop::MonitorUpdateFail(pk, err, _) = res {
45664564
let result: Result<(), _> = Err(err);

0 commit comments

Comments
 (0)