Skip to content

Commit dd84b1b

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 9d776c2 commit dd84b1b

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
@@ -4300,7 +4300,6 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
43004300
let mut expected_amt_msat = None;
43014301
let mut valid_mpp = true;
43024302
let mut errs = Vec::new();
4303-
let mut claimed_any_htlcs = false;
43044303
let mut channel_state_lock = self.channel_state.lock().unwrap();
43054304
let channel_state = &mut *channel_state_lock;
43064305
for htlc in sources.iter() {
@@ -4350,13 +4349,14 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
43504349
}
43514350
if valid_mpp {
43524351
for htlc in sources.drain(..) {
4353-
match self.claim_funds_from_hop(&mut channel_state_lock, htlc.prev_hop, payment_preimage) {
4352+
match self.claim_funds_from_hop(&mut channel_state_lock, htlc.prev_hop, payment_preimage,
4353+
|_| Some(MonitorUpdateCompletionAction::PaymentClaimed { payment_hash }))
4354+
{
43544355
ClaimFundsFromHop::MonitorUpdateFail(pk, err, _) => {
43554356
if let msgs::ErrorAction::IgnoreError = err.err.action {
43564357
// We got a temporary failure updating monitor, but will claim the
43574358
// HTLC when the monitor updating is restored (or on chain).
43584359
log_error!(self.logger, "Temporary failure claiming HTLC, treating as success: {}", err.err.err);
4359-
claimed_any_htlcs = true;
43604360
} else { errs.push((pk, err)); }
43614361
},
43624362
ClaimFundsFromHop::PrevHopForceClosed => unreachable!("We already checked for channel existence, we can't fail here!"),
@@ -4366,7 +4366,7 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
43664366
// available to be claimed. Thus, it does not make sense to set
43674367
// `claimed_any_htlcs`.
43684368
},
4369-
ClaimFundsFromHop::Success(_) => claimed_any_htlcs = true,
4369+
ClaimFundsFromHop::Success(_) => {},
43704370
}
43714371
}
43724372
}
@@ -4382,22 +4382,17 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
43824382
}
43834383
}
43844384

4385-
let ClaimingPayment { amount_msat, payment_purpose: purpose, receiver_node_id } =
4386-
self.pending_claimed_payments.lock().unwrap().remove(&payment_hash).unwrap();
4387-
if claimed_any_htlcs {
4388-
self.pending_events.lock().unwrap().push(events::Event::PaymentClaimed {
4389-
payment_hash, purpose, amount_msat, receiver_node_id: Some(receiver_node_id),
4390-
});
4391-
}
4392-
43934385
// Now we can handle any errors which were generated.
43944386
for (counterparty_node_id, err) in errs.drain(..) {
43954387
let res: Result<(), _> = Err(err);
43964388
let _ = handle_error!(self, res, counterparty_node_id);
43974389
}
43984390
}
43994391

4400-
fn claim_funds_from_hop(&self, channel_state_lock: &mut MutexGuard<ChannelHolder<<K::Target as KeysInterface>::Signer>>, prev_hop: HTLCPreviousHopData, payment_preimage: PaymentPreimage) -> ClaimFundsFromHop {
4392+
fn claim_funds_from_hop<ComplFunc: FnOnce(Option<u64>) -> Option<MonitorUpdateCompletionAction>>(&self,
4393+
channel_state_lock: &mut MutexGuard<ChannelHolder<<K::Target as KeysInterface>::Signer>>,
4394+
prev_hop: HTLCPreviousHopData, payment_preimage: PaymentPreimage, completion_action: ComplFunc)
4395+
-> ClaimFundsFromHop {
44014396
//TODO: Delay the claimed_funds relaying just like we do outbound relay!
44024397

44034398
let chan_id = prev_hop.outpoint.to_channel_id();
@@ -4412,6 +4407,7 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
44124407
log_given_level!(self.logger, if e == ChannelMonitorUpdateStatus::PermanentFailure { Level::Error } else { Level::Debug },
44134408
"Failed to update channel monitor with preimage {:?}: {:?}",
44144409
payment_preimage, e);
4410+
self.handle_monitor_update_completion_actions(completion_action(Some(htlc_value_msat)));
44154411
return ClaimFundsFromHop::MonitorUpdateFail(
44164412
chan.get().get_counterparty_node_id(),
44174413
handle_monitor_update_res!(self, e, chan, RAACommitmentOrder::CommitmentFirst, false, msgs.is_some()).unwrap_err(),
@@ -4434,6 +4430,7 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
44344430
}
44354431
});
44364432
}
4433+
self.handle_monitor_update_completion_actions(completion_action(Some(htlc_value_msat)));
44374434
return ClaimFundsFromHop::Success(htlc_value_msat);
44384435
} else {
44394436
return ClaimFundsFromHop::DuplicateClaim;
@@ -4453,10 +4450,14 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
44534450
if drop {
44544451
chan.remove_entry();
44554452
}
4453+
self.handle_monitor_update_completion_actions(completion_action(None));
44564454
return ClaimFundsFromHop::MonitorUpdateFail(counterparty_node_id, res, None);
44574455
},
44584456
}
4459-
} else { return ClaimFundsFromHop::PrevHopForceClosed }
4457+
} else {
4458+
self.handle_monitor_update_completion_actions(completion_action(None));
4459+
return ClaimFundsFromHop::PrevHopForceClosed
4460+
}
44604461
}
44614462

