gossipd: fix json_listpeers printing node information.

json_listpeers returns an array of peers, and an array of nodes: the latter
is a subset of the former, and is used for printing alias/color information.

This changes it so there is a 1:1 correspondance between the peer information
and nodes, meaning no more O(n^2) search.

If there is no node_announce for a peer, we use a negative timestamp
(already used to indicate that the rest of the gossip_getnodes_entry
is not valid).

Other fixes:
1. Use get_node instead of iterating through the node map.
2. A node without addresses is perfectly valid: we have to use the timestamp
   to see if the alias/color are set.  Previously we wouldn't print that
   if it didn't also advertize an address.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
Rusty Russell
2018-06-22 09:22:57 +09:30
committed by Christian Decker
parent a9cf73380a
commit 7b735fbeee
3 changed files with 30 additions and 38 deletions

View File

@@ -704,27 +704,18 @@ struct getpeers_args {
};
static void json_add_node_decoration(struct json_result *response,
struct gossip_getnodes_entry **nodes,
const struct pubkey *id)
struct gossip_getnodes_entry *node)
{
for (size_t i = 0; i < tal_count(nodes); i++) {
struct json_escaped *esc;
struct json_escaped *esc;
/* If no addresses, then this node announcement hasn't been
* received yet So no alias information either.
*/
if (nodes[i]->addresses == NULL)
continue;
/* If node announcement hasn't been received yet, no alias information.
*/
if (node->last_timestamp < 0)
return;
if (!pubkey_eq(&nodes[i]->nodeid, id))
continue;
esc = json_escape(NULL, (const char *)nodes[i]->alias);
json_add_escaped_string(response, "alias", take(esc));
json_add_hex(response, "color",
nodes[i]->color, ARRAY_SIZE(nodes[i]->color));
break;
}
esc = json_escape(NULL, (const char *)node->alias);
json_add_escaped_string(response, "alias", take(esc));
json_add_hex(response, "color", node->color, ARRAY_SIZE(node->color));
}
static void gossipd_getpeers_complete(struct subd *gossip, const u8 *msg,
@@ -778,7 +769,14 @@ static void gossipd_getpeers_complete(struct subd *gossip, const u8 *msg,
json_array_end(response);
}
json_add_node_decoration(response, nodes, &p->id);
/* Search gossip reply for this ID, to add extra info. */
for (size_t i = 0; i < tal_len(nodes); i++) {
if (pubkey_eq(&nodes[i]->nodeid, &p->id)) {
json_add_node_decoration(response, nodes[i]);
break;
}
}
json_array_start(response, "channels");
json_add_uncommitted_channel(response, p->uncommitted_channel);
@@ -909,7 +907,7 @@ static void gossipd_getpeers_complete(struct subd *gossip, const u8 *msg,
/* Fake state. */
json_add_string(response, "state", "GOSSIPING");
json_add_pubkey(response, "id", ids+i);
json_add_node_decoration(response, nodes, ids+i);
json_add_node_decoration(response, nodes[i]);
json_array_start(response, "netaddr");
if (addrs[i].itype != ADDR_INTERNAL_WIREADDR
|| addrs[i].u.wireaddr.type != ADDR_TYPE_PADDING)