Skip to content

Commit b7d46c8

Browse files
committed
renepay: add rpc batching utilities
Changelog-None Signed-off-by: Lagrang3 <[email protected]>
1 parent 1a3bc06 commit b7d46c8

File tree

3 files changed

+160
-0
lines changed

3 files changed

+160
-0
lines changed

plugins/renepay/Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ PLUGIN_RENEPAY_SRC := \
77
plugins/renepay/sendpay.c \
88
plugins/renepay/mods.c \
99
plugins/renepay/errorcodes.c \
10+
plugins/renepay/utils.c \
1011
plugins/renepay/json.c
1112

1213
PLUGIN_RENEPAY_HDRS := \
@@ -20,6 +21,7 @@ PLUGIN_RENEPAY_HDRS := \
2021
plugins/renepay/sendpay.h \
2122
plugins/renepay/mods.h \
2223
plugins/renepay/errorcodes.h \
24+
plugins/renepay/utils.h \
2325
plugins/renepay/json.c
2426

2527
PLUGIN_RENEPAY_OBJS := $(PLUGIN_RENEPAY_SRC:.c=.o)

plugins/renepay/utils.c

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
#include "config.h"
2+
#include <plugins/libplugin.h>
3+
#include <plugins/renepay/utils.h>
4+
5+
struct rpcbatch {
6+
size_t num_remaining;
7+
struct command *cmd;
8+
struct command_result *(*finalcb)(struct command *, void *);
9+
void *arg;
10+
};
11+
12+
struct rpcbatch_aux {
13+
struct rpcbatch *batch;
14+
void *arg;
15+
struct command_result *(*cb)(struct command *cmd, const char *,
16+
const char *, const jsmntok_t *, void *);
17+
struct command_result *(*errcb)(struct command *cmd, const char *,
18+
const char *, const jsmntok_t *,
19+
void *);
20+
};
21+
22+
struct rpcbatch *
23+
rpcbatch_new_(struct command *cmd,
24+
struct command_result *(*finalcb)(struct command *, void *),
25+
void *arg)
26+
{
27+
struct rpcbatch *batch = tal(cmd, struct rpcbatch);
28+
batch->num_remaining = 0;
29+
batch->cmd = cmd;
30+
batch->finalcb = finalcb;
31+
batch->arg = arg;
32+
return batch;
33+
}
34+
35+
static struct command_result *batch_one_complete(struct rpcbatch *batch)
36+
{
37+
assert(batch->num_remaining);
38+
if (--batch->num_remaining != 0)
39+
return command_still_pending(batch->cmd);
40+
struct command *cmd = batch->cmd;
41+
void *arg = batch->arg;
42+
struct command_result *(*finalcb)(struct command *, void *) =
43+
batch->finalcb;
44+
tal_free(batch);
45+
return finalcb(cmd, arg);
46+
}
47+
48+
static struct command_result *
49+
batch_one_success(struct command *cmd, const char *method, const char *buf,
50+
const jsmntok_t *result, struct rpcbatch_aux *aux)
51+
{
52+
/* Little hack to get the value of "complete" from libplugin. */
53+
struct command_result *complete = command_param_failed();
54+
/* If this frees stuff (e.g. fails), just return */
55+
if (aux->cb && aux->cb(cmd, method, buf, result, aux->arg) == complete)
56+
return complete;
57+
struct rpcbatch *batch = aux->batch;
58+
tal_free(aux);
59+
return batch_one_complete(batch);
60+
}
61+
62+
static struct command_result *
63+
batch_one_failed(struct command *cmd, const char *method, const char *buf,
64+
const jsmntok_t *result, struct rpcbatch_aux *aux)
65+
{
66+
/* Little hack to get the value of "complete" from libplugin. */
67+
struct command_result *complete = command_param_failed();
68+
/* If this frees stuff (e.g. fails), just return */
69+
if (aux->errcb &&
70+
aux->errcb(cmd, method, buf, result, aux->arg) == complete)
71+
return complete;
72+
struct rpcbatch *batch = aux->batch;
73+
tal_free(aux);
74+
return batch_one_complete(batch);
75+
}
76+
77+
struct out_req *add_to_rpcbatch_(
78+
struct rpcbatch *batch, const char *cmdname,
79+
struct command_result *(*cb)(struct command *, const char *, const char *,
80+
const jsmntok_t *, void *arg),
81+
struct command_result *(*errcb)(struct command *, const char *,
82+
const char *, const jsmntok_t *, void *arg),
83+
void *arg)
84+
{
85+
batch->num_remaining++;
86+
struct rpcbatch_aux *aux = tal(batch, struct rpcbatch_aux);
87+
aux->arg = arg;
88+
aux->batch = batch;
89+
aux->cb = cb;
90+
aux->errcb = errcb;
91+
return jsonrpc_request_start(batch->cmd, cmdname, batch_one_success,
92+
batch_one_failed, aux);
93+
}
94+
95+
/* Runs finalcb immediately if batch is empty. */
96+
struct command_result *rpcbatch_done(struct rpcbatch *batch)
97+
{
98+
/* Same path as completion */
99+
batch->num_remaining++;
100+
return batch_one_complete(batch);
101+
}

plugins/renepay/utils.h

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
#ifndef LIGHTNING_PLUGINS_RENEPAY_UTILS_H
2+
#define LIGHTNING_PLUGINS_RENEPAY_UTILS_H
3+
4+
#include "config.h"
5+
6+
struct rpcbatch;
7+
8+
struct rpcbatch *
9+
rpcbatch_new_(struct command *cmd,
10+
struct command_result *(*finalcb)(struct command *, void *),
11+
void *arg);
12+
13+
/* Returns a new rpcbatch object. This is meant to process multiple RPC calls
14+
* and execute a callback function after all of them have returned.
15+
*
16+
* @cmd: command involved in all RPC requests
17+
* @finalcb: after all RPCs have returned this function is called
18+
* @arg: argument passed to finalcb
19+
* */
20+
#define rpcbatch_new(cmd, finalcb, arg) \
21+
rpcbatch_new_((cmd), \
22+
typesafe_cb_preargs(struct command_result *, void *, \
23+
(finalcb), (arg), \
24+
struct command *command), \
25+
(arg))
26+
27+
struct out_req *add_to_rpcbatch_(
28+
struct rpcbatch *batch, const char *cmdname,
29+
struct command_result *(*cb)(struct command *, const char *, const char *,
30+
const jsmntok_t *, void *arg),
31+
struct command_result *(*errcb)(struct command *, const char *,
32+
const char *, const jsmntok_t *, void *arg),
33+
void *arg);
34+
35+
/* Append a new RPC request to this batch.
36+
*
37+
* @batch: RPC request batch
38+
* @cmdname: RPC name
39+
* @cb: callback function on success
40+
* @errcb: callback function on failure
41+
* @arg: callback functions argument
42+
* */
43+
#define add_to_rpcbatch(batch, cmdname, cb, errcb, arg) \
44+
add_to_rpcbatch_( \
45+
(batch), (cmdname), \
46+
typesafe_cb_preargs(struct command_result *, void *, (cb), (arg), \
47+
struct command *command, const char *method, \
48+
const char *buf, const jsmntok_t *result), \
49+
typesafe_cb_preargs(struct command_result *, void *, (errcb), \
50+
(arg), struct command *command, \
51+
const char *method, const char *buf, \
52+
const jsmntok_t *result), \
53+
(arg))
54+
55+
struct command_result *rpcbatch_done(struct rpcbatch *batch);
56+
57+
#endif /* LIGHTNING_PLUGINS_RENEPAY_UTILS_H */

0 commit comments

Comments
 (0)