Skip to content

Commit 7d8c723

Browse files
rustyrussellvincenzopalazzo
authored andcommitted
libplugin: cleanly separate apply and unapplying payment route.
They're close, but different. In particular: - Removal can't fail. - Removal is much simpler. Signed-off-by: Rusty Russell <[email protected]>
1 parent aff8f0f commit 7d8c723

File tree

1 file changed

+40
-33
lines changed

1 file changed

+40
-33
lines changed

plugins/libplugin-pay.c

Lines changed: 40 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -505,22 +505,16 @@ static struct channel_hint *payment_chanhints_get(struct payment *p,
505505
* calls don't accidentally try to use those out-of-date estimates. We unapply
506506
* if the payment failed, i.e., all HTLCs we might have added have been torn
507507
* down again. Finally we leave the update in place if the payment went
508-
* through, since the balances really changed in that case. The `remove`
509-
* argument indicates whether we want to apply (`remove=false`), or clear a
510-
* prior application (`remove=true`). */
511-
static bool payment_chanhints_apply_route(struct payment *p, bool remove)
508+
* through, since the balances really changed in that case.
509+
*/
510+
static bool payment_chanhints_apply_route(struct payment *p)
512511
{
513512
bool apply;
514513
struct route_hop *curhop;
515514
struct channel_hint *curhint;
516515
struct payment *root = payment_root(p);
517516
assert(p->route != NULL);
518517

519-
/* No need to check for applicability if we increase
520-
* capacity and budgets. */
521-
if (remove)
522-
goto apply_changes;
523-
524518
/* First round: make sure we can cleanly apply the update. */
525519
for (size_t i = 0; i < tal_count(p->route); i++) {
526520
curhop = &p->route[i];
@@ -566,7 +560,6 @@ static bool payment_chanhints_apply_route(struct payment *p, bool remove)
566560
}
567561
}
568562

569-
apply_changes:
570563
/* Second round: apply the changes, now that we know they'll succeed. */
571564
for (size_t i = 0; i < tal_count(p->route); i++) {
572565
curhop = &p->route[i];
@@ -577,29 +570,12 @@ static bool payment_chanhints_apply_route(struct payment *p, bool remove)
577570
/* Update the number of htlcs for any local
578571
* channel in the route */
579572
if (curhint->local) {
580-
if (remove)
581-
curhint->local->htlc_budget++;
582-
else
583-
curhint->local->htlc_budget--;
573+
curhint->local->htlc_budget--;
584574
}
585575

586-
/* Don't get fancy and replace this with remove && !amount_msat_add
587-
* It won't work! */
588-
if (remove) {
589-
if (!amount_msat_add(
590-
&curhint->estimated_capacity,
591-
curhint->estimated_capacity,
592-
curhop->amount)) {
593-
/* This should never happen, it'd mean
594-
* that we unapply a route that would
595-
* result in a msatoshi
596-
* wrap-around. */
597-
abort();
598-
}
599-
} else if (!amount_msat_sub(
600-
&curhint->estimated_capacity,
601-
curhint->estimated_capacity,
602-
curhop->amount)) {
576+
if (!amount_msat_sub(&curhint->estimated_capacity,
577+
curhint->estimated_capacity,
578+
curhop->amount)) {
603579
/* Given our preemptive test
604580
* above, this should never
605581
* happen either. */
@@ -609,6 +585,37 @@ static bool payment_chanhints_apply_route(struct payment *p, bool remove)
609585
return true;
610586
}
611587

588+
/* Undo route changes above */
589+
static void payment_chanhints_unapply_route(struct payment *p)
590+
{
591+
struct payment *root = payment_root(p);
592+
593+
for (size_t i = 0; i < tal_count(p->route); i++) {
594+
struct route_hop *curhop;
595+
struct channel_hint *curhint;
596+
597+
curhop = &p->route[i];
598+
curhint = payment_chanhints_get(root, curhop);
599+
if (!curhint)
600+
continue;
601+
602+
/* Update the number of htlcs for any local
603+
* channel in the route */
604+
if (curhint->local)
605+
curhint->local->htlc_budget++;
606+
607+
if (!amount_msat_add(&curhint->estimated_capacity,
608+
curhint->estimated_capacity,
609+
curhop->amount)) {
610+
/* This should never happen, it'd mean
611+
* that we unapply a route that would
612+
* result in a msatoshi
613+
* wrap-around. */
614+
abort();
615+
}
616+
}
617+
}
618+
612619
static const struct short_channel_id_dir *
613620
payment_get_excluded_channels(const tal_t *ctx, struct payment *p)
614621
{
@@ -1585,7 +1592,7 @@ payment_waitsendpay_finished(struct command *cmd, const char *buffer,
15851592
return command_still_pending(cmd);
15861593
}
15871594

1588-
payment_chanhints_apply_route(p, true);
1595+
payment_chanhints_unapply_route(p);
15891596

15901597
/* Tell gossipd, if we received an update */
15911598
update = channel_update_from_onion_error(tmpctx, p->result->raw_message);
@@ -1780,7 +1787,7 @@ static void payment_compute_onion_payloads(struct payment *p)
17801787
/* Now that we are about to fix the route parameters by
17811788
* encoding them in an onion is the right time to update the
17821789
* channel hints. */
1783-
if (!payment_chanhints_apply_route(p, false)) {
1790+
if (!payment_chanhints_apply_route(p)) {
17841791
/* We can still end up with a failed channel_hints
17851792
* update, either because a plugin changed the route,
17861793
* or because a modifier was not synchronous, allowing

0 commit comments

Comments
 (0)