diff --git a/lightningd/channel.c b/lightningd/channel.c index 47d819f38..0d8fa436b 100644 --- a/lightningd/channel.c +++ b/lightningd/channel.c @@ -18,6 +18,7 @@ #include #include #include +#include #include #include #include @@ -384,6 +385,8 @@ void channel_set_state(struct channel *channel, enum channel_state old_state, enum channel_state state) { + struct channel_id cid; + log_info(channel->log, "State changed from %s to %s", channel_state_name(channel), channel_state_str(state)); if (channel->state != old_state) @@ -394,6 +397,15 @@ void channel_set_state(struct channel *channel, /* TODO(cdecker) Selectively save updated fields to DB */ wallet_channel_save(channel->peer->ld->wallet, channel); + + /* plugin notification channel_state_changed */ + derive_channel_id(&cid, &channel->funding_txid, channel->funding_outnum); + notify_channel_state_changed(channel->peer->ld, + &channel->peer->id, + &cid, + channel->scid, + old_state, + state); } void channel_fail_permanent(struct channel *channel, const char *fmt, ...) diff --git a/lightningd/notification.c b/lightningd/notification.c index 377da2c13..e25ed2144 100644 --- a/lightningd/notification.c +++ b/lightningd/notification.c @@ -210,6 +210,51 @@ void notify_channel_opened(struct lightningd *ld, struct node_id *node_id, plugins_notify(ld->plugins, take(n)); } +static void channel_state_changed_notification_serialize(struct json_stream *stream, + struct node_id *peer_id, + struct channel_id *cid, + struct short_channel_id *scid, + enum channel_state old_state, + enum channel_state new_state) +{ + json_object_start(stream, "channel_state_changed"); + json_add_node_id(stream, "peer_id", peer_id); + json_add_string(stream, "channel_id", + type_to_string(tmpctx, struct channel_id, cid)); + if (scid) + json_add_short_channel_id(stream, "short_channel_id", scid); + else + json_add_null(stream, "short_channel_id"); + json_add_string(stream, "old_state", channel_state_str(old_state)); + json_add_string(stream, "new_state", channel_state_str(new_state)); + json_object_end(stream); +} + + +REGISTER_NOTIFICATION(channel_state_changed, + channel_state_changed_notification_serialize) + +void notify_channel_state_changed(struct lightningd *ld, + struct node_id *peer_id, + struct channel_id *cid, + struct short_channel_id *scid, + enum channel_state old_state, + enum channel_state new_state) +{ + void (*serialize)(struct json_stream *, + struct node_id *, + struct channel_id *, + struct short_channel_id *, + enum channel_state, + enum channel_state) = channel_state_changed_notification_gen.serialize; + + struct jsonrpc_notification *n + = jsonrpc_notification_start(NULL, channel_state_changed_notification_gen.topic); + serialize(n->stream, peer_id, cid, scid, old_state, new_state); + jsonrpc_notification_end(n); + plugins_notify(ld->plugins, take(n)); +} + static void forward_event_notification_serialize(struct json_stream *stream, const struct htlc_in *in, const struct short_channel_id *scid_out, diff --git a/lightningd/notification.h b/lightningd/notification.h index 940479887..000c76d28 100644 --- a/lightningd/notification.h +++ b/lightningd/notification.h @@ -7,9 +7,11 @@ #include #include #include +#include #include #include #include +#include #include #include #include @@ -55,6 +57,13 @@ void notify_channel_opened(struct lightningd *ld, struct node_id *node_id, struct amount_sat *funding_sat, struct bitcoin_txid *funding_txid, bool *funding_locked); +void notify_channel_state_changed(struct lightningd *ld, + struct node_id *peer_id, + struct channel_id *cid, + struct short_channel_id *scid, + enum channel_state old_state, + enum channel_state new_state); + void notify_forward_event(struct lightningd *ld, const struct htlc_in *in, /* May be NULL if we don't know. */ diff --git a/wallet/db_postgres_sqlgen.c b/wallet/db_postgres_sqlgen.c index a5ad0885b..59054dd69 100644 --- a/wallet/db_postgres_sqlgen.c +++ b/wallet/db_postgres_sqlgen.c @@ -1648,4 +1648,4 @@ struct db_query db_postgres_queries[] = { #endif /* LIGHTNINGD_WALLET_GEN_DB_POSTGRES */ -// SHA256STAMP:e55a8febb74330a9167e8498b26e556609d95b7ca6ef984da1fa200e53e36a7e +// SHA256STAMP:4c9787464f33fe9bfd209efbd84daebeb5584d52daa1d83f4c34f9a0a6108b46 diff --git a/wallet/db_sqlite3_sqlgen.c b/wallet/db_sqlite3_sqlgen.c index c3b0c0fc2..d8dbc441e 100644 --- a/wallet/db_sqlite3_sqlgen.c +++ b/wallet/db_sqlite3_sqlgen.c @@ -1648,4 +1648,4 @@ struct db_query db_sqlite3_queries[] = { #endif /* LIGHTNINGD_WALLET_GEN_DB_SQLITE3 */ -// SHA256STAMP:e55a8febb74330a9167e8498b26e556609d95b7ca6ef984da1fa200e53e36a7e +// SHA256STAMP:4c9787464f33fe9bfd209efbd84daebeb5584d52daa1d83f4c34f9a0a6108b46 diff --git a/wallet/statements_gettextgen.po b/wallet/statements_gettextgen.po index 896e00e9c..09d558dab 100644 --- a/wallet/statements_gettextgen.po +++ b/wallet/statements_gettextgen.po @@ -1082,7 +1082,7 @@ msgstr "" msgid "not a valid SQL statement" msgstr "" -#: wallet/test/run-wallet.c:1351 +#: wallet/test/run-wallet.c:1359 msgid "INSERT INTO channels (id) VALUES (1);" msgstr "" -# SHA256STAMP:de15ae770286fe050f7b2d712bcc2a3311a8737e920353bb8c7d8dbf0188db69 +# SHA256STAMP:108fcc46e6fa6e190b27773161a9c6bf9a83cb25e9d3164fa40e9de9e6f98644 diff --git a/wallet/test/run-wallet.c b/wallet/test/run-wallet.c index a6834fed9..13bbc4e60 100644 --- a/wallet/test/run-wallet.c +++ b/wallet/test/run-wallet.c @@ -428,6 +428,14 @@ void notify_chain_mvt(struct lightningd *ld UNNEEDED, const struct chain_coin_mv /* Generated stub for notify_channel_mvt */ void notify_channel_mvt(struct lightningd *ld UNNEEDED, const struct channel_coin_mvt *mvt UNNEEDED) { fprintf(stderr, "notify_channel_mvt called!\n"); abort(); } +/* Generated stub for notify_channel_state_changed */ +void notify_channel_state_changed(struct lightningd *ld UNNEEDED, + struct node_id *peer_id UNNEEDED, + struct channel_id *cid UNNEEDED, + struct short_channel_id *scid UNNEEDED, + enum channel_state old_state UNNEEDED, + enum channel_state new_state UNNEEDED) +{ fprintf(stderr, "notify_channel_state_changed called!\n"); abort(); } /* Generated stub for notify_connect */ void notify_connect(struct lightningd *ld UNNEEDED, struct node_id *nodeid UNNEEDED, struct wireaddr_internal *addr UNNEEDED)