Skip to content

Commit c8c3322

Browse files
Add force_close_channel API endpoint and handler
Introduce ForceCloseChannelRequest and ForceCloseChannelResponse to the API, implement the handle_force_close_channel_request function, and register the new endpoint in the service router.
1 parent de7013e commit c8c3322

File tree

5 files changed

+61
-0
lines changed

5 files changed

+61
-0
lines changed

ldk-server-protos/src/api.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,26 @@ pub struct CloseChannelRequest {
286286
#[allow(clippy::derive_partial_eq_without_eq)]
287287
#[derive(Clone, PartialEq, ::prost::Message)]
288288
pub struct CloseChannelResponse {}
289+
/// Force closes the channel specified by given request.
290+
/// See more: <https://docs.rs/ldk-node/latest/ldk_node/struct.Node.html#method.force_close_channel>
291+
#[allow(clippy::derive_partial_eq_without_eq)]
292+
#[derive(Clone, PartialEq, ::prost::Message)]
293+
pub struct ForceCloseChannelRequest {
294+
/// The local `user_channel_id` of this channel.
295+
#[prost(string, tag = "1")]
296+
pub user_channel_id: ::prost::alloc::string::String,
297+
/// The hex-encoded public key of the node to close a channel with.
298+
#[prost(string, tag = "2")]
299+
pub counterparty_node_id: ::prost::alloc::string::String,
300+
/// The reason for force-closing, can only be set while force closing a channel.
301+
#[prost(string, optional, tag = "3")]
302+
pub force_close_reason: ::core::option::Option<::prost::alloc::string::String>,
303+
}
304+
/// The response `content` for the `ForceCloseChannel` API, when HttpStatusCode is OK (200).
305+
/// When HttpStatusCode is not OK (non-200), the response `content` contains a serialized `ErrorResponse`.
306+
#[allow(clippy::derive_partial_eq_without_eq)]
307+
#[derive(Clone, PartialEq, ::prost::Message)]
308+
pub struct ForceCloseChannelResponse {}
289309
/// Returns a list of known channels.
290310
/// See more: <https://docs.rs/ldk-node/latest/ldk_node/struct.Node.html#method.list_channels>
291311
#[allow(clippy::derive_partial_eq_without_eq)]

ldk-server-protos/src/proto/api.proto

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,20 @@ message CloseChannelRequest {
275275
// When HttpStatusCode is not OK (non-200), the response `content` contains a serialized `ErrorResponse`.
276276
message CloseChannelResponse {}
277277

278+
// Force closes the channel specified by given request.
279+
// See more: https://docs.rs/ldk-node/latest/ldk_node/struct.Node.html#method.force_close_channel
280+
message ForceCloseChannelRequest {
281+
// The local `user_channel_id` of this channel.
282+
string user_channel_id = 1;
283+
// The hex-encoded public key of the node to close a channel with.
284+
string counterparty_node_id = 2;
285+
// The reason for force-closing, can only be set while force closing a channel.
286+
optional string force_close_reason = 3;
287+
}
278288

289+
/ The response `content` for the `ForceCloseChannel` API, when HttpStatusCode is OK (200).
290+
// When HttpStatusCode is not OK (non-200), the response `content` contains a serialized `ErrorResponse`.
291+
message ForceCloseChannelResponse {}
279292

280293
// Returns a list of known channels.
281294
// See more: https://docs.rs/ldk-node/latest/ldk_node/struct.Node.html#method.list_channels
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
use crate::api::{error::LdkServerError, parse_counterparty_node_id, parse_user_channel_id};
2+
use crate::service::Context;
3+
use ldk_server_protos::api::{ForceCloseChannelRequest, ForceCloseChannelResponse};
4+
5+
pub(crate) const FORCE_CLOSE_CHANNEL_PATH: &str = "ForceCloseChannel";
6+
7+
pub(crate) fn handle_force_close_channel_request(
8+
context: Context, request: ForceCloseChannelRequest,
9+
) -> Result<ForceCloseChannelResponse, LdkServerError> {
10+
let user_channel_id = parse_user_channel_id(&request.user_channel_id)?;
11+
let counterparty_node_id = parse_counterparty_node_id(&request.counterparty_node_id)?;
12+
13+
context.node.force_close_channel(
14+
&user_channel_id,
15+
counterparty_node_id,
16+
request.force_close_reason,
17+
)?;
18+
19+
let response = ForceCloseChannelResponse {};
20+
Ok(response)
21+
}

ldk-server/src/api/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ pub(crate) mod bolt12_receive;
44
pub(crate) mod bolt12_send;
55
pub(crate) mod close_channel;
66
pub(crate) mod error;
7+
pub(crate) mod force_close_channel;
78
pub(crate) mod get_balances;
89
pub(crate) mod get_node_info;
910
pub(crate) mod get_payment_details;

ldk-server/src/service.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ use crate::api::bolt12_send::{handle_bolt12_send_request, BOLT12_SEND_PATH};
1414
use crate::api::close_channel::{handle_close_channel_request, CLOSE_CHANNEL_PATH};
1515
use crate::api::error::LdkServerError;
1616
use crate::api::error::LdkServerErrorCode::InvalidRequestError;
17+
use crate::api::force_close_channel::{
18+
handle_force_close_channel_request, FORCE_CLOSE_CHANNEL_PATH,
19+
};
1720
use crate::api::get_balances::{handle_get_balances_request, GET_BALANCES};
1821
use crate::api::get_node_info::{handle_get_node_info_request, GET_NODE_INFO};
1922
use crate::api::get_payment_details::{
@@ -85,6 +88,9 @@ impl Service<Request<Incoming>> for NodeService {
8588
CLOSE_CHANNEL_PATH => {
8689
Box::pin(handle_request(context, req, handle_close_channel_request))
8790
},
91+
FORCE_CLOSE_CHANNEL_PATH => {
92+
Box::pin(handle_request(context, req, handle_force_close_channel_request))
93+
},
8894
LIST_CHANNELS_PATH => {
8995
Box::pin(handle_request(context, req, handle_list_channels_request))
9096
},

0 commit comments

Comments
 (0)