@@ -1360,7 +1360,8 @@ where
1360
1360
/// Event::OpenChannelRequest { temporary_channel_id, counterparty_node_id, .. } => {
1361
1361
/// if !is_trusted(counterparty_node_id) {
1362
1362
/// match channel_manager.force_close_without_broadcasting_txn(
1363
- /// &temporary_channel_id, &counterparty_node_id
1363
+ /// &temporary_channel_id, &counterparty_node_id, false, Some("Untrusted
1364
+ /// counterparty node id")
1364
1365
/// ) {
1365
1366
/// Ok(()) => println!("Rejecting channel {}", temporary_channel_id),
1366
1367
/// Err(e) => println!("Error rejecting channel {}: {:?}", temporary_channel_id, e),
@@ -3700,19 +3701,28 @@ where
3700
3701
Ok(counterparty_node_id)
3701
3702
}
3702
3703
3703
- fn force_close_sending_error(&self, channel_id: &ChannelId, counterparty_node_id: &PublicKey, broadcast: bool) -> Result<(), APIError> {
3704
+ fn force_close_sending_error(
3705
+ &self, channel_id: &ChannelId, counterparty_node_id: &PublicKey, broadcast: bool, disconnect_peer: bool, err_msg: Option<&str>
3706
+ ) -> Result<(), APIError> {
3704
3707
let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(self);
3705
3708
match self.force_close_channel_with_peer(channel_id, counterparty_node_id, None, broadcast) {
3706
3709
Ok(counterparty_node_id) => {
3707
3710
let per_peer_state = self.per_peer_state.read().unwrap();
3708
3711
if let Some(peer_state_mutex) = per_peer_state.get(&counterparty_node_id) {
3709
3712
let mut peer_state = peer_state_mutex.lock().unwrap();
3713
+ let msg = msgs::ErrorMessage {
3714
+ channel_id: *channel_id,
3715
+ data: err_msg.unwrap_or("Channel force-closed").to_owned()
3716
+ };
3717
+ let action = if disconnect_peer {
3718
+ msgs::ErrorAction::DisconnectPeer { msg: Some(msg) }
3719
+ } else {
3720
+ msgs::ErrorAction::SendErrorMessage { msg }
3721
+ };
3710
3722
peer_state.pending_msg_events.push(
3711
3723
events::MessageSendEvent::HandleError {
3712
3724
node_id: counterparty_node_id,
3713
- action: msgs::ErrorAction::DisconnectPeer {
3714
- msg: Some(msgs::ErrorMessage { channel_id: *channel_id, data: "Channel force-closed".to_owned() })
3715
- },
3725
+ action,
3716
3726
}
3717
3727
);
3718
3728
}
@@ -3726,35 +3736,41 @@ where
3726
3736
/// rejecting new HTLCs on the given channel. Fails if `channel_id` is unknown to
3727
3737
/// the manager, or if the `counterparty_node_id` isn't the counterparty of the corresponding
3728
3738
/// channel.
3729
- pub fn force_close_broadcasting_latest_txn(&self, channel_id: &ChannelId, counterparty_node_id: &PublicKey)
3730
- -> Result<(), APIError> {
3731
- self.force_close_sending_error(channel_id, counterparty_node_id, true)
3739
+ ///
3740
+ /// By default, this will send a generic force close error message to the peer if `err_msg`
3741
+ /// is not specified. If `disconnect_peer` is set to true, the peer will also be disconnected.
3742
+ pub fn force_close_broadcasting_latest_txn(
3743
+ &self, channel_id: &ChannelId, counterparty_node_id: &PublicKey, disconnect_peer: bool, err_msg: Option<&str>
3744
+ ) -> Result<(), APIError> {
3745
+ self.force_close_sending_error(channel_id, counterparty_node_id, true, disconnect_peer, err_msg)
3732
3746
}
3733
3747
3734
3748
/// Force closes a channel, rejecting new HTLCs on the given channel but skips broadcasting
3735
3749
/// the latest local transaction(s). Fails if `channel_id` is unknown to the manager, or if the
3736
3750
/// `counterparty_node_id` isn't the counterparty of the corresponding channel.
3737
3751
///
3738
3752
/// You can always broadcast the latest local transaction(s) via
3739
- /// [`ChannelMonitor::broadcast_latest_holder_commitment_txn`].
3740
- pub fn force_close_without_broadcasting_txn(&self, channel_id: &ChannelId, counterparty_node_id: &PublicKey)
3741
- -> Result<(), APIError> {
3742
- self.force_close_sending_error(channel_id, counterparty_node_id, false)
3753
+ /// [`ChannelMonitor::broadcast_latest_holder_commitment_txn`]. Similarly, you can specify
3754
+ /// whether to disconnect the peer, as well as a custom error message to send to the peer.
3755
+ pub fn force_close_without_broadcasting_txn(
3756
+ &self, channel_id: &ChannelId, counterparty_node_id: &PublicKey, disconnect_peer: bool, err_msg: Option<&str>
3757
+ ) -> Result<(), APIError> {
3758
+ self.force_close_sending_error(channel_id, counterparty_node_id, false, disconnect_peer, err_msg)
3743
3759
}
3744
3760
3745
3761
/// Force close all channels, immediately broadcasting the latest local commitment transaction
3746
3762
/// for each to the chain and rejecting new HTLCs on each.
3747
3763
pub fn force_close_all_channels_broadcasting_latest_txn(&self) {
3748
3764
for chan in self.list_channels() {
3749
- let _ = self.force_close_broadcasting_latest_txn(&chan.channel_id, &chan.counterparty.node_id);
3765
+ let _ = self.force_close_broadcasting_latest_txn(&chan.channel_id, &chan.counterparty.node_id, true, None );
3750
3766
}
3751
3767
}
3752
3768
3753
3769
/// Force close all channels rejecting new HTLCs on each but without broadcasting the latest
3754
3770
/// local transaction(s).
3755
3771
pub fn force_close_all_channels_without_broadcasting_txn(&self) {
3756
3772
for chan in self.list_channels() {
3757
- let _ = self.force_close_without_broadcasting_txn(&chan.channel_id, &chan.counterparty.node_id);
3773
+ let _ = self.force_close_without_broadcasting_txn(&chan.channel_id, &chan.counterparty.node_id, true, None );
3758
3774
}
3759
3775
}
3760
3776
@@ -12970,7 +12986,7 @@ mod tests {
12970
12986
nodes[0].node.peer_disconnected(&nodes[1].node.get_our_node_id());
12971
12987
nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id());
12972
12988
12973
- nodes[0].node.force_close_broadcasting_latest_txn(&chan.2, &nodes[1].node.get_our_node_id()).unwrap();
12989
+ nodes[0].node.force_close_broadcasting_latest_txn(&chan.2, &nodes[1].node.get_our_node_id(), true, None ).unwrap();
12974
12990
check_closed_broadcast!(nodes[0], true);
12975
12991
check_added_monitors!(nodes[0], 1);
12976
12992
check_closed_event!(nodes[0], 1, ClosureReason::HolderForceClosed, [nodes[1].node.get_our_node_id()], 100000);
@@ -13195,9 +13211,9 @@ mod tests {
13195
13211
13196
13212
check_unkown_peer_error(nodes[0].node.close_channel(&channel_id, &unkown_public_key), unkown_public_key);
13197
13213
13198
- check_unkown_peer_error(nodes[0].node.force_close_broadcasting_latest_txn(&channel_id, &unkown_public_key), unkown_public_key);
13214
+ check_unkown_peer_error(nodes[0].node.force_close_broadcasting_latest_txn(&channel_id, &unkown_public_key, true, None ), unkown_public_key);
13199
13215
13200
- check_unkown_peer_error(nodes[0].node.force_close_without_broadcasting_txn(&channel_id, &unkown_public_key), unkown_public_key);
13216
+ check_unkown_peer_error(nodes[0].node.force_close_without_broadcasting_txn(&channel_id, &unkown_public_key, true, None ), unkown_public_key);
13201
13217
13202
13218
check_unkown_peer_error(nodes[0].node.forward_intercepted_htlc(intercept_id, &channel_id, unkown_public_key, 1_000_000), unkown_public_key);
13203
13219
@@ -13225,9 +13241,9 @@ mod tests {
13225
13241
13226
13242
check_channel_unavailable_error(nodes[0].node.close_channel(&channel_id, &counterparty_node_id), channel_id, counterparty_node_id);
13227
13243
13228
- check_channel_unavailable_error(nodes[0].node.force_close_broadcasting_latest_txn(&channel_id, &counterparty_node_id), channel_id, counterparty_node_id);
13244
+ check_channel_unavailable_error(nodes[0].node.force_close_broadcasting_latest_txn(&channel_id, &counterparty_node_id, true, None ), channel_id, counterparty_node_id);
13229
13245
13230
- check_channel_unavailable_error(nodes[0].node.force_close_without_broadcasting_txn(&channel_id, &counterparty_node_id), channel_id, counterparty_node_id);
13246
+ check_channel_unavailable_error(nodes[0].node.force_close_without_broadcasting_txn(&channel_id, &counterparty_node_id, true, None ), channel_id, counterparty_node_id);
13231
13247
13232
13248
check_channel_unavailable_error(nodes[0].node.forward_intercepted_htlc(InterceptId([0; 32]), &channel_id, counterparty_node_id, 1_000_000), channel_id, counterparty_node_id);
13233
13249
@@ -13590,7 +13606,7 @@ mod tests {
13590
13606
let events = nodes[1].node.get_and_clear_pending_events();
13591
13607
match events[0] {
13592
13608
Event::OpenChannelRequest { temporary_channel_id, .. } => {
13593
- nodes[1].node.force_close_broadcasting_latest_txn(&temporary_channel_id, &nodes[0].node.get_our_node_id()).unwrap();
13609
+ nodes[1].node.force_close_broadcasting_latest_txn(&temporary_channel_id, &nodes[0].node.get_our_node_id(), true, None ).unwrap();
13594
13610
}
13595
13611
_ => panic!("Unexpected event"),
13596
13612
}
@@ -13703,7 +13719,7 @@ mod tests {
13703
13719
let (_, _, chan_id, funding_tx) = create_announced_chan_between_nodes(&nodes, 0, 1);
13704
13720
nodes[0].node.peer_disconnected(&nodes[1].node.get_our_node_id());
13705
13721
nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id());
13706
- nodes[0].node.force_close_broadcasting_latest_txn(&chan_id, &nodes[1].node.get_our_node_id()).unwrap();
13722
+ nodes[0].node.force_close_broadcasting_latest_txn(&chan_id, &nodes[1].node.get_our_node_id(), true, None ).unwrap();
13707
13723
check_closed_broadcast(&nodes[0], 1, true);
13708
13724
check_added_monitors(&nodes[0], 1);
13709
13725
check_closed_event!(nodes[0], 1, ClosureReason::HolderForceClosed, [nodes[1].node.get_our_node_id()], 100000);
0 commit comments