Browse Source

refactor: Moving gossip/routing specific state into its own struct

This used to be part of `lightningd_state` which is being split up for
the various subdaemons. The main change is the addition of the `struct
routing_state` in `routing.h` and the addition of `rstate` in `struct
lightningd_state` for backwards compatibility.
ppa-0.6.1
Christian Decker 8 years ago
committed by Rusty Russell
parent
commit
3cb576d69d
  1. 2
      daemon/db.c
  2. 6
      daemon/irc_announce.c
  3. 4
      daemon/lightningd.c
  4. 4
      daemon/lightningd.h
  5. 10
      daemon/p2p_announce.c
  6. 11
      daemon/pay.c
  7. 2
      daemon/peer.c
  8. 152
      daemon/routing.c
  9. 34
      daemon/routing.h
  10. 1
      lightningd/lightningd.c

2
daemon/db.c

@ -910,7 +910,7 @@ static void restore_peer_local_visible_state(struct peer *peer)
&peer->local.next_revocation_hash); &peer->local.next_revocation_hash);
if (state_is_normal(peer->state)) if (state_is_normal(peer->state))
peer->nc = add_connection(peer->dstate, peer->nc = add_connection(peer->dstate->rstate,
&peer->dstate->id, peer->id, &peer->dstate->id, peer->id,
peer->dstate->config.fee_base, peer->dstate->config.fee_base,
peer->dstate->config.fee_per_satoshi, peer->dstate->config.fee_per_satoshi,

6
daemon/irc_announce.c

@ -165,7 +165,7 @@ static void handle_channel_announcement(
* that the endpoints match. * that the endpoints match.
*/ */
add_connection(istate->dstate, pk1, pk2, atoi(splits[6]), add_connection(istate->dstate->rstate, pk1, pk2, atoi(splits[6]),
atoi(splits[7]), atoi(splits[8]), 6); atoi(splits[7]), atoi(splits[8]), 6);
} }
@ -189,7 +189,7 @@ static void handle_node_announcement(
splits[1]); splits[1]);
} }
struct node *node = add_node(istate->dstate, pk, hostname, port); struct node *node = add_node(istate->dstate->rstate, pk, hostname, port);
if (splits[4] != NULL){ if (splits[4] != NULL){
tal_free(node->alias); tal_free(node->alias);
node->alias = tal_hexdata(node, splits[4], strlen(splits[4])); node->alias = tal_hexdata(node, splits[4], strlen(splits[4]));
@ -231,7 +231,7 @@ static void handle_irc_command(struct ircstate *istate, const struct irccommand
log_debug(dstate->base_log, "Detected my own IP as %s", dstate->external_ip); log_debug(dstate->base_log, "Detected my own IP as %s", dstate->external_ip);
// Add our node to the node_map for completeness // Add our node to the node_map for completeness
add_node(istate->dstate, &dstate->id, add_node(istate->dstate->rstate, &dstate->id,
dstate->external_ip, dstate->portnum); dstate->external_ip, dstate->portnum);
} else if (streq(cmd->command, "JOIN")) { } else if (streq(cmd->command, "JOIN")) {
unsigned int delay; unsigned int delay;

4
daemon/lightningd.c

@ -49,7 +49,7 @@ static struct lightningd_state *lightningd_state(void)
dstate->dev_never_routefail = false; dstate->dev_never_routefail = false;
dstate->dev_no_broadcast = false; dstate->dev_no_broadcast = false;
dstate->bitcoin_req_running = false; dstate->bitcoin_req_running = false;
dstate->nodes = empty_node_map(dstate); dstate->rstate = new_routing_state(dstate, dstate->base_log);
dstate->reexec = NULL; dstate->reexec = NULL;
dstate->external_ip = NULL; dstate->external_ip = NULL;
dstate->announce = NULL; dstate->announce = NULL;
@ -82,7 +82,7 @@ int main(int argc, char *argv[])
/* Set up node ID and private key. */ /* Set up node ID and private key. */
secrets_init(dstate); secrets_init(dstate);
new_node(dstate, &dstate->id); new_node(dstate->rstate, &dstate->id);
/* Read or create database. */ /* Read or create database. */
db_init(dstate); db_init(dstate);

4
daemon/lightningd.h

@ -127,8 +127,8 @@ struct lightningd_state {
/* Maintained by invoices.c */ /* Maintained by invoices.c */
struct invoices *invoices; struct invoices *invoices;
/* All known nodes. */ /* Routing information */
struct node_map *nodes; struct routing_state *rstate;
/* For testing: don't fail if we can't route. */ /* For testing: don't fail if we can't route. */
bool dev_never_routefail; bool dev_never_routefail;

10
daemon/p2p_announce.c

@ -178,18 +178,18 @@ static bool add_channel_direction(struct lightningd_state *dstate,
const struct channel_id *channel_id, const struct channel_id *channel_id,
const u8 *announcement) const u8 *announcement)
{ {
struct node_connection *c = get_connection(dstate, from, to); struct node_connection *c = get_connection(dstate->rstate, from, to);
if (c){ if (c){
/* Do not clobber connections added otherwise */ /* Do not clobber connections added otherwise */
memcpy(&c->channel_id, channel_id, sizeof(c->channel_id)); memcpy(&c->channel_id, channel_id, sizeof(c->channel_id));
c->flags = direction; c->flags = direction;
printf("Found node_connection via get_connection"); printf("Found node_connection via get_connection");
return false; return false;
}else if(get_connection_by_cid(dstate, channel_id, direction)) { }else if(get_connection_by_cid(dstate->rstate, channel_id, direction)) {
return false; return false;
} }
c = half_add_connection(dstate, from, to, channel_id, direction); c = half_add_connection(dstate->rstate, from, to, channel_id, direction);
/* Remember the announcement so we can forward it to new peers */ /* Remember the announcement so we can forward it to new peers */
tal_free(c->channel_announcement); tal_free(c->channel_announcement);
@ -292,7 +292,7 @@ void handle_channel_update(struct peer *peer, const u8 *update, size_t len)
flags & 0x01 flags & 0x01
); );
c = get_connection_by_cid(peer->dstate, &channel_id, flags & 0x1); c = get_connection_by_cid(peer->dstate->rstate, &channel_id, flags & 0x1);
if (!c) { if (!c) {
log_debug(peer->log, "Ignoring update for unknown channel %d:%d:%d", log_debug(peer->log, "Ignoring update for unknown channel %d:%d:%d",
@ -370,7 +370,7 @@ void handle_node_announcement(
tal_free(tmpctx); tal_free(tmpctx);
return; return;
} }
node = get_node(peer->dstate, &node_id); node = get_node(peer->dstate->rstate, &node_id);
if (!node) { if (!node) {
log_debug(peer->dstate->base_log, log_debug(peer->dstate->base_log,

11
daemon/pay.c

@ -84,7 +84,7 @@ static void check_routing_failure(struct lightningd_state *dstate,
/* Don't remove route if it's last node (obviously) */ /* Don't remove route if it's last node (obviously) */
for (i = 0; i+1 < tal_count(pc->ids); i++) { for (i = 0; i+1 < tal_count(pc->ids); i++) {
if (structeq(&pc->ids[i], &id)) { if (structeq(&pc->ids[i], &id)) {
remove_connection(dstate, &pc->ids[i], &pc->ids[i+1]); remove_connection(dstate->rstate, &pc->ids[i], &pc->ids[i+1]);
return; return;
} }
} }
@ -200,6 +200,7 @@ static void json_getroute(struct command *cmd,
const char *buffer, const jsmntok_t *params) const char *buffer, const jsmntok_t *params)
{ {
struct pubkey id; struct pubkey id;
struct pubkey *first;
jsmntok_t *idtok, *msatoshitok, *riskfactortok; jsmntok_t *idtok, *msatoshitok, *riskfactortok;
struct json_result *response; struct json_result *response;
int i; int i;
@ -240,9 +241,11 @@ static void json_getroute(struct command *cmd,
return; return;
} }
peer = find_route(cmd, cmd->dstate, &id, msatoshi, riskfactor, first = find_route(cmd, cmd->dstate->rstate, &cmd->dstate->id, &id, msatoshi,
&fee, &route); riskfactor, &fee, &route);
if (!peer) { if (first)
peer = find_peer(cmd->dstate, first);
if (!first || !peer) {
command_fail(cmd, "no route found"); command_fail(cmd, "no route found");
return; return;
} }

2
daemon/peer.c

@ -296,7 +296,7 @@ static void peer_open_complete(struct peer *peer, const char *problem)
log_debug(peer->log, "peer open complete"); log_debug(peer->log, "peer open complete");
assert(!peer->nc); assert(!peer->nc);
/* We're connected, so record it. */ /* We're connected, so record it. */
peer->nc = add_connection(peer->dstate, peer->nc = add_connection(peer->dstate->rstate,
&peer->dstate->id, peer->id, &peer->dstate->id, peer->id,
peer->dstate->config.fee_base, peer->dstate->config.fee_base,
peer->dstate->config.fee_per_satoshi, peer->dstate->config.fee_per_satoshi,

152
daemon/routing.c

@ -15,6 +15,15 @@
/* 365.25 * 24 * 60 / 10 */ /* 365.25 * 24 * 60 / 10 */
#define BLOCKS_PER_YEAR 52596 #define BLOCKS_PER_YEAR 52596
struct routing_state *new_routing_state(const tal_t *ctx, struct log *base_log)
{
struct routing_state *rstate = tal(ctx, struct routing_state);
rstate->base_log = base_log;
rstate->nodes = empty_node_map(rstate);
return rstate;
}
static const secp256k1_pubkey *keyof_node(const struct node *n) static const secp256k1_pubkey *keyof_node(const struct node *n)
{ {
return &n->id.pubkey; return &n->id.pubkey;
@ -32,17 +41,17 @@ static bool node_eq(const struct node *n, const secp256k1_pubkey *key)
HTABLE_DEFINE_TYPE(struct node, keyof_node, hash_key, node_eq, node_map); HTABLE_DEFINE_TYPE(struct node, keyof_node, hash_key, node_eq, node_map);
struct node_map *empty_node_map(struct lightningd_state *dstate) struct node_map *empty_node_map(const tal_t *ctx)
{ {
struct node_map *map = tal(dstate, struct node_map); struct node_map *map = tal(ctx, struct node_map);
node_map_init(map); node_map_init(map);
return map; return map;
} }
struct node *get_node(struct lightningd_state *dstate, struct node *get_node(struct routing_state *rstate,
const struct pubkey *id) const struct pubkey *id)
{ {
return node_map_get(dstate->nodes, &id->pubkey); return node_map_get(rstate->nodes, &id->pubkey);
} }
static void destroy_node(struct node *node) static void destroy_node(struct node *node)
@ -54,14 +63,14 @@ static void destroy_node(struct node *node)
tal_free(node->out[0]); tal_free(node->out[0]);
} }
struct node *new_node(struct lightningd_state *dstate, struct node *new_node(struct routing_state *rstate,
const struct pubkey *id) const struct pubkey *id)
{ {
struct node *n; struct node *n;
assert(!get_node(dstate, id)); assert(!get_node(rstate, id));
n = tal(dstate, struct node); n = tal(rstate, struct node);
n->id = *id; n->id = *id;
n->in = tal_arr(n, struct node_connection *, 0); n->in = tal_arr(n, struct node_connection *, 0);
n->out = tal_arr(n, struct node_connection *, 0); n->out = tal_arr(n, struct node_connection *, 0);
@ -69,25 +78,25 @@ struct node *new_node(struct lightningd_state *dstate,
n->alias = NULL; n->alias = NULL;
n->hostname = NULL; n->hostname = NULL;
n->node_announcement = NULL; n->node_announcement = NULL;
node_map_add(dstate->nodes, n); node_map_add(rstate->nodes, n);
tal_add_destructor(n, destroy_node); tal_add_destructor(n, destroy_node);
return n; return n;
} }
struct node *add_node( struct node *add_node(
struct lightningd_state *dstate, struct routing_state *rstate,
const struct pubkey *pk, const struct pubkey *pk,
char *hostname, char *hostname,
int port) int port)
{ {
struct node *n = get_node(dstate, pk); struct node *n = get_node(rstate, pk);
if (!n) { if (!n) {
n = new_node(dstate, pk); n = new_node(rstate, pk);
log_debug_struct(dstate->base_log, "Creating new node %s", log_debug_struct(rstate->base_log, "Creating new node %s",
struct pubkey, pk); struct pubkey, pk);
} else { } else {
log_debug_struct(dstate->base_log, "Update existing node %s", log_debug_struct(rstate->base_log, "Update existing node %s",
struct pubkey, pk); struct pubkey, pk);
} }
n->hostname = tal_steal(n, hostname); n->hostname = tal_steal(n, hostname);
@ -119,14 +128,14 @@ static void destroy_connection(struct node_connection *nc)
fatal("Connection not found in array?!"); fatal("Connection not found in array?!");
} }
struct node_connection * get_connection(struct lightningd_state *dstate, struct node_connection * get_connection(struct routing_state *rstate,
const struct pubkey *from_id, const struct pubkey *from_id,
const struct pubkey *to_id) const struct pubkey *to_id)
{ {
int i, n; int i, n;
struct node *from, *to; struct node *from, *to;
from = get_node(dstate, from_id); from = get_node(rstate, from_id);
to = get_node(dstate, to_id); to = get_node(rstate, to_id);
if (!from || ! to) if (!from || ! to)
return NULL; return NULL;
@ -138,13 +147,13 @@ struct node_connection * get_connection(struct lightningd_state *dstate,
return NULL; return NULL;
} }
struct node_connection *get_connection_by_cid(const struct lightningd_state *dstate, struct node_connection *get_connection_by_cid(const struct routing_state *rstate,
const struct channel_id *chanid, const struct channel_id *chanid,
const u8 direction) const u8 direction)
{ {
struct node *n; struct node *n;
int i, num_conn; int i, num_conn;
struct node_map *nodes = dstate->nodes; struct node_map *nodes = rstate->nodes;
struct node_connection *c; struct node_connection *c;
struct node_map_iter it; struct node_map_iter it;
@ -162,7 +171,7 @@ struct node_connection *get_connection_by_cid(const struct lightningd_state *dst
} }
static struct node_connection * static struct node_connection *
get_or_make_connection(struct lightningd_state *dstate, get_or_make_connection(struct routing_state *rstate,
const struct pubkey *from_id, const struct pubkey *from_id,
const struct pubkey *to_id) const struct pubkey *to_id)
{ {
@ -170,35 +179,35 @@ get_or_make_connection(struct lightningd_state *dstate,
struct node *from, *to; struct node *from, *to;
struct node_connection *nc; struct node_connection *nc;
from = get_node(dstate, from_id); from = get_node(rstate, from_id);
if (!from) if (!from)
from = new_node(dstate, from_id); from = new_node(rstate, from_id);
to = get_node(dstate, to_id); to = get_node(rstate, to_id);
if (!to) if (!to)
to = new_node(dstate, to_id); to = new_node(rstate, to_id);
n = tal_count(to->in); n = tal_count(to->in);
for (i = 0; i < n; i++) { for (i = 0; i < n; i++) {
if (to->in[i]->src == from) { if (to->in[i]->src == from) {
log_debug_struct(dstate->base_log, log_debug_struct(rstate->base_log,
"Updating existing route from %s", "Updating existing route from %s",
struct pubkey, &from->id); struct pubkey, &from->id);
log_add_struct(dstate->base_log, " to %s", log_add_struct(rstate->base_log, " to %s",
struct pubkey, &to->id); struct pubkey, &to->id);
return to->in[i]; return to->in[i];
} }
} }
log_debug_struct(dstate->base_log, "Creating new route from %s", log_debug_struct(rstate->base_log, "Creating new route from %s",
struct pubkey, &from->id); struct pubkey, &from->id);
log_add_struct(dstate->base_log, " to %s", struct pubkey, &to->id); log_add_struct(rstate->base_log, " to %s", struct pubkey, &to->id);
nc = tal(dstate, struct node_connection); nc = tal(rstate, struct node_connection);
nc->src = from; nc->src = from;
nc->dst = to; nc->dst = to;
nc->channel_announcement = NULL; nc->channel_announcement = NULL;
nc->channel_update = NULL; nc->channel_update = NULL;
log_add(dstate->base_log, " = %p (%p->%p)", nc, from, to); log_add(rstate->base_log, " = %p (%p->%p)", nc, from, to);
/* Hook it into in/out arrays. */ /* Hook it into in/out arrays. */
i = tal_count(to->in); i = tal_count(to->in);
@ -212,7 +221,7 @@ get_or_make_connection(struct lightningd_state *dstate,
return nc; return nc;
} }
struct node_connection *half_add_connection(struct lightningd_state *dstate, struct node_connection *half_add_connection(struct routing_state *rstate,
const struct pubkey *from, const struct pubkey *from,
const struct pubkey *to, const struct pubkey *to,
const struct channel_id *chanid, const struct channel_id *chanid,
@ -220,7 +229,7 @@ struct node_connection *half_add_connection(struct lightningd_state *dstate,
) )
{ {
struct node_connection *nc; struct node_connection *nc;
nc = get_or_make_connection(dstate, from, to); nc = get_or_make_connection(rstate, from, to);
memcpy(&nc->channel_id, chanid, sizeof(nc->channel_id)); memcpy(&nc->channel_id, chanid, sizeof(nc->channel_id));
nc->active = false; nc->active = false;
nc->last_timestamp = 0; nc->last_timestamp = 0;
@ -235,13 +244,13 @@ struct node_connection *half_add_connection(struct lightningd_state *dstate,
/* Updates existing route if required. */ /* Updates existing route if required. */
struct node_connection *add_connection(struct lightningd_state *dstate, struct node_connection *add_connection(struct routing_state *rstate,
const struct pubkey *from, const struct pubkey *from,
const struct pubkey *to, const struct pubkey *to,
u32 base_fee, s32 proportional_fee, u32 base_fee, s32 proportional_fee,
u32 delay, u32 min_blocks) u32 delay, u32 min_blocks)
{ {
struct node_connection *c = get_or_make_connection(dstate, from, to); struct node_connection *c = get_or_make_connection(rstate, from, to);
c->base_fee = base_fee; c->base_fee = base_fee;
c->proportional_fee = proportional_fee; c->proportional_fee = proportional_fee;
c->delay = delay; c->delay = delay;
@ -253,20 +262,20 @@ struct node_connection *add_connection(struct lightningd_state *dstate,
return c; return c;
} }
void remove_connection(struct lightningd_state *dstate, void remove_connection(struct routing_state *rstate,
const struct pubkey *src, const struct pubkey *dst) const struct pubkey *src, const struct pubkey *dst)
{ {
struct node *from, *to; struct node *from, *to;
size_t i, num_edges; size_t i, num_edges;
log_debug_struct(dstate->base_log, "Removing route from %s", log_debug_struct(rstate->base_log, "Removing route from %s",
struct pubkey, src); struct pubkey, src);
log_add_struct(dstate->base_log, " to %s", struct pubkey, dst); log_add_struct(rstate->base_log, " to %s", struct pubkey, dst);
from = get_node(dstate, src); from = get_node(rstate, src);
to = get_node(dstate, dst); to = get_node(rstate, dst);
if (!from || !to) { if (!from || !to) {
log_debug(dstate->base_log, "Not found: src=%p dst=%p", log_debug(rstate->base_log, "Not found: src=%p dst=%p",
from, to); from, to);
return; return;
} }
@ -277,13 +286,13 @@ void remove_connection(struct lightningd_state *dstate,
if (from->out[i]->dst != to) if (from->out[i]->dst != to)
continue; continue;
log_add(dstate->base_log, " Matched route %zu of %zu", log_add(rstate->base_log, " Matched route %zu of %zu",
i, num_edges); i, num_edges);
/* Destructor makes it delete itself */ /* Destructor makes it delete itself */
tal_free(from->out[i]); tal_free(from->out[i]);
return; return;
} }
log_add(dstate->base_log, " None of %zu routes matched", num_edges); log_add(rstate->base_log, " None of %zu routes matched", num_edges);
} }
/* Too big to reach, but don't overflow if added. */ /* Too big to reach, but don't overflow if added. */
@ -347,8 +356,9 @@ static void bfg_one_edge(struct node *node, size_t edgenum, double riskfactor)
} }
} }
struct peer *find_route(const tal_t *ctx, struct pubkey *find_route(const tal_t *ctx,
struct lightningd_state *dstate, struct routing_state *rstate,
const struct pubkey *from,
const struct pubkey *to, const struct pubkey *to,
u64 msatoshi, u64 msatoshi,
double riskfactor, double riskfactor,
@ -357,26 +367,26 @@ struct peer *find_route(const tal_t *ctx,
{ {
struct node *n, *src, *dst; struct node *n, *src, *dst;
struct node_map_iter it; struct node_map_iter it;
struct peer *first; struct pubkey *first;
int runs, i, best; int runs, i, best;
/* Note: we map backwards, since we know the amount of satoshi we want /* Note: we map backwards, since we know the amount of satoshi we want
* at the end, and need to derive how much we need to send. */ * at the end, and need to derive how much we need to send. */
dst = get_node(dstate, &dstate->id); dst = get_node(rstate, from);
src = get_node(dstate, to); src = get_node(rstate, to);
if (!src) { if (!src) {
log_info_struct(dstate->base_log, "find_route: cannot find %s", log_info_struct(rstate->base_log, "find_route: cannot find %s",
struct pubkey, to); struct pubkey, to);
return NULL; return NULL;
} else if (dst == src) { } else if (dst == src) {
log_info_struct(dstate->base_log, "find_route: this is %s, refusing to create empty route", log_info_struct(rstate->base_log, "find_route: this is %s, refusing to create empty route",
struct pubkey, to); struct pubkey, to);
return NULL; return NULL;
} }
/* Reset all the information. */ /* Reset all the information. */
clear_bfg(dstate->nodes); clear_bfg(rstate->nodes);
/* Bellman-Ford-Gibson: like Bellman-Ford, but keep values for /* Bellman-Ford-Gibson: like Bellman-Ford, but keep values for
* every path length. */ * every path length. */
@ -384,23 +394,23 @@ struct peer *find_route(const tal_t *ctx,
src->bfg[0].risk = 0; src->bfg[0].risk = 0;
for (runs = 0; runs < ROUTING_MAX_HOPS; runs++) { for (runs = 0; runs < ROUTING_MAX_HOPS; runs++) {
log_debug(dstate->base_log, "Run %i", runs); log_debug(rstate->base_log, "Run %i", runs);
/* Run through every edge. */ /* Run through every edge. */
for (n = node_map_first(dstate->nodes, &it); for (n = node_map_first(rstate->nodes, &it);
n; n;
n = node_map_next(dstate->nodes, &it)) { n = node_map_next(rstate->nodes, &it)) {
size_t num_edges = tal_count(n->in); size_t num_edges = tal_count(n->in);
for (i = 0; i < num_edges; i++) { for (i = 0; i < num_edges; i++) {
if (!n->in[i]->active) if (!n->in[i]->active)
continue; continue;
bfg_one_edge(n, i, riskfactor); bfg_one_edge(n, i, riskfactor);
log_debug(dstate->base_log, "We seek %p->%p, this is %p -> %p", log_debug(rstate->base_log, "We seek %p->%p, this is %p -> %p",
dst, src, n->in[i]->src, n->in[i]->dst); dst, src, n->in[i]->src, n->in[i]->dst);
log_debug_struct(dstate->base_log, log_debug_struct(rstate->base_log,
"Checking from %s", "Checking from %s",
struct pubkey, struct pubkey,
&n->in[i]->src->id); &n->in[i]->src->id);
log_add_struct(dstate->base_log, log_add_struct(rstate->base_log,
" to %s", " to %s",
struct pubkey, struct pubkey,
&n->in[i]->dst->id); &n->in[i]->dst->id);
@ -416,7 +426,7 @@ struct peer *find_route(const tal_t *ctx,
/* No route? */ /* No route? */
if (dst->bfg[best].total >= INFINITE) { if (dst->bfg[best].total >= INFINITE) {
log_info_struct(dstate->base_log, "find_route: No route to %s", log_info_struct(rstate->base_log, "find_route: No route to %s",
struct pubkey, to); struct pubkey, to);
return NULL; return NULL;
} }
@ -427,13 +437,7 @@ struct peer *find_route(const tal_t *ctx,
dst = dst->bfg[best].prev->dst; dst = dst->bfg[best].prev->dst;
best--; best--;
/* We should only add routes if we have a peer. */ first = &dst->id;
first = find_peer(dstate, &dst->id);
if (!first) {
log_broken_struct(dstate->base_log, "No peer %s?",
struct pubkey, &(*route)[0]->src->id);
return NULL;
}
*fee = dst->bfg[best].total - msatoshi; *fee = dst->bfg[best].total - msatoshi;
*route = tal_arr(ctx, struct node_connection *, best); *route = tal_arr(ctx, struct node_connection *, best);
@ -445,20 +449,20 @@ struct peer *find_route(const tal_t *ctx,
assert(n == src); assert(n == src);
msatoshi += *fee; msatoshi += *fee;
log_info(dstate->base_log, "find_route:"); log_info(rstate->base_log, "find_route:");
log_add_struct(dstate->base_log, "via %s", struct pubkey, first->id); log_add_struct(rstate->base_log, "via %s", struct pubkey, first);
/* If there are intermidiaries, dump them, and total fees. */ /* If there are intermidiaries, dump them, and total fees. */
if (best != 0) { if (best != 0) {
for (i = 0; i < best; i++) { for (i = 0; i < best; i++) {
log_add_struct(dstate->base_log, " %s", log_add_struct(rstate->base_log, " %s",
struct pubkey, &(*route)[i]->dst->id); struct pubkey, &(*route)[i]->dst->id);
log_add(dstate->base_log, "(%i+%i=%"PRIu64")", log_add(rstate->base_log, "(%i+%i=%"PRIu64")",
(*route)[i]->base_fee, (*route)[i]->base_fee,
(*route)[i]->proportional_fee, (*route)[i]->proportional_fee,
connection_fee((*route)[i], msatoshi)); connection_fee((*route)[i], msatoshi));
msatoshi -= connection_fee((*route)[i], msatoshi); msatoshi -= connection_fee((*route)[i], msatoshi);
} }
log_add(dstate->base_log, "=%"PRIi64"(%+"PRIi64")", log_add(rstate->base_log, "=%"PRIi64"(%+"PRIi64")",
(*route)[best-1]->dst->bfg[best-1].total, *fee); (*route)[best-1]->dst->bfg[best-1].total, *fee);
} }
return first; return first;
@ -502,7 +506,7 @@ char *opt_add_route(const char *arg, struct lightningd_state *dstate)
if (*arg) if (*arg)
return "Data after minblocks"; return "Data after minblocks";
add_connection(dstate, &src, &dst, base, var, delay, minblocks); add_connection(dstate->rstate, &src, &dst, base, var, delay, minblocks);
return NULL; return NULL;
} }
@ -550,7 +554,7 @@ static void json_add_route(struct command *cmd,
return; return;
} }
add_connection(cmd->dstate, &src, &dst, base, var, delay, minblocks); add_connection(cmd->dstate->rstate, &src, &dst, base, var, delay, minblocks);
command_success(cmd, null_response(cmd)); command_success(cmd, null_response(cmd));
} }
@ -560,7 +564,7 @@ void sync_routing_table(struct lightningd_state *dstate, struct peer *peer)
struct node_map_iter it; struct node_map_iter it;
int i; int i;
struct node_connection *nc; struct node_connection *nc;
for (n = node_map_first(dstate->nodes, &it); n; n = node_map_next(dstate->nodes, &it)) { for (n = node_map_first(dstate->rstate->nodes, &it); n; n = node_map_next(dstate->rstate->nodes, &it)) {
size_t num_edges = tal_count(n->out); size_t num_edges = tal_count(n->out);
for (i = 0; i < num_edges; i++) { for (i = 0; i < num_edges; i++) {
nc = n->out[i]; nc = n->out[i];
@ -588,7 +592,7 @@ static void json_getchannels(struct command *cmd,
struct json_result *response = new_json_result(cmd); struct json_result *response = new_json_result(cmd);
struct node_map_iter it; struct node_map_iter it;
struct node *n; struct node *n;
struct node_map *nodes = cmd->dstate->nodes; struct node_map *nodes = cmd->dstate->rstate->nodes;
struct node_connection *c; struct node_connection *c;
int num_conn, i; int num_conn, i;
@ -660,7 +664,7 @@ static void json_getnodes(struct command *cmd,
struct node *n; struct node *n;
struct node_map_iter i; struct node_map_iter i;
n = node_map_first(cmd->dstate->nodes, &i); n = node_map_first(cmd->dstate->rstate->nodes, &i);
json_object_start(response, NULL); json_object_start(response, NULL);
json_array_start(response, "nodes"); json_array_start(response, "nodes");
@ -675,7 +679,7 @@ static void json_getnodes(struct command *cmd,
json_add_string(response, "hostname", n->hostname); json_add_string(response, "hostname", n->hostname);
json_object_end(response); json_object_end(response);
n = node_map_next(cmd->dstate->nodes, &i); n = node_map_next(cmd->dstate->rstate->nodes, &i);
} }
json_array_end(response); json_array_end(response);

34
daemon/routing.h

@ -72,10 +72,20 @@ struct node {
struct lightningd_state; struct lightningd_state;
struct node *new_node(struct lightningd_state *dstate, struct routing_state {
/* All known nodes. */
struct node_map *nodes;
struct log *base_log;
};
//FIXME(cdecker) The log will have to be replaced for the new subdaemon, keeping for now to keep changes small.
struct routing_state *new_routing_state(const tal_t *ctx, struct log *base_log);
struct node *new_node(struct routing_state *rstate,
const struct pubkey *id); const struct pubkey *id);
struct node *get_node(struct lightningd_state *dstate, struct node *get_node(struct routing_state *rstate,
const struct pubkey *id); const struct pubkey *id);
/* msatoshi must be possible (< 21 million BTC), ie < 2^60. /* msatoshi must be possible (< 21 million BTC), ie < 2^60.
@ -83,13 +93,13 @@ struct node *get_node(struct lightningd_state *dstate,
s64 connection_fee(const struct node_connection *c, u64 msatoshi); s64 connection_fee(const struct node_connection *c, u64 msatoshi);
/* Updates existing node, or creates a new one as required. */ /* Updates existing node, or creates a new one as required. */
struct node *add_node(struct lightningd_state *dstate, struct node *add_node(struct routing_state *rstate,
const struct pubkey *pk, const struct pubkey *pk,
char *hostname, char *hostname,
int port); int port);
/* Updates existing connection, or creates new one as required. */ /* Updates existing connection, or creates new one as required. */
struct node_connection *add_connection(struct lightningd_state *dstate, struct node_connection *add_connection(struct routing_state *rstate,
const struct pubkey *from, const struct pubkey *from,
const struct pubkey *to, const struct pubkey *to,
u32 base_fee, s32 proportional_fee, u32 base_fee, s32 proportional_fee,
@ -99,7 +109,7 @@ struct node_connection *add_connection(struct lightningd_state *dstate,
* yet. Used by channel_announcements before the channel_update comes * yet. Used by channel_announcements before the channel_update comes
* in. */ * in. */
struct node_connection *half_add_connection(struct lightningd_state *dstate, struct node_connection *half_add_connection(struct routing_state *rstate,
const struct pubkey *from, const struct pubkey *from,
const struct pubkey *to, const struct pubkey *to,
const struct channel_id *chanid, const struct channel_id *chanid,
@ -107,32 +117,34 @@ struct node_connection *half_add_connection(struct lightningd_state *dstate,
/* Get an existing connection between `from` and `to`, NULL if no such /* Get an existing connection between `from` and `to`, NULL if no such
* connection exists. */ * connection exists. */
struct node_connection *get_connection(struct lightningd_state *dstate, struct node_connection *get_connection(struct routing_state *rstate,
const struct pubkey *from, const struct pubkey *from,
const struct pubkey *to); const struct pubkey *to);
/* Given a channel_id, retrieve the matching connection, or NULL if it is /* Given a channel_id, retrieve the matching connection, or NULL if it is
* unknown. */ * unknown. */
struct node_connection *get_connection_by_cid(const struct lightningd_state *dstate, struct node_connection *get_connection_by_cid(const struct routing_state *rstate,
const struct channel_id *chanid, const struct channel_id *chanid,
const u8 direction); const u8 direction);
void remove_connection(struct lightningd_state *dstate, void remove_connection(struct routing_state *rstate,
const struct pubkey *src, const struct pubkey *dst); const struct pubkey *src, const struct pubkey *dst);
struct peer *find_route(const tal_t *ctx, struct pubkey *find_route(const tal_t *ctx,
struct lightningd_state *dstate, struct routing_state *rstate,
const struct pubkey *from,
const struct pubkey *to, const struct pubkey *to,
u64 msatoshi, u64 msatoshi,
double riskfactor, double riskfactor,
s64 *fee, s64 *fee,
struct node_connection ***route); struct node_connection ***route);
struct node_map *empty_node_map(struct lightningd_state *dstate); struct node_map *empty_node_map(const tal_t *ctx);
char *opt_add_route(const char *arg, struct lightningd_state *dstate); char *opt_add_route(const char *arg, struct lightningd_state *dstate);
/* Dump all known channels and nodes to the peer. Used when a new connection was established. */ /* Dump all known channels and nodes to the peer. Used when a new connection was established. */
//FIXME(cdecker) Not used in the gossip subdaemon, remove once old daemon is retired
void sync_routing_table(struct lightningd_state *dstate, struct peer *peer); void sync_routing_table(struct lightningd_state *dstate, struct peer *peer);
#endif /* LIGHTNING_DAEMON_ROUTING_H */ #endif /* LIGHTNING_DAEMON_ROUTING_H */

1
lightningd/lightningd.c

@ -90,7 +90,6 @@ static struct lightningd *new_lightningd(const tal_t *ctx)
ld->dstate.dev_never_routefail = false; ld->dstate.dev_never_routefail = false;
ld->dstate.dev_no_broadcast = false; ld->dstate.dev_no_broadcast = false;
ld->dstate.bitcoin_req_running = false; ld->dstate.bitcoin_req_running = false;
ld->dstate.nodes = empty_node_map(&ld->dstate);
ld->dstate.reexec = NULL; ld->dstate.reexec = NULL;
ld->dstate.external_ip = NULL; ld->dstate.external_ip = NULL;
ld->dstate.announce = NULL; ld->dstate.announce = NULL;

Loading…
Cancel
Save