Skip to content

Commit cc9a9ef

Browse files
committed
f dry?
1 parent 46d0711 commit cc9a9ef

File tree

1 file changed

+74
-105
lines changed

1 file changed

+74
-105
lines changed

lightning/src/ln/channelmanager.rs

+74-105
Original file line numberDiff line numberDiff line change
@@ -1394,6 +1394,54 @@ macro_rules! emit_channel_ready_event {
13941394
}
13951395
}
13961396

1397+
macro_rules! handle_monitor_update_completion {
1398+
($self: ident, $update_id: expr, $peer_state_lock: expr, $peer_state: expr, $chan: expr) => { {
1399+
let mut updates = $chan.monitor_updating_restored(&$self.logger,
1400+
&$self.node_signer, $self.genesis_hash, &$self.default_configuration,
1401+
$self.best_block.read().unwrap().height());
1402+
let counterparty_node_id = $chan.get_counterparty_node_id();
1403+
let channel_update = if updates.channel_ready.is_some() && $chan.is_usable() {
1404+
// We only send a channel_update in the case where we are just now sending a
1405+
// channel_ready and the channel is in a usable state. We may re-send a
1406+
// channel_update later through the announcement_signatures process for public
1407+
// channels, but there's no reason not to just inform our counterparty of our fees
1408+
// now.
1409+
if let Ok(msg) = $self.get_channel_update_for_unicast($chan) {
1410+
Some(events::MessageSendEvent::SendChannelUpdate {
1411+
node_id: counterparty_node_id,
1412+
msg,
1413+
})
1414+
} else { None }
1415+
} else { None };
1416+
1417+
let update_actions = $peer_state.monitor_update_blocked_actions
1418+
.remove(&$chan.channel_id()).unwrap_or(Vec::new());
1419+
1420+
let htlc_forwards = $self.handle_channel_resumption(
1421+
&mut $peer_state.pending_msg_events, $chan, updates.raa,
1422+
updates.commitment_update, updates.order, updates.accepted_htlcs,
1423+
updates.funding_broadcastable, updates.channel_ready,
1424+
updates.announcement_sigs);
1425+
if let Some(upd) = channel_update {
1426+
$peer_state.pending_msg_events.push(upd);
1427+
}
1428+
1429+
let channel_id = $chan.channel_id();
1430+
core::mem::drop($peer_state_lock);
1431+
1432+
$self.handle_monitor_update_completion_actions(update_actions);
1433+
1434+
if let Some(forwards) = htlc_forwards {
1435+
$self.forward_htlcs(&mut [forwards][..]);
1436+
}
1437+
$self.finalize_claims(updates.finalized_claimed_htlcs);
1438+
for failure in updates.failed_htlcs.drain(..) {
1439+
let receiver = HTLCDestination::NextHopChannel { node_id: Some(counterparty_node_id), channel_id };
1440+
$self.fail_htlc_backwards_internal(&failure.0, &failure.1, &failure.2, receiver);
1441+
}
1442+
} }
1443+
}
1444+
13971445
macro_rules! handle_new_monitor_update {
13981446
($self: ident, $update_res: expr, $update_id: expr, $peer_state_lock: expr, $peer_state: expr, $chan: expr, MANUALLY_REMOVING, $remove: expr) => { {
13991447
// update_maps_on_chan_removal needs to be able to take id_to_peer, so make sure we can in
@@ -1422,49 +1470,7 @@ macro_rules! handle_new_monitor_update {
14221470
.update_id == $update_id) &&
14231471
$chan.get_latest_monitor_update_id() == $update_id
14241472
{
1425-
let mut updates = $chan.monitor_updating_restored(&$self.logger,
1426-
&$self.node_signer, $self.genesis_hash, &$self.default_configuration,
1427-
$self.best_block.read().unwrap().height());
1428-
let counterparty_node_id = $chan.get_counterparty_node_id();
1429-
let channel_update = if updates.channel_ready.is_some() && $chan.is_usable() {
1430-
// We only send a channel_update in the case where we are just now sending a
1431-
// channel_ready and the channel is in a usable state. We may re-send a
1432-
// channel_update later through the announcement_signatures process for public
1433-
// channels, but there's no reason not to just inform our counterparty of our fees
1434-
// now.
1435-
if let Ok(msg) = $self.get_channel_update_for_unicast($chan) {
1436-
Some(events::MessageSendEvent::SendChannelUpdate {
1437-
node_id: counterparty_node_id,
1438-
msg,
1439-
})
1440-
} else { None }
1441-
} else { None };
1442-
1443-
let update_actions = $peer_state.monitor_update_blocked_actions
1444-
.remove(&$chan.channel_id()).unwrap_or(Vec::new());
1445-
1446-
let htlc_forwards = $self.handle_channel_resumption(
1447-
&mut $peer_state.pending_msg_events, $chan, updates.raa,
1448-
updates.commitment_update, updates.order, updates.accepted_htlcs,
1449-
updates.funding_broadcastable, updates.channel_ready,
1450-
updates.announcement_sigs);
1451-
if let Some(upd) = channel_update {
1452-
$peer_state.pending_msg_events.push(upd);
1453-
}
1454-
1455-
let channel_id = $chan.channel_id();
1456-
core::mem::drop($peer_state_lock);
1457-
1458-
$self.handle_monitor_update_completion_actions(update_actions);
1459-
1460-
if let Some(forwards) = htlc_forwards {
1461-
$self.forward_htlcs(&mut [forwards][..]);
1462-
}
1463-
$self.finalize_claims(updates.finalized_claimed_htlcs);
1464-
for failure in updates.failed_htlcs.drain(..) {
1465-
let receiver = HTLCDestination::NextHopChannel { node_id: Some(counterparty_node_id), channel_id };
1466-
$self.fail_htlc_backwards_internal(&failure.0, &failure.1, &failure.2, receiver);
1467-
}
1473+
handle_monitor_update_completion!($self, $update_id, $peer_state_lock, $peer_state, $chan);
14681474
}
14691475
Ok(())
14701476
},
@@ -4138,73 +4144,36 @@ where
41384144
fn channel_monitor_updated(&self, funding_txo: &OutPoint, highest_applied_update_id: u64, counterparty_node_id: Option<&PublicKey>) {
41394145
let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(&self.total_consistency_lock, &self.persistence_notifier);
41404146

4141-
let htlc_forwards;
4142-
let (mut pending_failures, finalized_claims, update_actions, counterparty_node_id) = {
4143-
let counterparty_node_id = match counterparty_node_id {
4144-
Some(cp_id) => cp_id.clone(),
4145-
None => {
4146-
// TODO: Once we can rely on the counterparty_node_id from the
4147-
// monitor event, this and the id_to_peer map should be removed.
4148-
let id_to_peer = self.id_to_peer.lock().unwrap();
4149-
match id_to_peer.get(&funding_txo.to_channel_id()) {
4150-
Some(cp_id) => cp_id.clone(),
4151-
None => return,
4152-
}
4153-
}
4154-
};
4155-
let per_peer_state = self.per_peer_state.read().unwrap();
4156-
let mut peer_state_lock;
4157-
let peer_state_mutex_opt = per_peer_state.get(&counterparty_node_id);
4158-
if peer_state_mutex_opt.is_none() { return }
4159-
peer_state_lock = peer_state_mutex_opt.unwrap().lock().unwrap();
4160-
let peer_state = &mut *peer_state_lock;
4161-
let mut channel = {
4162-
match peer_state.channel_by_id.entry(funding_txo.to_channel_id()){
4163-
hash_map::Entry::Occupied(chan) => chan,
4164-
hash_map::Entry::Vacant(_) => return,
4147+
let counterparty_node_id = match counterparty_node_id {
4148+
Some(cp_id) => cp_id.clone(),
4149+
None => {
4150+
// TODO: Once we can rely on the counterparty_node_id from the
4151+
// monitor event, this and the id_to_peer map should be removed.
4152+
let id_to_peer = self.id_to_peer.lock().unwrap();
4153+
match id_to_peer.get(&funding_txo.to_channel_id()) {
4154+
Some(cp_id) => cp_id.clone(),
4155+
None => return,
41654156
}
4166-
};
4167-
log_trace!(self.logger, "ChannelMonitor updated to {}. Current highest is {}",
4168-
highest_applied_update_id, channel.get().get_latest_monitor_update_id());
4169-
if !channel.get().is_awaiting_monitor_update() || channel.get().get_latest_monitor_update_id() != highest_applied_update_id {
4170-
return;
41714157
}
4172-
4173-
let updates = channel.get_mut().monitor_updating_restored(&self.logger, &self.node_signer, self.genesis_hash, &self.default_configuration, self.best_block.read().unwrap().height());
4174-
let channel_update = if updates.channel_ready.is_some() && channel.get().is_usable() {
4175-
// We only send a channel_update in the case where we are just now sending a
4176-
// channel_ready and the channel is in a usable state. We may re-send a
4177-
// channel_update later through the announcement_signatures process for public
4178-
// channels, but there's no reason not to just inform our counterparty of our fees
4179-
// now.
4180-
if let Ok(msg) = self.get_channel_update_for_unicast(channel.get()) {
4181-
Some(events::MessageSendEvent::SendChannelUpdate {
4182-
node_id: channel.get().get_counterparty_node_id(),
4183-
msg,
4184-
})
4185-
} else { None }
4186-
} else { None };
4187-
let update_actions = peer_state.monitor_update_blocked_actions
4188-
.remove(&channel.get().channel_id()).unwrap_or(Vec::new());
4189-
htlc_forwards = self.handle_channel_resumption(&mut peer_state.pending_msg_events,
4190-
channel.get_mut(), updates.raa, updates.commitment_update, updates.order,
4191-
updates.accepted_htlcs, updates.funding_broadcastable, updates.channel_ready,
4192-
updates.announcement_sigs);
4193-
if let Some(upd) = channel_update {
4194-
peer_state.pending_msg_events.push(upd);
4158+
};
4159+
let per_peer_state = self.per_peer_state.read().unwrap();
4160+
let mut peer_state_lock;
4161+
let peer_state_mutex_opt = per_peer_state.get(&counterparty_node_id);
4162+
if peer_state_mutex_opt.is_none() { return }
4163+
peer_state_lock = peer_state_mutex_opt.unwrap().lock().unwrap();
4164+
let peer_state = &mut *peer_state_lock;
4165+
let mut channel = {
4166+
match peer_state.channel_by_id.entry(funding_txo.to_channel_id()){
4167+
hash_map::Entry::Occupied(chan) => chan,
4168+
hash_map::Entry::Vacant(_) => return,
41954169
}
4196-
4197-
(updates.failed_htlcs, updates.finalized_claimed_htlcs, update_actions, counterparty_node_id)
41984170
};
4199-
self.handle_monitor_update_completion_actions(update_actions);
4200-
if let Some(forwards) = htlc_forwards {
4201-
self.forward_htlcs(&mut [forwards][..]);
4202-
}
4203-
self.finalize_claims(finalized_claims);
4204-
for failure in pending_failures.drain(..) {
4205-
let receiver = HTLCDestination::NextHopChannel { node_id: Some(counterparty_node_id), channel_id: funding_txo.to_channel_id() };
4206-
self.fail_htlc_backwards_internal(&failure.0, &failure.1, &failure.2, receiver);
4171+
log_trace!(self.logger, "ChannelMonitor updated to {}. Current highest is {}",
4172+
highest_applied_update_id, channel.get().get_latest_monitor_update_id());
4173+
if !channel.get().is_awaiting_monitor_update() || channel.get().get_latest_monitor_update_id() != highest_applied_update_id {
4174+
return;
42074175
}
4176+
handle_monitor_update_completion!(self, highest_applied_update_id, peer_state_lock, peer_state, channel.get_mut());
42084177
}
42094178

42104179
/// Accepts a request to open a channel after a [`Event::OpenChannelRequest`].

0 commit comments

Comments
 (0)