Skip to content

Commit 536751d

Browse files
committed
Handle claim result event generation in claim_funds_from_hop
Currently `claim_funds` and `claim_funds_internal` call `claim_funds_from_hop` and then surface and `Event` to the user informing them of the forwarded/claimed payment based on it's result. In both places we assume that a claim "completed" even if a monitor update is being done async. Instead, here we push that event generation through a `MonitorUpdateCompletionAction` and a call to `handle_monitor_update_completion_action`. This will allow us to hold the event(s) until async monitor updates complete in the future.
1 parent 9b76869 commit 536751d

File tree

1 file changed

+33
-40
lines changed

1 file changed

+33
-40
lines changed

lightning/src/ln/channelmanager.rs

Lines changed: 33 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -4147,7 +4147,6 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
41474147
let mut expected_amt_msat = None;
41484148
let mut valid_mpp = true;
41494149
let mut errs = Vec::new();
4150-
let mut claimed_any_htlcs = false;
41514150
let mut channel_state_lock = self.channel_state.lock().unwrap();
41524151
let channel_state = &mut *channel_state_lock;
41534152
for htlc in sources.iter() {
@@ -4197,13 +4196,14 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
41974196
}
41984197
if valid_mpp {
41994198
for htlc in sources.drain(..) {
4200-
match self.claim_funds_from_hop(&mut channel_state_lock, htlc.prev_hop, payment_preimage) {
4199+
match self.claim_funds_from_hop(&mut channel_state_lock, htlc.prev_hop, payment_preimage,
4200+
|_| Some(MonitorUpdateCompletionAction::PaymentClaimed { payment_hash }))
4201+
{
42014202
ClaimFundsFromHop::MonitorUpdateFail(pk, err, _) => {
42024203
if let msgs::ErrorAction::IgnoreError = err.err.action {
42034204
// We got a temporary failure updating monitor, but will claim the
42044205
// HTLC when the monitor updating is restored (or on chain).
42054206
log_error!(self.logger, "Temporary failure claiming HTLC, treating as success: {}", err.err.err);
4206-
claimed_any_htlcs = true;
42074207
} else { errs.push((pk, err)); }
42084208
},
42094209
ClaimFundsFromHop::PrevHopForceClosed => unreachable!("We already checked for channel existence, we can't fail here!"),
@@ -4213,7 +4213,7 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
42134213
// available to be claimed. Thus, it does not make sense to set
42144214
// `claimed_any_htlcs`.
42154215
},
4216-
ClaimFundsFromHop::Success(_) => claimed_any_htlcs = true,
4216+
ClaimFundsFromHop::Success(_) => {},
42174217
}
42184218
}
42194219
}
@@ -4230,22 +4230,17 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
42304230
}
42314231
}
42324232

4233-
let PendingClaimingPayment { amount_msat, payment_purpose: purpose, receiver_node_id } =
4234-
self.pending_claimed_payments.lock().unwrap().remove(&payment_hash).unwrap();
4235-
if claimed_any_htlcs {
4236-
self.pending_events.lock().unwrap().push(events::Event::PaymentClaimed {
4237-
payment_hash, purpose, amount_msat, receiver_node_id: Some(receiver_node_id),
4238-
});
4239-
}
4240-
42414233
// Now we can handle any errors which were generated.
42424234
for (counterparty_node_id, err) in errs.drain(..) {
42434235
let res: Result<(), _> = Err(err);
42444236
let _ = handle_error!(self, res, counterparty_node_id);
42454237
}
42464238
}
42474239

