Skip to content

Commit 99ff94f

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 934be71 commit 99ff94f

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
@@ -4287,7 +4287,6 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
42874287
let mut expected_amt_msat = None;
42884288
let mut valid_mpp = true;
42894289
let mut errs = Vec::new();
4290-
let mut claimed_any_htlcs = false;
42914290
let mut channel_state_lock = self.channel_state.lock().unwrap();
42924291
let channel_state = &mut *channel_state_lock;
42934292
for htlc in sources.iter() {
@@ -4337,13 +4336,14 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
43374336
}
43384337
if valid_mpp {
43394338
for htlc in sources.drain(..) {
4340-
match self.claim_funds_from_hop(&mut channel_state_lock, htlc.prev_hop, payment_preimage) {
4339+
match self.claim_funds_from_hop(&mut channel_state_lock, htlc.prev_hop, payment_preimage,
4340+
|_| Some(MonitorUpdateCompletionAction::PaymentClaimed { payment_hash }))
4341+
{
43414342
ClaimFundsFromHop::MonitorUpdateFail(pk, err, _) => {
43424343
if let msgs::ErrorAction::IgnoreError = err.err.action {
43434344
// We got a temporary failure updating monitor, but will claim the
43444345
// HTLC when the monitor updating is restored (or on chain).
43454346
log_error!(self.logger, "Temporary failure claiming HTLC, treating as success: {}", err.err.err);
4346-
claimed_any_htlcs = true;
43474347
} else { errs.push((pk, err)); }
43484348
},
43494349
ClaimFundsFromHop::PrevHopForceClosed => unreachable!("We already checked for channel existence, we can't fail here!"),
@@ -4353,7 +4353,7 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
43534353
// available to be claimed. Thus, it does not make sense to set
43544354
// `claimed_any_htlcs`.
43554355
},
4356-
ClaimFundsFromHop::Success(_) => claimed_any_htlcs = true,
4356+
ClaimFundsFromHop::Success(_) => {},
43574357
}
43584358
}
43594359
}
@@ -4370,22 +4370,17 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
43704370
}
43714371
}
43724372

4373-
let PendingClaimingPayment { amount_msat, payment_purpose: purpose, receiver_node_id } =
4374-
self.pending_claimed_payments.lock().unwrap().remove(&payment_hash).unwrap();
4375-
if claimed_any_htlcs {
4376-
self.pending_events.lock().unwrap().push(events::Event::PaymentClaimed {
4377-
payment_hash, purpose, amount_msat, receiver_node_id: Some(receiver_node_id),
4378-
});
4379-
}
4380-
43814373
// Now we can handle any errors which were generated.
43824374
for (counterparty_node_id, err) in errs.drain(..) {
43834375
let res: Result<(), _> = Err(err);
43844376
let _ = handle_error!(self, res, counterparty_node_id);
43854377
}
43864378
}
43874379

4388-
fn claim_funds_from_hop(&self, channel_state_lock: &mut MutexGuard<ChannelHolder<<K::Target as KeysInterface>::Signer>>, prev_hop: HTLCPreviousHopData, payment_preimage: PaymentPreimage) -> ClaimFundsFromHop {
4380+
fn claim_funds_from_hop<ComplFunc: FnOnce(Option<u64>) -> Option<MonitorUpdateCompletionAction>>(&self,
4381+
channel_state_lock: &mut MutexGuard<ChannelHolder<<K::Target as KeysInterface>::Signer>>,
4382+
prev_hop: HTLCPreviousHopData, payment_preimage: PaymentPreimage, completion_action: ComplFunc)
4383+
-> ClaimFundsFromHop {
43894384
//TODO: Delay the claimed_funds relaying just like we do outbound relay!
43904385

43914386
let chan_id = prev_hop.outpoint.to_channel_id();
@@ -4400,6 +4395,7 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
44004395
log_given_level!(self.logger, if e == ChannelMonitorUpdateStatus::PermanentFailure { Level::Error } else { Level::Debug },
44014396
"Failed to update channel monitor with preimage {:?}: {:?}",
44024397
payment_preimage, e);
4398+
self.handle_monitor_update_completion_actions(completion_action(Some(htlc_value_msat)));
44034399
return ClaimFundsFromHop::MonitorUpdateFail(
44044400
chan.get().get_counterparty_node_id(),
44054401
handle_monitor_update_res!(self, e, chan, RAACommitmentOrder::CommitmentFirst, false, msgs.is_some()).unwrap_err(),
@@ -4422,6 +4418,7 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
44224418
}
44234419
});
44244420
}
4421+
self.handle_monitor_update_completion_actions(completion_action(Some(htlc_value_msat)));
44254422
return ClaimFundsFromHop::Success(htlc_value_msat);
44264423
} else {
44274424
return ClaimFundsFromHop::DuplicateClaim;
@@ -4441,10 +4438,14 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
44414438
if drop {
44424439
chan.remove_entry();
44434440
}
4441+
self.handle_monitor_update_completion_actions(completion_action(None));
44444442
return ClaimFundsFromHop::MonitorUpdateFail(counterparty_node_id, res, None);
44454443
},
44464444
}
4447-
} else { return ClaimFundsFromHop::PrevHopForceClosed }
4445+
} else {
4446+
self.handle_monitor_update_completion_actions(completion_action(None));
4447+
return ClaimFundsFromHop::PrevHopForceClosed
4448+
}
44484449
}
44494450

