From 2a7e757053c872890856c61bb154cfb8d5bd107d Mon Sep 17 00:00:00 2001 From: Christian Decker Date: Sun, 22 Jan 2017 16:16:51 +0100 Subject: [PATCH] refactor: Moving functionality out of p2p_announce Further decoupling the old daemons from the new daemons. --- daemon/p2p_announce.c | 31 ++----------------------------- daemon/p2p_announce.h | 2 ++ daemon/routing.c | 26 ++++++++++++++++++++++++++ daemon/routing.h | 7 +++++++ 4 files changed, 37 insertions(+), 29 deletions(-) diff --git a/daemon/p2p_announce.c b/daemon/p2p_announce.c index f51ea41f5..346838cf1 100644 --- a/daemon/p2p_announce.c +++ b/daemon/p2p_announce.c @@ -171,33 +171,6 @@ static void queue_broadcast(struct lightningd_state *dstate, list_add_tail(&dstate->broadcast_queue, &msg->list); } -static bool add_channel_direction(struct lightningd_state *dstate, - const struct pubkey *from, - const struct pubkey *to, - const int direction, - const struct channel_id *channel_id, - const u8 *announcement) -{ - struct node_connection *c = get_connection(dstate->rstate, from, to); - if (c){ - /* Do not clobber connections added otherwise */ - memcpy(&c->channel_id, channel_id, sizeof(c->channel_id)); - c->flags = direction; - printf("Found node_connection via get_connection"); - return false; - }else if(get_connection_by_cid(dstate->rstate, channel_id, direction)) { - return false; - } - - c = half_add_connection(dstate->rstate, from, to, channel_id, direction); - - /* Remember the announcement so we can forward it to new peers */ - tal_free(c->channel_announcement); - c->channel_announcement = tal_dup_arr(c, u8, announcement, - tal_count(announcement), 0); - return true; -} - void handle_channel_announcement( struct peer *peer, const u8 *announce, size_t len) @@ -240,10 +213,10 @@ void handle_channel_announcement( channel_id.outnum ); - forward |= add_channel_direction(peer->dstate, &node_id_1, + forward |= add_channel_direction(peer->dstate->rstate, &node_id_1, &node_id_2, 0, &channel_id, serialized); - forward |= add_channel_direction(peer->dstate, &node_id_2, + forward |= add_channel_direction(peer->dstate->rstate, &node_id_2, &node_id_1, 1, &channel_id, serialized); if (!forward){ diff --git a/daemon/p2p_announce.h b/daemon/p2p_announce.h index 3d51717fb..f0ab557bc 100644 --- a/daemon/p2p_announce.h +++ b/daemon/p2p_announce.h @@ -1,6 +1,8 @@ #ifndef LIGHTNING_DAEMON_P2P_ANNOUNCE_H #define LIGHTNING_DAEMON_P2P_ANNOUNCE_H #include "config.h" +#include "daemon/lightningd.h" +#include "daemon/routing.h" #include "lightningd.h" #include "wire/gen_peer_wire.h" diff --git a/daemon/routing.c b/daemon/routing.c index 38f991b9c..6dfb1881f 100644 --- a/daemon/routing.c +++ b/daemon/routing.c @@ -670,3 +670,29 @@ static const struct json_command getnodes_command = { "Returns a 'nodes' array" }; AUTODATA(json_command, &getnodes_command); + +bool add_channel_direction(struct routing_state *rstate, + const struct pubkey *from, + const struct pubkey *to, + const int direction, + const struct channel_id *channel_id, + const u8 *announcement) +{ + struct node_connection *c = get_connection(rstate, from, to); + if (c){ + /* Do not clobber connections added otherwise */ + memcpy(&c->channel_id, channel_id, sizeof(c->channel_id)); + c->flags = direction; + return false; + }else if(get_connection_by_cid(rstate, channel_id, direction)) { + return false; + } + + c = half_add_connection(rstate, from, to, channel_id, direction); + + /* Remember the announcement so we can forward it to new peers */ + tal_free(c->channel_announcement); + c->channel_announcement = tal_dup_arr(c, u8, announcement, + tal_count(announcement), 0); + return true; +} diff --git a/daemon/routing.h b/daemon/routing.h index 74edd155e..bd1461b28 100644 --- a/daemon/routing.h +++ b/daemon/routing.h @@ -149,4 +149,11 @@ struct node_map *empty_node_map(const tal_t *ctx); char *opt_add_route(const char *arg, struct lightningd_state *dstate); +bool add_channel_direction(struct routing_state *rstate, + const struct pubkey *from, + const struct pubkey *to, + const int direction, + const struct channel_id *channel_id, + const u8 *announcement); + #endif /* LIGHTNING_DAEMON_ROUTING_H */