@@ -2616,6 +2616,8 @@ where
2616
2616
// it does not exist for this peer. Either way, we can attempt to force-close it.
2617
2617
//
2618
2618
// An appropriate error will be returned for non-existence of the channel if that's the case.
2619
+ mem::drop(peer_state_lock);
2620
+ mem::drop(per_peer_state);
2619
2621
return self.force_close_channel_with_peer(&channel_id, counterparty_node_id, None, false).map(|_| ())
2620
2622
},
2621
2623
}
@@ -4001,7 +4003,7 @@ where
4001
4003
for channel_id in channel_ids {
4002
4004
if !peer_state.has_channel(channel_id) {
4003
4005
return Err(APIError::ChannelUnavailable {
4004
- err: format!("Channel with ID {} was not found for the passed counterparty_node_id {}", channel_id, counterparty_node_id),
4006
+ err: format!("Channel with id {} not found for the passed counterparty node_id {}", channel_id, counterparty_node_id),
4005
4007
});
4006
4008
};
4007
4009
}
@@ -4112,7 +4114,7 @@ where
4112
4114
next_hop_channel_id, next_node_id)
4113
4115
}),
4114
4116
None => return Err(APIError::ChannelUnavailable {
4115
- err: format!("Channel with id {} not found for the passed counterparty node_id {}. ",
4117
+ err: format!("Channel with id {} not found for the passed counterparty node_id {}",
4116
4118
next_hop_channel_id, next_node_id)
4117
4119
})
4118
4120
}
@@ -10750,6 +10752,16 @@ mod tests {
10750
10752
check_api_error_message(expected_message, res_err)
10751
10753
}
10752
10754
10755
+ fn check_channel_unavailable_error<T>(res_err: Result<T, APIError>, expected_channel_id: ChannelId, peer_node_id: PublicKey) {
10756
+ let expected_message = format!("Channel with id {} not found for the passed counterparty node_id {}", expected_channel_id, peer_node_id);
10757
+ check_api_error_message(expected_message, res_err)
10758
+ }
10759
+
10760
+ fn check_api_misuse_error<T>(res_err: Result<T, APIError>) {
10761
+ let expected_message = "No such channel awaiting to be accepted.".to_string();
10762
+ check_api_error_message(expected_message, res_err)
10763
+ }
10764
+
10753
10765
fn check_api_error_message<T>(expected_err_message: String, res_err: Result<T, APIError>) {
10754
10766
match res_err {
10755
10767
Err(APIError::APIMisuseError { err }) => {
@@ -10794,6 +10806,36 @@ mod tests {
10794
10806
check_unkown_peer_error(nodes[0].node.update_channel_config(&unkown_public_key, &[channel_id], &ChannelConfig::default()), unkown_public_key);
10795
10807
}
10796
10808
10809
+ #[test]
10810
+ fn test_api_calls_with_unavailable_channel() {
10811
+ // Tests that our API functions that expects a `counterparty_node_id` and a `channel_id`
10812
+ // as input, behaves as expected if the `counterparty_node_id` is a known peer in the
10813
+ // `ChannelManager::per_peer_state` map, but the peer state doesn't contain a channel with
10814
+ // the given `channel_id`.
10815
+ let chanmon_cfg = create_chanmon_cfgs(2);
10816
+ let node_cfg = create_node_cfgs(2, &chanmon_cfg);
10817
+ let node_chanmgr = create_node_chanmgrs(2, &node_cfg, &[None, None]);
10818
+ let nodes = create_network(2, &node_cfg, &node_chanmgr);
10819
+
10820
+ let counterparty_node_id = nodes[1].node.get_our_node_id();
10821
+
10822
+ // Dummy values
10823
+ let channel_id = ChannelId::from_bytes([4; 32]);
10824
+
10825
+ // Test the API functions.
10826
+ check_api_misuse_error(nodes[0].node.accept_inbound_channel(&channel_id, &counterparty_node_id, 42));
10827
+
10828
+ check_channel_unavailable_error(nodes[0].node.close_channel(&channel_id, &counterparty_node_id), channel_id, counterparty_node_id);
10829
+
10830
+ check_channel_unavailable_error(nodes[0].node.force_close_broadcasting_latest_txn(&channel_id, &counterparty_node_id), channel_id, counterparty_node_id);
10831
+
10832
+ check_channel_unavailable_error(nodes[0].node.force_close_without_broadcasting_txn(&channel_id, &counterparty_node_id), channel_id, counterparty_node_id);
10833
+
10834
+ 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);
10835
+
10836
+ check_channel_unavailable_error(nodes[0].node.update_channel_config(&counterparty_node_id, &[channel_id], &ChannelConfig::default()), channel_id, counterparty_node_id);
10837
+ }
10838
+
10797
10839
#[test]
10798
10840
fn test_connection_limiting() {
10799
10841
// Test that we limit un-channel'd peers and un-funded channels properly.
0 commit comments