paymod: Add function to set the current step

This is necessary otherwise we would not be calling modifiers for the newly
set state which can lead to unexpected results.
This commit is contained in:
Christian Decker 2020-06-09 15:52:50 +02:00
parent 4aed45e98d
commit 4b3e849ce9
2 changed files with 26 additions and 4 deletions

View File

@ -731,7 +731,7 @@ payment_waitsendpay_finished(struct command *cmd, const char *buffer,
json_tok_full_len(toks), json_tok_full(buffer, toks));
if (p->result->state == PAYMENT_COMPLETE) {
p->step = PAYMENT_STEP_SUCCESS;
payment_set_step(p, PAYMENT_STEP_SUCCESS);
p->end_time = time_now();
payment_continue(p);
return command_still_pending(cmd);
@ -1230,6 +1230,12 @@ static void payment_finished(struct payment *p)
}
}
void payment_set_step(struct payment *p, enum payment_step newstep)
{
p->current_modifier = -1;
p->step = newstep;
}
void payment_continue(struct payment *p)
{
struct payment_modifier *mod;
@ -1279,9 +1285,15 @@ void payment_continue(struct payment *p)
void payment_fail(struct payment *p, const char *fmt, ...)
{
va_list ap;
p->end_time = time_now();
p->step = PAYMENT_STEP_FAILED;
p->failreason = tal_steal(p, reason);
payment_set_step(p, PAYMENT_STEP_FAILED);
va_start(ap, fmt);
p->failreason = tal_vfmt(p, fmt, ap);
va_end(ap);
plugin_log(p->plugin, LOG_INFORM, "%s", p->failreason);
payment_continue(p);
}
@ -1398,7 +1410,7 @@ static inline void retry_step_cb(struct retry_mod_data *rd,
if (rdata->retries > 0) {
subpayment = payment_new(p, NULL, p, p->modifiers);
payment_start(subpayment);
p->step = PAYMENT_STEP_RETRY;
payment_set_step(p, PAYMENT_STEP_RETRY);
subpayment->why =
tal_fmt(subpayment, "Still have %d attempts left",
rdata->retries - 1);

View File

@ -344,6 +344,16 @@ struct payment *payment_new(tal_t *ctx, struct command *cmd,
void payment_start(struct payment *p);
void payment_continue(struct payment *p);
/**
* Set the payment to the current step.
*
* This must be used by modifiers if they want to skip to a specific step. It
* ensures that the internal state is reset correctly and that all modifier
* callbacks are called once `payment_continue` is called again.
*/
void payment_set_step(struct payment *p, enum payment_step newstep);
/* Fails a partial payment and continues with the core flow. */
void payment_fail(struct payment *p, const char *fmt, ...) PRINTF_FMT(2,3);