From 1290f305a2a79d93c191fe11f4fb7909e1dbe764 Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Wed, 14 Mar 2018 02:08:55 +1030 Subject: [PATCH] gossipd: remove tag. We only access via index. We do, however, want to clean up when we delete nodes and channels, so we tie lifetimes to that. This leads us to put the index into 'struct queued_message'. Signed-off-by: Rusty Russell --- gossipd/broadcast.c | 31 ++++++++++++++++---------- gossipd/broadcast.h | 8 +++---- gossipd/routing.c | 19 ++++------------ gossipd/test/run-bench-find_route.c | 14 ++---------- gossipd/test/run-find_route-specific.c | 14 ++---------- gossipd/test/run-find_route.c | 14 ++---------- 6 files changed, 33 insertions(+), 67 deletions(-) diff --git a/gossipd/broadcast.c b/gossipd/broadcast.c index ae981c059..d720c0cdf 100644 --- a/gossipd/broadcast.c +++ b/gossipd/broadcast.c @@ -10,35 +10,42 @@ struct broadcast_state *new_broadcast_state(tal_t *ctx) return bstate; } -static struct queued_message *new_queued_message(tal_t *ctx, - const u8 *tag, - const u8 *payload) +static void destroy_queued_message(struct queued_message *msg, + struct broadcast_state *bstate) +{ + uintmap_del(&bstate->broadcasts, msg->index); +} + +static struct queued_message *new_queued_message(const tal_t *ctx, + struct broadcast_state *bstate, + const u8 *payload, + u64 index) { struct queued_message *msg = tal(ctx, struct queued_message); - msg->tag = tal_dup_arr(msg, u8, tag, tal_len(tag), 0); msg->payload = tal_dup_arr(msg, u8, payload, tal_len(payload), 0); + msg->index = index; + uintmap_add(&bstate->broadcasts, index, msg); + tal_add_destructor2(msg, destroy_queued_message, bstate); return msg; } -bool replace_broadcast(struct broadcast_state *bstate, u64 *index, - const u8 *tag, const u8 *payload) +bool replace_broadcast(const tal_t *ctx, + struct broadcast_state *bstate, + u64 *index, + const u8 *payload) { struct queued_message *msg; bool evicted = false; msg = uintmap_get(&bstate->broadcasts, *index); if (msg) { - assert(memeq(msg->tag, tal_len(msg->tag), tag, tal_len(tag))); - uintmap_del(&bstate->broadcasts, *index); tal_free(msg); evicted = true; } - *index = bstate->next_index; /* Now add the message to the queue */ - msg = new_queued_message(bstate, tag, payload); - uintmap_add(&bstate->broadcasts, *index, msg); - bstate->next_index++; + msg = new_queued_message(ctx, bstate, payload, bstate->next_index++); + *index = msg->index; return evicted; } diff --git a/gossipd/broadcast.h b/gossipd/broadcast.h index 9da74d36f..5ba87097b 100644 --- a/gossipd/broadcast.h +++ b/gossipd/broadcast.h @@ -10,8 +10,8 @@ /* Common functionality to implement staggered broadcasts with replacement. */ struct queued_message { - /* Unique tag specifying the msg origin */ - void *tag; + /* Broadcast index. */ + u64 index; /* Serialized payload */ u8 *payload; @@ -28,9 +28,9 @@ struct broadcast_state *new_broadcast_state(tal_t *ctx); * tag for the new message. The new message will be queued with the * next highest index. @index is updated to hold the index of the * newly queued message*/ -bool replace_broadcast(struct broadcast_state *bstate, +bool replace_broadcast(const tal_t *ctx, + struct broadcast_state *bstate, u64 *index, - const u8 *tag, const u8 *payload); diff --git a/gossipd/routing.c b/gossipd/routing.c index badad2f83..7e474b9e6 100644 --- a/gossipd/routing.c +++ b/gossipd/routing.c @@ -753,7 +753,6 @@ bool handle_pending_cannouncement(struct routing_state *rstate, const u8 *outscript) { bool local; - u8 *tag; const u8 *s; struct pending_cannouncement *pending; struct chan *chan; @@ -762,9 +761,6 @@ bool handle_pending_cannouncement(struct routing_state *rstate, if (!pending) return false; - tag = tal_arr(pending, u8, 0); - towire_short_channel_id(&tag, scid); - /* BOLT #7: * * The receiving node MUST ignore the message if this output is spent. @@ -816,9 +812,9 @@ bool handle_pending_cannouncement(struct routing_state *rstate, tal_free(chan->channel_announcement); chan->channel_announcement = tal_steal(chan, pending->announce); - if (replace_broadcast(rstate->broadcasts, + if (replace_broadcast(chan, rstate->broadcasts, &chan->channel_announce_msgidx, - tag, pending->announce)) + pending->announce)) status_failed(STATUS_FAIL_INTERNAL_ERROR, "Announcement %s was replaced?", tal_hex(trc, pending->announce)); @@ -999,12 +995,8 @@ u8 *handle_channel_update(struct routing_state *rstate, const u8 *update) timestamp, htlc_minimum_msat); - u8 *tag = tal_arr(tmpctx, u8, 0); - towire_short_channel_id(&tag, &short_channel_id); - towire_u16(&tag, direction); - replace_broadcast(rstate->broadcasts, + replace_broadcast(chan, rstate->broadcasts, &chan->half[direction].channel_update_msgidx, - tag, serialized); tal_free(c->channel_update); @@ -1188,11 +1180,8 @@ u8 *handle_node_announcement(struct routing_state *rstate, const u8 *node_ann) tal_free(node->alias); node->alias = tal_dup_arr(node, u8, alias, 32, 0); - u8 *tag = tal_arr(tmpctx, u8, 0); - towire_pubkey(&tag, &node_id); - replace_broadcast(rstate->broadcasts, + replace_broadcast(node, rstate->broadcasts, &node->announcement_idx, - tag, serialized); tal_free(node->node_announcement); node->node_announcement = tal_steal(node, serialized); diff --git a/gossipd/test/run-bench-find_route.c b/gossipd/test/run-bench-find_route.c index 6820f6078..fa0deaaf6 100644 --- a/gossipd/test/run-bench-find_route.c +++ b/gossipd/test/run-bench-find_route.c @@ -74,9 +74,9 @@ bool fromwire_wireaddr(const u8 **cursor UNNEEDED, size_t *max UNNEEDED, struct const char *onion_type_name(int e UNNEEDED) { fprintf(stderr, "onion_type_name called!\n"); abort(); } /* Generated stub for replace_broadcast */ -bool replace_broadcast(struct broadcast_state *bstate UNNEEDED, +bool replace_broadcast(const tal_t *ctx UNNEEDED, + struct broadcast_state *bstate UNNEEDED, u64 *index UNNEEDED, - const u8 *tag UNNEEDED, const u8 *payload UNNEEDED) { fprintf(stderr, "replace_broadcast called!\n"); abort(); } /* Generated stub for sanitize_error */ @@ -92,16 +92,6 @@ u8 *towire_errorfmt(const tal_t *ctx UNNEEDED, const struct channel_id *channel UNNEEDED, const char *fmt UNNEEDED, ...) { fprintf(stderr, "towire_errorfmt called!\n"); abort(); } -/* Generated stub for towire_pubkey */ -void towire_pubkey(u8 **pptr UNNEEDED, const struct pubkey *pubkey UNNEEDED) -{ fprintf(stderr, "towire_pubkey called!\n"); abort(); } -/* Generated stub for towire_short_channel_id */ -void towire_short_channel_id(u8 **pptr UNNEEDED, - const struct short_channel_id *short_channel_id UNNEEDED) -{ fprintf(stderr, "towire_short_channel_id called!\n"); abort(); } -/* Generated stub for towire_u16 */ -void towire_u16(u8 **pptr UNNEEDED, u16 v UNNEEDED) -{ fprintf(stderr, "towire_u16 called!\n"); abort(); } /* AUTOGENERATED MOCKS END */ const void *trc; diff --git a/gossipd/test/run-find_route-specific.c b/gossipd/test/run-find_route-specific.c index cb18ff315..8cf85acd0 100644 --- a/gossipd/test/run-find_route-specific.c +++ b/gossipd/test/run-find_route-specific.c @@ -38,9 +38,9 @@ bool fromwire_wireaddr(const u8 **cursor UNNEEDED, size_t *max UNNEEDED, struct const char *onion_type_name(int e UNNEEDED) { fprintf(stderr, "onion_type_name called!\n"); abort(); } /* Generated stub for replace_broadcast */ -bool replace_broadcast(struct broadcast_state *bstate UNNEEDED, +bool replace_broadcast(const tal_t *ctx UNNEEDED, + struct broadcast_state *bstate UNNEEDED, u64 *index UNNEEDED, - const u8 *tag UNNEEDED, const u8 *payload UNNEEDED) { fprintf(stderr, "replace_broadcast called!\n"); abort(); } /* Generated stub for sanitize_error */ @@ -56,16 +56,6 @@ u8 *towire_errorfmt(const tal_t *ctx UNNEEDED, const struct channel_id *channel UNNEEDED, const char *fmt UNNEEDED, ...) { fprintf(stderr, "towire_errorfmt called!\n"); abort(); } -/* Generated stub for towire_pubkey */ -void towire_pubkey(u8 **pptr UNNEEDED, const struct pubkey *pubkey UNNEEDED) -{ fprintf(stderr, "towire_pubkey called!\n"); abort(); } -/* Generated stub for towire_short_channel_id */ -void towire_short_channel_id(u8 **pptr UNNEEDED, - const struct short_channel_id *short_channel_id UNNEEDED) -{ fprintf(stderr, "towire_short_channel_id called!\n"); abort(); } -/* Generated stub for towire_u16 */ -void towire_u16(u8 **pptr UNNEEDED, u16 v UNNEEDED) -{ fprintf(stderr, "towire_u16 called!\n"); abort(); } /* AUTOGENERATED MOCKS END */ const void *trc; diff --git a/gossipd/test/run-find_route.c b/gossipd/test/run-find_route.c index 381272cf8..522c4eeca 100644 --- a/gossipd/test/run-find_route.c +++ b/gossipd/test/run-find_route.c @@ -36,9 +36,9 @@ bool fromwire_wireaddr(const u8 **cursor UNNEEDED, size_t *max UNNEEDED, struct const char *onion_type_name(int e UNNEEDED) { fprintf(stderr, "onion_type_name called!\n"); abort(); } /* Generated stub for replace_broadcast */ -bool replace_broadcast(struct broadcast_state *bstate UNNEEDED, +bool replace_broadcast(const tal_t *ctx UNNEEDED, + struct broadcast_state *bstate UNNEEDED, u64 *index UNNEEDED, - const u8 *tag UNNEEDED, const u8 *payload UNNEEDED) { fprintf(stderr, "replace_broadcast called!\n"); abort(); } /* Generated stub for sanitize_error */ @@ -54,16 +54,6 @@ u8 *towire_errorfmt(const tal_t *ctx UNNEEDED, const struct channel_id *channel UNNEEDED, const char *fmt UNNEEDED, ...) { fprintf(stderr, "towire_errorfmt called!\n"); abort(); } -/* Generated stub for towire_pubkey */ -void towire_pubkey(u8 **pptr UNNEEDED, const struct pubkey *pubkey UNNEEDED) -{ fprintf(stderr, "towire_pubkey called!\n"); abort(); } -/* Generated stub for towire_short_channel_id */ -void towire_short_channel_id(u8 **pptr UNNEEDED, - const struct short_channel_id *short_channel_id UNNEEDED) -{ fprintf(stderr, "towire_short_channel_id called!\n"); abort(); } -/* Generated stub for towire_u16 */ -void towire_u16(u8 **pptr UNNEEDED, u16 v UNNEEDED) -{ fprintf(stderr, "towire_u16 called!\n"); abort(); } /* AUTOGENERATED MOCKS END */ const void *trc;