44504451
fn finalize_claims(&self, mut sources: Vec<HTLCSource>) {
@@ -4517,13 +4518,24 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
45174518
},
45184519
HTLCSource::PreviousHopData(hop_data) => {
45194520
let prev_outpoint = hop_data.outpoint;
4520-
let res = self.claim_funds_from_hop(&mut channel_state_lock, hop_data, payment_preimage);
4521-
let claimed_htlc = if let ClaimFundsFromHop::DuplicateClaim = res { false } else { true };
4522-
let htlc_claim_value_msat = match res {
4523-
ClaimFundsFromHop::MonitorUpdateFail(_, _, amt_opt) => amt_opt,
4524-
ClaimFundsFromHop::Success(amt) => Some(amt),
4525-
_ => None,
4526-
};
4521+
let res = self.claim_funds_from_hop(&mut channel_state_lock, hop_data, payment_preimage,
4522+
|htlc_claim_value_msat| {
4523+
if let Some(forwarded_htlc_value) = forwarded_htlc_value_msat {
4524+
let fee_earned_msat = if let Some(claimed_htlc_value) = htlc_claim_value_msat {
4525+
Some(claimed_htlc_value - forwarded_htlc_value)
4526+
} else { None };
4527+
4528+
let prev_channel_id = Some(prev_outpoint.to_channel_id());
4529+
let next_channel_id = Some(next_channel_id);
4530+
4531+
Some(MonitorUpdateCompletionAction::SurfaceEvent { event: events::Event::PaymentForwarded {
4532+
fee_earned_msat,
4533+
claim_from_onchain_tx: from_onchain,
4534+
prev_channel_id,
4535+
next_channel_id,
4536+
}})
4537+
} else { None }
4538+
});
45274539
if let ClaimFundsFromHop::PrevHopForceClosed = res {
45284540
let preimage_update = ChannelMonitorUpdate {
45294541
update_id: CLOSED_CHANNEL_UPDATE_ID,
@@ -4554,25 +4566,6 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
45544566
let result: Result<(), _> = Err(err);
45554567
let _ = handle_error!(self, result, pk);
45564568
}
4557-
4558-
if claimed_htlc {
4559-
if let Some(forwarded_htlc_value) = forwarded_htlc_value_msat {
4560-
let fee_earned_msat = if let Some(claimed_htlc_value) = htlc_claim_value_msat {
4561-
Some(claimed_htlc_value - forwarded_htlc_value)
4562-
} else { None };
4563-
4564-
let mut pending_events = self.pending_events.lock().unwrap();
4565-
let prev_channel_id = Some(prev_outpoint.to_channel_id());
4566-
let next_channel_id = Some(next_channel_id);
4567-
4568-
pending_events.push(events::Event::PaymentForwarded {
4569-
fee_earned_msat,
4570-
claim_from_onchain_tx: from_onchain,
4571-
prev_channel_id,
4572-
next_channel_id,
4573-
});
4574-
}
4575-
}
45764569
},
45774570
}
45784571
}

0 commit comments

Comments
 (0)