Skip to content

Commit e10671e

Browse files
committed
Rewrite ChannelManager::timer_tick_occurred
Exposing ChannelPhase in ChannelManager has led to verbose match statements, which need to be modified each time a ChannelPhase is added. Making ChannelPhase an implementation detail of Channel would help avoid this. As a step in this direction, update ChannelManager::timer_tick_occurred to use ChannelPhase::as_funded_mut and a new ChannelPhase::unfunded_context_mut method.
1 parent 44e62dd commit e10671e

File tree

2 files changed

+37
-38
lines changed

2 files changed

+37
-38
lines changed

lightning/src/ln/channel.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1156,6 +1156,15 @@ impl<'a, SP: Deref> ChannelPhase<SP> where
11561156
}
11571157
}
11581158

1159+
pub fn unfunded_context_mut(&mut self) -> Option<&mut UnfundedChannelContext> {
1160+
match self {
1161+
ChannelPhase::Funded(_) => { debug_assert!(false); None },
1162+
ChannelPhase::UnfundedOutboundV1(chan) => Some(&mut chan.unfunded_context),
1163+
ChannelPhase::UnfundedInboundV1(chan) => Some(&mut chan.unfunded_context),
1164+
ChannelPhase::UnfundedV2(chan) => Some(&mut chan.unfunded_context),
1165+
}
1166+
}
1167+
11591168
pub fn is_funded(&self) -> bool {
11601169
self.as_funded().is_some()
11611170
}

lightning/src/ln/channelmanager.rs

Lines changed: 28 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -6429,34 +6429,6 @@ where
64296429
let mut pending_peers_awaiting_removal = Vec::new();
64306430
let mut shutdown_channels = Vec::new();
64316431

6432-
macro_rules! process_unfunded_channel_tick {
6433-
($peer_state: expr, $chan: expr, $pending_msg_events: expr) => { {
6434-
let context = &mut $chan.context;
6435-
context.maybe_expire_prev_config();
6436-
if $chan.unfunded_context.should_expire_unfunded_channel() {
6437-
let logger = WithChannelContext::from(&self.logger, context, None);
6438-
log_error!(logger,
6439-
"Force-closing pending channel with ID {} for not establishing in a timely manner",
6440-
context.channel_id());
6441-
let mut close_res = context.force_shutdown(false, ClosureReason::HolderForceClosed { broadcasted_latest_txn: Some(false) });
6442-
locked_close_channel!(self, $peer_state, context, close_res);
6443-
shutdown_channels.push(close_res);
6444-
$pending_msg_events.push(MessageSendEvent::HandleError {
6445-
node_id: context.get_counterparty_node_id(),
6446-
action: msgs::ErrorAction::SendErrorMessage {
6447-
msg: msgs::ErrorMessage {
6448-
channel_id: context.channel_id(),
6449-
data: "Force-closing pending channel due to timeout awaiting establishment handshake".to_owned(),
6450-
},
6451-
},
6452-
});
6453-
false
6454-
} else {
6455-
true
6456-
}
6457-
} }
6458-
}
6459-
64606432
{
64616433
let per_peer_state = self.per_peer_state.read().unwrap();
64626434
for (counterparty_node_id, peer_state_mutex) in per_peer_state.iter() {
@@ -6465,8 +6437,8 @@ where
64656437
let pending_msg_events = &mut peer_state.pending_msg_events;
64666438
let counterparty_node_id = *counterparty_node_id;
64676439
peer_state.channel_by_id.retain(|chan_id, phase| {
6468-
match phase {
6469-
ChannelPhase::Funded(chan) => {
6440+
match phase.as_funded_mut() {
6441+
Some(chan) => {
64706442
let new_feerate = if chan.context.get_channel_type().supports_anchors_zero_fee_htlc_tx() {
64716443
anchor_feerate
64726444
} else {
@@ -6540,14 +6512,32 @@ where
65406512

65416513
true
65426514
},
6543-
ChannelPhase::UnfundedInboundV1(chan) => {
6544-
process_unfunded_channel_tick!(peer_state, chan, pending_msg_events)
6545-
},
6546-
ChannelPhase::UnfundedOutboundV1(chan) => {
6547-
process_unfunded_channel_tick!(peer_state, chan, pending_msg_events)
6548-
},
6549-
ChannelPhase::UnfundedV2(chan) => {
6550-
process_unfunded_channel_tick!(peer_state, chan, pending_msg_events)
6515+
None => {
6516+
phase.context_mut().maybe_expire_prev_config();
6517+
let unfunded_context = phase.unfunded_context_mut().expect("channel should be unfunded");
6518+
if unfunded_context.should_expire_unfunded_channel() {
6519+
let context = phase.context();
6520+
let logger = WithChannelContext::from(&self.logger, context, None);
6521+
log_error!(logger,
6522+
"Force-closing pending channel with ID {} for not establishing in a timely manner",
6523+
context.channel_id());
6524+
let context = phase.context_mut();
6525+
let mut close_res = context.force_shutdown(false, ClosureReason::HolderForceClosed { broadcasted_latest_txn: Some(false) });
6526+
locked_close_channel!(self, peer_state, context, close_res);
6527+
shutdown_channels.push(close_res);
6528+
pending_msg_events.push(MessageSendEvent::HandleError {
6529+
node_id: context.get_counterparty_node_id(),
6530+
action: msgs::ErrorAction::SendErrorMessage {
6531+
msg: msgs::ErrorMessage {
6532+
channel_id: context.channel_id(),
6533+
data: "Force-closing pending channel due to timeout awaiting establishment handshake".to_owned(),
6534+
},
6535+
},
6536+
});
6537+
false
6538+
} else {
6539+
true
6540+
}
65516541
},
65526542
}
65536543
});

0 commit comments

Comments
 (0)