Skip to content

Commit 944d20d

Browse files
committed
renepay: generic callback for batches of requests
Use two generic (success and fail) callback functions to handle batches of RPC requests. This removes duplicate code. Signed-off-by: Lagrang3 <[email protected]>
1 parent 42d5600 commit 944d20d

File tree

1 file changed

+42
-68
lines changed

1 file changed

+42
-68
lines changed

plugins/renepay/mods.c

Lines changed: 42 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,41 @@ static struct command_result *payment_rpc_failure(struct command *cmd,
9090
json_tok_full_len(toks), json_tok_full(buffer, toks));
9191
}
9292

93+
/* Generic handling of multiple RPC calls */
94+
struct request_batch {
95+
size_t num_requests;
96+
struct payment *payment;
97+
};
98+
99+
static struct command_result *one_request_done(struct command *cmd,
100+
const char *buf UNUSED,
101+
const jsmntok_t *result UNUSED,
102+
struct request_batch *batch)
103+
{
104+
assert(batch->num_requests);
105+
assert(batch->payment);
106+
batch->num_requests--;
107+
108+
if (!batch->num_requests) {
109+
struct payment *p = batch->payment;
110+
tal_free(batch);
111+
return payment_continue(p);
112+
}
113+
114+
return command_still_pending(cmd);
115+
}
116+
117+
static struct command_result *one_request_failed(struct command *cmd,
118+
const char *buf,
119+
const jsmntok_t *result,
120+
struct request_batch *batch)
121+
{
122+
plugin_log(cmd->plugin, LOG_UNUSUAL, "failed RPC batch: %.*s",
123+
json_tok_full_len(result), json_tok_full(buf, result));
124+
return one_request_done(cmd, buf, result, batch);
125+
}
126+
127+
93128
/*****************************************************************************
94129
* previoussuccess
95130
*
@@ -354,36 +389,6 @@ REGISTER_PAYMENT_MODIFIER(refreshgossmap, refreshgossmap_cb);
354389
* network.
355390
*/
356391

357-
struct routehints_batch {
358-
size_t num_elements;
359-
struct payment *payment;
360-
};
361-
362-
static struct command_result *add_one_hint_done(struct command *cmd,
363-
const char *buf UNUSED,
364-
const jsmntok_t *result UNUSED,
365-
struct routehints_batch *batch)
366-
{
367-
assert(batch->num_elements);
368-
assert(batch->payment);
369-
batch->num_elements--;
370-
371-
if (!batch->num_elements)
372-
return payment_continue(batch->payment);
373-
374-
return command_still_pending(cmd);
375-
}
376-
377-
static struct command_result *
378-
add_one_hint_failed(struct command *cmd, const char *buf,
379-
const jsmntok_t *result, struct routehints_batch *batch)
380-
{
381-
plugin_log(cmd->plugin, LOG_UNUSUAL,
382-
"failed to create channel hint: %.*s",
383-
json_tok_full_len(result), json_tok_full(buf, result));
384-
return add_one_hint_done(cmd, buf, result, batch);
385-
}
386-
387392
static struct command_result *routehints_cb(struct payment *payment)
388393
{
389394
struct command *cmd = payment_command(payment);
@@ -406,8 +411,8 @@ static struct command_result *routehints_cb(struct payment *payment)
406411

407412
const struct node_id *destination = &payment->payment_info.destination;
408413
const size_t nhints = tal_count(routehints);
409-
struct routehints_batch *batch = tal(cmd, struct routehints_batch);
410-
batch->num_elements = 0;
414+
struct request_batch *batch = tal(cmd, struct request_batch);
415+
batch->num_requests = 0;
411416
batch->payment = payment;
412417

413418
for (size_t i = 0; i < nhints; i++) {
@@ -428,7 +433,7 @@ static struct command_result *routehints_cb(struct payment *payment)
428433

429434
struct out_req *req = jsonrpc_request_start(
430435
cmd->plugin, cmd, "askrene-create-channel",
431-
add_one_hint_done, add_one_hint_failed, batch);
436+
one_request_done, one_request_failed, batch);
432437

433438
json_add_string(req->js, "layer",
434439
payment->private_layer);
@@ -452,7 +457,7 @@ static struct command_result *routehints_cb(struct payment *payment)
452457

453458
send_outreq(cmd->plugin, req);
454459

455-
batch->num_elements++;
460+
batch->num_requests++;
456461
end = &r[j].pubkey;
457462
}
458463
}
@@ -1030,36 +1035,6 @@ REGISTER_PAYMENT_MODIFIER(knowledgerelax, knowledgerelax_cb);
10301035
* FIXME: shall we set these threshold parameters as plugin options?
10311036
*/
10321037

1033-
struct channelfilter_batch {
1034-
size_t num_requests;
1035-
struct payment *payment;
1036-
};
1037-
1038-
static struct command_result *
1039-
one_channelfilter_done(struct command *cmd, const char *buf UNUSED,
1040-
const jsmntok_t *result UNUSED,
1041-
struct channelfilter_batch *batch)
1042-
{
1043-
assert(batch->num_requests);
1044-
assert(batch->payment);
1045-
batch->num_requests--;
1046-
1047-
if (!batch->num_requests)
1048-
return payment_continue(batch->payment);
1049-
1050-
return command_still_pending(cmd);
1051-
}
1052-
1053-
static struct command_result *
1054-
one_channelfilter_failed(struct command *cmd, const char *buf,
1055-
const jsmntok_t *result,
1056-
struct channelfilter_batch *batch)
1057-
{
1058-
plugin_log(cmd->plugin, LOG_UNUSUAL, "failed to disable channel: %.*s",
1059-
json_tok_full_len(result), json_tok_full(buf, result));
1060-
return one_channelfilter_done(cmd, buf, result, batch);
1061-
}
1062-
10631038
static struct command_result *channelfilter_cb(struct payment *payment)
10641039
{
10651040
assert(payment);
@@ -1082,8 +1057,7 @@ static struct command_result *channelfilter_cb(struct payment *payment)
10821057
htlc_max_threshold =
10831058
amount_msat_min(htlc_max_threshold, HTLC_MAX_STOP_MSAT);
10841059

1085-
struct channelfilter_batch *batch =
1086-
tal(cmd, struct channelfilter_batch);
1060+
struct request_batch *batch = tal(cmd, struct request_batch);
10871061
assert(batch);
10881062
batch->num_requests = 0;
10891063
batch->payment = payment;
@@ -1107,8 +1081,8 @@ static struct command_result *channelfilter_cb(struct payment *payment)
11071081
* liquidity to 0 */
11081082
struct out_req *req = jsonrpc_request_start(
11091083
cmd->plugin, cmd, "askrene-inform-channel",
1110-
one_channelfilter_done,
1111-
one_channelfilter_failed, batch);
1084+
one_request_done,
1085+
one_request_failed, batch);
11121086

11131087
/* This constraint only applies to this payment
11141088
*/

0 commit comments

Comments
 (0)