jsonrpc: register base on linkage.

Other than being neater (no more global list to edit!), this lets the
new daemon and old daemon have their own separate routines.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
Rusty Russell 2017-01-04 14:08:15 +10:30
parent c7b69abdaa
commit 12b30ab4fc
8 changed files with 73 additions and 96 deletions

View File

@ -592,12 +592,13 @@ static void json_dev_broadcast(struct command *cmd,
command_success(cmd, null_response(cmd));
}
const struct json_command dev_broadcast_command = {
static const struct json_command dev_broadcast_command = {
"dev-broadcast",
json_dev_broadcast,
"Pretend we broadcast txs, but don't send to bitcoind",
"Returns an empty result on success (waits for flush if enabled)"
};
AUTODATA(json_command, &dev_broadcast_command);
void setup_topology(struct lightningd_state *dstate)
{

View File

@ -196,12 +196,13 @@ static void json_invoice(struct command *cmd,
command_success(cmd, response);
}
const struct json_command invoice_command = {
static const struct json_command invoice_command = {
"invoice",
json_invoice,
"Create invoice for {msatoshi} with {label} (with a set {r}, otherwise generate one)",
"Returns the {rhash} on success. "
};
AUTODATA(json_command, &invoice_command);
static void json_add_invoices(struct json_result *response,
const struct list_head *list,
@ -246,12 +247,13 @@ static void json_listinvoice(struct command *cmd,
command_success(cmd, response);
}
const struct json_command listinvoice_command = {
static const struct json_command listinvoice_command = {
"listinvoice",
json_listinvoice,
"Show invoice {label} (or all, if no {label}))",
"Returns an array of {label}, {rhash}, {msatoshi} and {complete} on success. "
};
AUTODATA(json_command, &listinvoice_command);
static void json_delinvoice(struct command *cmd,
const char *buffer, const jsmntok_t *params)
@ -291,12 +293,13 @@ static void json_delinvoice(struct command *cmd,
tal_free(i);
}
const struct json_command delinvoice_command = {
static const struct json_command delinvoice_command = {
"delinvoice",
json_delinvoice,
"Delete unpaid invoice {label}))",
"Returns {label}, {rhash} and {msatoshi} on success. "
};
AUTODATA(json_command, &delinvoice_command);
static void json_waitinvoice(struct command *cmd,
const char *buffer, const jsmntok_t *params)
@ -340,9 +343,10 @@ static void json_waitinvoice(struct command *cmd,
list_add_tail(&invs->invoice_waiters, &w->list);
}
const struct json_command waitinvoice_command = {
static const struct json_command waitinvoice_command = {
"waitinvoice",
json_waitinvoice,
"Wait for the next invoice to be paid, after {label} (if supplied)))",
"Returns {label}, {rhash} and {msatoshi} on success. "
};
AUTODATA(json_command, &waitinvoice_command);

View File

@ -44,6 +44,7 @@ static const struct json_command help_command = {
"describe commands",
"[<command>] if specified gives details about a single command."
};
AUTODATA(json_command, &help_command);
static void json_stop(struct command *cmd,
const char *buffer, const jsmntok_t *params)
@ -62,6 +63,7 @@ static const struct json_command stop_command = {
"Shutdown the lightningd process",
"What part of shutdown wasn't clear?"
};
AUTODATA(json_command, &stop_command);
struct log_info {
enum log_level level;
@ -174,6 +176,7 @@ static const struct json_command getlog_command = {
"Get logs, with optional level: [io|debug|info|unusual]",
"Returns log array"
};
AUTODATA(json_command, &getlog_command);
static void json_rhash(struct command *cmd,
const char *buffer, const jsmntok_t *params)
@ -212,6 +215,7 @@ static const struct json_command dev_rhash_command = {
"SHA256 of {secret}",
"Returns a hash value"
};
AUTODATA(json_command, &dev_rhash_command);
static void json_crash(struct command *cmd,
const char *buffer, const jsmntok_t *params)
@ -225,6 +229,7 @@ static const struct json_command dev_crash_command = {
"Call fatal().",
"Simple crash test for developers"
};
AUTODATA(json_command, &dev_crash_command);
static void json_restart(struct command *cmd,
const char *buffer, const jsmntok_t *params)
@ -256,6 +261,7 @@ static const struct json_command dev_restart_command = {
"Re-exec the given {binary}.",
"Simple restart test for developers"
};
AUTODATA(json_command, &dev_restart_command);
static void json_getinfo(struct command *cmd,
const char *buffer, const jsmntok_t *params)
@ -280,51 +286,28 @@ static const struct json_command getinfo_command = {
"Get general information about this node",
"Returns {id}, {port}, {testnet}, etc."
};
AUTODATA(json_command, &getinfo_command);
static const struct json_command *cmdlist[] = {
&help_command,
&stop_command,
&getlog_command,
&connect_command,
&getpeers_command,
&getnodes_command,
&gethtlcs_command,
&close_command,
&newaddr_command,
&invoice_command,
&listinvoice_command,
&delinvoice_command,
&waitinvoice_command,
&getchannels_command,
&getroute_command,
&sendpay_command,
&getinfo_command,
/* Developer/debugging options. */
&dev_newhtlc_command,
&dev_fulfillhtlc_command,
&dev_failhtlc_command,
&dev_commit_command,
&dev_feerate_command,
&dev_rhash_command,
&dev_crash_command,
&dev_restart_command,
&dev_disconnect_command,
&dev_reconnect_command,
&dev_signcommit_command,
&dev_output_command,
&dev_add_route_command,
&dev_routefail_command,
&dev_broadcast_command,
};
static size_t num_cmdlist;
static struct json_command **get_cmdlist(void)
{
static struct json_command **cmdlist;
if (!cmdlist)
cmdlist = autodata_get(json_command, &num_cmdlist);
return cmdlist;
}
static void json_help(struct command *cmd,
const char *buffer, const jsmntok_t *params)
{
unsigned int i;
struct json_result *response = new_json_result(cmd);
struct json_command **cmdlist = get_cmdlist();
json_array_start(response, NULL);
for (i = 0; i < ARRAY_SIZE(cmdlist); i++) {
for (i = 0; i < num_cmdlist; i++) {
json_add_object(response,
"command", JSMN_STRING,
cmdlist[i]->name,
@ -340,9 +323,10 @@ static const struct json_command *find_cmd(const char *buffer,
const jsmntok_t *tok)
{
unsigned int i;
struct json_command **cmdlist = get_cmdlist();
/* cmdlist[i]->name can be NULL in test code. */
for (i = 0; i < ARRAY_SIZE(cmdlist); i++)
for (i = 0; i < num_cmdlist; i++)
if (cmdlist[i]->name
&& json_tok_streq(buffer, tok, cmdlist[i]->name))
return cmdlist[i];

View File

@ -2,6 +2,7 @@
#define LIGHTNING_DAEMON_JSONRPC_H
#include "config.h"
#include "json.h"
#include <ccan/autodata/autodata.h>
#include <ccan/list/list.h>
/* Context for a command (from JSON, but might outlive the connection!)
@ -56,38 +57,5 @@ void PRINTF_FMT(2, 3) command_fail(struct command *cmd, const char *fmt, ...);
/* For initialization */
void setup_jsonrpc(struct lightningd_state *dstate, const char *rpc_filename);
/* Peer management */
extern const struct json_command newaddr_command;
extern const struct json_command connect_command;
extern const struct json_command close_command;
extern const struct json_command getchannels_command;
extern const struct json_command getpeers_command;
extern const struct json_command getnodes_command;
/* Invoice management. */
extern const struct json_command invoice_command;
extern const struct json_command listinvoice_command;
extern const struct json_command delinvoice_command;
extern const struct json_command waitinvoice_command;
/* Payment management. */
extern const struct json_command getroute_command;
extern const struct json_command sendpay_command;
/* Low-level commands. */
extern const struct json_command gethtlcs_command;
/* Developer commands. */
extern const struct json_command dev_add_route_command;
extern const struct json_command dev_newhtlc_command;
extern const struct json_command dev_fulfillhtlc_command;
extern const struct json_command dev_failhtlc_command;
extern const struct json_command dev_commit_command;
extern const struct json_command dev_reconnect_command;
extern const struct json_command dev_disconnect_command;
extern const struct json_command dev_signcommit_command;
extern const struct json_command dev_output_command;
extern const struct json_command dev_routefail_command;
extern const struct json_command dev_feerate_command;
extern const struct json_command dev_broadcast_command;
AUTODATA_TYPE(json_command, struct json_command);
#endif /* LIGHTNING_DAEMON_JSONRPC_H */

View File

@ -281,12 +281,13 @@ static void json_getroute(struct command *cmd,
command_success(cmd, response);
}
const struct json_command getroute_command = {
static const struct json_command getroute_command = {
"getroute",
json_getroute,
"Return route to {id} for {msatoshi}, using {riskfactor}",
"Returns a {route} array of {id} {msatoshi} {delay}: msatoshi and delay (in blocks) is cumulative."
};
AUTODATA(json_command, &getroute_command);
static void json_sendpay(struct command *cmd,
const char *buffer, const jsmntok_t *params)
@ -494,9 +495,10 @@ static void json_sendpay(struct command *cmd,
tal_add_destructor(cmd, remove_cmd_from_pc);
}
const struct json_command sendpay_command = {
static const struct json_command sendpay_command = {
"sendpay",
json_sendpay,
"Send along {route} in return for preimage of {rhash}",
"Returns the {preimage} on success"
};
AUTODATA(json_command, &sendpay_command);

View File

@ -3225,12 +3225,13 @@ static void json_connect(struct command *cmd,
tal_free(tmpctx);
}
const struct json_command connect_command = {
static const struct json_command connect_command = {
"connect",
json_connect,
"Connect to a {host} at {port} using hex-encoded {tx} to fund",
"Returns the {id} on success (once channel established)"
};
AUTODATA(json_command, &connect_command);
/* Have any of our HTLCs passed their deadline? */
static bool any_deadline_past(struct peer *peer)
@ -4568,12 +4569,13 @@ static void json_getpeers(struct command *cmd,
command_success(cmd, response);
}
const struct json_command getpeers_command = {
static const struct json_command getpeers_command = {
"getpeers",
json_getpeers,
"List the current peers",
"Returns a 'peers' array"
};
AUTODATA(json_command, &getpeers_command);
static void json_gethtlcs(struct command *cmd,
const char *buffer, const jsmntok_t *params)
@ -4640,12 +4642,13 @@ static void json_gethtlcs(struct command *cmd,
command_success(cmd, response);
}
const struct json_command gethtlcs_command = {
static const struct json_command gethtlcs_command = {
"gethtlcs",
json_gethtlcs,
"List HTLCs for {peer}; all if {resolved} is true.",
"Returns a 'htlcs' array"
};
AUTODATA(json_command, &gethtlcs_command);
/* To avoid freeing underneath ourselves, we free outside event loop. */
void cleanup_peers(struct lightningd_state *dstate)
@ -4751,12 +4754,13 @@ static void json_newhtlc(struct command *cmd,
command_success(cmd, response);
}
const struct json_command dev_newhtlc_command = {
static const struct json_command dev_newhtlc_command = {
"dev-newhtlc",
json_newhtlc,
"Offer {peerid} an HTLC worth {msatoshi} in {expiry} (block number) with {rhash}",
"Returns { id: u64 } result on success"
};
AUTODATA(json_command, &dev_newhtlc_command);
static void json_fulfillhtlc(struct command *cmd,
const char *buffer, const jsmntok_t *params)
@ -4852,12 +4856,13 @@ static void json_fulfillhtlc(struct command *cmd,
state_name(peer->state));
}
const struct json_command dev_fulfillhtlc_command = {
static const struct json_command dev_fulfillhtlc_command = {
"dev-fulfillhtlc",
json_fulfillhtlc,
"Redeem htlc proposed by {peerid} of {id} using {r}",
"Returns an empty result on success"
};
AUTODATA(json_command, &dev_fulfillhtlc_command);
static void json_failhtlc(struct command *cmd,
const char *buffer, const jsmntok_t *params)
@ -4931,12 +4936,13 @@ static void json_failhtlc(struct command *cmd,
state_name(peer->state));
}
const struct json_command dev_failhtlc_command = {
static const struct json_command dev_failhtlc_command = {
"dev-failhtlc",
json_failhtlc,
"Fail htlc proposed by {peerid} which has {id}, using {reason}",
"Returns an empty result on success"
};
AUTODATA(json_command, &dev_failhtlc_command);
static void json_commit(struct command *cmd,
const char *buffer, const jsmntok_t *params)
@ -4975,12 +4981,13 @@ static void json_commit(struct command *cmd,
do_commit(peer, cmd);
}
const struct json_command dev_commit_command = {
static const struct json_command dev_commit_command = {
"dev-commit",
json_commit,
"Commit all staged HTLC changes with {peerid}",
"Returns an empty result on success"
};
AUTODATA(json_command, &dev_commit_command);
static void json_close(struct command *cmd,
const char *buffer, const jsmntok_t *params)
@ -5015,12 +5022,13 @@ static void json_close(struct command *cmd,
command_success(cmd, null_response(cmd));
}
const struct json_command close_command = {
static const struct json_command close_command = {
"close",
json_close,
"Close the channel with peer {peerid}",
"Returns an empty result on success"
};
AUTODATA(json_command, &close_command);
static void json_feerate(struct command *cmd,
const char *buffer, const jsmntok_t *params)
@ -5045,12 +5053,13 @@ static void json_feerate(struct command *cmd,
command_success(cmd, null_response(cmd));
}
const struct json_command dev_feerate_command = {
static const struct json_command dev_feerate_command = {
"dev-feerate",
json_feerate,
"Change the (default) fee rate to {feerate}",
"Returns an empty result on success"
};
AUTODATA(json_command, &dev_feerate_command);
static void json_disconnect(struct command *cmd,
const char *buffer, const jsmntok_t *params)
@ -5196,30 +5205,34 @@ static void json_output(struct command *cmd,
command_success(cmd, null_response(cmd));
}
const struct json_command dev_output_command = {
static const struct json_command dev_output_command = {
"dev-output",
json_output,
"Enable/disable any messages to peer {peerid} depending on {enable}",
"Returns an empty result on success"
};
AUTODATA(json_command, &dev_output_command);
const struct json_command dev_disconnect_command = {
static const struct json_command dev_disconnect_command = {
"dev-disconnect",
json_disconnect,
"Force a disconnect with peer {peerid}",
"Returns an empty result on success"
};
AUTODATA(json_command, &dev_disconnect_command);
const struct json_command dev_reconnect_command = {
static const struct json_command dev_reconnect_command = {
"dev-reconnect",
json_reconnect,
"Force a reconnect with peer {peerid}",
"Returns an empty result on success"
};
AUTODATA(json_command, &dev_reconnect_command);
const struct json_command dev_signcommit_command = {
static const struct json_command dev_signcommit_command = {
"dev-signcommit",
json_signcommit,
"Sign and return the current commit with peer {peerid}",
"Returns a hex string on success"
};
AUTODATA(json_command, &dev_signcommit_command);

View File

@ -574,12 +574,13 @@ void sync_routing_table(struct lightningd_state *dstate, struct peer *peer)
}
}
const struct json_command dev_add_route_command = {
static const struct json_command dev_add_route_command = {
"dev-add-route",
json_add_route,
"Add route from {src} to {dst}, {base} rate in msatoshi, {var} rate in msatoshi, {delay} blocks delay and {minblocks} minimum timeout",
"Returns an empty result on success"
};
AUTODATA(json_command, &dev_add_route_command);
static void json_getchannels(struct command *cmd,
const char *buffer, const jsmntok_t *params)
@ -612,12 +613,13 @@ static void json_getchannels(struct command *cmd,
command_success(cmd, response);
}
const struct json_command getchannels_command = {
static const struct json_command getchannels_command = {
"getchannels",
json_getchannels,
"List all known channels.",
"Returns a 'channels' array with all known channels including their fees."
};
AUTODATA(json_command, &getchannels_command);
static void json_routefail(struct command *cmd,
const char *buffer, const jsmntok_t *params)
@ -643,12 +645,13 @@ static void json_routefail(struct command *cmd,
command_success(cmd, null_response(cmd));
}
const struct json_command dev_routefail_command = {
static const struct json_command dev_routefail_command = {
"dev-routefail",
json_routefail,
"FAIL htlcs that we can't route if {enable}",
"Returns an empty result on success"
};
AUTODATA(json_command, &dev_routefail_command);
static void json_getnodes(struct command *cmd,
const char *buffer, const jsmntok_t *params)
@ -680,9 +683,10 @@ static void json_getnodes(struct command *cmd,
command_success(cmd, response);
}
const struct json_command getnodes_command = {
static const struct json_command getnodes_command = {
"getnodes",
json_getnodes,
"List all known nodes in the network.",
"Returns a 'nodes' array"
};
AUTODATA(json_command, &getnodes_command);

View File

@ -135,9 +135,10 @@ static void json_newaddr(struct command *cmd,
command_success(cmd, response);
}
const struct json_command newaddr_command = {
static const struct json_command newaddr_command = {
"newaddr",
json_newaddr,
"Get a new address to fund a channel",
"Returns {address} a p2sh address"
};
AUTODATA(json_command, &newaddr_command);