From 26b5588656a3f19dcc159417037a11a245bac64c Mon Sep 17 00:00:00 2001 From: Christian Decker Date: Wed, 31 Jan 2018 17:53:50 +0100 Subject: [PATCH] gossip: Use replace_broadcast when enqueuing a channel_announcement Signed-off-by: Christian Decker --- gossipd/routing.c | 23 ++++++++++++++++++++--- gossipd/routing.h | 2 ++ gossipd/test/run-bench-find_route.c | 7 +++++++ gossipd/test/run-find_route-specific.c | 7 +++++++ gossipd/test/run-find_route.c | 7 +++++++ 5 files changed, 43 insertions(+), 3 deletions(-) diff --git a/gossipd/routing.c b/gossipd/routing.c index f6269a7f8..25a18124b 100644 --- a/gossipd/routing.c +++ b/gossipd/routing.c @@ -50,6 +50,16 @@ struct pending_cannouncement { const u8 *updates[2]; }; +/** + * routing_channel keeps track of the indices in the broadcast queue + * for the corresponding messages. This way we always know exactly + * which broadcast to replace, and don't have to search for it */ +enum gossip_msg_indexes { + MSG_INDEX_CUPDATE_0, + MSG_INDEX_CUPDATE_1, + MSG_INDEX_CANNOUNCE +}; + static struct node_map *empty_node_map(const tal_t *ctx) { struct node_map *map = tal(ctx, struct node_map); @@ -564,6 +574,7 @@ routing_channel_new(const tal_t *ctx, struct short_channel_id *scid) chan->nodes[0] = chan->nodes[1] = NULL; chan->txout_script = NULL; chan->state = TXOUT_FETCHING; + memset(&chan->msg_indexes, 0, sizeof(chan->msg_indexes)); return chan; } @@ -677,6 +688,11 @@ bool handle_pending_cannouncement(struct routing_state *rstate, u8 *tag; const u8 *s; struct pending_cannouncement *pending; + struct routing_channel *chan; + u64 uscid = short_channel_id_to_uint(scid); + + chan = uintmap_get(&rstate->channels, uscid); + assert(chan); pending = find_pending_cannouncement(rstate, scid); assert(pending); @@ -732,9 +748,10 @@ bool handle_pending_cannouncement(struct routing_state *rstate, &pending->short_channel_id, pending->announce); if (forward) { - if (queue_broadcast(rstate->broadcasts, - WIRE_CHANNEL_ANNOUNCEMENT, - tag, pending->announce)) + if (replace_broadcast(rstate->broadcasts, + &chan->msg_indexes[MSG_INDEX_CANNOUNCE], + WIRE_CHANNEL_ANNOUNCEMENT, + (u8*)tag, pending->announce)) status_failed(STATUS_FAIL_INTERNAL_ERROR, "Announcement %s was replaced?", tal_hex(trc, pending->announce)); diff --git a/gossipd/routing.h b/gossipd/routing.h index bb2bf6320..10f2322c6 100644 --- a/gossipd/routing.h +++ b/gossipd/routing.h @@ -94,6 +94,8 @@ struct routing_channel { struct node_connection *connections[2]; struct node *nodes[2]; + + u64 msg_indexes[3]; }; struct routing_state { diff --git a/gossipd/test/run-bench-find_route.c b/gossipd/test/run-bench-find_route.c index 9be41701e..988f10ff4 100644 --- a/gossipd/test/run-bench-find_route.c +++ b/gossipd/test/run-bench-find_route.c @@ -75,6 +75,13 @@ bool queue_broadcast(struct broadcast_state *bstate UNNEEDED, const u8 *tag UNNEEDED, const u8 *payload UNNEEDED) { fprintf(stderr, "queue_broadcast called!\n"); abort(); } +/* Generated stub for replace_broadcast */ +bool replace_broadcast(struct broadcast_state *bstate UNNEEDED, + u64 *index UNNEEDED, + const int type UNNEEDED, + const u8 *tag UNNEEDED, + const u8 *payload UNNEEDED) +{ fprintf(stderr, "replace_broadcast called!\n"); abort(); } /* Generated stub for status_failed */ void status_failed(enum status_fail code UNNEEDED, const char *fmt UNNEEDED, ...) { fprintf(stderr, "status_failed called!\n"); abort(); } diff --git a/gossipd/test/run-find_route-specific.c b/gossipd/test/run-find_route-specific.c index 29c89485f..092c8a6b2 100644 --- a/gossipd/test/run-find_route-specific.c +++ b/gossipd/test/run-find_route-specific.c @@ -46,6 +46,13 @@ bool queue_broadcast(struct broadcast_state *bstate UNNEEDED, const u8 *tag UNNEEDED, const u8 *payload UNNEEDED) { fprintf(stderr, "queue_broadcast called!\n"); abort(); } +/* Generated stub for replace_broadcast */ +bool replace_broadcast(struct broadcast_state *bstate UNNEEDED, + u64 *index UNNEEDED, + const int type UNNEEDED, + const u8 *tag UNNEEDED, + const u8 *payload UNNEEDED) +{ fprintf(stderr, "replace_broadcast called!\n"); abort(); } /* Generated stub for status_failed */ void status_failed(enum status_fail code UNNEEDED, const char *fmt UNNEEDED, ...) { fprintf(stderr, "status_failed called!\n"); abort(); } diff --git a/gossipd/test/run-find_route.c b/gossipd/test/run-find_route.c index 0883e4806..96d07fcfa 100644 --- a/gossipd/test/run-find_route.c +++ b/gossipd/test/run-find_route.c @@ -39,6 +39,13 @@ bool queue_broadcast(struct broadcast_state *bstate UNNEEDED, const u8 *tag UNNEEDED, const u8 *payload UNNEEDED) { fprintf(stderr, "queue_broadcast called!\n"); abort(); } +/* Generated stub for replace_broadcast */ +bool replace_broadcast(struct broadcast_state *bstate UNNEEDED, + u64 *index UNNEEDED, + const int type UNNEEDED, + const u8 *tag UNNEEDED, + const u8 *payload UNNEEDED) +{ fprintf(stderr, "replace_broadcast called!\n"); abort(); } /* Generated stub for status_failed */ void status_failed(enum status_fail code UNNEEDED, const char *fmt UNNEEDED, ...) { fprintf(stderr, "status_failed called!\n"); abort(); }