Skip to content

Commit 2868948

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

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,
@@ -272,7 +282,7 @@ static struct command_result *handle_failure(struct routefail *r)
272282
if (*result->erring_index == 0)
273283
node_type = ORIGIN_NODE;
274284

275-
switch (result->failcode) {
285+
switch (failcode) {
276286
// intermediate only
277287
case WIRE_INVALID_ONION_VERSION:
278288
case WIRE_INVALID_ONION_HMAC:
@@ -282,8 +292,8 @@ static struct command_result *handle_failure(struct routefail *r)
282292
payment_note(payment, LOG_UNUSUAL,
283293
"Final node reported strange "
284294
"error code %04x (%s)",
285-
result->failcode,
286-
onion_wire_name(result->failcode));
295+
failcode,
296+
onion_wire_name(failcode));
287297
break;
288298
case ORIGIN_NODE:
289299
case INTERMEDIATE_NODE:
@@ -299,8 +309,8 @@ static struct command_result *handle_failure(struct routefail *r)
299309
route_final_error(
300310
route, PAY_DESTINATION_PERM_FAIL,
301311
"Received error code %04x (%s) at final node.",
302-
result->failcode,
303-
onion_wire_name(result->failcode));
312+
failcode,
313+
onion_wire_name(failcode));
304314

305315
break;
306316
case INTERMEDIATE_NODE:
@@ -313,7 +323,7 @@ static struct command_result *handle_failure(struct routefail *r)
313323
payment_disable_node(
314324
payment, route->hops[*result->erring_index].node_id,
315325
LOG_DBG, "received %s from previous hop",
316-
onion_wire_name(result->failcode));
326+
onion_wire_name(failcode));
317327
break;
318328
case UNKNOWN_NODE:
319329
break;
@@ -329,8 +339,8 @@ static struct command_result *handle_failure(struct routefail *r)
329339
payment_note(payment, LOG_UNUSUAL,
330340
"Intermediate node reported strange "
331341
"error code %04x (%s)",
332-
result->failcode,
333-
onion_wire_name(result->failcode));
342+
failcode,
343+
onion_wire_name(failcode));
334344
break;
335345
case ORIGIN_NODE:
336346
case FINAL_NODE:
@@ -347,15 +357,15 @@ static struct command_result *handle_failure(struct routefail *r)
347357
route_final_error(
348358
route, PAY_DESTINATION_PERM_FAIL,
349359
"Received error code %04x (%s) at final node.",
350-
result->failcode,
351-
onion_wire_name(result->failcode));
360+
failcode,
361+
onion_wire_name(failcode));
352362
break;
353363
case ORIGIN_NODE:
354364
route_final_error(
355365
route, PAY_UNSPECIFIED_ERROR,
356366
"Error code %04x (%s) reported at the origin.",
357-
result->failcode,
358-
onion_wire_name(result->failcode));
367+
failcode,
368+
onion_wire_name(failcode));
359369
break;
360370
case INTERMEDIATE_NODE:
361371
if (!route->hops)
@@ -364,7 +374,7 @@ static struct command_result *handle_failure(struct routefail *r)
364374
payment,
365375
route->hops[*result->erring_index - 1].node_id,
366376
LOG_INFORM, "received error %s",
367-
onion_wire_name(result->failcode));
377+
onion_wire_name(failcode));
368378
break;
369379
case UNKNOWN_NODE:
370380
break;
@@ -382,22 +392,22 @@ static struct command_result *handle_failure(struct routefail *r)
382392
payment_note(payment, LOG_UNUSUAL,
383393
"Final node reported strange "
384394
"error code %04x (%s)",
385-
result->failcode,
386-
onion_wire_name(result->failcode));
395+
failcode,
396+
onion_wire_name(failcode));
387397

388398
route_final_error(
389399
route, PAY_DESTINATION_PERM_FAIL,
390400
"Received error code %04x (%s) at final node.",
391-
result->failcode,
392-
onion_wire_name(result->failcode));
401+
failcode,
402+
onion_wire_name(failcode));
393403

394404
break;
395405
case ORIGIN_NODE:
396406
payment_note(payment, LOG_UNUSUAL,
397407
"First node reported strange "
398408
"error code %04x (%s)",
399-
result->failcode,
400-
onion_wire_name(result->failcode));
409+
failcode,
410+
onion_wire_name(failcode));
401411

402412
break;
403413
case INTERMEDIATE_NODE:
@@ -407,7 +417,7 @@ static struct command_result *handle_failure(struct routefail *r)
407417
.scid = route->hops[*result->erring_index].scid,
408418
.dir = route->hops[*result->erring_index].direction};
409419
payment_disable_chan(payment, scidd, LOG_INFORM, "%s",
410-
onion_wire_name(result->failcode));
420+
onion_wire_name(failcode));
411421

412422
break;
413423
case UNKNOWN_NODE:
@@ -425,16 +435,16 @@ static struct command_result *handle_failure(struct routefail *r)
425435
payment_note(payment, LOG_UNUSUAL,
426436
"Intermediate node reported strange "
427437
"error code %04x (%s)",
428-
result->failcode,
429-
onion_wire_name(result->failcode));
438+
failcode,
439+
onion_wire_name(failcode));
430440

431441
if (!route->hops)
432442
break;
433443
payment_disable_node(
434444
payment,
435445
route->hops[*result->erring_index - 1].node_id,
436446
LOG_INFORM, "received error %s",
437-
onion_wire_name(result->failcode));
447+
onion_wire_name(failcode));
438448

439449
break;
440450
case ORIGIN_NODE:
@@ -454,22 +464,22 @@ static struct command_result *handle_failure(struct routefail *r)
454464
payment_note(payment, LOG_UNUSUAL,
455465
"Final node reported strange "
456466
"error code %04x (%s)",
457-
result->failcode,
458-
onion_wire_name(result->failcode));
467+
failcode,
468+
onion_wire_name(failcode));
459469

460470
route_final_error(
461471
route, PAY_DESTINATION_PERM_FAIL,
462472
"Received error code %04x (%s) at final node.",
463-
result->failcode,
464-
onion_wire_name(result->failcode));
473+
failcode,
474+
onion_wire_name(failcode));
465475

466476
break;
467477
case ORIGIN_NODE:
468478
payment_note(payment, LOG_UNUSUAL,
469479
"First node reported strange "
470480
"error code %04x (%s)",
471-
result->failcode,
472-
onion_wire_name(result->failcode));
481+
failcode,
482+
onion_wire_name(failcode));
473483

474484
break;
475485
case INTERMEDIATE_NODE:
@@ -483,7 +493,7 @@ static struct command_result *handle_failure(struct routefail *r)
483493
.dir = route->hops[*result->erring_index].direction};
484494
payment_warn_chan(payment, scidd, LOG_INFORM,
485495
"received error %s",
486-
onion_wire_name(result->failcode));
496+
onion_wire_name(failcode));
487497

488498
break;
489499
case UNKNOWN_NODE:
@@ -501,14 +511,14 @@ static struct command_result *handle_failure(struct routefail *r)
501511
payment_note(payment, LOG_UNUSUAL,
502512
"Final node reported strange "
503513
"error code %04x (%s)",
504-
result->failcode,
505-
onion_wire_name(result->failcode));
514+
failcode,
515+
onion_wire_name(failcode));
506516

507517
route_final_error(
508518
route, PAY_DESTINATION_PERM_FAIL,
509519
"Received error code %04x (%s) at final node.",
510-
result->failcode,
511-
onion_wire_name(result->failcode));
520+
failcode,
521+
onion_wire_name(failcode));
512522

513523
break;
514524
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)