Skip to content

Commit ffda105

Browse files
committed
Refactor to use helper function to issue channel closing events
1 parent 57b5b4e commit ffda105

File tree

1 file changed

+15
-20
lines changed

1 file changed

+15
-20
lines changed

lightning/src/ln/channelmanager.rs

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1326,6 +1326,12 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
13261326
self.list_channels_with_filter(|&(_, ref channel)| channel.is_live())
13271327
}
13281328

1329+
/// Helper function that issues the channel close events
1330+
fn issue_channel_close_events(&self, channel: &Channel<Signer>, closure_reason: ClosureReason) {
1331+
let mut pending_events_lock = self.pending_events.lock().unwrap();
1332+
pending_events_lock.push(events::Event::ChannelClosed { channel_id: channel.channel_id(), reason: closure_reason });
1333+
}
1334+
13291335
fn close_channel_internal(&self, channel_id: &[u8; 32], target_feerate_sats_per_1000_weight: Option<u32>) -> Result<(), APIError> {
13301336
let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(&self.total_consistency_lock, &self.persistence_notifier);
13311337

@@ -1372,12 +1378,7 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
13721378
msg: channel_update
13731379
});
13741380
}
1375-
if let Ok(mut pending_events_lock) = self.pending_events.lock() {
1376-
pending_events_lock.push(events::Event::ChannelClosed {
1377-
channel_id: *channel_id,
1378-
reason: ClosureReason::HolderForceClosed
1379-
});
1380-
}
1381+
self.issue_channel_close_events(&channel, ClosureReason::HolderForceClosed);
13811382
}
13821383
break Ok(());
13831384
},
@@ -1468,13 +1469,12 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
14681469
if let Some(short_id) = chan.get().get_short_channel_id() {
14691470
channel_state.short_to_id.remove(&short_id);
14701471
}
1471-
let mut pending_events_lock = self.pending_events.lock().unwrap();
14721472
if peer_node_id.is_some() {
14731473
if let Some(peer_msg) = peer_msg {
1474-
pending_events_lock.push(events::Event::ChannelClosed { channel_id: *channel_id, reason: ClosureReason::CounterpartyForceClosed { peer_msg: peer_msg.to_string() } });
1474+
self.issue_channel_close_events(chan.get(),ClosureReason::CounterpartyForceClosed { peer_msg: peer_msg.to_string() });
14751475
}
14761476
} else {
1477-
pending_events_lock.push(events::Event::ChannelClosed { channel_id: *channel_id, reason: ClosureReason::HolderForceClosed });
1477+
self.issue_channel_close_events(chan.get(),ClosureReason::HolderForceClosed);
14781478
}
14791479
chan.remove_entry().1
14801480
} else {
@@ -3574,7 +3574,7 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
35743574
msg: update
35753575
});
35763576
}
3577-
self.pending_events.lock().unwrap().push(events::Event::ChannelClosed { channel_id: msg.channel_id, reason: ClosureReason::CooperativeClosure });
3577+
self.issue_channel_close_events(&chan, ClosureReason::CooperativeClosure);
35783578
}
35793579
Ok(())
35803580
}
@@ -3986,7 +3986,7 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
39863986
msg: update
39873987
});
39883988
}
3989-
self.pending_events.lock().unwrap().push(events::Event::ChannelClosed { channel_id: chan.channel_id(), reason: ClosureReason::CommitmentTxConfirmed });
3989+
self.issue_channel_close_events(&chan, ClosureReason::CommitmentTxConfirmed);
39903990
pending_msg_events.push(events::MessageSendEvent::HandleError {
39913991
node_id: chan.get_counterparty_node_id(),
39923992
action: msgs::ErrorAction::SendErrorMessage {
@@ -4102,12 +4102,7 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
41024102
});
41034103
}
41044104

4105-
if let Ok(mut pending_events_lock) = self.pending_events.lock() {
4106-
pending_events_lock.push(events::Event::ChannelClosed {
4107-
channel_id: *channel_id,
4108-
reason: ClosureReason::CooperativeClosure
4109-
});
4110-
}
4105+
self.issue_channel_close_events(chan, ClosureReason::CooperativeClosure);
41114106

41124107
log_info!(self.logger, "Broadcasting {}", log_tx!(tx));
41134108
self.tx_broadcaster.broadcast_transaction(&tx);
@@ -4529,7 +4524,7 @@ where
45294524
msg: update
45304525
});
45314526
}
4532-
self.pending_events.lock().unwrap().push(events::Event::ChannelClosed { channel_id: channel.channel_id(), reason: ClosureReason::CommitmentTxConfirmed });
4527+
self.issue_channel_close_events(channel, ClosureReason::CommitmentTxConfirmed);
45334528
pending_msg_events.push(events::MessageSendEvent::HandleError {
45344529
node_id: channel.get_counterparty_node_id(),
45354530
action: msgs::ErrorAction::SendErrorMessage { msg: e },
@@ -4720,7 +4715,7 @@ impl<Signer: Sign, M: Deref , T: Deref , K: Deref , F: Deref , L: Deref >
47204715
msg: update
47214716
});
47224717
}
4723-
self.pending_events.lock().unwrap().push(events::Event::ChannelClosed { channel_id: chan.channel_id(), reason: ClosureReason::DisconnectedPeer });
4718+
self.issue_channel_close_events(chan, ClosureReason::DisconnectedPeer);
47244719
false
47254720
} else {
47264721
true
@@ -4735,7 +4730,7 @@ impl<Signer: Sign, M: Deref , T: Deref , K: Deref , F: Deref , L: Deref >
47354730
if let Some(short_id) = chan.get_short_channel_id() {
47364731
short_to_id.remove(&short_id);
47374732
}
4738-
self.pending_events.lock().unwrap().push(events::Event::ChannelClosed { channel_id: chan.channel_id(), reason: ClosureReason::DisconnectedPeer });
4733+
self.issue_channel_close_events(chan, ClosureReason::DisconnectedPeer);
47394734
return false;
47404735
} else {
47414736
no_channels_remain = false;

0 commit comments

Comments
 (0)