Skip to content

Commit 2035f63

Browse files
committed
f Introduce force_close_channel method
1 parent f4a381a commit 2035f63

File tree

6 files changed

+31
-14
lines changed

6 files changed

+31
-14
lines changed

bindings/kotlin/ldk-node-jvm/lib/src/test/kotlin/org/lightningdevkit/ldknode/LibraryTest.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ class LibraryTest {
239239
assert(node1.listPayments().size == 1)
240240
assert(node2.listPayments().size == 1)
241241

242-
node2.closeChannel(userChannelId, nodeId1, false)
242+
node2.closeChannel(userChannelId, nodeId1)
243243

244244
val channelClosedEvent1 = node1.waitNextEvent()
245245
println("Got event: $channelClosedEvent1")

bindings/ldk_node.udl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,9 @@ interface Node {
7070
[Throws=NodeError]
7171
UserChannelId connect_open_channel(PublicKey node_id, SocketAddress address, u64 channel_amount_sats, u64? push_to_counterparty_msat, ChannelConfig? channel_config, boolean announce_channel);
7272
[Throws=NodeError]
73-
void close_channel([ByRef]UserChannelId user_channel_id, PublicKey counterparty_node_id, boolean force);
73+
void close_channel([ByRef]UserChannelId user_channel_id, PublicKey counterparty_node_id);
74+
[Throws=NodeError]
75+
void force_close_channel([ByRef]UserChannelId user_channel_id, PublicKey counterparty_node_id);
7476
[Throws=NodeError]
7577
void update_channel_config([ByRef]UserChannelId user_channel_id, PublicKey counterparty_node_id, ChannelConfig channel_config);
7678
[Throws=NodeError]

bindings/python/src/ldk_node/test_ldk_node.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ def test_channel_full_cycle(self):
198198
print("EVENT:", payment_received_event_2)
199199
node_2.event_handled()
200200

201-
node_2.close_channel(channel_ready_event_2.user_channel_id, node_id_1, False)
201+
node_2.close_channel(channel_ready_event_2.user_channel_id, node_id_1)
202202

203203
channel_closed_event_1 = node_1.wait_next_event()
204204
assert isinstance(channel_closed_event_1, Event.CHANNEL_CLOSED)

src/lib.rs

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1177,16 +1177,32 @@ impl Node {
11771177

11781178
/// Close a previously opened channel.
11791179
///
1180-
/// If `force` is set to `true`, we will force-close the channel, potentially broadcasting our
1181-
/// latest state. Note that in contrast to cooperative closure, force-closing will have the
1182-
/// channel funds time-locked, i.e., they will only be available after the counterparty had
1183-
/// time to contest our claim. Force-closing channels also more costly in terms of on-chain
1184-
/// fees. So cooperative closure should always be preferred (and tried first).
1180+
/// Will attempt to close a channel coopertively. If this fails, users might need to resort to
1181+
/// [`Node::force_close_channel`].
1182+
pub fn close_channel(
1183+
&self, user_channel_id: &UserChannelId, counterparty_node_id: PublicKey,
1184+
) -> Result<(), Error> {
1185+
self.close_channel_internal(user_channel_id, counterparty_node_id, false)
1186+
}
1187+
1188+
/// Force-close a previously opened channel.
1189+
///
1190+
/// Will force-close the channel, potentially broadcasting our latest state. Note that in
1191+
/// contrast to cooperative closure, force-closing will have the channel funds time-locked,
1192+
/// i.e., they will only be available after the counterparty had time to contest our claim.
1193+
/// Force-closing channels also more costly in terms of on-chain fees. So cooperative closure
1194+
/// should always be preferred (and tried first).
11851195
///
11861196
/// Broadcasting the closing transactions will be omitted for Anchor channels if we trust the
11871197
/// counterparty to broadcast for us (see [`AnchorChannelsConfig::trusted_peers_no_reserve`]
11881198
/// for more information).
1189-
pub fn close_channel(
1199+
pub fn force_close_channel(
1200+
&self, user_channel_id: &UserChannelId, counterparty_node_id: PublicKey,
1201+
) -> Result<(), Error> {
1202+
self.close_channel_internal(user_channel_id, counterparty_node_id, true)
1203+
}
1204+
1205+
fn close_channel_internal(
11901206
&self, user_channel_id: &UserChannelId, counterparty_node_id: PublicKey, force: bool,
11911207
) -> Result<(), Error> {
11921208
let open_channels =
@@ -1235,10 +1251,9 @@ impl Node {
12351251
if open_channels.len() == 1 {
12361252
self.peer_store.remove_peer(&counterparty_node_id)?;
12371253
}
1238-
Ok(())
1239-
} else {
1240-
Ok(())
12411254
}
1255+
1256+
Ok(())
12421257
}
12431258

12441259
/// Update the config for a previously opened channel.

tests/common/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -583,7 +583,7 @@ pub(crate) fn do_channel_full_cycle<E: ElectrumApi>(
583583
assert_eq!(node_b.list_payments().len(), 5);
584584

585585
println!("\nB close_channel");
586-
node_b.close_channel(&user_channel_id, node_a.node_id(), false).unwrap();
586+
node_b.close_channel(&user_channel_id, node_a.node_id()).unwrap();
587587
expect_event!(node_a, ChannelClosed);
588588
expect_event!(node_b, ChannelClosed);
589589

tests/integration_tests_cln.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ fn test_cln() {
111111
cln_client.pay(&ldk_invoice.to_string(), Default::default()).unwrap();
112112
common::expect_event!(node, PaymentReceived);
113113

114-
node.close_channel(&user_channel_id, cln_node_id, false).unwrap();
114+
node.close_channel(&user_channel_id, cln_node_id).unwrap();
115115
common::expect_event!(node, ChannelClosed);
116116
node.stop().unwrap();
117117
}

0 commit comments

Comments
 (0)