Skip to content

Commit 5fa0653

Browse files
plugin: add channel_closed notification
Changelog-None Signed-off-by: Nishant Bansal <[email protected]>
1 parent 0db282a commit 5fa0653

File tree

5 files changed

+73
-0
lines changed

5 files changed

+73
-0
lines changed

lightningd/closing_control.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#include <lightningd/jsonrpc.h>
3232
#include <lightningd/lightningd.h>
3333
#include <lightningd/log.h>
34+
#include <lightningd/notification.h>
3435
#include <lightningd/opening_common.h>
3536
#include <lightningd/options.h>
3637
#include <lightningd/peer_control.h>
@@ -330,6 +331,13 @@ static void peer_closing_complete(struct channel *channel, const u8 *msg)
330331
REASON_UNKNOWN,
331332
"Closing complete");
332333

334+
struct bitcoin_txid closing_txid;
335+
bitcoin_txid(channel->last_tx, &closing_txid);
336+
337+
/* Tell plugins about the success */
338+
notify_channel_closed(channel->peer->ld, &channel->peer->id,
339+
&closing_txid);
340+
333341
/* Channel gets dropped to chain cooperatively. */
334342
drop_to_chain(channel->peer->ld, channel, true, NULL);
335343
}
@@ -907,6 +915,9 @@ static struct command_result *json_close(struct command *cmd,
907915
/* In case we changed anything, save it. */
908916
wallet_channel_save(cmd->ld->wallet, channel);
909917

918+
919+
920+
910921
/* Wait until close drops down to chain. */
911922
return command_still_pending(cmd);
912923

lightningd/notification.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,25 @@ void notify_channel_opened(struct lightningd *ld,
292292
notify_send(ld, n);
293293
}
294294

295+
static void channel_closed_notification_serialize(
296+
struct json_stream *stream, struct lightningd *ld,
297+
const struct node_id *node_id, const struct bitcoin_txid *closing_txid)
298+
{
299+
json_add_node_id(stream, "id", node_id);
300+
json_add_txid(stream, "closing_txid", closing_txid);
301+
}
302+
303+
REGISTER_NOTIFICATION(channel_closed)
304+
305+
void notify_channel_closed(struct lightningd *ld, const struct node_id *node_id,
306+
const struct bitcoin_txid *closing_txid)
307+
{
308+
struct jsonrpc_notification *n = notify_start("channel_closed");
309+
channel_closed_notification_serialize(n->stream, ld, node_id,
310+
closing_txid);
311+
notify_send(ld, n);
312+
}
313+
295314
static void channel_state_changed_notification_serialize(struct json_stream *stream,
296315
const struct node_id *peer_id,
297316
const struct channel_id *cid,

lightningd/notification.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@ void notify_channel_opened(struct lightningd *ld,
5050
const struct bitcoin_txid *funding_txid,
5151
bool channel_ready);
5252

53+
void notify_channel_closed(struct lightningd *ld, const struct node_id *node_id,
54+
const struct bitcoin_txid *closing_txid);
55+
5356
void notify_channel_state_changed(struct lightningd *ld,
5457
const struct node_id *peer_id,
5558
const struct channel_id *cid,

tests/plugins/misc_notifications.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,15 @@ def channel_opened(plugin, channel_opened, **kwargs):
2222
channel_opened["funding_txid"]))
2323

2424

25+
@plugin.subscribe("channel_closed")
26+
def channel_closed(plugin, channel_closed, **kwargs):
27+
plugin.log(
28+
"A channel was closed to us by {}, with a closing transaction ID: {}".format(
29+
channel_closed["id"], channel_closed["closing_txid"]
30+
)
31+
)
32+
33+
2534
@plugin.subscribe("channel_state_changed")
2635
def channel_state_changed(plugin, channel_state_changed, **kwargs):
2736
plugin.log("channel_state_changed {}".format(channel_state_changed))

tests/test_plugin.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1335,6 +1335,37 @@ def test_channel_opened_notification(node_factory):
13351335
.format(l1.info["id"], amount))
13361336

13371337

1338+
def test_channel_closed_notification(node_factory):
1339+
"""
1340+
Test the 'channel_closed' notification sent at channel closing success.
1341+
"""
1342+
opts = [
1343+
{},
1344+
{"plugin": os.path.join(os.getcwd(), "tests/plugins/misc_notifications.py")},
1345+
]
1346+
amount = 10**6
1347+
l1, l2 = node_factory.line_graph(2, fundchannel=True, fundamount=amount, opts=opts)
1348+
1349+
cid = l2.rpc.listpeerchannels()["channels"][0]["channel_id"]
1350+
closing_txid = only_one(l1.rpc.close(cid)['txids'])
1351+
1352+
# bitcoind.generate_block(6, wait_for_mempool=1)
1353+
# sync_blockheight(bitcoind, [l1, l2])
1354+
1355+
l1.daemon.wait_for_log(
1356+
r"A channel was closed to us by {}, with closing transaction id: {}".format(
1357+
l2.rpc.getinfo()["id"],
1358+
closing_txid,
1359+
)
1360+
)
1361+
l2.daemon.wait_for_log(
1362+
r"A channel was closed to us by {}, with closing transaction id: {}".format(
1363+
l1.rpc.getinfo()["id"],
1364+
closing_txid,
1365+
)
1366+
)
1367+
1368+
13381369
def test_forward_event_notification(node_factory, bitcoind, executor):
13391370
""" test 'forward_event' notifications
13401371
"""

0 commit comments

Comments
 (0)