Skip to content

Commit ba92a56

Browse files
committed
lightningd: refactor payment failed.
Don't assume we have an outgoing HTLC at this level. Note that previously we didn't save the failed onion unless it was unparsable: we keep that both for space savings and because our `waitsendpay` logic assumes that when it fetches from the db if there's a failonion it was unparsable! Signed-off-by: Rusty Russell <[email protected]>
1 parent c439589 commit ba92a56

File tree

3 files changed

+62
-38
lines changed

3 files changed

+62
-38
lines changed

lightningd/pay.c

Lines changed: 31 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -410,7 +410,6 @@ immediate_routing_failure(const tal_t *ctx,
410410
static struct routing_failure*
411411
local_routing_failure(const tal_t *ctx,
412412
const struct lightningd *ld,
413-
const struct htlc_out *hout,
414413
enum onion_wire failcode,
415414
const struct wallet_payment *payment)
416415
{
@@ -435,9 +434,6 @@ local_routing_failure(const tal_t *ctx,
435434

436435
routing_failure->msg = NULL;
437436

438-
log_debug(hout->key.channel->log, "local_routing_failure: %u (%s)",
439-
routing_failure->failcode,
440-
onion_wire_name(routing_failure->failcode));
441437
return routing_failure;
442438
}
443439

@@ -545,28 +541,32 @@ remote_routing_failure(const tal_t *ctx,
545541
return routing_failure;
546542
}
547543

548-
void payment_failed(struct lightningd *ld, const struct htlc_out *hout,
544+
void payment_failed(struct lightningd *ld,
545+
struct logger *log,
546+
const struct sha256 *payment_hash,
547+
u64 partid, u64 groupid,
548+
const struct onionreply *failonion,
549+
const u8 *failmsg,
549550
const char *localfail)
550551
{
551552
struct wallet_payment *payment;
552553
struct routing_failure* fail = NULL;
553554
const char *failstr;
554555
enum jsonrpc_errcode pay_errcode;
555-
const u8 *failmsg;
556556
int origin_index;
557557

558558
payment = wallet_payment_by_hash(tmpctx, ld->wallet,
559-
&hout->payment_hash,
560-
hout->partid, hout->groupid);
559+
payment_hash,
560+
partid, groupid);
561561

562562
#ifdef COMPAT_V052
563563
/* Prior to "pay: delete HTLC when we delete payment." we would
564564
* delete a payment on retry, but leave the HTLC. */
565565
if (!payment) {
566-
log_unusual(hout->key.channel->log,
566+
log_unusual(log,
567567
"No payment for %s:"
568568
" was this an old database?",
569-
fmt_sha256(tmpctx, &hout->payment_hash));
569+
fmt_sha256(tmpctx, payment_hash));
570570
return;
571571
}
572572
#else
@@ -578,10 +578,12 @@ void payment_failed(struct lightningd *ld, const struct htlc_out *hout,
578578
if (localfail) {
579579
/* Use temporary_channel_failure if failmsg has it */
580580
enum onion_wire failcode;
581-
failcode = fromwire_peektype(hout->failmsg);
581+
failcode = fromwire_peektype(failmsg);
582582

583-
fail = local_routing_failure(tmpctx, ld, hout, failcode,
584-
payment);
583+
fail = local_routing_failure(tmpctx, ld, failcode, payment);
584+
log_debug(log, "local_routing_failure: %u (%s)",
585+
fail->failcode,
586+
onion_wire_name(fail->failcode));
585587
failstr = localfail;
586588
pay_errcode = PAY_TRY_OTHER_ROUTE;
587589
} else if (payment->path_secrets == NULL) {
@@ -593,11 +595,10 @@ void payment_failed(struct lightningd *ld, const struct htlc_out *hout,
593595
pay_errcode = PAY_UNPARSEABLE_ONION;
594596
fail = NULL;
595597
failstr = NULL;
596-
} else if (hout->failmsg) {
598+
} else if (failmsg) {
597599
/* This can happen when a direct peer told channeld it's a
598600
* malformed onion using update_fail_malformed_htlc. */
599601
failstr = "local failure";
600-
failmsg = hout->failmsg;
601602
origin_index = 0;
602603
pay_errcode = PAY_TRY_OTHER_ROUTE;
603604
goto use_failmsg;
@@ -609,12 +610,11 @@ void payment_failed(struct lightningd *ld, const struct htlc_out *hout,
609610

610611
failmsg = unwrap_onionreply(tmpctx, path_secrets,
611612
tal_count(path_secrets),
612-
hout->failonion, &origin_index);
613+
failonion, &origin_index);
613614
if (!failmsg) {
614-
log_info(hout->key.channel->log,
615-
"htlc %"PRIu64" failed with bad reply (%s)",
616-
hout->key.id,
617-
tal_hex(tmpctx, hout->failonion->contents));
615+
log_info(log,
616+
"htlc failed with bad reply (%s)",
617+
tal_hex(tmpctx, failonion->contents));
618618
/* Cannot record failure. */
619619
fail = NULL;
620620
pay_errcode = PAY_UNPARSEABLE_ONION;
@@ -623,28 +623,27 @@ void payment_failed(struct lightningd *ld, const struct htlc_out *hout,
623623

624624
use_failmsg:
625625
failcode = fromwire_peektype(failmsg);
626-
log_info(hout->key.channel->log,
627-
"htlc %"PRIu64" "
628-
"failed from %ith node "
626+
log_info(log,
627+
"htlc failed from %ith node "
629628
"with code 0x%04x (%s)",
630-
hout->key.id,
631629
origin_index,
632630
failcode, onion_wire_name(failcode));
633631
fail = remote_routing_failure(tmpctx, ld,
634632
payment, failmsg,
635633
origin_index,
636-
hout->key.channel->log,
634+
log,
637635
&pay_errcode);
638636
}
639637
}
640638

641-
wallet_payment_set_status(ld->wallet, &hout->payment_hash,
642-
hout->partid, hout->groupid,
639+
wallet_payment_set_status(ld->wallet, payment_hash,
640+
partid, groupid,
643641
PAYMENT_FAILED, NULL);
644642
wallet_payment_set_failinfo(ld->wallet,
645-
&hout->payment_hash,
646-
hout->partid,
647-
fail ? NULL : hout->failonion,
643+
payment_hash,
644+
partid,
645+
/* We only save failonion if it's unparseable */
646+
fail ? NULL : failonion,
648647
pay_errcode == PAY_DESTINATION_PERM_FAIL,
649648
fail ? fail->erring_index : -1,
650649
fail ? fail->failcode : 0,
@@ -654,8 +653,8 @@ void payment_failed(struct lightningd *ld, const struct htlc_out *hout,
654653
failstr,
655654
fail ? fail->channel_dir : 0);
656655

657-
tell_waiters_failed(ld, &hout->payment_hash, payment, pay_errcode,
658-
hout->failonion, fail, failstr);
656+
tell_waiters_failed(ld, payment_hash, payment, pay_errcode,
657+
failonion, fail, failstr);
659658
}
660659

661660
/* Wait for a payment. If cmd is deleted, then wait_payment()

lightningd/pay.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,13 @@ void payment_succeeded(struct lightningd *ld,
1818
u64 partid, u64 groupid,
1919
const struct preimage *rval);
2020

21-
/* hout->failmsg or hout->failonion must be set. */
22-
void payment_failed(struct lightningd *ld, const struct htlc_out *hout,
21+
/* failmsg or failonion must be set. */
22+
void payment_failed(struct lightningd *ld,
23+
struct logger *log,
24+
const struct sha256 *payment_hash,
25+
u64 partid, u64 groupid,
26+
const struct onionreply *failonion,
27+
const u8 *failmsg,
2328
const char *localfail);
2429

2530
/* This json will be also used in 'sendpay_success' notifictaion. */

lightningd/peer_htlcs.c

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,14 @@ static void fail_out_htlc(struct htlc_out *hout, const char *localfail)
291291
assert(hout->failmsg || hout->failonion);
292292

293293
if (hout->am_origin) {
294-
payment_failed(hout->key.channel->peer->ld, hout, localfail);
294+
payment_failed(hout->key.channel->peer->ld,
295+
hout->key.channel->log,
296+
&hout->payment_hash,
297+
hout->partid,
298+
hout->groupid,
299+
hout->failonion,
300+
hout->failmsg,
301+
localfail);
295302
} else if (hout->in) {
296303
const struct onionreply *failonion;
297304

@@ -598,8 +605,14 @@ static void rcvd_htlc_reply(struct subd *subd, const u8 *msg, const int *fds UNU
598605
char *localfail = tal_fmt(msg, "%s: %s",
599606
onion_wire_name(fromwire_peektype(failmsg)),
600607
failurestr);
601-
payment_failed(ld, hout, localfail);
602-
608+
payment_failed(ld,
609+
hout->key.channel->log,
610+
&hout->payment_hash,
611+
hout->partid,
612+
hout->groupid,
613+
hout->failonion,
614+
hout->failmsg,
615+
localfail);
603616
} else if (hout->in) {
604617
struct onionreply *failonion;
605618
struct short_channel_id scid;
@@ -1783,7 +1796,14 @@ void onchain_failed_our_htlc(const struct channel *channel,
17831796
char *localfail = tal_fmt(channel, "%s: %s",
17841797
onion_wire_name(WIRE_PERMANENT_CHANNEL_FAILURE),
17851798
why);
1786-
payment_failed(ld, hout, localfail);
1799+
payment_failed(ld,
1800+
hout->key.channel->log,
1801+
&hout->payment_hash,
1802+
hout->partid,
1803+
hout->groupid,
1804+
hout->failonion,
1805+
hout->failmsg,
1806+
localfail);
17871807
tal_free(localfail);
17881808
} else if (hout->in) {
17891809
struct short_channel_id scid = channel_scid_or_local_alias(channel);

0 commit comments

Comments
 (0)