Skip to content

Commit acdfe20

Browse files
committed
Move ChannelManager-read preimage relay to after struct build
In a coming commit we'll use the existing `ChannelManager` claim flow to claim HTLCs which we found partially claimed on startup, necessitating having a full `ChannelManager` when we go to do so. Here we move the re-claim logic down in the `ChannelManager`-read logic so that we have that.
1 parent 3287c79 commit acdfe20

File tree

1 file changed

+67
-60
lines changed

1 file changed

+67
-60
lines changed

lightning/src/ln/channelmanager.rs

Lines changed: 67 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1087,7 +1087,7 @@ pub(crate) struct PendingMPPClaim {
10871087
}
10881088

10891089
#[derive(Clone, Debug, PartialEq, Eq)]
1090-
/// When we're claiming a(n MPP) payment, we want to store information about thay payment in the
1090+
/// When we're claiming a(n MPP) payment, we want to store information about that payment in the
10911091
/// [`ChannelMonitor`] so that we can replay the claim without any information from the
10921092
/// [`ChannelManager`] at all. This struct stores that information with enough to replay claims
10931093
/// against all MPP parts as well as generate an [`Event::PaymentClaimed`].
@@ -12986,65 +12986,6 @@ where
1298612986

1298712987
let bounded_fee_estimator = LowerBoundedFeeEstimator::new(args.fee_estimator);
1298812988

