mirror of
https://github.com/aljazceru/lightning.git
synced 2025-12-19 15:14:23 +01:00
plugin: Make unannounced notification topics no longer fatal
Since plugins will start sending them soon, and they are likely to get it wrong sometimes, be a bit more lenient, warn them in the logs instead and then make sure it doesn't accidentally work anyway.
This commit is contained in:
committed by
Rusty Russell
parent
c8c0c4dc99
commit
98aa3c3da7
@@ -464,33 +464,22 @@ static const char *plugin_notification_handle(struct plugin *plugin,
|
|||||||
|
|
||||||
methname = json_strdup(tmpctx, plugin->buffer, methtok);
|
methname = json_strdup(tmpctx, plugin->buffer, methtok);
|
||||||
|
|
||||||
if (notifications_have_topic(plugin->plugins, methname)) {
|
|
||||||
|
|
||||||
if (!plugin_notification_allowed(plugin, methname)) {
|
if (!plugin_notification_allowed(plugin, methname)) {
|
||||||
log_unusual(
|
log_unusual(plugin->log,
|
||||||
plugin->log,
|
|
||||||
"Plugin attempted to send a notification to topic "
|
"Plugin attempted to send a notification to topic "
|
||||||
"\"%s\" it hasn't declared in its manifest, not "
|
"\"%s\" it hasn't declared in its manifest, not "
|
||||||
"forwarding to subscribers.",
|
"forwarding to subscribers.",
|
||||||
methname);
|
methname);
|
||||||
return NULL;
|
} else if (notifications_have_topic(plugin->plugins, methname)) {
|
||||||
} else {
|
|
||||||
|
|
||||||
n = jsonrpc_notification_start(NULL, methname);
|
n = jsonrpc_notification_start(NULL, methname);
|
||||||
json_add_string(n->stream, "origin", plugin->shortname);
|
json_add_string(n->stream, "origin", plugin->shortname);
|
||||||
json_add_tok(n->stream, "payload", paramstok,
|
json_add_tok(n->stream, "payload", paramstok, plugin->buffer);
|
||||||
plugin->buffer);
|
|
||||||
jsonrpc_notification_end(n);
|
jsonrpc_notification_end(n);
|
||||||
|
|
||||||
plugins_notify(plugin->plugins, take(n));
|
plugins_notify(plugin->plugins, take(n));
|
||||||
|
}
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
return tal_fmt(plugin, "Unknown notification method %.*s",
|
|
||||||
json_tok_full_len(methtok),
|
|
||||||
json_tok_full(plugin->buffer, methtok));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Returns the error string, or NULL */
|
/* Returns the error string, or NULL */
|
||||||
static const char *plugin_response_handle(struct plugin *plugin,
|
static const char *plugin_response_handle(struct plugin *plugin,
|
||||||
|
|||||||
@@ -17,5 +17,19 @@ def emit(plugin):
|
|||||||
plugin.notify("custom", "Hello world")
|
plugin.notify("custom", "Hello world")
|
||||||
|
|
||||||
|
|
||||||
|
@plugin.method("faulty-emit")
|
||||||
|
def faulty_emit(plugin):
|
||||||
|
"""Emit a simple string notification to topic "custom"
|
||||||
|
"""
|
||||||
|
plugin.notify("ididntannouncethis", "Hello world")
|
||||||
|
|
||||||
|
|
||||||
|
@plugin.subscribe("ididntannouncethis")
|
||||||
|
def on_faulty_emit(origin, payload, **kwargs):
|
||||||
|
"""We should never receive this as it gets dropped.
|
||||||
|
"""
|
||||||
|
plugin.log("Got the ididntannouncethis event")
|
||||||
|
|
||||||
|
|
||||||
plugin.add_notification_topic("custom")
|
plugin.add_notification_topic("custom")
|
||||||
plugin.run()
|
plugin.run()
|
||||||
|
|||||||
@@ -2411,3 +2411,15 @@ def test_custom_notification_topics(node_factory):
|
|||||||
l1 = node_factory.get_node(options={'plugin': plugin})
|
l1 = node_factory.get_node(options={'plugin': plugin})
|
||||||
l1.rpc.emit()
|
l1.rpc.emit()
|
||||||
l1.daemon.wait_for_log(r'Got a custom notification Hello world')
|
l1.daemon.wait_for_log(r'Got a custom notification Hello world')
|
||||||
|
|
||||||
|
# And now make sure that we drop unannounced notifications
|
||||||
|
l1.rpc.faulty_emit()
|
||||||
|
l1.daemon.wait_for_log(
|
||||||
|
r"Plugin attempted to send a notification to topic .* not forwarding"
|
||||||
|
)
|
||||||
|
time.sleep(1)
|
||||||
|
assert not l1.daemon.is_in_log(r'Got the ididntannouncethis event')
|
||||||
|
|
||||||
|
# The plugin just dist what previously was a fatal mistake (emit
|
||||||
|
# an unknown notification), make sure we didn't kill it.
|
||||||
|
assert 'custom_notifications.py' in [p['name'] for p in l1.rpc.listconfigs()['plugins']]
|
||||||
|
|||||||
Reference in New Issue
Block a user