Skip to content

Commit

Permalink
renepay: generic callback for batches of requests
Browse files Browse the repository at this point in the history
Use two generic (success and fail) callback functions to handle batches
of RPC requests. This removes duplicate code.

Signed-off-by: Lagrang3 <[email protected]>
  • Loading branch information
Lagrang3 committed Sep 17, 2024
1 parent 42d5600 commit 944d20d
Showing 1 changed file with 42 additions and 68 deletions.
110 changes: 42 additions & 68 deletions plugins/renepay/mods.c
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,41 @@ static struct command_result *payment_rpc_failure(struct command *cmd,
json_tok_full_len(toks), json_tok_full(buffer, toks));
}

/* Generic handling of multiple RPC calls */
struct request_batch {
size_t num_requests;
struct payment *payment;
};

static struct command_result *one_request_done(struct command *cmd,
const char *buf UNUSED,
const jsmntok_t *result UNUSED,
struct request_batch *batch)
{
assert(batch->num_requests);
assert(batch->payment);
batch->num_requests--;

if (!batch->num_requests) {
struct payment *p = batch->payment;
tal_free(batch);
return payment_continue(p);
}

return command_still_pending(cmd);
}

static struct command_result *one_request_failed(struct command *cmd,
const char *buf,
const jsmntok_t *result,
struct request_batch *batch)
{
plugin_log(cmd->plugin, LOG_UNUSUAL, "failed RPC batch: %.*s",
json_tok_full_len(result), json_tok_full(buf, result));
return one_request_done(cmd, buf, result, batch);
}


/*****************************************************************************
* previoussuccess
*
Expand Down Expand Up @@ -354,36 +389,6 @@ REGISTER_PAYMENT_MODIFIER(refreshgossmap, refreshgossmap_cb);
* network.
*/

struct routehints_batch {
size_t num_elements;
struct payment *payment;
};

static struct command_result *add_one_hint_done(struct command *cmd,
const char *buf UNUSED,
const jsmntok_t *result UNUSED,
struct routehints_batch *batch)
{
assert(batch->num_elements);
assert(batch->payment);
batch->num_elements--;

if (!batch->num_elements)
return payment_continue(batch->payment);

return command_still_pending(cmd);
}

static struct command_result *
add_one_hint_failed(struct command *cmd, const char *buf,
const jsmntok_t *result, struct routehints_batch *batch)
{
plugin_log(cmd->plugin, LOG_UNUSUAL,
"failed to create channel hint: %.*s",
json_tok_full_len(result), json_tok_full(buf, result));
return add_one_hint_done(cmd, buf, result, batch);
}

static struct command_result *routehints_cb(struct payment *payment)
{
struct command *cmd = payment_command(payment);
Expand All @@ -406,8 +411,8 @@ static struct command_result *routehints_cb(struct payment *payment)

const struct node_id *destination = &payment->payment_info.destination;
const size_t nhints = tal_count(routehints);
struct routehints_batch *batch = tal(cmd, struct routehints_batch);
batch->num_elements = 0;
struct request_batch *batch = tal(cmd, struct request_batch);
batch->num_requests = 0;
batch->payment = payment;

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

struct out_req *req = jsonrpc_request_start(
cmd->plugin, cmd, "askrene-create-channel",
add_one_hint_done, add_one_hint_failed, batch);
one_request_done, one_request_failed, batch);

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

send_outreq(cmd->plugin, req);

batch->num_elements++;
batch->num_requests++;
end = &r[j].pubkey;
}
}
Expand Down Expand Up @@ -1030,36 +1035,6 @@ REGISTER_PAYMENT_MODIFIER(knowledgerelax, knowledgerelax_cb);
* FIXME: shall we set these threshold parameters as plugin options?
*/

struct channelfilter_batch {
size_t num_requests;
struct payment *payment;
};

static struct command_result *
one_channelfilter_done(struct command *cmd, const char *buf UNUSED,
const jsmntok_t *result UNUSED,
struct channelfilter_batch *batch)
{
assert(batch->num_requests);
assert(batch->payment);
batch->num_requests--;

if (!batch->num_requests)
return payment_continue(batch->payment);

return command_still_pending(cmd);
}

static struct command_result *
one_channelfilter_failed(struct command *cmd, const char *buf,
const jsmntok_t *result,
struct channelfilter_batch *batch)
{
plugin_log(cmd->plugin, LOG_UNUSUAL, "failed to disable channel: %.*s",
json_tok_full_len(result), json_tok_full(buf, result));
return one_channelfilter_done(cmd, buf, result, batch);
}

static struct command_result *channelfilter_cb(struct payment *payment)
{
assert(payment);
Expand All @@ -1082,8 +1057,7 @@ static struct command_result *channelfilter_cb(struct payment *payment)
htlc_max_threshold =
amount_msat_min(htlc_max_threshold, HTLC_MAX_STOP_MSAT);

struct channelfilter_batch *batch =
tal(cmd, struct channelfilter_batch);
struct request_batch *batch = tal(cmd, struct request_batch);
assert(batch);
batch->num_requests = 0;
batch->payment = payment;
Expand All @@ -1107,8 +1081,8 @@ static struct command_result *channelfilter_cb(struct payment *payment)
* liquidity to 0 */
struct out_req *req = jsonrpc_request_start(
cmd->plugin, cmd, "askrene-inform-channel",
one_channelfilter_done,
one_channelfilter_failed, batch);
one_request_done,
one_request_failed, batch);

/* This constraint only applies to this payment
*/
Expand Down

0 comments on commit 944d20d

Please sign in to comment.