Skip to content

Commit a683d06

Browse files
committed
renepay: define a new struct rpcaction
struct rpcaction: encapsulates the concept of an RPC action, ie. an RPC call object. It can be attached to an rpcbatch. Changelog-None. Signed-off-by: Lagrang3 <[email protected]>
1 parent 19a435e commit a683d06

File tree

2 files changed

+75
-0
lines changed

2 files changed

+75
-0
lines changed

plugins/renepay/utils.c

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "config.h"
2+
#include <ccan/tal/str/str.h>
23
#include <plugins/libplugin.h>
34
#include <plugins/renepay/utils.h>
45

@@ -24,6 +25,44 @@ struct rpcbatch_aux {
2425
void *);
2526
};
2627

28+
struct rpcaction {
29+
const char *method;
30+
void *arg;
31+
void (*json_cb)(struct json_stream *, void *);
32+
struct command_result *(*cb)(struct command *cmd, const char *,
33+
const char *, const jsmntok_t *, void *);
34+
struct command_result *(*errcb)(struct command *cmd, const char *,
35+
const char *, const jsmntok_t *,
36+
void *);
37+
};
38+
39+
struct rpcaction *rpcaction_new_(
40+
const tal_t *ctx, const char *cmdname,
41+
struct command_result *(*cb)(struct command *, const char *, const char *,
42+
const jsmntok_t *, void *),
43+
struct command_result *(*errcb)(struct command *, const char *,
44+
const char *, const jsmntok_t *, void *),
45+
void (*json_cb)(struct json_stream *, void *), void *arg)
46+
{
47+
struct rpcaction *action = tal(ctx, struct rpcaction);
48+
action->method = tal_strdup(action, cmdname);
49+
action->arg = arg;
50+
action->cb = cb;
51+
action->errcb = errcb;
52+
action->json_cb = json_cb;
53+
return action;
54+
}
55+
56+
struct out_req *rpcbatch_append_action(struct rpcbatch *batch,
57+
struct rpcaction *action)
58+
{
59+
struct out_req *req = add_to_rpcbatch_(
60+
batch, action->method, action->cb, action->errcb, action->arg);
61+
if (action->json_cb)
62+
action->json_cb(req->js, action->arg);
63+
return req;
64+
}
65+
2766
struct rpcbatch *
2867
rpcbatch_new_(struct command *cmd,
2968
struct command_result *(*finalcb)(struct command *, void *),

plugins/renepay/utils.h

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,4 +56,40 @@ struct out_req *add_to_rpcbatch_(
5656

5757
struct command_result *rpcbatch_done(struct rpcbatch *batch);
5858

59+
struct rpcaction *rpcaction_new_(
60+
const tal_t *ctx, const char *cmdname,
61+
struct command_result *(*cb)(struct command *, const char *, const char *,
62+
const jsmntok_t *, void *),
63+
struct command_result *(*errcb)(struct command *, const char *,
64+
const char *, const jsmntok_t *, void *),
65+
void (*json_cb)(struct json_stream *, void *), void *arg);
66+
67+
/* Returns a new rpcaction object. An rpcaction represents a single step an
68+
* rpcbatch. It can be created before the batch and then appended.
69+
*
70+
* @ctx: allocator
71+
* @cmdname: RPC name
72+
* @cb: callback function on success
73+
* @errcb: callback function on failure
74+
* @jsoncb: callback function to populate the json stream
75+
* @arg: callback functions argument
76+
* */
77+
#define rpcaction_new(ctx, cmdname, cb, errcb, jsoncb, arg) \
78+
rpcaction_new_( \
79+
(ctx), (cmdname), \
80+
typesafe_cb_preargs(struct command_result *, void *, (cb), (arg), \
81+
struct command *command, const char *method, \
82+
const char *buf, const jsmntok_t *result), \
83+
typesafe_cb_preargs(struct command_result *, void *, (errcb), \
84+
(arg), struct command *command, \
85+
const char *method, const char *buf, \
86+
const jsmntok_t *result), \
87+
typesafe_cb_preargs(void, void *, (jsoncb), (arg), \
88+
struct json_stream *), \
89+
(arg))
90+
91+
/* Append an action into a batch. */
92+
struct out_req *rpcbatch_append_action(struct rpcbatch *batch,
93+
struct rpcaction *action);
94+
5995
#endif /* LIGHTNING_PLUGINS_RENEPAY_UTILS_H */

0 commit comments

Comments
 (0)