Skip to content

Commit 2ea1478

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 55d175e commit 2ea1478

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
@@ -4294,7 +4294,6 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
42944294
let mut expected_amt_msat = None;
42954295
let mut valid_mpp = true;
42964296
let mut errs = Vec::new();
4297-
let mut claimed_any_htlcs = false;
42984297
let mut channel_state_lock = self.channel_state.lock().unwrap();
42994298
let channel_state = &mut *channel_state_lock;
43004299
for htlc in sources.iter() {
@@ -4344,13 +4343,14 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
43444343
}
43454344
if valid_mpp {
43464345
for htlc in sources.drain(..) {
4347-
match self.claim_funds_from_hop(&mut channel_state_lock, htlc.prev_hop, payment_preimage) {
4346+
match self.claim_funds_from_hop(&mut channel_state_lock, htlc.prev_hop, payment_preimage,
4347+
|_| Some(MonitorUpdateCompletionAction::PaymentClaimed { payment_hash }))
4348+
{
43484349
ClaimFundsFromHop::MonitorUpdateFail(pk, err, _) => {
43494350
if let msgs::ErrorAction::IgnoreError = err.err.action {
43504351
// We got a temporary failure updating monitor, but will claim the
43514352
// HTLC when the monitor updating is restored (or on chain).
43524353
log_error!(self.logger, "Temporary failure claiming HTLC, treating as success: {}", err.err.err);
4353-
claimed_any_htlcs = true;
43544354
} else { errs.push((pk, err)); }
43554355
},
43564356
ClaimFundsFromHop::PrevHopForceClosed => unreachable!("We already checked for channel existence, we can't fail here!"),
@@ -4360,7 +4360,7 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
43604360
// available to be claimed. Thus, it does not make sense to set
43614361
// `claimed_any_htlcs`.
43624362
},
4363-
ClaimFundsFromHop::Success(_) => claimed_any_htlcs = true,
4363+
ClaimFundsFromHop::Success(_) => {},
43644364
}
43654365
}
43664366
}
@@ -4377,22 +4377,17 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
43774377
}
43784378
}
43794379

4380-
let PendingClaimingPayment { amount_msat, payment_purpose: purpose, receiver_node_id } =
4381-
self.pending_claimed_payments.lock().unwrap().remove(&payment_hash).unwrap();
4382-
if claimed_any_htlcs {
4383-
self.pending_events.lock().unwrap().push(events::Event::PaymentClaimed {
4384-
payment_hash, purpose, amount_msat, receiver_node_id: Some(receiver_node_id),
4385-
});
4386-
}
4387-
43884380
// Now we can handle any errors which were generated.
43894381
for (counterparty_node_id, err) in errs.drain(..) {
43904382
let res: Result<(), _> = Err(err);
43914383
let _ = handle_error!(self, res, counterparty_node_id);
43924384
}
43934385
}
43944386

4395-
fn claim_funds_from_hop(&self, channel_state_lock: &mut MutexGuard<ChannelHolder<<K::Target as KeysInterface>::Signer>>, prev_hop: HTLCPreviousHopData, payment_preimage: PaymentPreimage) -> ClaimFundsFromHop {
4387+
fn claim_funds_from_hop<ComplFunc: FnOnce(Option<u64>) -> Option<MonitorUpdateCompletionAction>>(&self,
4388+
channel_state_lock: &mut MutexGuard<ChannelHolder<<K::Target as KeysInterface>::Signer>>,
4389+
prev_hop: HTLCPreviousHopData, payment_preimage: PaymentPreimage, completion_action: ComplFunc)
4390+
-> ClaimFundsFromHop {
43964391
//TODO: Delay the claimed_funds relaying just like we do outbound relay!
43974392

43984393
let chan_id = prev_hop.outpoint.to_channel_id();
@@ -4407,6 +4402,7 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
44074402
log_given_level!(self.logger, if e == ChannelMonitorUpdateStatus::PermanentFailure { Level::Error } else { Level::Debug },
44084403
"Failed to update channel monitor with preimage {:?}: {:?}",
44094404
payment_preimage, e);
4405+
self.handle_monitor_update_completion_actions(completion_action(Some(htlc_value_msat)));
44104406
return ClaimFundsFromHop::MonitorUpdateFail(
44114407
chan.get().get_counterparty_node_id(),
44124408
handle_monitor_update_res!(self, e, chan, RAACommitmentOrder::CommitmentFirst, false, msgs.is_some()).unwrap_err(),
@@ -4429,6 +4425,7 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
44294425
}
44304426
});
44314427
}
4428+
self.handle_monitor_update_completion_actions(completion_action(Some(htlc_value_msat)));
44324429
return ClaimFundsFromHop::Success(htlc_value_msat);
44334430
} else {
44344431
return ClaimFundsFromHop::DuplicateClaim;
@@ -4448,10 +4445,14 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
44484445
if drop {
44494446
chan.remove_entry();
44504447
}
4448+
self.handle_monitor_update_completion_actions(completion_action(None));
44514449
return ClaimFundsFromHop::MonitorUpdateFail(counterparty_node_id, res, None);
44524450
},
44534451
}
4454-
} else { return ClaimFundsFromHop::PrevHopForceClosed }
4452+
} else {
4453+
self.handle_monitor_update_completion_actions(completion_action(None));
4454+
return ClaimFundsFromHop::PrevHopForceClosed
4455+
}
44554456
}
44564457

