Skip to content

Commit

Permalink
pay: Factor out handling of waitsendpay errors from deciding what t…
Browse files Browse the repository at this point in the history
…o do due to those errors.
  • Loading branch information
ZmnSCPxj committed Dec 26, 2019
1 parent 138a7e0 commit c340ae8
Showing 1 changed file with 42 additions and 4 deletions.
46 changes: 42 additions & 4 deletions plugins/pay.c
Expand Up @@ -37,6 +37,10 @@ struct pay_attempt {
struct json_out *failure;
/* The non-failure result (NULL on failure) */
const char *result;
/* A string of a JSON error message from `waitsendpay`.
* NULL if no failure from `waitsendpay`.
*/
const char *waitsendpay_err;
};

struct pay_status {
Expand Down Expand Up @@ -320,15 +324,31 @@ static struct command_result *next_routehint(struct command *cmd,
"Could not find a route");
}

static struct command_result *waitsendpay_error(struct command *cmd,
const char *buf,
const jsmntok_t *error,
struct pay_command *pc)
/* Perform handling in case of payment error. */
static struct command_result *
handle_payment_error(struct command *cmd,
struct pay_command *pc)
{
struct pay_attempt *attempt = current_attempt(pc);
const char *buf;
const jsmntok_t *error;
bool parse_valid;
const jsmntok_t *codetok, *failcodetok, *nodeidtok, *scidtok, *dirtok;
int code, failcode;
bool node_err = false;

assert(attempt->waitsendpay_err);
/* Make the error string temporary to this function. */
buf = tal_steal(tmpctx, attempt->waitsendpay_err);
attempt->waitsendpay_err = NULL;
/* Re-parse the string.
* This should succeed since it succeeded before.
*/
error = json_parse_input(tmpctx, buf, tal_count(buf) - 1,
&parse_valid);
assert(parse_valid);
assert(error);

attempt_failed_tok(pc, "waitsendpay", buf, error);

codetok = json_get_member(buf, error, "code");
Expand Down Expand Up @@ -402,6 +422,23 @@ static struct command_result *waitsendpay_error(struct command *cmd,
pc->excludes[tal_count(pc->excludes)-1]);
}

/* Save the `waitsendpay` error message for later processing by
* handle_payment_error.
*/
static struct command_result * waitsendpay_error(struct command *cmd,
const char *buf,
const jsmntok_t *error,
struct pay_command *pc)
{
struct pay_attempt *attempt = current_attempt(pc);

attempt->waitsendpay_err = tal_strndup(pc,
buf + error->start,
error->end - error->start);

return handle_payment_error(cmd, pc);
}

static struct command_result *waitsendpay_done(struct command *cmd,
const char *buf,
const jsmntok_t *result,
Expand Down Expand Up @@ -809,6 +846,7 @@ static struct command_result *start_pay_attempt(struct command *cmd,
attempt->result = NULL;
attempt->sendpay = false;
attempt->why = tal_vfmt(pc->ps, fmt, ap);
attempt->waitsendpay_err = NULL;
va_end(ap);

return execute_getroute(cmd, pc);
Expand Down

0 comments on commit c340ae8

Please sign in to comment.