Skip to content

Commit 42ddc30

Browse files
committed
renepay: don't fail parsing sendpay error
Changelog-None. Signed-off-by: Lagrang3 <[email protected]>
1 parent 611b2aa commit 42ddc30

File tree

4 files changed

+75
-53
lines changed

4 files changed

+75
-53
lines changed

plugins/renepay/json.c

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ struct payment_result *tal_sendpay_result_from_json(const tal_t *ctx,
7676
const char *buffer,
7777
const jsmntok_t *toks)
7878
{
79-
const jsmntok_t *idtok = json_get_member(buffer, toks, "id");
79+
const jsmntok_t *idtok = json_get_member(buffer, toks, "created_index");
8080
const jsmntok_t *hashtok =
8181
json_get_member(buffer, toks, "payment_hash");
8282
const jsmntok_t *senttok =
@@ -85,37 +85,48 @@ struct payment_result *tal_sendpay_result_from_json(const tal_t *ctx,
8585
const jsmntok_t *preimagetok =
8686
json_get_member(buffer, toks, "payment_preimage");
8787
const jsmntok_t *codetok = json_get_member(buffer, toks, "code");
88+
const jsmntok_t *msgtok = json_get_member(buffer, toks, "message");
8889
const jsmntok_t *datatok = json_get_member(buffer, toks, "data");
89-
const jsmntok_t *erridxtok, *msgtok, *failcodetok, *rawmsgtok,
90+
const jsmntok_t *erridxtok, *failcodetok, *rawmsgtok,
9091
*failcodenametok, *errchantok, *errnodetok, *errdirtok;
9192
struct payment_result *result;
9293

9394
/* Check if we have an error and need to descend into data to get
9495
* details. */
9596
if (codetok != NULL && datatok != NULL) {
96-
idtok = json_get_member(buffer, datatok, "id");
97+
idtok = json_get_member(buffer, datatok, "create_index");
9798
hashtok = json_get_member(buffer, datatok, "payment_hash");
9899
senttok = json_get_member(buffer, datatok, "amount_sent_msat");
99100
statustok = json_get_member(buffer, datatok, "status");
100101
}
101102

102103
/* Initial sanity checks, all these fields must exist. */
103-
if (idtok == NULL || idtok->type != JSMN_PRIMITIVE || hashtok == NULL ||
104-
hashtok->type != JSMN_STRING || senttok == NULL ||
105-
statustok == NULL || statustok->type != JSMN_STRING) {
104+
if (hashtok == NULL || hashtok->type != JSMN_STRING ||
105+
senttok == NULL || statustok == NULL ||
106+
statustok->type != JSMN_STRING) {
106107
return NULL;
107108
}
108109

109110
result = tal(ctx, struct payment_result);
110111

112+
if (msgtok)
113+
result->message = json_strdup(result, buffer, msgtok);
114+
else
115+
result->message = NULL;
116+
111117
if (codetok != NULL)
112118
// u32? isn't this an int?
113119
// json_to_u32(buffer, codetok, &result->code);
114120
json_to_int(buffer, codetok, &result->code);
115121
else
116122
result->code = 0;
117123

118-
json_to_u64(buffer, idtok, &result->id);
124+
if (idtok) {
125+
result->created_index = tal(result, u64);
126+
json_to_u64(buffer, idtok, result->created_index);
127+
} else
128+
result->created_index = NULL;
129+
119130
json_to_msat(buffer, senttok, &result->amount_sent);
120131
if (json_tok_streq(buffer, statustok, "pending")) {
121132
result->status = SENDPAY_PENDING;
@@ -133,7 +144,7 @@ struct payment_result *tal_sendpay_result_from_json(const tal_t *ctx,
133144
}
134145

135146
/* Now extract the error details if the error code is not 0 */
136-
if (result->code != 0) {
147+
if (result->code != 0 && datatok) {
137148
erridxtok = json_get_member(buffer, datatok, "erring_index");
138149
errnodetok = json_get_member(buffer, datatok, "erring_node");
139150
errchantok = json_get_member(buffer, datatok, "erring_channel");
@@ -142,17 +153,16 @@ struct payment_result *tal_sendpay_result_from_json(const tal_t *ctx,
142153
failcodetok = json_get_member(buffer, datatok, "failcode");
143154
failcodenametok =
144155
json_get_member(buffer, datatok, "failcodename");
145-
msgtok = json_get_member(buffer, toks, "message");
146156
rawmsgtok = json_get_member(buffer, datatok, "raw_message");
147-
if (failcodetok == NULL ||
148-
failcodetok->type != JSMN_PRIMITIVE ||
157+
/* check type for sanity */
158+
if ((failcodetok != NULL &&
159+
failcodetok->type != JSMN_PRIMITIVE) ||
149160
(failcodenametok != NULL &&
150161
failcodenametok->type != JSMN_STRING) ||
151162
(erridxtok != NULL && erridxtok->type != JSMN_PRIMITIVE) ||
152163
(errnodetok != NULL && errnodetok->type != JSMN_STRING) ||
153164
(errchantok != NULL && errchantok->type != JSMN_STRING) ||
154165
(errdirtok != NULL && errdirtok->type != JSMN_PRIMITIVE) ||
155-
msgtok == NULL || msgtok->type != JSMN_STRING ||
156166
(rawmsgtok != NULL && rawmsgtok->type != JSMN_STRING))
157167
goto fail;
158168

@@ -168,9 +178,11 @@ struct payment_result *tal_sendpay_result_from_json(const tal_t *ctx,
168178
else
169179
result->failcodename = NULL;
170180

171-
json_to_u32(buffer, failcodetok, &result->failcode);
172-
result->message = json_strdup(result, buffer, msgtok);
173-
181+
if(failcodetok){
182+
result->failcode = tal(result, enum onion_wire);
183+
json_to_u32(buffer, failcodetok, result->failcode);
184+
}else
185+
result->failcode = NULL;
174186
if (erridxtok != NULL) {
175187
result->erring_index = tal(result, u32);
176188
json_to_u32(buffer, erridxtok, result->erring_index);

plugins/renepay/route.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,13 @@ enum sendpay_result_status {
2929
struct payment_result {
3030
/* DB internal id */
3131
// TODO check all this variables
32-
u64 id;
32+
u64 *created_index;
3333
struct preimage *payment_preimage;
3434
enum sendpay_result_status status;
3535
struct amount_msat amount_sent;
3636
enum jsonrpc_errcode code;
3737
const char *failcodename;
38-
enum onion_wire failcode;
38+
enum onion_wire *failcode;
3939
const u8 *raw_message;
4040
const char *message;
4141
u32 *erring_index;

plugins/renepay/routefail.c

Lines changed: 44 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,16 @@ static struct command_result *handle_failure(struct routefail *r)
253253
if (route->hops)
254254
path_len = tal_count(route->hops);
255255

256+
enum onion_wire failcode;
257+
if(result->failcode)
258+
failcode = *result->failcode;
259+
else{
260+
payment_note(
261+
payment, LOG_UNUSUAL,
262+
"The failcode is unknown we skip error handling");
263+
goto finish;
264+
}
265+
256266
if (!result->erring_index) {
257267
payment_note(
258268
payment, LOG_UNUSUAL,
@@ -270,7 +280,7 @@ static struct command_result *handle_failure(struct routefail *r)
270280
node_type = INTERMEDIATE_NODE;
271281
}
272282

273-
switch (result->failcode) {
283+
switch (failcode) {
274284
// intermediate only
275285
case WIRE_INVALID_ONION_VERSION:
276286
case WIRE_INVALID_ONION_HMAC:
@@ -280,8 +290,8 @@ static struct command_result *handle_failure(struct routefail *r)
280290
payment_note(payment, LOG_UNUSUAL,
281291
"Final node reported strange "
282292
"error code %04x (%s)",
283-
result->failcode,
284-
onion_wire_name(result->failcode));
293+
failcode,
294+
onion_wire_name(failcode));
285295
break;
286296
case ORIGIN_NODE:
287297
case INTERMEDIATE_NODE:
@@ -297,8 +307,8 @@ static struct command_result *handle_failure(struct routefail *r)
297307
route_final_error(
298308
route, PAY_DESTINATION_PERM_FAIL,
299309
"Received error code %04x (%s) at final node.",
300-
result->failcode,
301-
onion_wire_name(result->failcode));
310+
failcode,
311+
onion_wire_name(failcode));
302312

303313
break;
304314
case INTERMEDIATE_NODE:
@@ -311,7 +321,7 @@ static struct command_result *handle_failure(struct routefail *r)
311321
payment_disable_node(
312322
payment, route->hops[*result->erring_index].node_id,
313323
LOG_DBG, "received %s from previous hop",
314-
onion_wire_name(result->failcode));
324+
onion_wire_name(failcode));
315325
break;
316326
case UNKNOWN_NODE:
317327
break;
@@ -353,8 +363,8 @@ static struct command_result *handle_failure(struct routefail *r)
353363
payment_note(payment, LOG_UNUSUAL,
354364
"Intermediate node reported strange "
355365
"error code %04x (%s)",
356-
result->failcode,
357-
onion_wire_name(result->failcode));
366+
failcode,
367+
onion_wire_name(failcode));
358368
break;
359369
case ORIGIN_NODE:
360370
case FINAL_NODE:
@@ -371,15 +381,15 @@ static struct command_result *handle_failure(struct routefail *r)
371381
route_final_error(
372382
route, PAY_DESTINATION_PERM_FAIL,
373383
"Received error code %04x (%s) at final node.",
374-
result->failcode,
375-
onion_wire_name(result->failcode));
384+
failcode,
385+
onion_wire_name(failcode));
376386
break;
377387
case ORIGIN_NODE:
378388
route_final_error(
379389
route, PAY_UNSPECIFIED_ERROR,
380390
"Error code %04x (%s) reported at the origin.",
381-
result->failcode,
382-
onion_wire_name(result->failcode));
391+
failcode,
392+
onion_wire_name(failcode));
383393
break;
384394
case INTERMEDIATE_NODE:
385395
if (!route->hops)
@@ -388,7 +398,7 @@ static struct command_result *handle_failure(struct routefail *r)
388398
payment,
389399
route->hops[*result->erring_index - 1].node_id,
390400
LOG_INFORM, "received error %s",
391-
onion_wire_name(result->failcode));
401+
onion_wire_name(failcode));
392402
break;
393403
case UNKNOWN_NODE:
394404
break;
@@ -406,22 +416,22 @@ static struct command_result *handle_failure(struct routefail *r)
406416
payment_note(payment, LOG_UNUSUAL,
407417
"Final node reported strange "
408418
"error code %04x (%s)",
409-
result->failcode,
410-
onion_wire_name(result->failcode));
419+
failcode,
420+
onion_wire_name(failcode));
411421

412422
route_final_error(
413423
route, PAY_DESTINATION_PERM_FAIL,
414424
"Received error code %04x (%s) at final node.",
415-
result->failcode,
416-
onion_wire_name(result->failcode));
425+
failcode,
426+
onion_wire_name(failcode));
417427

418428
break;
419429
case ORIGIN_NODE:
420430
payment_note(payment, LOG_UNUSUAL,
421431
"First node reported strange "
422432
"error code %04x (%s)",
423-
result->failcode,
424-
onion_wire_name(result->failcode));
433+
failcode,
434+
onion_wire_name(failcode));
425435

426436
break;
427437
case INTERMEDIATE_NODE:
@@ -431,7 +441,7 @@ static struct command_result *handle_failure(struct routefail *r)
431441
.scid = route->hops[*result->erring_index].scid,
432442
.dir = route->hops[*result->erring_index].direction};
433443
payment_disable_chan(payment, scidd, LOG_INFORM, "%s",
434-
onion_wire_name(result->failcode));
444+
onion_wire_name(failcode));
435445

436446
break;
437447
case UNKNOWN_NODE:
@@ -449,16 +459,16 @@ static struct command_result *handle_failure(struct routefail *r)
449459
payment_note(payment, LOG_UNUSUAL,
450460
"Intermediate node reported strange "
451461
"error code %04x (%s)",
452-
result->failcode,
453-
onion_wire_name(result->failcode));
462+
failcode,
463+
onion_wire_name(failcode));
454464

455465
if (!route->hops)
456466
break;
457467
payment_disable_node(
458468
payment,
459469
route->hops[*result->erring_index - 1].node_id,
460470
LOG_INFORM, "received error %s",
461-
onion_wire_name(result->failcode));
471+
onion_wire_name(failcode));
462472