4248-
fn claim_funds_from_hop(&self, channel_state_lock: &mut MutexGuard<ChannelHolder<<K::Target as KeysInterface>::Signer>>, prev_hop: HTLCPreviousHopData, payment_preimage: PaymentPreimage) -> ClaimFundsFromHop {
4240+
fn claim_funds_from_hop<ComplFunc: FnOnce(Option<u64>) -> Option<MonitorUpdateCompletionAction>>(&self,
4241+
channel_state_lock: &mut MutexGuard<ChannelHolder<<K::Target as KeysInterface>::Signer>>,
4242+
prev_hop: HTLCPreviousHopData, payment_preimage: PaymentPreimage, completion_action: ComplFunc)
4243+
-> ClaimFundsFromHop {
42494244
//TODO: Delay the claimed_funds relaying just like we do outbound relay!
42504245

42514246
let chan_id = prev_hop.outpoint.to_channel_id();
@@ -4260,6 +4255,7 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
42604255
log_given_level!(self.logger, if e == ChannelMonitorUpdateStatus::PermanentFailure { Level::Error } else { Level::Debug },
42614256
"Failed to update channel monitor with preimage {:?}: {:?}",
42624257
payment_preimage, e);
4258+
self.handle_monitor_update_completion_actions(completion_action(Some(htlc_value_msat)));
42634259
return ClaimFundsFromHop::MonitorUpdateFail(
42644260
chan.get().get_counterparty_node_id(),
42654261
handle_monitor_update_res!(self, e, chan, RAACommitmentOrder::CommitmentFirst, false, msgs.is_some()).unwrap_err(),
@@ -4282,6 +4278,7 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
42824278
}
42834279
});
42844280
}
4281+
self.handle_monitor_update_completion_actions(completion_action(Some(htlc_value_msat)));
42854282
return ClaimFundsFromHop::Success(htlc_value_msat);
42864283
} else {
42874284
return ClaimFundsFromHop::DuplicateClaim;
@@ -4301,10 +4298,14 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
43014298
if drop {
43024299
chan.remove_entry();
43034300
}
4301+
self.handle_monitor_update_completion_actions(completion_action(None));
43044302
return ClaimFundsFromHop::MonitorUpdateFail(counterparty_node_id, res, None);
43054303
},
43064304
}
4307-
} else { return ClaimFundsFromHop::PrevHopForceClosed }
4305+
} else {
4306+
self.handle_monitor_update_completion_actions(completion_action(None));
4307+
return ClaimFundsFromHop::PrevHopForceClosed
4308+
}
43084309
}
43094310

43104311
fn finalize_claims(&self, mut sources: Vec<HTLCSource>) {
@@ -4377,13 +4378,24 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
43774378
},
43784379
HTLCSource::PreviousHopData(hop_data) => {
43794380
let prev_outpoint = hop_data.outpoint;
4380-
let res = self.claim_funds_from_hop(&mut channel_state_lock, hop_data, payment_preimage);
4381-
let claimed_htlc = if let ClaimFundsFromHop::DuplicateClaim = res { false } else { true };
4382-
let htlc_claim_value_msat = match res {
4383-
ClaimFundsFromHop::MonitorUpdateFail(_, _, amt_opt) => amt_opt,
4384-
ClaimFundsFromHop::Success(amt) => Some(amt),
4385-
_ => None,
4386-
};
4381+
let res = self.claim_funds_from_hop(&mut channel_state_lock, hop_data, payment_preimage,
4382+
|htlc_claim_value_msat| {
4383+
if let Some(forwarded_htlc_value) = forwarded_htlc_value_msat {
4384+
let fee_earned_msat = if let Some(claimed_htlc_value) = htlc_claim_value_msat {
4385+
Some(claimed_htlc_value - forwarded_htlc_value)
4386+
} else { None };
4387+
4388+
let prev_channel_id = Some(prev_outpoint.to_channel_id());
4389+
let next_channel_id = Some(next_channel_id);
4390+
4391+
Some(MonitorUpdateCompletionAction::SurfaceEvent { event: events::Event::PaymentForwarded {
4392+
fee_earned_msat,
4393+
claim_from_onchain_tx: from_onchain,
4394+
prev_channel_id,
4395+
next_channel_id,
4396+
}})
4397+
} else { None }
4398+
});
43874399
if let ClaimFundsFromHop::PrevHopForceClosed = res {
43884400
let preimage_update = ChannelMonitorUpdate {
43894401
update_id: CLOSED_CHANNEL_UPDATE_ID,
@@ -4414,25 +4426,6 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
44144426
let result: Result<(), _> = Err(err);
44154427
let _ = handle_error!(self, result, pk);
44164428
}
4417-
4418-
if claimed_htlc {
4419-
if let Some(forwarded_htlc_value) = forwarded_htlc_value_msat {
4420-
let fee_earned_msat = if let Some(claimed_htlc_value) = htlc_claim_value_msat {
4421-
Some(claimed_htlc_value - forwarded_htlc_value)
4422-
} else { None };
4423-
4424-
let mut pending_events = self.pending_events.lock().unwrap();
4425-
let prev_channel_id = Some(prev_outpoint.to_channel_id());
4426-
let next_channel_id = Some(next_channel_id);
4427-
4428-
pending_events.push(events::Event::PaymentForwarded {
4429-
fee_earned_msat,
4430-
claim_from_onchain_tx: from_onchain,
4431-
prev_channel_id,
4432-
next_channel_id,
4433-
});
4434-
}
4435-
}
44364429
},
44374430
}
44384431
}

0 commit comments

Comments
 (0)