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

Function json_commando

plugins/commando.c:655–723  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

653}
654
655static struct command_result *json_commando(struct command *cmd,
656 const char *buffer,
657 const jsmntok_t *params)
658{
659 struct node_id *peer;
660 const char *method, *cparams;
661 const char *rune, *filter;
662 struct commando *ocmd;
663 struct outgoing *outgoing;
664 char *json;
665 size_t jsonlen;
666 u64 oid;
667
668 if (!param(cmd, buffer, params,
669 p_req("peer_id", param_node_id, &peer),
670 p_req("method", param_string, &method),
671 p_opt("params", param_string, &cparams),
672 p_opt("rune", param_string, &rune),
673 p_opt("filter", param_string, &filter),
674 NULL))
675 return command_param_failed();
676
677 do {
678 oid = pseudorand_u64();
679 } while (find_commando(outgoing_commands, NULL, &oid));
680
681 ocmd = new_commando(cmd, cmd, peer, oid);
682 ocmd->contents = tal_arr(ocmd, u8, 0);
683 ocmd->json_id = tal_strdup(ocmd, cmd->id);
684
685 tal_arr_expand(&outgoing_commands, ocmd);
686 tal_add_destructor2(ocmd, destroy_commando, &outgoing_commands);
687
688 /* We pass through their JSON id untouched. */
689 json = tal_fmt(tmpctx,
690 "{\"method\":\"%s\",\"id\":%s,\"params\":%s", method,
691 ocmd->json_id, cparams ? cparams : "{}");
692 if (rune)
693 tal_append_fmt(&json, ",\"rune\":\"%s\"", rune);
694 if (filter)
695 tal_append_fmt(&json, ",\"filter\":%s", filter);
696 tal_append_fmt(&json, "}");
697
698 outgoing = tal(cmd, struct outgoing);
699 outgoing->peer = *peer;
700 outgoing->msg_off = 0;
701 /* 65000 per message gives sufficient headroom. */
702 jsonlen = tal_bytelen(json)-1;
703 outgoing->msgs = tal_arr(cmd, u8 *, (jsonlen + 64999) / 65000);
704 for (size_t i = 0; i < tal_count(outgoing->msgs); i++) {
705 u8 *cmd_msg = tal_arr(outgoing, u8, 0);
706 bool terminal = (i == tal_count(outgoing->msgs) - 1);
707 size_t off = i * 65000, len;
708
709 if (terminal)
710 len = jsonlen - off;
711 else
712 len = 65000;

Callers

nothing calls this directly

Calls 10

find_commandoFunction · 0.85
new_commandoFunction · 0.85
tal_append_fmtFunction · 0.85
tal_bytelenFunction · 0.85
send_more_cmdFunction · 0.85
command_param_failedFunction · 0.70
paramFunction · 0.50
towire_u16Function · 0.50
towire_u64Function · 0.50
towireFunction · 0.50

Tested by

no test coverage detected