463473
break;
464474
case ORIGIN_NODE:
@@ -478,22 +488,22 @@ static struct command_result *handle_failure(struct routefail *r)
478488
payment_note(payment, LOG_UNUSUAL,
479489
"Final node reported strange "
480490
"error code %04x (%s)",
481-
result->failcode,
482-
onion_wire_name(result->failcode));
491+
failcode,
492+
onion_wire_name(failcode));
483493

484494
route_final_error(
485495
route, PAY_DESTINATION_PERM_FAIL,
486496
"Received error code %04x (%s) at final node.",
487-
result->failcode,
488-
onion_wire_name(result->failcode));
497+
failcode,
498+
onion_wire_name(failcode));
489499

490500
break;
491501
case ORIGIN_NODE:
492502
payment_note(payment, LOG_UNUSUAL,
493503
"First node reported strange "
494504
"error code %04x (%s)",
495-
result->failcode,
496-
onion_wire_name(result->failcode));
505+
failcode,
506+
onion_wire_name(failcode));
497507

498508
break;
499509
case INTERMEDIATE_NODE:
@@ -507,7 +517,7 @@ static struct command_result *handle_failure(struct routefail *r)
507517
.dir = route->hops[*result->erring_index].direction};
508518
payment_warn_chan(payment, scidd, LOG_INFORM,
509519
"received error %s",
510-
onion_wire_name(result->failcode));
520+
onion_wire_name(failcode));
511521