12989-
for (_, monitor) in args.channel_monitors.iter() {
12990-
for (payment_hash, (payment_preimage, _)) in monitor.get_stored_preimages() {
12991-
if let Some(payment) = claimable_payments.remove(&payment_hash) {
12992-
log_info!(args.logger, "Re-claiming HTLCs with payment hash {} as we've released the preimage to a ChannelMonitor!", &payment_hash);
12993-
let mut claimable_amt_msat = 0;
12994-
let mut receiver_node_id = Some(our_network_pubkey);
12995-
let phantom_shared_secret = payment.htlcs[0].prev_hop.phantom_shared_secret;
12996-
if phantom_shared_secret.is_some() {
12997-
let phantom_pubkey = args.node_signer.get_node_id(Recipient::PhantomNode)
12998-
.expect("Failed to get node_id for phantom node recipient");
12999-
receiver_node_id = Some(phantom_pubkey)
13000-
}
13001-
for claimable_htlc in &payment.htlcs {
13002-
claimable_amt_msat += claimable_htlc.value;
13003-
13004-
// Add a holding-cell claim of the payment to the Channel, which should be
13005-
// applied ~immediately on peer reconnection. Because it won't generate a
13006-
// new commitment transaction we can just provide the payment preimage to
13007-
// the corresponding ChannelMonitor and nothing else.
13008-
//
13009-
// We do so directly instead of via the normal ChannelMonitor update
13010-
// procedure as the ChainMonitor hasn't yet been initialized, implying
13011-
// we're not allowed to call it directly yet. Further, we do the update
13012-
// without incrementing the ChannelMonitor update ID as there isn't any
13013-
// reason to.
13014-
// If we were to generate a new ChannelMonitor update ID here and then
13015-
// crash before the user finishes block connect we'd end up force-closing
13016-
// this channel as well. On the flip side, there's no harm in restarting
13017-
// without the new monitor persisted - we'll end up right back here on
13018-
// restart.
13019-
let previous_channel_id = claimable_htlc.prev_hop.channel_id;
13020-
if let Some(peer_node_id) = outpoint_to_peer.get(&claimable_htlc.prev_hop.outpoint) {
13021-
let peer_state_mutex = per_peer_state.get(peer_node_id).unwrap();
13022-
let mut peer_state_lock = peer_state_mutex.lock().unwrap();
13023-
let peer_state = &mut *peer_state_lock;
13024-
if let Some(ChannelPhase::Funded(channel)) = peer_state.channel_by_id.get_mut(&previous_channel_id) {
13025-
let logger = WithChannelContext::from(&args.logger, &channel.context, Some(payment_hash));
13026-
channel.claim_htlc_while_disconnected_dropping_mon_update(claimable_htlc.prev_hop.htlc_id, payment_preimage, &&logger);
13027-
}
13028-
}
13029-
if let Some(previous_hop_monitor) = args.channel_monitors.get(&claimable_htlc.prev_hop.outpoint) {
13030-
previous_hop_monitor.provide_payment_preimage(&payment_hash, &payment_preimage, &args.tx_broadcaster, &bounded_fee_estimator, &args.logger);
13031-
}
13032-
}
13033-
let payment_id = payment.inbound_payment_id(&inbound_payment_id_secret.unwrap());
13034-
pending_events_read.push_back((events::Event::PaymentClaimed {
13035-
receiver_node_id,
13036-
payment_hash,
13037-
purpose: payment.purpose,
13038-
amount_msat: claimable_amt_msat,
13039-
htlcs: payment.htlcs.iter().map(events::ClaimedHTLC::from).collect(),
13040-
sender_intended_total_msat: payment.htlcs.first().map(|htlc| htlc.total_msat),
13041-
onion_fields: payment.onion_fields,
13042-
payment_id: Some(payment_id),
13043-
}, None));
13044-
}
13045-
}
13046-
}
13047-
1304812989
for (node_id, monitor_update_blocked_actions) in monitor_update_blocked_actions_per_peer.unwrap() {
1304912990
if let Some(peer_state) = per_peer_state.get(&node_id) {
1305012991
for (channel_id, actions) in monitor_update_blocked_actions.iter() {
@@ -13145,6 +13086,72 @@ where
1314513086
default_configuration: args.default_config,
1314613087
};
1314713088

13089+
for (_, monitor) in args.channel_monitors.iter() {
13090+
for (payment_hash, (payment_preimage, _)) in monitor.get_stored_preimages() {
13091+
let per_peer_state = channel_manager.per_peer_state.read().unwrap();
13092+
let mut claimable_payments = channel_manager.claimable_payments.lock().unwrap();
13093+
let payment = claimable_payments.claimable_payments.remove(&payment_hash);
13094+
mem::drop(claimable_payments);
13095+
if let Some(payment) = payment {
13096+
log_info!(channel_manager.logger, "Re-claiming HTLCs with payment hash {} as we've released the preimage to a ChannelMonitor!", &payment_hash);
13097+
let mut claimable_amt_msat = 0;
13098+
let mut receiver_node_id = Some(our_network_pubkey);
13099+
let phantom_shared_secret = payment.htlcs[0].prev_hop.phantom_shared_secret;
13100+
if phantom_shared_secret.is_some() {
13101+
let phantom_pubkey = channel_manager.node_signer.get_node_id(Recipient::PhantomNode)
13102+
.expect("Failed to get node_id for phantom node recipient");
13103+
receiver_node_id = Some(phantom_pubkey)
13104+
}
13105+
for claimable_htlc in &payment.htlcs {
13106+
claimable_amt_msat += claimable_htlc.value;
13107+
13108+
// Add a holding-cell claim of the payment to the Channel, which should be
13109+
// applied ~immediately on peer reconnection. Because it won't generate a
13110+
// new commitment transaction we can just provide the payment preimage to
13111+
// the corresponding ChannelMonitor and nothing else.
13112+
//
13113+
// We do so directly instead of via the normal ChannelMonitor update
13114+
// procedure as the ChainMonitor hasn't yet been initialized, implying
13115+
// we're not allowed to call it directly yet. Further, we do the update
13116+
// without incrementing the ChannelMonitor update ID as there isn't any
13117+
// reason to.
13118+
// If we were to generate a new ChannelMonitor update ID here and then
13119+
// crash before the user finishes block connect we'd end up force-closing
13120+
// this channel as well. On the flip side, there's no harm in restarting
13121+
// without the new monitor persisted - we'll end up right back here on
13122+
// restart.
13123+
let previous_channel_id = claimable_htlc.prev_hop.channel_id;
13124+
let peer_node_id_opt = channel_manager.outpoint_to_peer.lock().unwrap()
13125+
.get(&claimable_htlc.prev_hop.outpoint).cloned();
13126+
if let Some(peer_node_id) = peer_node_id_opt {
13127+
let peer_state_mutex = per_peer_state.get(&peer_node_id).unwrap();
13128+
let mut peer_state_lock = peer_state_mutex.lock().unwrap();
13129+
let peer_state = &mut *peer_state_lock;
13130+
if let Some(ChannelPhase::Funded(channel)) = peer_state.channel_by_id.get_mut(&previous_channel_id) {
13131+
let logger = WithChannelContext::from(&channel_manager.logger, &channel.context, Some(payment_hash));
13132+
channel.claim_htlc_while_disconnected_dropping_mon_update(claimable_htlc.prev_hop.htlc_id, payment_preimage, &&logger);
13133+
}
13134+
}
13135+
if let Some(previous_hop_monitor) = args.channel_monitors.get(&claimable_htlc.prev_hop.outpoint) {
13136+
previous_hop_monitor.provide_payment_preimage(&payment_hash, &payment_preimage, &channel_manager.tx_broadcaster, &channel_manager.fee_estimator, &channel_manager.logger);
13137+
}
13138+
}
13139+
let mut pending_events = channel_manager.pending_events.lock().unwrap();
13140+
let payment_id = payment.inbound_payment_id(&inbound_payment_id_secret.unwrap());
13141+
pending_events.push_back((events::Event::PaymentClaimed {
13142+
receiver_node_id,
13143+
payment_hash,
13144+
purpose: payment.purpose,
13145+
amount_msat: claimable_amt_msat,
13146+
htlcs: payment.htlcs.iter().map(events::ClaimedHTLC::from).collect(),
13147+
sender_intended_total_msat: payment.htlcs.first().map(|htlc| htlc.total_msat),
13148+
onion_fields: payment.onion_fields,
13149+
payment_id: Some(payment_id),
13150+
}, None));
13151+
}
13152+
}
13153+
}
13154+
1314813155
for htlc_source in failed_htlcs.drain(..) {
1314913156
let (source, payment_hash, counterparty_node_id, channel_id) = htlc_source;
1315013157
let receiver = HTLCDestination::NextHopChannel { node_id: Some(counterparty_node_id), channel_id };

0 commit comments

Comments
 (0)