mirror of
https://github.com/aljazceru/lightning.git
synced 2026-02-23 06:54:30 +01:00
Merge remote-tracking branch 'origin/pr/44'
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
@@ -28,6 +28,7 @@ static bool node_eq(const struct node *n, const secp256k1_pubkey *key)
|
||||
{
|
||||
return structeq(&n->id.pubkey, key);
|
||||
}
|
||||
|
||||
HTABLE_DEFINE_TYPE(struct node, keyof_node, hash_key, node_eq, node_map);
|
||||
|
||||
struct node_map *empty_node_map(struct lightningd_state *dstate)
|
||||
@@ -63,12 +64,33 @@ struct node *new_node(struct lightningd_state *dstate,
|
||||
n->id = *id;
|
||||
n->in = tal_arr(n, struct node_connection *, 0);
|
||||
n->out = tal_arr(n, struct node_connection *, 0);
|
||||
n->port = 0;
|
||||
node_map_add(dstate->nodes, n);
|
||||
tal_add_destructor(n, destroy_node);
|
||||
|
||||
return n;
|
||||
}
|
||||
|
||||
struct node *add_node(
|
||||
struct lightningd_state *dstate,
|
||||
const struct pubkey *pk,
|
||||
char *hostname,
|
||||
int port)
|
||||
{
|
||||
struct node *n = get_node(dstate, pk);
|
||||
if (!n) {
|
||||
n = new_node(dstate, pk);
|
||||
log_debug_struct(dstate->base_log, "Creating new node %s",
|
||||
struct pubkey, pk);
|
||||
} else {
|
||||
log_debug_struct(dstate->base_log, "Update existing node %s",
|
||||
struct pubkey, pk);
|
||||
}
|
||||
n->hostname = tal_steal(n, hostname);
|
||||
n->port = port;
|
||||
return n;
|
||||
}
|
||||
|
||||
static bool remove_conn_from_array(struct node_connection ***conns,
|
||||
struct node_connection *nc)
|
||||
{
|
||||
@@ -526,4 +548,40 @@ const struct json_command dev_routefail_command = {
|
||||
"Returns an empty result on success"
|
||||
};
|
||||
|
||||
static void json_getnodes(struct command *cmd,
|
||||
const char *buffer, const jsmntok_t *params)
|
||||
{
|
||||
struct json_result *response = new_json_result(cmd);
|
||||
struct node *n;
|
||||
struct node_map_iter i;
|
||||
|
||||
n = node_map_first(cmd->dstate->nodes, &i);
|
||||
|
||||
json_object_start(response, NULL);
|
||||
json_array_start(response, "nodes");
|
||||
|
||||
while (n != NULL) {
|
||||
json_object_start(response, NULL);
|
||||
json_add_pubkey(response, cmd->dstate->secpctx,
|
||||
"nodeid", &n->id);
|
||||
json_add_num(response, "port", n->port);
|
||||
if (!n->port)
|
||||
json_add_null(response, "hostname");
|
||||
else
|
||||
json_add_string(response, "hostname", n->hostname);
|
||||
|
||||
json_object_end(response);
|
||||
n = node_map_next(cmd->dstate->nodes, &i);
|
||||
}
|
||||
|
||||
json_array_end(response);
|
||||
json_object_end(response);
|
||||
command_success(cmd, response);
|
||||
}
|
||||
|
||||
const struct json_command getnodes_command = {
|
||||
"getnodes",
|
||||
json_getnodes,
|
||||
"List all known nodes in the network.",
|
||||
"Returns a 'nodes' array"
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user