Skip to content

Commit

Permalink
lightningd: have json_stream_success start the "result" object.
Browse files Browse the repository at this point in the history
"result" should always be an object (so that we can add new fields),
so make that implicit in json_stream_success.

This makes our primitives well-formed: we previously used NULL as our
fieldname when calling the first json_object_start, which is a hack
since we're actually in an object and the fieldname is 'result' (which
was already written by json_object_start).

There were only two cases which didn't do this:
1. dev-memdump returned an array.  No API guarantees on this.
2. shutdown returned a string.

I temporarily made shutdown return an empty object, which shouldn't
break anything, but I want to fix that later anyway.

Signed-off-by: Rusty Russell <[email protected]>
  • Loading branch information
rustyrussell committed Jun 12, 2019
1 parent 0e336ac commit bb7bbd0
Show file tree
Hide file tree
Showing 17 changed files with 31 additions and 124 deletions.
5 changes: 1 addition & 4 deletions lightningd/chaintopology.c
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ static void broadcast_remainder(struct bitcoind *bitcoind,
if (txs->cursor == tal_count(txs->txs)) {
if (txs->cmd)
was_pending(command_success(txs->cmd,
null_response(txs->cmd)));
json_stream_success(txs->cmd)));
tal_free(txs);
return;
}
Expand Down Expand Up @@ -491,7 +491,6 @@ static struct command_result *json_feerates(struct command *cmd,
}

response = json_stream_success(cmd);
json_object_start(response, NULL);
json_object_start(response, json_feerate_style_name(*style));
for (size_t i = 0; i < ARRAY_SIZE(feerates); i++) {
if (!feerates[i])
Expand Down Expand Up @@ -522,8 +521,6 @@ static struct command_result *json_feerates(struct command *cmd,
json_object_end(response);
}

json_object_end(response);

return command_success(cmd, response);
}

Expand Down
2 changes: 0 additions & 2 deletions lightningd/connect_control.c
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,7 @@ static struct command_result *connect_cmd_succeed(struct command *cmd,
const struct node_id *id)
{
struct json_stream *response = json_stream_success(cmd);
json_object_start(response, NULL);
json_add_node_id(response, "id", id);
json_object_end(response);
return command_success(cmd, response);
}

Expand Down
18 changes: 4 additions & 14 deletions lightningd/gossip_control.c
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,6 @@ static void json_getnodes_reply(struct subd *gossip UNUSED, const u8 *reply,
}

response = json_stream_success(cmd);
json_object_start(response, NULL);
json_array_start(response, "nodes");

for (i = 0; i < tal_count(nodes); i++) {
Expand Down Expand Up @@ -229,7 +228,6 @@ static void json_getnodes_reply(struct subd *gossip UNUSED, const u8 *reply,
json_object_end(response);
}
json_array_end(response);
json_object_end(response);
was_pending(command_success(cmd, response));
}

Expand Down Expand Up @@ -274,9 +272,7 @@ static void json_getroute_reply(struct subd *gossip UNUSED, const u8 *reply, con
}

response = json_stream_success(cmd);
json_object_start(response, NULL);
json_add_route(response, "route", hops, tal_count(hops));
json_object_end(response);
was_pending(command_success(cmd, response));
}