44574458
fn finalize_claims(&self, mut sources: Vec<HTLCSource>) {
@@ -4524,13 +4525,24 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
45244525
},
45254526
HTLCSource::PreviousHopData(hop_data) => {
45264527
let prev_outpoint = hop_data.outpoint;
4527-
let res = self.claim_funds_from_hop(&mut channel_state_lock, hop_data, payment_preimage);
4528-
let claimed_htlc = if let ClaimFundsFromHop::DuplicateClaim = res { false } else { true };
4529-
let htlc_claim_value_msat = match res {
4530-
ClaimFundsFromHop::MonitorUpdateFail(_, _, amt_opt) => amt_opt,
4531-
ClaimFundsFromHop::Success(amt) => Some(amt),
4532-
_ => None,
4533-
};
4528+
let res = self.claim_funds_from_hop(&mut channel_state_lock, hop_data, payment_preimage,
4529+
|htlc_claim_value_msat| {
4530+
if let Some(forwarded_htlc_value) = forwarded_htlc_value_msat {
4531+
let fee_earned_msat = if let Some(claimed_htlc_value) = htlc_claim_value_msat {
4532+
Some(claimed_htlc_value - forwarded_htlc_value)
4533+
} else { None };
4534+
4535+
let prev_channel_id = Some(prev_outpoint.to_channel_id());
4536+
let next_channel_id = Some(next_channel_id);
4537+
4538+
Some(MonitorUpdateCompletionAction::SurfaceEvent { event: events::Event::PaymentForwarded {
4539+
fee_earned_msat,
4540+
claim_from_onchain_tx: from_onchain,
4541+
prev_channel_id,
4542+
next_channel_id,
4543+
}})
4544+
} else { None }
4545+
});
45344546
if let ClaimFundsFromHop::PrevHopForceClosed = res {
45354547
let preimage_update = ChannelMonitorUpdate {
45364548
update_id: CLOSED_CHANNEL_UPDATE_ID,
@@ -4561,25 +4573,6 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
45614573
let result: Result<(), _> = Err(err);
45624574
let _ = handle_error!(self, result, pk);
45634575
}
4564-
4565-
if claimed_htlc {
4566-
if let Some(forwarded_htlc_value) = forwarded_htlc_value_msat {
4567-
let fee_earned_msat = if let Some(claimed_htlc_value) = htlc_claim_value_msat {
4568-
Some(claimed_htlc_value - forwarded_htlc_value)
4569-
} else { None };
4570-
4571-
let mut pending_events = self.pending_events.lock().unwrap();
4572-
let prev_channel_id = Some(prev_outpoint.to_channel_id());
4573-
let next_channel_id = Some(next_channel_id);
4574-
4575-
pending_events.push(events::Event::PaymentForwarded {
4576-
fee_earned_msat,
4577-
claim_from_onchain_tx: from_onchain,
4578-
prev_channel_id,
4579-
next_channel_id,
4580-
});
4581-
}
4582-
}
45834576
},
45844577
}
45854578
}

0 commit comments

Comments
 (0)