Skip to content

Commit c3e4234

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 53935c1 commit c3e4234

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
@@ -4455,7 +4455,26 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
44554455
return ClaimFundsFromHop::MonitorUpdateFail(counterparty_node_id, res, None);
44564456
},
44574457
}
4458-
} else { return ClaimFundsFromHop::PrevHopForceClosed }
4458+
} else {
4459+
let preimage_update = ChannelMonitorUpdate {
4460+
update_id: CLOSED_CHANNEL_UPDATE_ID,
4461+
updates: vec![ChannelMonitorUpdateStep::PaymentPreimage {
4462+
payment_preimage,
4463+
}],
4464+
};
4465+
// We update the ChannelMonitor on the backward link, after
4466+
// receiving an `update_fulfill_htlc` from the forward link.
4467+
let update_res = self.chain_monitor.update_channel(prev_hop.outpoint, preimage_update);
4468+
if update_res != ChannelMonitorUpdateStatus::Completed {
4469+
// TODO: This needs to be handled somehow - if we receive a monitor update
4470+
// with a preimage we *must* somehow manage to propagate it to the upstream
4471+
// channel, or we must have an ability to receive the same event and try
4472+
// again on restart.
4473+
log_error!(self.logger, "Critical error: failed to update channel monitor with preimage {:?}: {:?}",
4474+
payment_preimage, update_res);
4475+
}
4476+
return ClaimFundsFromHop::PrevHopForceClosed
4477+
}
44594478
}
44604479

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

0 commit comments

Comments
 (0)