mirror of
https://github.com/aljazceru/lightning.git
synced 2025-12-19 15:14:23 +01:00
plugin: Introduce plugin type to allow singleton and chaining
The newly introduced type is used to determine what the call semantics of the
hook are. We have `single` corresponding to the old behavior, as well as
`chain` which allows multiple plugins to register for the hook, and they are
then called sequentially (if all plugins return `{"result": "continue"}`) or
exit the chain if the hook event was handled.
This commit is contained in:
committed by
Rusty Russell
parent
30580731a6
commit
9a2a09efd6
@@ -269,6 +269,7 @@ invoice_payment_hook_cb(struct invoice_payment_hook_payload *payload,
|
|||||||
}
|
}
|
||||||
|
|
||||||
REGISTER_PLUGIN_HOOK(invoice_payment,
|
REGISTER_PLUGIN_HOOK(invoice_payment,
|
||||||
|
PLUGIN_HOOK_SINGLE,
|
||||||
invoice_payment_hook_cb,
|
invoice_payment_hook_cb,
|
||||||
struct invoice_payment_hook_payload *,
|
struct invoice_payment_hook_payload *,
|
||||||
invoice_payment_serialize,
|
invoice_payment_serialize,
|
||||||
|
|||||||
@@ -753,7 +753,8 @@ rpc_command_hook_callback(struct rpc_command_hook_payload *p,
|
|||||||
"Bad response to 'rpc_command' hook."));
|
"Bad response to 'rpc_command' hook."));
|
||||||
}
|
}
|
||||||
|
|
||||||
REGISTER_PLUGIN_HOOK(rpc_command, rpc_command_hook_callback,
|
REGISTER_PLUGIN_HOOK(rpc_command, PLUGIN_HOOK_SINGLE,
|
||||||
|
rpc_command_hook_callback,
|
||||||
struct rpc_command_hook_payload *,
|
struct rpc_command_hook_payload *,
|
||||||
rpc_command_hook_serialize,
|
rpc_command_hook_serialize,
|
||||||
struct rpc_command_hook_payload *);
|
struct rpc_command_hook_payload *);
|
||||||
|
|||||||
@@ -821,6 +821,7 @@ static void openchannel_hook_cb(struct openchannel_hook_payload *payload,
|
|||||||
}
|
}
|
||||||
|
|
||||||
REGISTER_PLUGIN_HOOK(openchannel,
|
REGISTER_PLUGIN_HOOK(openchannel,
|
||||||
|
PLUGIN_HOOK_SINGLE,
|
||||||
openchannel_hook_cb,
|
openchannel_hook_cb,
|
||||||
struct openchannel_hook_payload *,
|
struct openchannel_hook_payload *,
|
||||||
openchannel_hook_serialize,
|
openchannel_hook_serialize,
|
||||||
|
|||||||
@@ -908,7 +908,8 @@ send_error:
|
|||||||
tal_free(payload);
|
tal_free(payload);
|
||||||
}
|
}
|
||||||
|
|
||||||
REGISTER_PLUGIN_HOOK(peer_connected, peer_connected_hook_cb,
|
REGISTER_PLUGIN_HOOK(peer_connected, PLUGIN_HOOK_SINGLE,
|
||||||
|
peer_connected_hook_cb,
|
||||||
struct peer_connected_hook_payload *,
|
struct peer_connected_hook_payload *,
|
||||||
peer_connected_serialize,
|
peer_connected_serialize,
|
||||||
struct peer_connected_hook_payload *);
|
struct peer_connected_hook_payload *);
|
||||||
@@ -2393,8 +2394,9 @@ static void custommsg_payload_serialize(struct custommsg_payload *payload,
|
|||||||
json_add_node_id(stream, "peer_id", &payload->peer_id);
|
json_add_node_id(stream, "peer_id", &payload->peer_id);
|
||||||
}
|
}
|
||||||
|
|
||||||
REGISTER_PLUGIN_HOOK(custommsg, custommsg_callback, struct custommsg_payload *,
|
REGISTER_PLUGIN_HOOK(custommsg, PLUGIN_HOOK_SINGLE, custommsg_callback,
|
||||||
custommsg_payload_serialize, struct custommsg_payload *);
|
struct custommsg_payload *, custommsg_payload_serialize,
|
||||||
|
struct custommsg_payload *);
|
||||||
|
|
||||||
void handle_custommsg_in(struct lightningd *ld, const struct node_id *peer_id,
|
void handle_custommsg_in(struct lightningd *ld, const struct node_id *peer_id,
|
||||||
const u8 *msg)
|
const u8 *msg)
|
||||||
|
|||||||
@@ -873,7 +873,8 @@ htlc_accepted_hook_callback(struct htlc_accepted_hook_payload *request,
|
|||||||
tal_free(request);
|
tal_free(request);
|
||||||
}
|
}
|
||||||
|
|
||||||
REGISTER_PLUGIN_HOOK(htlc_accepted, htlc_accepted_hook_callback,
|
REGISTER_PLUGIN_HOOK(htlc_accepted, PLUGIN_HOOK_SINGLE,
|
||||||
|
htlc_accepted_hook_callback,
|
||||||
struct htlc_accepted_hook_payload *,
|
struct htlc_accepted_hook_payload *,
|
||||||
htlc_accepted_hook_serialize,
|
htlc_accepted_hook_serialize,
|
||||||
struct htlc_accepted_hook_payload *);
|
struct htlc_accepted_hook_payload *);
|
||||||
|
|||||||
@@ -130,7 +130,8 @@ void plugin_hook_call_(struct lightningd *ld, const struct plugin_hook *hook,
|
|||||||
* annoying, and to make it clear that it's totally synchronous. */
|
* annoying, and to make it clear that it's totally synchronous. */
|
||||||
|
|
||||||
/* Special synchronous hook for db */
|
/* Special synchronous hook for db */
|
||||||
static struct plugin_hook db_write_hook = { "db_write", NULL, NULL, NULL };
|
static struct plugin_hook db_write_hook = {"db_write", PLUGIN_HOOK_SINGLE, NULL,
|
||||||
|
NULL, NULL};
|
||||||
AUTODATA(hooks, &db_write_hook);
|
AUTODATA(hooks, &db_write_hook);
|
||||||
|
|
||||||
static void db_hook_response(const char *buffer, const jsmntok_t *toks,
|
static void db_hook_response(const char *buffer, const jsmntok_t *toks,
|
||||||
|
|||||||
@@ -44,8 +44,18 @@
|
|||||||
* and callback have the correct type.
|
* and callback have the correct type.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
enum plugin_hook_type {
|
||||||
|
PLUGIN_HOOK_SINGLE,
|
||||||
|
PLUGIN_HOOK_CHAIN,
|
||||||
|
};
|
||||||
|
|
||||||
struct plugin_hook {
|
struct plugin_hook {
|
||||||
const char *name;
|
const char *name;
|
||||||
|
|
||||||
|
/* Which type of plugin is this? It'll determine how many plugins can
|
||||||
|
* register this hook, and how the hooks are called. */
|
||||||
|
enum plugin_hook_type type;
|
||||||
|
|
||||||
void (*response_cb)(void *arg, const char *buffer, const jsmntok_t *toks);
|
void (*response_cb)(void *arg, const char *buffer, const jsmntok_t *toks);
|
||||||
void (*serialize_payload)(void *src, struct json_stream *dest);
|
void (*serialize_payload)(void *src, struct json_stream *dest);
|
||||||
|
|
||||||
@@ -84,14 +94,16 @@ void plugin_hook_call_(struct lightningd *ld, const struct plugin_hook *hook,
|
|||||||
* response_cb function accepts the deserialized response format and
|
* response_cb function accepts the deserialized response format and
|
||||||
* an arbitrary extra argument used to maintain context.
|
* an arbitrary extra argument used to maintain context.
|
||||||
*/
|
*/
|
||||||
#define REGISTER_PLUGIN_HOOK(name, response_cb, response_cb_arg_type, \
|
#define REGISTER_PLUGIN_HOOK(name, type, response_cb, response_cb_arg_type, \
|
||||||
serialize_payload, payload_type) \
|
serialize_payload, payload_type) \
|
||||||
struct plugin_hook name##_hook_gen = { \
|
struct plugin_hook name##_hook_gen = { \
|
||||||
stringify(name), \
|
stringify(name), \
|
||||||
typesafe_cb_cast(void (*)(void *, const char *, const jsmntok_t *),\
|
type, \
|
||||||
void (*)(response_cb_arg_type, \
|
typesafe_cb_cast( \
|
||||||
const char *, const jsmntok_t *), \
|
void (*)(void *, const char *, const jsmntok_t *), \
|
||||||
response_cb), \
|
void (*)(response_cb_arg_type, const char *, \
|
||||||
|
const jsmntok_t *), \
|
||||||
|
response_cb), \
|
||||||
typesafe_cb_cast(void (*)(void *, struct json_stream *), \
|
typesafe_cb_cast(void (*)(void *, struct json_stream *), \
|
||||||
void (*)(payload_type, struct json_stream *), \
|
void (*)(payload_type, struct json_stream *), \
|
||||||
serialize_payload), \
|
serialize_payload), \
|
||||||
|
|||||||
Reference in New Issue
Block a user