Browse Source

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 <rusty@rustcorp.com.au>
ppa-0.6.1
Rusty Russell 8 years ago
parent
commit
de651bf8fb
  1. 17
      lightningd/gossip/gossip.c
  2. 5
      lightningd/gossip/gossip_wire.csv
  3. 29
      lightningd/gossip_control.c

17
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);
}

5
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 <lightningd/gossip_msg.h>
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

Can't render this file because it has a wrong number of fields in line 2.

29
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;
}

Loading…
Cancel
Save