From de651bf8fb1fe63be17941f4a40882b43f5c6fa6 Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Thu, 16 Mar 2017 14:35:26 +1030 Subject: [PATCH] gossip: don't use hand-coded nested messages for getnodes array. We can simply tell it 'nodes' is 'num_nodes*struct gossip_getnodes_entry'. Signed-off-by: Rusty Russell --- lightningd/gossip/gossip.c | 17 ++++++++++------- lightningd/gossip/gossip_wire.csv | 5 +++-- lightningd/gossip_control.c | 29 +++++++++++++---------------- 3 files changed, 26 insertions(+), 25 deletions(-) diff --git a/lightningd/gossip/gossip.c b/lightningd/gossip/gossip.c index e1640b26e..8e836aaff 100644 --- a/lightningd/gossip/gossip.c +++ b/lightningd/gossip/gossip.c @@ -442,20 +442,23 @@ static struct io_plan *release_peer(struct io_conn *conn, struct daemon *daemon, static struct io_plan *getnodes(struct io_conn *conn, struct daemon *daemon) { tal_t *tmpctx = tal_tmpctx(daemon); - u8 *out, *reply = tal_arr(tmpctx, u8, 0); + u8 *out; struct node *n; struct node_map_iter i; + struct gossip_getnodes_entry *nodes; + size_t node_count = 0; + nodes = tal_arr(tmpctx, struct gossip_getnodes_entry, node_count); n = node_map_first(daemon->rstate->nodes, &i); while (n != NULL) { - struct gossip_getnodes_entry entry; - entry.nodeid = n->id; - entry.hostname = n->hostname; - entry.port = n->port; - towire_gossip_getnodes_entry(&reply, &entry); + tal_resize(&nodes, node_count + 1); + nodes[node_count].nodeid = n->id; + nodes[node_count].hostname = n->hostname; + nodes[node_count].port = n->port; + node_count++; n = node_map_next(daemon->rstate->nodes, &i); } - out = towire_gossip_getnodes_reply(daemon, reply); + out = towire_gossip_getnodes_reply(daemon, nodes); tal_free(tmpctx); return io_write_wire(conn, take(out), next_req_in, daemon); } diff --git a/lightningd/gossip/gossip_wire.csv b/lightningd/gossip/gossip_wire.csv index b162816e5..70e0f7c6b 100644 --- a/lightningd/gossip/gossip_wire.csv +++ b/lightningd/gossip/gossip_wire.csv @@ -50,6 +50,7 @@ gossipstatus_peer_nongossip,156,msg,len*u8 # Pass JSON-RPC getnodes call through gossip_getnodes_request,5 +#include gossip_getnodes_reply,105 -gossip_getnodes_reply,0,replen,u16 -gossip_getnodes_reply,2,reply,replen*u8 +gossip_getnodes_reply,0,num_nodes,u16 +gossip_getnodes_reply,2,nodes,num_nodes*struct gossip_getnodes_entry diff --git a/lightningd/gossip_control.c b/lightningd/gossip_control.c index 406e872ba..ebe943ff2 100644 --- a/lightningd/gossip_control.c +++ b/lightningd/gossip_control.c @@ -154,35 +154,32 @@ void gossip_init(struct lightningd *ld) static bool json_getnodes_reply(struct subd *gossip, const u8 *reply, struct command *cmd) { - u8 *inner; - const u8 *cursor; - size_t max; - + struct gossip_getnodes_entry *nodes; struct json_result *response = new_json_result(cmd); - fromwire_gossip_getnodes_reply(reply, reply, NULL, &inner); - max = tal_len(inner); - cursor = inner; + size_t i; + + if (!fromwire_gossip_getnodes_reply(reply, reply, NULL, &nodes)) { + command_fail(cmd, "Malformed gossip_getnodes response"); + return true; + } + json_object_start(response, NULL); json_array_start(response, "nodes"); - while (max > 0) { - struct gossip_getnodes_entry *entry = tal(reply, struct gossip_getnodes_entry); - fromwire_gossip_getnodes_entry(&cursor, &max, entry); + for (i = 0; i < tal_count(nodes); i++) { json_object_start(response, NULL); - json_add_pubkey(response, "nodeid", &entry->nodeid); - if (tal_len(entry->hostname) > 0) { - json_add_string(response, "hostname", entry->hostname); + json_add_pubkey(response, "nodeid", &nodes[i].nodeid); + if (tal_len(nodes[i].hostname) > 0) { + json_add_string(response, "hostname", nodes[i].hostname); } else { json_add_null(response, "hostname"); } - json_add_num(response, "port", entry->port); + json_add_num(response, "port", nodes[i].port); json_object_end(response); - tal_free(entry); } json_array_end(response); json_object_end(response); command_success(cmd, response); - tal_free(reply); return true; }