MCPcopy Create free account
hub / github.com/ElementsProject/lightning / handle_reply

Function handle_reply

plugins/commando.c:491–575  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

489}
490
491static struct command_result *handle_reply(struct node_id *peer,
492 u64 idnum,
493 const u8 *msg, size_t msglen,
494 bool terminal)
495{
496 struct commando *ocmd;
497 struct json_stream *res;
498 const jsmntok_t *toks, *result, *err;
499 const char *replystr;
500 size_t i;
501 const jsmntok_t *t;
502
503 ocmd = find_commando(outgoing_commands, peer, &idnum);
504 if (!ocmd) {
505 plugin_log(plugin, LOG_DBG,
506 "Ignoring unexpected %s reply from %s (id %"PRIu64")",
507 terminal ? "terminal" : "partial",
508 node_id_to_hexstr(tmpctx, peer),
509 idnum);
510 return NULL;
511 }
512
513 /* FIXME: We buffer, but ideally we would stream! */
514 /* listchannels is 71MB, so we need to allow some headroom! */
515 append_contents(ocmd, msg, msglen, 500*1024*1024);
516
517 if (!terminal)
518 return NULL;
519
520 if (!ocmd->contents)
521 return command_fail(ocmd->cmd, COMMANDO_ERROR_LOCAL, "Reply was oversize");
522
523 replystr = (const char *)ocmd->contents;
524 toks = json_parse_simple(ocmd, replystr, tal_bytelen(ocmd->contents));
525 if (!toks || toks[0].type != JSMN_OBJECT)
526 return command_fail(ocmd->cmd, COMMANDO_ERROR_LOCAL,
527 "Reply was unparsable: '%.*s'",
528 (int)tal_bytelen(ocmd->contents), replystr);
529
530 err = json_get_member(replystr, toks, "error");
531 if (err) {
532 const jsmntok_t *code = json_get_member(replystr, err, "code");
533 const jsmntok_t *message = json_get_member(replystr, err, "message");
534 const jsmntok_t *datatok = json_get_member(replystr, err, "data");
535 struct json_out *data;
536 int ecode;
537 if (!code || !json_to_int(replystr, code, &ecode)) {
538 return command_fail(ocmd->cmd, COMMANDO_ERROR_LOCAL,
539 "Error '%.*s' had no valid code",
540 json_tok_full_len(err),
541 json_tok_full(replystr, err));
542 }
543 if (!message) {
544 return command_fail(ocmd->cmd, COMMANDO_ERROR_LOCAL,
545 "Error had no message");
546 }
547 if (datatok) {
548 data = json_out_new(ocmd->cmd);

Callers 1

handle_custommsgFunction · 0.85

Calls 15

find_commandoFunction · 0.85
append_contentsFunction · 0.85
tal_bytelenFunction · 0.85
json_out_newFunction · 0.85
json_out_directFunction · 0.85
command_done_errFunction · 0.85
json_add_jsonstrFunction · 0.85
plugin_logFunction · 0.70
command_failFunction · 0.70
jsonrpc_stream_successFunction · 0.70
command_finishedFunction · 0.70
node_id_to_hexstrFunction · 0.50

Tested by

no test coverage detected