Skip to content

Commit d12ad9f

Browse files
committed
Move most handle_new_monitor_update calls to pass the update
Most of the calls to the `handle_new_monitor_update` macro had the exact same pattern - calling `update_monitor` followed by the macro. Given that common pattern will grow to first pushing the new monitor onto an in-flight set and then calling `update_monitor` unifying the pattern into a single macro now avoids more code churn in the coming commits.
1 parent 04dce6a commit d12ad9f

File tree

1 file changed

+22
-29
lines changed

1 file changed

+22
-29
lines changed

lightning/src/ln/channelmanager.rs

+22-29
Original file line numberDiff line numberDiff line change
@@ -1855,7 +1855,7 @@ macro_rules! handle_monitor_update_completion {
18551855
}
18561856

18571857
macro_rules! handle_new_monitor_update {
1858-
($self: ident, $update_res: expr, $update_id: expr, $peer_state_lock: expr, $peer_state: expr, $per_peer_state_lock: expr, $chan: expr, MANUALLY_REMOVING, $remove: expr) => { {
1858+
($self: ident, $update_res: expr, $update_id: expr, $peer_state_lock: expr, $peer_state: expr, $per_peer_state_lock: expr, $chan: expr, MANUALLY_REMOVING_ALREADY_APPLIED, $remove: expr) => { {
18591859
// update_maps_on_chan_removal needs to be able to take id_to_peer, so make sure we can in
18601860
// any case so that it won't deadlock.
18611861
debug_assert_ne!($self.id_to_peer.held_by_thread(), LockHeldState::HeldByThread);
@@ -1888,8 +1888,15 @@ macro_rules! handle_new_monitor_update {
18881888
},
18891889
}
18901890
} };
1891-
($self: ident, $update_res: expr, $update_id: expr, $peer_state_lock: expr, $peer_state: expr, $per_peer_state_lock: expr, $chan_entry: expr) => {
1892-
handle_new_monitor_update!($self, $update_res, $update_id, $peer_state_lock, $peer_state, $per_peer_state_lock, $chan_entry.get_mut(), MANUALLY_REMOVING, $chan_entry.remove_entry())
1891+
($self: ident, $update_res: expr, $update_id: expr, $peer_state_lock: expr, $peer_state: expr, $per_peer_state_lock: expr, $chan_entry: expr, ALREADY_APPLIED) => {
1892+
handle_new_monitor_update!($self, $update_res, $update_id, $peer_state_lock, $peer_state, $per_peer_state_lock, $chan_entry.get_mut(), MANUALLY_REMOVING_ALREADY_APPLIED, $chan_entry.remove_entry())
1893+
};
1894+
($self: ident, $funding_txo: expr, $update: expr, $peer_state_lock: expr, $peer_state: expr, $per_peer_state_lock: expr, $chan: expr, MANUALLY_REMOVING, $remove: expr) => { {
1895+
let update_res = $self.chain_monitor.update_channel($funding_txo, &$update);
1896+
handle_new_monitor_update!($self, update_res, $update.update_id, $peer_state_lock, $peer_state, $per_peer_state_lock, $chan, MANUALLY_REMOVING_ALREADY_APPLIED, $remove)
1897+
} };
1898+
($self: ident, $funding_txo: expr, $update: expr, $peer_state_lock: expr, $peer_state: expr, $per_peer_state_lock: expr, $chan_entry: expr) => {
1899+
handle_new_monitor_update!($self, $funding_txo, $update, $peer_state_lock, $peer_state, $per_peer_state_lock, $chan_entry.get_mut(), MANUALLY_REMOVING, $chan_entry.remove_entry())
18931900
}
18941901
}
18951902

@@ -2304,9 +2311,7 @@ where
23042311

23052312
// Update the monitor with the shutdown script if necessary.
23062313
if let Some(monitor_update) = monitor_update_opt.take() {
2307-
let update_id = monitor_update.update_id;
2308-
let update_res = self.chain_monitor.update_channel(funding_txo_opt.unwrap(), &monitor_update);
2309-
break handle_new_monitor_update!(self, update_res, update_id, peer_state_lock, peer_state, per_peer_state, chan_entry);
2314+
break handle_new_monitor_update!(self, funding_txo_opt.unwrap(), monitor_update, peer_state_lock, peer_state, per_peer_state, chan_entry);
23102315
}
23112316

23122317
if chan_entry.get().is_shutdown() {
@@ -2989,7 +2994,7 @@ where
29892994
Some(monitor_update) => {
29902995
let update_id = monitor_update.update_id;
29912996
let update_res = self.chain_monitor.update_channel(funding_txo, &monitor_update);
2992-
if let Err(e) = handle_new_monitor_update!(self, update_res, update_id, peer_state_lock, peer_state, per_peer_state, chan) {
2997+
if let Err(e) = handle_new_monitor_update!(self, update_res, update_id, peer_state_lock, peer_state, per_peer_state, chan, ALREADY_APPLIED) {
29932998
break Err(e);
29942999
}
29953000
if update_res == ChannelMonitorUpdateStatus::InProgress {
@@ -4027,7 +4032,7 @@ where
40274032
let peer_state = &mut *peer_state_lock;
40284033
match peer_state.channel_by_id.entry(funding_txo.to_channel_id()) {
40294034
hash_map::Entry::Occupied(mut chan) => {
4030-
handle_new_monitor_update!(self, update_res, update.update_id, peer_state_lock, peer_state, per_peer_state, chan)
4035+
handle_new_monitor_update!(self, update_res, update.update_id, peer_state_lock, peer_state, per_peer_state, chan, ALREADY_APPLIED)
40314036
},
40324037
hash_map::Entry::Vacant(_) => Ok(()),
40334038
}
@@ -4613,9 +4618,7 @@ where
46134618
log_bytes!(chan_id), action);
46144619
peer_state.monitor_update_blocked_actions.entry(chan_id).or_insert(Vec::new()).push(action);
46154620
}
4616-
let update_id = monitor_update.update_id;
4617-
let update_res = self.chain_monitor.update_channel(prev_hop.outpoint, &monitor_update);
4618-
let res = handle_new_monitor_update!(self, update_res, update_id, peer_state_lock,
4621+
let res = handle_new_monitor_update!(self, prev_hop.outpoint, monitor_update, peer_state_lock,
46194622
peer_state, per_peer_state, chan);
46204623
if let Err(e) = res {
46214624
// TODO: This is a *critical* error - we probably updated the outbound edge
@@ -5152,7 +5155,8 @@ where
51525155

51535156
let chan = e.insert(chan);
51545157
let mut res = handle_new_monitor_update!(self, monitor_res, 0, peer_state_lock, peer_state,
5155-
per_peer_state, chan, MANUALLY_REMOVING, { peer_state.channel_by_id.remove(&new_channel_id) });
5158+
per_peer_state, chan, MANUALLY_REMOVING_ALREADY_APPLIED,
5159+
{ peer_state.channel_by_id.remove(&new_channel_id) });
51565160

51575161
// Note that we reply with the new channel_id in error messages if we gave up on the
51585162
// channel, not the temporary_channel_id. This is compatible with ourselves, but the
@@ -5185,7 +5189,7 @@ where
51855189
let monitor = try_chan_entry!(self,
51865190
chan.get_mut().funding_signed(&msg, best_block, &self.signer_provider, &self.logger), chan);
51875191
let update_res = self.chain_monitor.watch_channel(chan.get().context.get_funding_txo().unwrap(), monitor);
5188-
let mut res = handle_new_monitor_update!(self, update_res, 0, peer_state_lock, peer_state, per_peer_state, chan);
5192+
let mut res = handle_new_monitor_update!(self, update_res, 0, peer_state_lock, peer_state, per_peer_state, chan, ALREADY_APPLIED);
51895193
if let Err(MsgHandleErrInternal { ref mut shutdown_finish, .. }) = res {
51905194
// We weren't able to watch the channel to begin with, so no updates should be made on
51915195
// it. Previously, full_stack_target found an (unreachable) panic when the
@@ -5282,9 +5286,7 @@ where
52825286

52835287
// Update the monitor with the shutdown script if necessary.
52845288
if let Some(monitor_update) = monitor_update_opt {
5285-
let update_id = monitor_update.update_id;
5286-
let update_res = self.chain_monitor.update_channel(funding_txo_opt.unwrap(), &monitor_update);
5287-
break handle_new_monitor_update!(self, update_res, update_id, peer_state_lock, peer_state, per_peer_state, chan_entry);
5289+
break handle_new_monitor_update!(self, funding_txo_opt.unwrap(), monitor_update, peer_state_lock, peer_state, per_peer_state, chan_entry);
52885290
}
52895291
break Ok(());
52905292
},
@@ -5474,9 +5476,7 @@ where
54745476
let funding_txo = chan.get().context.get_funding_txo();
54755477
let monitor_update_opt = try_chan_entry!(self, chan.get_mut().commitment_signed(&msg, &self.logger), chan);
54765478
if let Some(monitor_update) = monitor_update_opt {
5477-
let update_res = self.chain_monitor.update_channel(funding_txo.unwrap(), &monitor_update);
5478-
let update_id = monitor_update.update_id;
5479-
handle_new_monitor_update!(self, update_res, update_id, peer_state_lock,
5479+
handle_new_monitor_update!(self, funding_txo.unwrap(), monitor_update, peer_state_lock,
54805480
peer_state, per_peer_state, chan)
54815481
} else { Ok(()) }
54825482
},
@@ -5613,9 +5613,7 @@ where
56135613
let funding_txo = chan.get().context.get_funding_txo();
56145614
let (htlcs_to_fail, monitor_update_opt) = try_chan_entry!(self, chan.get_mut().revoke_and_ack(&msg, &self.logger), chan);
56155615
let res = if let Some(monitor_update) = monitor_update_opt {
5616-
let update_res = self.chain_monitor.update_channel(funding_txo.unwrap(), &monitor_update);
5617-
let update_id = monitor_update.update_id;
5618-
handle_new_monitor_update!(self, update_res, update_id,
5616+
handle_new_monitor_update!(self, funding_txo.unwrap(), monitor_update,
56195617
peer_state_lock, peer_state, per_peer_state, chan)
56205618
} else { Ok(()) };
56215619
(htlcs_to_fail, res)
@@ -5891,11 +5889,8 @@ where
58915889
if let Some(monitor_update) = monitor_opt {
58925890
has_monitor_update = true;
58935891

5894-
let update_res = self.chain_monitor.update_channel(
5895-
funding_txo.expect("channel is live"), &monitor_update);
5896-
let update_id = monitor_update.update_id;
58975892
let channel_id: [u8; 32] = *channel_id;
5898-
let res = handle_new_monitor_update!(self, update_res, update_id,
5893+
let res = handle_new_monitor_update!(self, funding_txo.unwrap(), monitor_update,
58995894
peer_state_lock, peer_state, per_peer_state, chan, MANUALLY_REMOVING,
59005895
peer_state.channel_by_id.remove(&channel_id));
59015896
if res.is_err() {
@@ -6237,9 +6232,7 @@ where
62376232
if let Some((monitor_update, further_update_exists)) = chan.get_mut().unblock_next_blocked_monitor_update() {
62386233
log_debug!(self.logger, "Unlocking monitor updating for channel {} and updating monitor",
62396234
log_bytes!(&channel_funding_outpoint.to_channel_id()[..]));
6240-
let update_res = self.chain_monitor.update_channel(channel_funding_outpoint, &monitor_update);
6241-
let update_id = monitor_update.update_id;
6242-
if let Err(e) = handle_new_monitor_update!(self, update_res, update_id,
6235+
if let Err(e) = handle_new_monitor_update!(self, channel_funding_outpoint, monitor_update,
62436236
peer_state_lck, peer_state, per_peer_state, chan)
62446237
{
62456238
errors.push((e, counterparty_node_id));

0 commit comments

Comments
 (0)