From 8207b4eac872f7e2bff50597dea42370903c07a3 Mon Sep 17 00:00:00 2001 From: Christian Decker Date: Mon, 18 May 2020 17:46:50 +0200 Subject: [PATCH] paymod: Use reasonable partids We were just handwaving the partid generation, which broke some tests that expected the first payment attempt to always have partid=0, so here we just track the partids assigned in the payment tree, starting at 0. --- plugins/libplugin-pay.c | 21 ++++++++++++++++++--- plugins/libplugin-pay.h | 2 ++ 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/plugins/libplugin-pay.c b/plugins/libplugin-pay.c index 06fd91a75..a39c5eb40 100644 --- a/plugins/libplugin-pay.c +++ b/plugins/libplugin-pay.c @@ -42,6 +42,10 @@ struct payment *payment_new(tal_t *ctx, struct command *cmd, p->destination = p->getroute_destination = parent->destination; p->amount = parent->amount; p->payment_hash = parent->payment_hash; + p->partid = payment_root(p->parent)->next_partid++; + } else { + p->partid = 0; + p->next_partid = 1; } /* Initialize all modifier data so we can point to the fields when @@ -60,6 +64,14 @@ struct payment *payment_new(tal_t *ctx, struct command *cmd, return p; } +struct payment *payment_root(struct payment *p) +{ + if (p->parent == NULL) + return p; + else + return payment_root(p->parent); +} + /* Generic handler for RPC failures that should end up failing the payment. */ static struct command_result *payment_rpc_failure(struct command *cmd, const char *buffer, @@ -307,11 +319,14 @@ static struct payment_result *tal_sendpay_result_from_json(const tal_t *ctx, result = tal(ctx, struct payment_result); + /* If the partid is 0 it'd be omitted in waitsendpay, fix this here. */ + if (partidtok != NULL) + json_to_u32(buffer, partidtok, &result->partid); + else + result->partid = 0; + json_to_u64(buffer, idtok, &result->id); - json_to_u32(buffer, partidtok, &result->partid); - /* TODO Fetch the payment_hash here */ json_to_msat(buffer, senttok, &result->amount_sent); - if (json_tok_streq(buffer, statustok, "pending")) { result->state = PAYMENT_PENDING; } else if (json_tok_streq(buffer, statustok, "complete")) { diff --git a/plugins/libplugin-pay.h b/plugins/libplugin-pay.h index c8921a709..6eb366861 100644 --- a/plugins/libplugin-pay.h +++ b/plugins/libplugin-pay.h @@ -117,6 +117,7 @@ struct payment { struct sha256 *payment_hash; u32 partid; + u32 next_partid; /* Destination we should ask `getroute` for. This might differ from * the above destination if we use rendez-vous routing of blinded @@ -205,5 +206,6 @@ struct payment *payment_new(tal_t *ctx, struct command *cmd, void payment_start(struct payment *p); void payment_continue(struct payment *p); +struct payment *payment_root(struct payment *p); #endif /* LIGHTNING_PLUGINS_LIBPLUGIN_PAY_H */