512522
break;
513523
case UNKNOWN_NODE:
@@ -525,14 +535,14 @@ static struct command_result *handle_failure(struct routefail *r)
525535
payment_note(payment, LOG_UNUSUAL,
526536
"Final node reported strange "
527537
"error code %04x (%s)",
528-
result->failcode,
529-
onion_wire_name(result->failcode));
538+
failcode,
539+
onion_wire_name(failcode));
530540

531541
route_final_error(
532542
route, PAY_DESTINATION_PERM_FAIL,
533543
"Received error code %04x (%s) at final node.",
534-
result->failcode,
535-
onion_wire_name(result->failcode));
544+
failcode,
545+
onion_wire_name(failcode));
536546

537547
break;
538548
case INTERMEDIATE_NODE:

plugins/renepay/routetracker.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ void route_failure_register(struct routetracker *routetracker,
102102
assert(result);
103103

104104
/* Update the knowledge in the uncertaity network. */
105-
if (route->hops) {
105+
if (route->hops && result->failcode) {
106106
assert(result->erring_index);
107107
int path_len = tal_count(route->hops);
108108

@@ -123,7 +123,7 @@ void route_failure_register(struct routetracker *routetracker,
123123
route->hops[i].direction);
124124
}
125125

126-
if (result->failcode == WIRE_TEMPORARY_CHANNEL_FAILURE &&
126+
if (*result->failcode == WIRE_TEMPORARY_CHANNEL_FAILURE &&
127127
(last_good_channel + 1) < path_len) {
128128
/* A WIRE_TEMPORARY_CHANNEL_FAILURE could mean not
129129
* enough liquidity to forward the payment or cannot add

0 commit comments

Comments
 (0)