lightningd: extract check_progress() from send_payment_core().

We'll need to do this for self-pay as well.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
Rusty Russell
2023-07-22 16:50:12 +09:30
parent b4f0929280
commit 8c64f2311e

View File

@@ -879,35 +879,24 @@ found:
return channel; return channel;
} }
/* destination/route_channels/route_nodes are NULL (and path_secrets may be NULL) /* Check if payment already in progress. Returns NULL if all good;
* if we're sending a raw onion. */ * sets old_payment to a previous attempt if there is one (otherwise
static struct command_result * * NULL). */
send_payment_core(struct lightningd *ld, static struct command_result *check_progress(struct lightningd *ld,
struct command *cmd, struct command *cmd,
const struct sha256 *rhash, const struct sha256 *rhash,
u64 partid,
u64 group,
const struct route_hop *first_hop,
struct amount_msat msat, struct amount_msat msat,
struct amount_msat total_msat, struct amount_msat total_msat,
const char *label TAKES, u64 partid,
const char *invstring TAKES, u64 group,
const char *description TAKES,
const struct onionpacket *packet,
const struct node_id *destination, const struct node_id *destination,
struct node_id *route_nodes TAKES, const struct wallet_payment **old_payment)
struct short_channel_id *route_channels TAKES,
struct secret *path_secrets,
const struct sha256 *local_invreq_id)
{ {
const struct wallet_payment **payments, *old_payment = NULL; const struct wallet_payment **payments;
struct channel *channel;
const u8 *failmsg;
struct htlc_out *hout;
struct routing_failure *fail;
struct amount_msat msat_already_pending = AMOUNT_MSAT(0);
bool have_complete = false; bool have_complete = false;
struct command_result *invreq_err; struct amount_msat msat_already_pending = AMOUNT_MSAT(0);
*old_payment = NULL;
/* Now, do we already have one or more payments? */ /* Now, do we already have one or more payments? */
payments = wallet_payment_list(tmpctx, ld->wallet, rhash); payments = wallet_payment_list(tmpctx, ld->wallet, rhash);
@@ -1022,7 +1011,7 @@ send_payment_core(struct lightningd *ld,
case PAYMENT_FAILED: case PAYMENT_FAILED:
if (payments[i]->partid == partid) if (payments[i]->partid == partid)
old_payment = payments[i]; *old_payment = payments[i];
} }
/* There is no way for us to add a payment with the /* There is no way for us to add a payment with the
* same (payment_hash, partid, groupid) tuple since * same (payment_hash, partid, groupid) tuple since
@@ -1039,7 +1028,6 @@ send_payment_core(struct lightningd *ld,
type_to_string(tmpctx, struct sha256, rhash), group, type_to_string(tmpctx, struct sha256, rhash), group,
partid); partid);
} }
} }
/* If any part has succeeded, you can't start a new one! */ /* If any part has succeeded, you can't start a new one! */
@@ -1064,9 +1052,47 @@ send_payment_core(struct lightningd *ld,
&total_msat)); &total_msat));
} }
invreq_err = check_invoice_request_usage(cmd, local_invreq_id); return NULL;
if (invreq_err) }
return invreq_err;
/* destination/route_channels/route_nodes are NULL (and path_secrets may be NULL)
* if we're sending a raw onion. */
static struct command_result *
send_payment_core(struct lightningd *ld,
struct command *cmd,
const struct sha256 *rhash,
u64 partid,
u64 group,
const struct route_hop *first_hop,
struct amount_msat msat,
struct amount_msat total_msat,
const char *label TAKES,
const char *invstring TAKES,
const char *description TAKES,
const struct onionpacket *packet,
const struct node_id *destination,
struct node_id *route_nodes TAKES,
struct short_channel_id *route_channels TAKES,
struct secret *path_secrets,
const struct sha256 *local_invreq_id)
{
const struct wallet_payment *old_payment;
struct channel *channel;
const u8 *failmsg;
struct htlc_out *hout;
struct routing_failure *fail;
struct command_result *ret;
struct wallet_payment *payment;
/* Reconcile this with previous attempts */
ret = check_progress(ld, cmd, rhash, msat, total_msat, partid, group, destination,
&old_payment);
if (ret)
return ret;
ret = check_invoice_request_usage(cmd, local_invreq_id);
if (ret)
return ret;
channel = find_channel_for_htlc_add(ld, &first_hop->node_id, channel = find_channel_for_htlc_add(ld, &first_hop->node_id,
&first_hop->scid, &msat); &first_hop->scid, &msat);
@@ -1112,7 +1138,7 @@ send_payment_core(struct lightningd *ld,
} }
/* If hout fails, payment should be freed too. */ /* If hout fails, payment should be freed too. */
struct wallet_payment *payment = tal(hout, struct wallet_payment); payment = tal(hout, struct wallet_payment);
payment->id = 0; payment->id = 0;
payment->payment_hash = *rhash; payment->payment_hash = *rhash;
payment->partid = partid; payment->partid = partid;