plugins/commando: create new_commando() allocator, use it.

We allocate one in three places, so at least a partial constructor
is a nice pattern to have.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
Rusty Russell
2023-07-25 11:13:43 +09:30
parent cd16690af8
commit 8db148871d

View File

@@ -77,6 +77,25 @@ static bool usage_eq_id(const struct usage *u, u64 id)
HTABLE_DEFINE_TYPE(struct usage, usage_id, id_hash, usage_eq_id, usage_table); HTABLE_DEFINE_TYPE(struct usage, usage_id, id_hash, usage_eq_id, usage_table);
static struct usage_table *usage_table; static struct usage_table *usage_table;
/* The minimum fields required to respond. */
static struct commando *new_commando(const tal_t *ctx,
struct command *cmd,
const struct node_id *peer,
u64 id)
{
struct commando *commando = tal(ctx, struct commando);
commando->cmd = cmd;
commando->peer = *peer;
commando->id = id;
commando->contents = NULL;
commando->json_id = NULL;
return commando;
}
/* The unique id is embedded with a special restriction with an empty field name */ /* The unique id is embedded with a special restriction with an empty field name */
static bool is_unique_id(struct rune_restr **restrs, unsigned int index) static bool is_unique_id(struct rune_restr **restrs, unsigned int index)
{ {
@@ -490,15 +509,12 @@ static void try_command(struct node_id *peer,
u64 idnum, u64 idnum,
const u8 *msg, size_t msglen) const u8 *msg, size_t msglen)
{ {
struct commando *incoming = tal(plugin, struct commando); struct commando *incoming = new_commando(plugin, NULL, peer, idnum);
const jsmntok_t *toks, *method, *params, *rune, *id, *filter; const jsmntok_t *toks, *method, *params, *rune, *id, *filter;
const char *buf = (const char *)msg, *failmsg; const char *buf = (const char *)msg, *failmsg;
struct out_req *req; struct out_req *req;
const char *cmdid_prefix; const char *cmdid_prefix;
incoming->peer = *peer;
incoming->id = idnum;
toks = json_parse_simple(incoming, buf, msglen); toks = json_parse_simple(incoming, buf, msglen);
if (!toks) { if (!toks) {
commando_error(incoming, COMMANDO_ERROR_REMOTE, commando_error(incoming, COMMANDO_ERROR_REMOTE,
@@ -611,10 +627,7 @@ static void handle_incmd(struct node_id *peer,
} }
if (!incmd) { if (!incmd) {
incmd = tal(plugin, struct commando); incmd = new_commando(plugin, NULL, peer, idnum);
incmd->id = idnum;
incmd->cmd = NULL;
incmd->peer = *peer;
incmd->contents = tal_arr(incmd, u8, 0); incmd->contents = tal_arr(incmd, u8, 0);
tal_arr_expand(&incoming_commands, incmd); tal_arr_expand(&incoming_commands, incmd);
tal_add_destructor2(incmd, destroy_commando, &incoming_commands); tal_add_destructor2(incmd, destroy_commando, &incoming_commands);
@@ -817,6 +830,7 @@ static struct command_result *json_commando(struct command *cmd,
struct outgoing *outgoing; struct outgoing *outgoing;
char *json; char *json;
size_t jsonlen; size_t jsonlen;
u64 oid;
if (!param(cmd, buffer, params, if (!param(cmd, buffer, params,
p_req("peer_id", param_node_id, &peer), p_req("peer_id", param_node_id, &peer),
@@ -827,14 +841,14 @@ static struct command_result *json_commando(struct command *cmd,
NULL)) NULL))
return command_param_failed(); return command_param_failed();
ocmd = tal(cmd, struct commando); do {
ocmd->cmd = cmd; oid = pseudorand_u64();
ocmd->peer = *peer; } while (find_commando(outgoing_commands, NULL, &oid));
ocmd = new_commando(cmd, cmd, peer, oid);
ocmd->contents = tal_arr(ocmd, u8, 0); ocmd->contents = tal_arr(ocmd, u8, 0);
ocmd->json_id = tal_strdup(ocmd, cmd->id); ocmd->json_id = tal_strdup(ocmd, cmd->id);
do {
ocmd->id = pseudorand_u64();
} while (find_commando(outgoing_commands, NULL, &ocmd->id));
tal_arr_expand(&outgoing_commands, ocmd); tal_arr_expand(&outgoing_commands, ocmd);
tal_add_destructor2(ocmd, destroy_commando, &outgoing_commands); tal_add_destructor2(ocmd, destroy_commando, &outgoing_commands);