mirror of
https://github.com/aljazceru/lightning.git
synced 2025-12-20 15:44:21 +01:00
feat: adds channel_state_changed notification
This notification will be raised whenever a channel state changes.
The payload includes the channel and peer identifiers and the
old and the new state.
Example payload:
```
{
"channel_state_changed": {
"peer_id": "03bc9337c7a28bb784d67742ebedd30a93bacdf7e4ca16436ef3798000242b2251",
"channel_id": "a2d0851832f0e30a0cf778a826d72f077ca86b69f72677e0267f23f63a0599b4",
"short_channel_id" : "561820x1020x1",
"old_state": "CHANNELD_NORMAL",
"new_state": "AWAITING_UNILATERAL"
}
}
```
Changelog-Added: Plugins: channel_state_changed notification
This commit is contained in:
committed by
Rusty Russell
parent
b80ad95f1c
commit
d86855d1f7
@@ -18,6 +18,7 @@
|
||||
#include <lightningd/jsonrpc.h>
|
||||
#include <lightningd/lightningd.h>
|
||||
#include <lightningd/log.h>
|
||||
#include <lightningd/notification.h>
|
||||
#include <lightningd/opening_control.h>
|
||||
#include <lightningd/peer_control.h>
|
||||
#include <lightningd/subd.h>
|
||||
@@ -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, ...)
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -7,9 +7,11 @@
|
||||
#include <ccan/json_escape/json_escape.h>
|
||||
#include <ccan/time/time.h>
|
||||
#include <common/amount.h>
|
||||
#include <common/channel_id.h>
|
||||
#include <common/coin_mvt.h>
|
||||
#include <common/errcode.h>
|
||||
#include <common/node_id.h>
|
||||
#include <lightningd/channel_state.h>
|
||||
#include <lightningd/htlc_end.h>
|
||||
#include <lightningd/jsonrpc.h>
|
||||
#include <lightningd/lightningd.h>
|
||||
@@ -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. */
|
||||
|
||||
2
wallet/db_postgres_sqlgen.c
generated
2
wallet/db_postgres_sqlgen.c
generated
@@ -1648,4 +1648,4 @@ struct db_query db_postgres_queries[] = {
|
||||
|
||||
#endif /* LIGHTNINGD_WALLET_GEN_DB_POSTGRES */
|
||||
|
||||
// SHA256STAMP:e55a8febb74330a9167e8498b26e556609d95b7ca6ef984da1fa200e53e36a7e
|
||||
// SHA256STAMP:4c9787464f33fe9bfd209efbd84daebeb5584d52daa1d83f4c34f9a0a6108b46
|
||||
|
||||
2
wallet/db_sqlite3_sqlgen.c
generated
2
wallet/db_sqlite3_sqlgen.c
generated
@@ -1648,4 +1648,4 @@ struct db_query db_sqlite3_queries[] = {
|
||||
|
||||
#endif /* LIGHTNINGD_WALLET_GEN_DB_SQLITE3 */
|
||||
|
||||
// SHA256STAMP:e55a8febb74330a9167e8498b26e556609d95b7ca6ef984da1fa200e53e36a7e
|
||||
// SHA256STAMP:4c9787464f33fe9bfd209efbd84daebeb5584d52daa1d83f4c34f9a0a6108b46
|
||||
|
||||
4
wallet/statements_gettextgen.po
generated
4
wallet/statements_gettextgen.po
generated
@@ -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
|
||||
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user