44624463
fn finalize_claims(&self, mut sources: Vec<HTLCSource>) {
@@ -4529,13 +4530,24 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
45294530
},
45304531
HTLCSource::PreviousHopData(hop_data) => {
45314532
let prev_outpoint = hop_data.outpoint;
4532-
let res = self.claim_funds_from_hop(&mut channel_state_lock, hop_data, payment_preimage);
4533-
let claimed_htlc = if let ClaimFundsFromHop::DuplicateClaim = res { false } else { true };
4534-
let htlc_claim_value_msat = match res {
4535-
ClaimFundsFromHop::MonitorUpdateFail(_, _, amt_opt) => amt_opt,
4536-
ClaimFundsFromHop::Success(amt) => Some(amt),
4537-
_ => None,
4538-
};
4533+
let res = self.claim_funds_from_hop(&mut channel_state_lock, hop_data, payment_preimage,
4534+
|htlc_claim_value_msat| {
4535+
if let Some(forwarded_htlc_value) = forwarded_htlc_value_msat {
4536+
let fee_earned_msat = if let Some(claimed_htlc_value) = htlc_claim_value_msat {
4537+
Some(claimed_htlc_value - forwarded_htlc_value)
4538+
} else { None };
4539+
4540+
let prev_channel_id = Some(prev_outpoint.to_channel_id());
4541+
let next_channel_id = Some(next_channel_id);
4542+
4543+
Some(MonitorUpdateCompletionAction::SurfaceEvent { event: events::Event::PaymentForwarded {
4544+
fee_earned_msat,
4545+
claim_from_onchain_tx: from_onchain,
4546+
prev_channel_id,
4547+
next_channel_id,
4548+
}})
4549+
} else { None }
4550+
});
45394551
if let ClaimFundsFromHop::PrevHopForceClosed = res {
45404552
let preimage_update = ChannelMonitorUpdate {
45414553
update_id: CLOSED_CHANNEL_UPDATE_ID,
@@ -4566,25 +4578,6 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
45664578
let result: Result<(), _> = Err(err);
45674579
let _ = handle_error!(self, result, pk);
45684580
}
4569-
4570-
if claimed_htlc {
4571-
if let Some(forwarded_htlc_value) = forwarded_htlc_value_msat {
4572-
let fee_earned_msat = if let Some(claimed_htlc_value) = htlc_claim_value_msat {
4573-
Some(claimed_htlc_value - forwarded_htlc_value)
4574-
} else { None };
4575-
4576-
let mut pending_events = self.pending_events.lock().unwrap();
4577-
let prev_channel_id = Some(prev_outpoint.to_channel_id());
4578-
let next_channel_id = Some(next_channel_id);
4579-
4580-
pending_events.push(events::Event::PaymentForwarded {
4581-
fee_earned_msat,
4582-
claim_from_onchain_tx: from_onchain,
4583-
prev_channel_id,
4584-
next_channel_id,
4585-
});
4586-
}
4587-
}
45884581
},
45894582
}
45904583
}

0 commit comments

Comments
 (0)