forked from ElementsProject/lightning
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pay.c
1518 lines (1340 loc) · 45.4 KB
/
pay.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "pay.h"
#include <ccan/str/hex/hex.h>
#include <ccan/tal/str/str.h>
#include <common/bolt11.h>
#include <common/json_command.h>
#include <common/json_helpers.h>
#include <common/jsonrpc_errors.h>
#include <common/onion.h>
#include <common/onionreply.h>
#include <common/param.h>
#include <common/timeout.h>
#include <gossipd/gen_gossip_wire.h>
#include <lightningd/chaintopology.h>
#include <lightningd/json.h>
#include <lightningd/jsonrpc.h>
#include <lightningd/lightningd.h>
#include <lightningd/log.h>
#include <lightningd/notification.h>
#include <lightningd/options.h>
#include <lightningd/peer_control.h>
#include <lightningd/peer_htlcs.h>
#include <lightningd/subd.h>
#include <sodium/randombytes.h>
/* Routing failure object */
struct routing_failure {
unsigned int erring_index;
enum onion_type failcode;
const struct node_id *erring_node;
const struct short_channel_id *erring_channel;
int channel_dir;
/* If remote sent us a message, this is it. */
const u8 *msg;
};
/* sendpay command */
struct sendpay_command {
struct list_node list;
struct sha256 payment_hash;
u64 partid;
struct command *cmd;
};
static void destroy_sendpay_command(struct sendpay_command *pc)
{
list_del(&pc->list);
}
/* Owned by cmd, if cmd is deleted, then sendpay_success/sendpay_fail will
* no longer be called. */
static void
add_sendpay_waiter(struct lightningd *ld,
struct command *cmd,
const struct sha256 *payment_hash,
u64 partid)
{
struct sendpay_command *pc = tal(cmd, struct sendpay_command);
pc->payment_hash = *payment_hash;
pc->partid = partid;
pc->cmd = cmd;
list_add(&ld->sendpay_commands, &pc->list);
tal_add_destructor(pc, destroy_sendpay_command);
}
/* Owned by cmd, if cmd is deleted, then sendpay_success/sendpay_fail will
* no longer be called. */
static void
add_waitsendpay_waiter(struct lightningd *ld,
struct command *cmd,
const struct sha256 *payment_hash,
u64 partid)
{
struct sendpay_command *pc = tal(cmd, struct sendpay_command);
pc->payment_hash = *payment_hash;
pc->partid = partid;
pc->cmd = cmd;
list_add(&ld->waitsendpay_commands, &pc->list);
tal_add_destructor(pc, destroy_sendpay_command);
}
/* Outputs fields, not a separate object*/
void json_add_payment_fields(struct json_stream *response,
const struct wallet_payment *t)
{
json_add_u64(response, "id", t->id);
json_add_sha256(response, "payment_hash", &t->payment_hash);
if (t->partid)
json_add_u64(response, "partid", t->partid);
if (t->destination != NULL)
json_add_node_id(response, "destination", t->destination);
/* If we have a 0 amount delivered at the remote end we simply don't
* know since the onion was generated externally. */
if (amount_msat_greater(t->msatoshi, AMOUNT_MSAT(0)))
json_add_amount_msat_compat(response, t->msatoshi, "msatoshi",
"amount_msat");
else
json_add_null(response, "amount_msat");
json_add_amount_msat_compat(response, t->msatoshi_sent,
"msatoshi_sent", "amount_sent_msat");
json_add_u64(response, "created_at", t->timestamp);
switch (t->status) {
case PAYMENT_PENDING:
json_add_string(response, "status", "pending");
break;
case PAYMENT_COMPLETE:
json_add_string(response, "status", "complete");
break;
case PAYMENT_FAILED:
json_add_string(response, "status", "failed");
break;
}
if (t->payment_preimage)
json_add_preimage(response, "payment_preimage",
t->payment_preimage);
if (t->label)
json_add_string(response, "label", t->label);
if (t->bolt11)
json_add_string(response, "bolt11", t->bolt11);
if (t->failonion)
json_add_hex(response, "erroronion", t->failonion,
tal_count(t->failonion));
}
static struct command_result *sendpay_success(struct command *cmd,
const struct wallet_payment *payment)
{
struct json_stream *response;
assert(payment->status == PAYMENT_COMPLETE);
response = json_stream_success(cmd);
json_add_payment_fields(response, payment);
return command_success(cmd, response);
}
static void
json_add_routefail_info(struct json_stream *js,
unsigned int erring_index,
enum onion_type failcode,
const struct node_id *erring_node,
const struct short_channel_id *erring_channel,
int channel_dir,
const u8 *msg)
{
const char *failcodename = onion_type_name(failcode);
json_add_num(js, "erring_index", erring_index);
json_add_num(js, "failcode", failcode);
/* FIXME: Better way to detect this? */
if (!strstarts(failcodename, "INVALID "))
json_add_string(js, "failcodename", failcodename);
if (erring_node != NULL)
json_add_node_id(js, "erring_node", erring_node);
if (erring_channel != NULL) {
json_add_short_channel_id(js, "erring_channel", erring_channel);
json_add_num(js, "erring_direction", channel_dir);
}
if (msg)
json_add_hex_talarr(js, "raw_message", msg);
}
void json_sendpay_fail_fields(struct json_stream *js,
const struct wallet_payment *payment,
errcode_t pay_errcode,
const struct onionreply *onionreply,
const struct routing_failure *fail)
{
/* "immediate_routing_failure" is before payment creation. */
if (payment)
json_add_payment_fields(js, payment);
if (pay_errcode == PAY_UNPARSEABLE_ONION && onionreply)
json_add_hex_talarr(js, "onionreply", onionreply->contents);
else
json_add_routefail_info(js,
fail->erring_index,
fail->failcode,
fail->erring_node,
fail->erring_channel,
fail->channel_dir,
fail->msg);
}
static const char *sendpay_errmsg_fmt(const tal_t *ctx, errcode_t pay_errcode,
const struct routing_failure *fail,
const char *details)
{
char *errmsg;
if (pay_errcode == PAY_UNPARSEABLE_ONION)
errmsg = "Malformed error reply";
else {
assert(fail);
errmsg = tal_fmt(ctx, "failed: %s (%s)",
onion_type_name(fail->failcode), details);
}
return errmsg;
}
/* onionreply used if pay_errcode == PAY_UNPARSEABLE_ONION */
static struct command_result *
sendpay_fail(struct command *cmd,
const struct wallet_payment *payment,
errcode_t pay_errcode,
const struct onionreply *onionreply,
const struct routing_failure *fail,
const char *errmsg)
{
struct json_stream *data;
data = json_stream_fail(cmd, pay_errcode,
errmsg);
json_sendpay_fail_fields(data,
payment,
pay_errcode,
onionreply,
fail);
json_object_end(data);
return command_failed(cmd, data);
}
/* We defer sendpay "success" until we know it's pending; consumes cmd */
static struct command_result *
json_sendpay_in_progress(struct command *cmd,
const struct wallet_payment *payment)
{
struct json_stream *response = json_stream_success(cmd);
json_add_string(response, "message",
"Monitor status with listpays or waitsendpay");
json_add_payment_fields(response, payment);
return command_success(cmd, response);
}
static void tell_waiters_failed(struct lightningd *ld,
const struct sha256 *payment_hash,
const struct wallet_payment *payment,
errcode_t pay_errcode,
const struct onionreply *onionreply,
const struct routing_failure *fail,
const char *details)
{
struct sendpay_command *pc;
struct sendpay_command *next;
const char *errmsg =
sendpay_errmsg_fmt(tmpctx, pay_errcode, fail, details);
/* Careful: sendpay_fail deletes cmd */
list_for_each_safe(&ld->waitsendpay_commands, pc, next, list) {
if (!sha256_eq(payment_hash, &pc->payment_hash))
continue;
if (payment->partid != pc->partid)
continue;
sendpay_fail(pc->cmd, payment, pay_errcode, onionreply, fail,
errmsg);
}
notify_sendpay_failure(ld,
payment,
pay_errcode,
onionreply,
fail,
errmsg);
}
static void tell_waiters_success(struct lightningd *ld,
const struct sha256 *payment_hash,
struct wallet_payment *payment)
{
struct sendpay_command *pc;
struct sendpay_command *next;
/* Careful: sendpay_success deletes cmd */
list_for_each_safe(&ld->waitsendpay_commands, pc, next, list) {
if (!sha256_eq(payment_hash, &pc->payment_hash))
continue;
if (payment->partid != pc->partid)
continue;
sendpay_success(pc->cmd, payment);
}
notify_sendpay_success(ld, payment);
}
void payment_succeeded(struct lightningd *ld, struct htlc_out *hout,
const struct preimage *rval)
{
struct wallet_payment *payment;
wallet_payment_set_status(ld->wallet, &hout->payment_hash,
hout->partid,
PAYMENT_COMPLETE, rval);
payment = wallet_payment_by_hash(tmpctx, ld->wallet,
&hout->payment_hash,
hout->partid);
assert(payment);
tell_waiters_success(ld, &hout->payment_hash, payment);
}
/* Return a struct routing_failure for an immediate failure
* (returned directly from send_htlc_out). The returned
* failure is allocated from the given context. */
static struct routing_failure*
immediate_routing_failure(const tal_t *ctx,
const struct lightningd *ld,
enum onion_type failcode,
const struct short_channel_id *channel0,
const struct node_id *dstid)
{
struct routing_failure *routing_failure;
assert(failcode);
routing_failure = tal(ctx, struct routing_failure);
routing_failure->erring_index = 0;
routing_failure->failcode = failcode;
routing_failure->erring_node =
tal_dup(routing_failure, struct node_id, &ld->id);
routing_failure->erring_channel =
tal_dup(routing_failure, struct short_channel_id, channel0);
routing_failure->channel_dir = node_id_idx(&ld->id, dstid);
routing_failure->msg = NULL;
return routing_failure;
}
/* Return a struct routing_failure for a local failure allocated
* from the given context. */
static struct routing_failure*
local_routing_failure(const tal_t *ctx,
const struct lightningd *ld,
const struct htlc_out *hout,
const struct wallet_payment *payment)
{
struct routing_failure *routing_failure;
routing_failure = tal(ctx, struct routing_failure);
routing_failure->erring_index = 0;
routing_failure->failcode = fromwire_peektype(hout->failmsg);
routing_failure->erring_node =
tal_dup(routing_failure, struct node_id, &ld->id);
if (payment->route_nodes != NULL && payment->route_channels != NULL) {
routing_failure->erring_channel =
tal_dup(routing_failure, struct short_channel_id,
&payment->route_channels[0]);
routing_failure->channel_dir =
node_id_idx(&ld->id, &payment->route_nodes[0]);
} else {
routing_failure->erring_channel = NULL;
}
routing_failure->msg = NULL;
log_debug(hout->key.channel->log, "local_routing_failure: %u (%s)",
routing_failure->failcode,
onion_type_name(routing_failure->failcode));
return routing_failure;
}
/* Fills in *pay_errcode with PAY_TRY_OTHER_ROUTE or PAY_DESTINATION_PERM_FAIL */
static struct routing_failure*
remote_routing_failure(const tal_t *ctx,
struct lightningd *ld,
const struct wallet_payment *payment,
const u8 *failuremsg,
int origin_index,
struct log *log,
errcode_t *pay_errcode)
{
enum onion_type failcode = fromwire_peektype(failuremsg);
struct routing_failure *routing_failure;
const struct node_id *route_nodes;
const struct node_id *erring_node;
const struct short_channel_id *route_channels;
const struct short_channel_id *erring_channel;
int dir;
routing_failure = tal(ctx, struct routing_failure);
route_nodes = payment->route_nodes;
route_channels = payment->route_channels;
assert(route_nodes == NULL || origin_index < tal_count(route_nodes));
/* Either we have both channels and nodes, or neither */
assert((route_nodes == NULL) == (route_channels == NULL));
if (route_nodes == NULL) {
/* This means we have the `shared_secrets`, but cannot infer
* the erring channel and node since we don't have them. This
* can happen if the payment was initialized using `sendonion`
* and the `shared_secrets` where specified. */
dir = 0;
erring_channel = NULL;
erring_node = NULL;
/* We don't know if there's another route, that'd depend on
* where the failure occured and whether it was a node
* failure. Let's assume it wasn't a terminal one, and have
* the sendonion caller deal with the actual decision. */
*pay_errcode = PAY_TRY_OTHER_ROUTE;
} else if (origin_index == tal_count(route_nodes) - 1) {
/* If any channel is to blame, it's the last one. */
erring_channel = &route_channels[origin_index];
/* Single hop? */
if (origin_index == 0)
dir = node_id_idx(&ld->id,
&route_nodes[origin_index]);
else
dir = node_id_idx(&route_nodes[origin_index - 1],
&route_nodes[origin_index]);
/* BOLT #4:
*
* - if the _final node_ is returning the error:
* - if the PERM bit is set:
* - SHOULD fail the payment.
* */
if (failcode & BADONION)
*pay_errcode = PAY_UNPARSEABLE_ONION;
else if (failcode & PERM)
*pay_errcode = PAY_DESTINATION_PERM_FAIL;
else
/* FIXME: not right for WIRE_FINAL_EXPIRY_TOO_SOON */
*pay_errcode = PAY_TRY_OTHER_ROUTE;
erring_node = &route_nodes[origin_index];
} else {
u8 *gossip_msg;
*pay_errcode = PAY_TRY_OTHER_ROUTE;
/* Report the *next* channel as failing. */
erring_channel = &route_channels[origin_index + 1];
dir = node_id_idx(&route_nodes[origin_index],
&route_nodes[origin_index+1]);
/* If the error is a BADONION, then it's on behalf of the
* following node. */
if (failcode & BADONION) {
log_debug(log, "failcode %u from onionreply %s",
failcode, tal_hex(tmpctx, failuremsg));
erring_node = &route_nodes[origin_index + 1];
} else
erring_node = &route_nodes[origin_index];
/* Tell gossipd: it may want to remove channels or even nodes
* in response to this, and there may be a channel_update
* embedded too */
gossip_msg = towire_gossip_payment_failure(NULL,
erring_node,
erring_channel,
dir,
failuremsg);
subd_send_msg(ld->gossip, take(gossip_msg));
}
routing_failure->erring_index = (unsigned int) (origin_index + 1);
routing_failure->failcode = failcode;
routing_failure->msg = tal_dup_talarr(routing_failure, u8, failuremsg);
if (erring_node != NULL)
routing_failure->erring_node =
tal_dup(routing_failure, struct node_id, erring_node);
else
routing_failure->erring_node = NULL;
if (erring_channel != NULL) {
routing_failure->erring_channel = tal_dup(
routing_failure, struct short_channel_id, erring_channel);
routing_failure->channel_dir = dir;
} else {
routing_failure->erring_channel = NULL;
routing_failure->channel_dir = 0;
}
return routing_failure;
}
void payment_store(struct lightningd *ld, struct wallet_payment *payment TAKES)
{
struct sendpay_command *pc;
struct sendpay_command *next;
/* Need to remember here otherwise wallet_payment_store will free us. */
bool ptaken = taken(payment);
wallet_payment_store(ld->wallet, payment);
/* Trigger any sendpay commands waiting for the store to occur. */
list_for_each_safe(&ld->sendpay_commands, pc, next, list) {
if (!sha256_eq(&payment->payment_hash, &pc->payment_hash))
continue;
/* Deletes from list, frees pc */
json_sendpay_in_progress(pc->cmd, payment);
}
if (ptaken)
tal_free(payment);
}
void payment_failed(struct lightningd *ld, const struct htlc_out *hout,
const char *localfail)
{
struct wallet_payment *payment;
struct routing_failure* fail = NULL;
const char *failstr;
errcode_t pay_errcode;
const u8 *failmsg;
int origin_index;
payment = wallet_payment_by_hash(tmpctx, ld->wallet,
&hout->payment_hash,
hout->partid);
#ifdef COMPAT_V052
/* Prior to "pay: delete HTLC when we delete payment." we would
* delete a payment on retry, but leave the HTLC. */
if (!payment) {
log_unusual(hout->key.channel->log,
"No payment for %s:"
" was this an old database?",
type_to_string(tmpctx, struct sha256,
&hout->payment_hash));
return;
}
#else
assert(payment);
#endif
assert((payment->route_channels == NULL) == (payment->route_nodes == NULL));
/* This gives more details than a generic failure message */
if (localfail) {
fail = local_routing_failure(tmpctx, ld, hout, payment);
failstr = localfail;
pay_errcode = PAY_TRY_OTHER_ROUTE;
} else if (payment->path_secrets == NULL) {
/* This was a payment initiated with `sendonion`, we therefore
* don't have the path secrets and cannot decode the error
* onion. Let's store it and hope whatever called `sendonion`
* knows how to deal with these. */
pay_errcode = PAY_UNPARSEABLE_ONION;
fail = NULL;
failstr = NULL;
} else if (hout->failmsg) {
/* This can happen when a direct peer told channeld it's a
* malformed onion using update_fail_malformed_htlc. */
failstr = "local failure";
failmsg = hout->failmsg;
origin_index = 0;
pay_errcode = PAY_TRY_OTHER_ROUTE;
goto use_failmsg;
} else {
/* Must be normal remote fail with an onion-wrapped error. */
failstr = "reply from remote";
/* Try to parse reply. */
struct secret *path_secrets = payment->path_secrets;
failmsg = unwrap_onionreply(tmpctx, path_secrets,
tal_count(path_secrets),
hout->failonion, &origin_index);
if (!failmsg) {
log_info(hout->key.channel->log,
"htlc %"PRIu64" failed with bad reply (%s)",
hout->key.id,
tal_hex(tmpctx, hout->failonion->contents));
/* Cannot record failure. */
fail = NULL;
pay_errcode = PAY_UNPARSEABLE_ONION;
} else {
enum onion_type failcode;
use_failmsg:
failcode = fromwire_peektype(failmsg);
log_info(hout->key.channel->log,
"htlc %"PRIu64" "
"failed from %ith node "
"with code 0x%04x (%s)",
hout->key.id,
origin_index,
failcode, onion_type_name(failcode));
fail = remote_routing_failure(tmpctx, ld,
payment, failmsg,
origin_index,
hout->key.channel->log,
&pay_errcode);
}
}
/* Save to DB */
payment_store(ld, payment);
wallet_payment_set_status(ld->wallet, &hout->payment_hash,
hout->partid,
PAYMENT_FAILED, NULL);
wallet_payment_set_failinfo(ld->wallet,
&hout->payment_hash,
hout->partid,
fail ? NULL : hout->failonion,
pay_errcode == PAY_DESTINATION_PERM_FAIL,
fail ? fail->erring_index : -1,
fail ? fail->failcode : 0,
fail ? fail->erring_node : NULL,
fail ? fail->erring_channel : NULL,
NULL,
failstr,
fail ? fail->channel_dir : 0);
tell_waiters_failed(ld, &hout->payment_hash, payment, pay_errcode,
hout->failonion, fail, failstr);
}
/* Wait for a payment. If cmd is deleted, then wait_payment()
* no longer be called.
* Return callback if we called already, otherwise NULL. */
static struct command_result *wait_payment(struct lightningd *ld,
struct command *cmd,
const struct sha256 *payment_hash,
u64 partid)
{
struct wallet_payment *payment;
struct onionreply *failonionreply;
bool faildestperm;
int failindex;
enum onion_type failcode;
struct node_id *failnode;
struct short_channel_id *failchannel;
u8 *failupdate;
char *faildetail;
struct routing_failure *fail;
int faildirection;
errcode_t rpcerrorcode;
payment = wallet_payment_by_hash(tmpctx, ld->wallet,
payment_hash, partid);
if (!payment) {
return command_fail(cmd, PAY_NO_SUCH_PAYMENT,
"Never attempted payment part %"PRIu64
" for '%s'",
partid,
type_to_string(tmpctx, struct sha256,
payment_hash));
}
log_debug(cmd->ld->log, "Payment part %"PRIu64"/%"PRIu64" status %u",
partid, payment->partid, payment->status);
switch (payment->status) {
case PAYMENT_PENDING:
add_waitsendpay_waiter(ld, cmd, payment_hash, partid);
return NULL;
case PAYMENT_COMPLETE:
return sendpay_success(cmd, payment);
case PAYMENT_FAILED:
/* Get error from DB */
wallet_payment_get_failinfo(tmpctx, ld->wallet,
payment_hash,
partid,
&failonionreply,
&faildestperm,
&failindex,
&failcode,
&failnode,
&failchannel,
&failupdate,
&faildetail,
&faildirection);
/* Old DB might not save failure information */
if (!failonionreply && !failnode) {
return command_fail(cmd, PAY_UNSPECIFIED_ERROR,
"Payment failure reason unknown");
} else if (failonionreply) {
/* failed to parse returned onion error */
return sendpay_fail(
cmd, payment, PAY_UNPARSEABLE_ONION, failonionreply,
NULL,
sendpay_errmsg_fmt(tmpctx, PAY_UNPARSEABLE_ONION,
NULL, faildetail));
} else {
/* Parsed onion error, get its details */
assert(failnode);
assert(failchannel);
fail = tal(tmpctx, struct routing_failure);
fail->erring_index = failindex;
fail->failcode = failcode;
fail->erring_node =
tal_dup(fail, struct node_id, failnode);
fail->erring_channel =
tal_dup(fail, struct short_channel_id, failchannel);
fail->channel_dir = faildirection;
/* FIXME: We don't store this! */
fail->msg = NULL;
rpcerrorcode = faildestperm ? PAY_DESTINATION_PERM_FAIL
: PAY_TRY_OTHER_ROUTE;
return sendpay_fail(
cmd, payment, rpcerrorcode, NULL, fail,
sendpay_errmsg_fmt(tmpctx, rpcerrorcode, fail,
faildetail));
}
}
/* Impossible. */
abort();
}
static bool should_use_tlv(enum route_hop_style style)
{
switch (style) {
case ROUTE_HOP_TLV:
return true;
/* Otherwise fall thru */
case ROUTE_HOP_LEGACY:
return false;
}
abort();
}
/* Returns failmsg on failure, tallocated off ctx */
static const u8 *send_onion(const tal_t *ctx, struct lightningd *ld,
const struct onionpacket *packet,
const struct route_hop *first_hop,
const struct sha256 *payment_hash,
u64 partid,
struct channel *channel,
struct htlc_out **hout)
{
const u8 *onion;
unsigned int base_expiry;
bool dont_care_about_channel_update;
base_expiry = get_block_height(ld->topology) + 1;
onion = serialize_onionpacket(tmpctx, packet);
return send_htlc_out(ctx, channel, first_hop->amount,
base_expiry + first_hop->delay,
payment_hash, partid, onion, NULL, hout,
&dont_care_about_channel_update);
}
/* 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,
const struct route_hop *first_hop,
struct amount_msat msat,
struct amount_msat total_msat,
const char *label TAKES,
const char *b11str 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 wallet_payment **payments, *old_payment = NULL;
struct channel *channel;
const u8 *failmsg;
struct htlc_out *hout;
struct routing_failure *fail;
struct amount_msat msat_already_pending = AMOUNT_MSAT(0);
/* Now, do we already have one or more payments? */
payments = wallet_payment_list(tmpctx, ld->wallet, rhash);
for (size_t i = 0; i < tal_count(payments); i++) {
log_debug(ld->log, "Payment %zu/%zu: %s %s",
i, tal_count(payments),
type_to_string(tmpctx, struct amount_msat,
&payments[i]->msatoshi),
payments[i]->status == PAYMENT_COMPLETE ? "COMPLETE"
: payments[i]->status == PAYMENT_PENDING ? "PENDING"
: "FAILED");
switch (payments[i]->status) {
case PAYMENT_COMPLETE:
if (payments[i]->partid != partid)
continue;
/* Must match successful payment parameters. */
if (!amount_msat_eq(payments[i]->msatoshi, msat)) {
return command_fail(cmd, PAY_RHASH_ALREADY_USED,
"Already succeeded "
"with amount %s",
type_to_string(tmpctx,
struct amount_msat,
&payments[i]->msatoshi));
}
if (payments[i]->destination && destination
&& !node_id_eq(payments[i]->destination,
destination)) {
return command_fail(cmd, PAY_RHASH_ALREADY_USED,
"Already succeeded to %s",
type_to_string(tmpctx,
struct node_id,
payments[i]->destination));
}
return sendpay_success(cmd, payments[i]);
case PAYMENT_PENDING:
/* Can't mix non-parallel and parallel payments! */
if (!payments[i]->partid != !partid) {
return command_fail(cmd, PAY_IN_PROGRESS,
"Already have %s payment in progress",
payments[i]->partid ? "parallel" : "non-parallel");
}
if (payments[i]->partid == partid)
return json_sendpay_in_progress(cmd, payments[i]);
/* You shouldn't change your mind about amount being
* sent, since we'll use it in onion! */
else if (!amount_msat_eq(payments[i]->total_msat,
total_msat))
return command_fail(cmd, JSONRPC2_INVALID_PARAMS,
"msatoshi was previously %s, now %s",
type_to_string(tmpctx,
struct amount_msat,
&payments[i]->total_msat),
type_to_string(tmpctx,
struct amount_msat,
&total_msat));
if (!amount_msat_add(&msat_already_pending,
msat_already_pending,
payments[i]->msatoshi)) {
return command_fail(cmd, LIGHTNINGD,
"Internal amount overflow!"
" %s + %s in %zu/%zu",
type_to_string(tmpctx,
struct amount_msat,
&msat_already_pending),
type_to_string(tmpctx,
struct amount_msat,
&payments[i]->msatoshi),
i, tal_count(payments));
}
break;
case PAYMENT_FAILED:
if (payments[i]->partid == partid)
old_payment = payments[i];
}
}
/* BOLT #4:
*
* - MUST NOT send another HTLC if the total `amount_msat` of the HTLC
* set is already greater or equal to `total_msat`.
*/
/* We don't do this for single 0-value payments (sendonion does this) */
if (!amount_msat_eq(total_msat, AMOUNT_MSAT(0))
&& amount_msat_greater_eq(msat_already_pending, total_msat)) {
return command_fail(cmd, PAY_IN_PROGRESS,
"Already have %s of %s payments in progress",
type_to_string(tmpctx, struct amount_msat,
&msat_already_pending),
type_to_string(tmpctx, struct amount_msat,
&total_msat));
}
channel = active_channel_by_id(ld, &first_hop->nodeid, NULL);
if (!channel) {
struct json_stream *data
= json_stream_fail(cmd, PAY_TRY_OTHER_ROUTE,
"No connection to first "
"peer found");
json_add_routefail_info(data, 0, WIRE_UNKNOWN_NEXT_PEER,
&ld->id, &first_hop->channel_id,
node_id_idx(&ld->id, &first_hop->nodeid),
NULL);
json_object_end(data);
return command_failed(cmd, data);
}
failmsg = send_onion(tmpctx, ld, packet, first_hop, rhash, partid,
channel, &hout);
if (failmsg) {
fail = immediate_routing_failure(cmd, ld,
fromwire_peektype(failmsg),
&first_hop->channel_id,
&channel->peer->id);
return sendpay_fail(
cmd, old_payment, PAY_TRY_OTHER_ROUTE, NULL, fail,
sendpay_errmsg_fmt(tmpctx, PAY_TRY_OTHER_ROUTE, fail,
"First peer not ready"));
}
/* If we're retrying, delete all trace of previous one. We delete
* outgoing HTLC, too, otherwise it gets reported to onchaind as
* a possibility, and we end up in handle_missing_htlc_output->
* onchain_failed_our_htlc->payment_failed with no payment.
*/
if (old_payment) {
wallet_payment_delete(ld->wallet, rhash, partid);
wallet_local_htlc_out_delete(ld->wallet, channel, rhash,
partid);
}
/* If hout fails, payment should be freed too. */
struct wallet_payment *payment = tal(hout, struct wallet_payment);
payment->id = 0;
payment->payment_hash = *rhash;
payment->partid = partid;
if (destination)
payment->destination = tal_dup(payment, struct node_id, destination);
else
payment->destination = NULL;
payment->status = PAYMENT_PENDING;
payment->msatoshi = msat;
payment->msatoshi_sent = first_hop->amount;
payment->total_msat = total_msat;
payment->timestamp = time_now().ts.tv_sec;
payment->payment_preimage = NULL;
payment->path_secrets = tal_steal(payment, path_secrets);
if (route_nodes)
payment->route_nodes = tal_steal(payment, route_nodes);
else
payment->route_nodes = NULL;
if (route_channels)
payment->route_channels = tal_steal(payment, route_channels);
else
payment->route_channels = NULL;
payment->failonion = NULL;
if (label != NULL)
payment->label = tal_strdup(payment, label);
else
payment->label = NULL;
if (b11str != NULL)
payment->bolt11 = tal_strdup(payment, b11str);
else
payment->bolt11 = NULL;
/* We write this into db when HTLC is actually sent. */
wallet_payment_setup(ld->wallet, payment);
add_sendpay_waiter(ld, cmd, rhash, partid);
return command_still_pending(cmd);
}
static struct command_result *
send_payment(struct lightningd *ld,
struct command *cmd,
const struct sha256 *rhash,
u64 partid,
const struct route_hop *route,
struct amount_msat msat,
struct amount_msat total_msat,
const char *label TAKES,
const char *b11str TAKES,
const struct secret *payment_secret)
{
unsigned int base_expiry;
struct onionpacket *packet;
struct secret *path_secrets;
size_t i, n_hops = tal_count(route);
struct node_id *ids = tal_arr(tmpctx, struct node_id, n_hops);
struct short_channel_id *channels;
struct sphinx_path *path;
struct pubkey pubkey;
bool final_tlv, ret;
u8 *onion;
/* Expiry for HTLCs is absolute. And add one to give some margin. */
base_expiry = get_block_height(ld->topology) + 1;
path = sphinx_path_new(tmpctx, rhash->u.u8);
/* Extract IDs for each hop: create_onionpacket wants array. */
for (i = 0; i < n_hops; i++)
ids[i] = route[i].nodeid;
/* Create sphinx path */
for (i = 0; i < n_hops - 1; i++) {
ret = pubkey_from_node_id(&pubkey, &ids[i]);
assert(ret);
sphinx_add_hop(path, &pubkey,
take(onion_nonfinal_hop(NULL,
should_use_tlv(route[i].style),
&route[i + 1].channel_id,
route[i + 1].amount,
base_expiry + route[i + 1].delay)));
}