Expand Down Expand Up @@ -436,7 +432,6 @@ static void json_listchannels_reply(struct subd *gossip UNUSED, const u8 *reply,
req, -1, 0, json_listchannels_reply, linfo);
} else {
json_array_end(linfo->response);
json_object_end(linfo->response);
was_pending(command_success(linfo->cmd, linfo->response));
}
}
Expand All @@ -462,7 +457,6 @@ static struct command_result *json_listchannels(struct command *cmd,

/* Start JSON response, then we stream. */
linfo->response = json_stream_success(cmd);
json_object_start(linfo->response, NULL);
json_array_start(linfo->response, "channels");

req = towire_gossip_getchannels_request(cmd, linfo->id, linfo->source,
Expand Down Expand Up @@ -501,9 +495,7 @@ static void json_scids_reply(struct subd *gossip UNUSED, const u8 *reply,
}

response = json_stream_success(cmd);
json_object_start(response, NULL);
json_add_bool(response, "complete", complete);
json_object_end(response);
was_pending(command_success(cmd, response));
}

Expand Down Expand Up @@ -573,7 +565,7 @@ json_dev_send_timestamp_filter(struct command *cmd,
msg = towire_gossip_send_timestamp_filter(NULL, id, *first, *range);
subd_send_msg(cmd->ld->gossip, take(msg));

return command_success(cmd, null_response(cmd));
return command_success(cmd, json_stream_success(cmd));
}

static const struct json_command dev_send_timestamp_filter = {
Expand Down Expand Up @@ -609,7 +601,6 @@ static void json_channel_range_reply(struct subd *gossip UNUSED, const u8 *reply
}

response = json_stream_success(cmd);
json_object_start(response, NULL);
/* As this is a dev interface, we don't bother saving and
* returning all the replies, just the final one. */
json_add_num(response, "final_first_block", final_first_block);
Expand All @@ -619,7 +610,6 @@ static void json_channel_range_reply(struct subd *gossip UNUSED, const u8 *reply
for (size_t i = 0; i < tal_count(scids); i++)
json_add_short_channel_id(response, NULL, &scids[i]);
json_array_end(response);
json_object_end(response);
was_pending(command_success(cmd, response));
}

Expand Down Expand Up @@ -671,7 +661,7 @@ json_dev_set_max_scids_encode_size(struct command *cmd,
msg = towire_gossip_dev_set_max_scids_encode_size(NULL, *max);
subd_send_msg(cmd->ld->gossip, take(msg));

return command_success(cmd, null_response(cmd));
return command_success(cmd, json_stream_success(cmd));
}

static const struct json_command dev_set_max_scids_encode_size = {
Expand All @@ -692,7 +682,7 @@ static struct command_result *json_dev_suppress_gossip(struct command *cmd,

subd_send_msg(cmd->ld->gossip, take(towire_gossip_dev_suppress(NULL)));

return command_success(cmd, null_response(cmd));
return command_success(cmd, json_stream_success(cmd));
}

static const struct json_command dev_suppress_gossip = {
Expand Down Expand Up @@ -720,7 +710,7 @@ static void dev_compact_gossip_store_reply(struct subd *gossip UNUSED,
was_pending(command_fail(cmd, LIGHTNINGD,
"gossip_compact_store failed"));
else
was_pending(command_success(cmd, null_response(cmd)));
was_pending(command_success(cmd, json_stream_success(cmd)));
}

static struct command_result *json_dev_compact_gossip_store(struct command *cmd,
Expand Down
17 changes: 6 additions & 11 deletions lightningd/invoice.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ static const char *invoice_status_str(const struct invoice_details *inv)
static void json_add_invoice(struct json_stream *response,
const struct invoice_details *inv)
{
json_object_start(response, NULL);
json_add_escaped_string(response, "label", inv->label);
json_add_string(response, "bolt11", inv->bolt11);
json_add_hex(response, "payment_hash", &inv->rhash, sizeof(inv->rhash));
Expand All @@ -66,8 +65,6 @@ static void json_add_invoice(struct json_stream *response,
json_add_string(response, "description", inv->description);

json_add_u64(response, "expires_at", inv->expiry_time);

json_object_end(response);
}

static struct command_result *tell_waiter(struct command *cmd,
Expand All @@ -86,6 +83,7 @@ static struct command_result *tell_waiter(struct command *cmd,
response = json_stream_fail(cmd, -2,
"invoice expired during wait");
json_add_invoice(response, details);
json_object_end(response);
return command_failed(cmd, response);
}
}
Expand Down Expand Up @@ -512,7 +510,6 @@ static void gossipd_incoming_channels_reply(struct subd *gossipd,
details = wallet_invoice_details(info, wallet, invoice);

response = json_stream_success(info->cmd);
json_object_start(response, NULL);
json_add_hex(response, "payment_hash", details->rhash.u.u8,
sizeof(details->rhash));
json_add_u64(response, "expires_at", details->expiry_time);
Expand All @@ -538,7 +535,6 @@ static void gossipd_incoming_channels_reply(struct subd *gossipd,
"No channels have sufficient"
" incoming capacity");
}
json_object_end(response);

was_pending(command_success(info->cmd, response));
}
Expand Down Expand Up @@ -809,15 +805,19 @@ static void json_add_invoices(struct json_stream *response,
struct invoice invoice;
if (wallet_invoice_find_by_label(wallet, &invoice, label)) {
details = wallet_invoice_details(response, wallet, invoice);
json_object_start(response, NULL);
json_add_invoice(response, details);
json_object_end(response);
}
return;
}

memset(&it, 0, sizeof(it));
while (wallet_invoice_iterate(wallet, &it)) {
details = wallet_invoice_iterator_deref(response, wallet, &it);
json_object_start(response, NULL);
json_add_invoice(response, details);
json_object_end(response);
}
}

Expand All @@ -834,11 +834,9 @@ static struct command_result *json_listinvoices(struct command *cmd,
NULL))
return command_param_failed();
response = json_stream_success(cmd);
json_object_start(response, NULL);
json_array_start(response, "invoices");
json_add_invoices(response, wallet, label);
json_array_end(response);
json_object_end(response);
return command_success(cmd, response);
}

Expand Down Expand Up @@ -918,7 +916,7 @@ static struct command_result *json_delexpiredinvoice(struct command *cmd,

wallet_invoice_delete_expired(cmd->ld->wallet, *maxexpirytime);

return command_success(cmd, null_response(cmd));
return command_success(cmd, json_stream_success(cmd));
}
static const struct json_command delexpiredinvoice_command = {
"delexpiredinvoice",
Expand Down Expand Up @@ -1069,8 +1067,6 @@ static struct command_result *json_decodepay(struct command *cmd,
}

response = json_stream_success(cmd);
json_object_start(response, NULL);

json_add_string(response, "currency", b11->chain->bip173_name);
json_add_u64(response, "created_at", b11->timestamp);
json_add_u64(response, "expiry", b11->expiry);
Expand Down Expand Up @@ -1151,7 +1147,6 @@ static struct command_result *json_decodepay(struct command *cmd,
json_add_string(response, "signature",
type_to_string(cmd, secp256k1_ecdsa_signature,
&b11->sig));
json_object_end(response);
return command_success(cmd, response);
}

Expand Down
28 changes: 4 additions & 24 deletions lightningd/jsonrpc.c
Original file line number Diff line number Diff line change
Expand Up @@ -182,16 +182,12 @@ static struct command_result *json_stop(struct command *cmd,
const jsmntok_t *obj UNNEEDED,
const jsmntok_t *params)
{
struct json_stream *response;

if (!param(cmd, buffer, params, NULL))
return command_param_failed();

/* This can't have closed yet! */
cmd->jcon->stop = true;
response = json_stream_success(cmd);
json_add_string(response, NULL, "Shutting down");
return command_success(cmd, response);
return command_success(cmd, json_stream_success(cmd));
}

static const struct json_command stop_command = {
Expand Down Expand Up @@ -219,9 +215,7 @@ static struct command_result *json_rhash(struct command *cmd,
/* Hash in place. */
sha256(secret, secret, sizeof(*secret));
response = json_stream_success(cmd);
json_object_start(response, NULL);
json_add_hex(response, "rhash", secret, sizeof(*secret));
json_object_end(response);
return command_success(cmd, response);
}

Expand All @@ -241,9 +235,7 @@ struct slowcmd {

static void slowcmd_finish(struct slowcmd *sc)
{
json_object_start(sc->js, NULL);
json_add_num(sc->js, "msec", *sc->msec);
json_object_end(sc->js);
was_pending(command_success(sc->cmd, sc->js));
}

Expand Down Expand Up @@ -388,14 +380,12 @@ static struct command_result *json_help(struct command *cmd,
asort(commands, tal_count(commands), compare_commands_name, NULL);

response = json_stream_success(cmd);
json_object_start(response, NULL);
json_array_start(response, "help");
for (size_t i = 0; i < tal_count(commands); i++) {
if (!one_cmd || one_cmd == commands[i])
json_add_help_command(cmd, response, commands[i]);
}
json_array_end(response);
json_object_end(response);

return command_success(cmd, response);
}
Expand All @@ -412,16 +402,6 @@ static const struct json_command *find_cmd(const struct jsonrpc *rpc,
return NULL;
}

struct json_stream *null_response(struct command *cmd)
{
struct json_stream *response;

response = json_stream_success(cmd);
json_object_start(response, NULL);
json_object_end(response);
return response;
}

/* This can be called directly on shutdown, even with unfinished cmd */
static void destroy_command(struct command *cmd)
{
Expand Down Expand Up @@ -451,7 +431,7 @@ struct command_result *command_success(struct command *cmd,
{
assert(cmd);
assert(cmd->have_json_stream);
json_stream_append(result, " }\n\n");
json_stream_append(result, " } }\n\n");

return command_raw_complete(cmd, result);
}
Expand Down Expand Up @@ -543,6 +523,7 @@ struct json_stream *json_stream_success(struct command *cmd)
{
struct json_stream *r = json_start(cmd);
json_stream_append(r, "\"result\" : ");
json_object_start(r, NULL);
return r;
}

Expand All @@ -568,6 +549,7 @@ struct json_stream *json_stream_fail(struct command *cmd,
struct json_stream *r = json_stream_fail_nodata(cmd, code, errmsg);

json_stream_append(r, ", \"data\" : ");
json_object_start(r, NULL);
return r;
}

Expand Down Expand Up @@ -1169,9 +1151,7 @@ static struct command_result *json_check(struct command *cmd,
return res;

response = json_stream_success(cmd);
json_object_start(response, NULL);
json_add_string(response, "command_to_check", cmd->json_cmd->name);
json_object_end(response);
return command_success(cmd, response);
}

Expand Down
8 changes: 4 additions & 4 deletions lightningd/jsonrpc.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ struct jsonrpc_request {
};

/**
* json_stream_success - start streaming a successful json result.
* json_stream_success - start streaming a successful json result object.
* @cmd: the command we're running.
*
* The returned value should go to command_success() when done.
Expand All @@ -86,13 +86,14 @@ struct jsonrpc_request {
struct json_stream *json_stream_success(struct command *cmd);

/**
* json_stream_fail - start streaming a failed json result.
* json_stream_fail - start streaming a failed json result, with data object.
* @cmd: the command we're running.
* @code: the error code from common/jsonrpc_errors.h
* @errmsg: the error string.
*
* The returned value should go to command_failed() when done;
* json_add_* will be placed into the 'data' field of the 'error' JSON reply.
* You need to json_object_end() once you're done!
*/
struct json_stream *json_stream_fail(struct command *cmd,
int code,
Expand All @@ -110,12 +111,11 @@ struct json_stream *json_stream_fail_nodata(struct command *cmd,
int code,
const char *errmsg);

struct json_stream *null_response(struct command *cmd);

/* These returned values are never NULL. */
struct command_result *command_success(struct command *cmd,
struct json_stream *response)
WARN_UNUSED_RESULT;

struct command_result *command_failed(struct command *cmd,
struct json_stream *result)
WARN_UNUSED_RESULT;
Expand Down
2 changes: 0 additions & 2 deletions lightningd/log.c
Original file line number Diff line number Diff line change
Expand Up @@ -732,12 +732,10 @@ static struct command_result *json_getlog(struct command *cmd,
response = json_stream_success(cmd);
/* Suppress logging for this stream, to not bloat io logs */
json_stream_log_suppress_for_cmd(response, cmd);
json_object_start(response, NULL);
json_add_time(response, "created_at", log_init_time(lr)->ts);
json_add_num(response, "bytes_used", (unsigned int) log_used(lr));
json_add_num(response, "bytes_max", (unsigned int) log_max_mem(lr));
json_add_log(response, lr, *minlevel);
json_object_end(response);
return command_success(cmd, response);
}

Expand Down
Loading

0 comments on commit bb7bbd0

Please sign in to comment.