From 01b7e2a7c0bd262a42abf808d57e8d24ce8ff25c Mon Sep 17 00:00:00 2001 From: Christian Decker Date: Wed, 31 Jan 2018 17:52:12 +0100 Subject: [PATCH] broadcast: Added option to replace a specific index We are wasting way too much time looking for announcements and updates in the broadcast. We can just hint where to find the message to be evicted and safe the traversal. Signed-off-by: Christian Decker --- gossipd/broadcast.c | 25 ++++++++++++++++++++++++- gossipd/broadcast.h | 15 +++++++++++++-- 2 files changed, 37 insertions(+), 3 deletions(-) diff --git a/gossipd/broadcast.c b/gossipd/broadcast.c index 08fe93ea1..93febfcd4 100644 --- a/gossipd/broadcast.c +++ b/gossipd/broadcast.c @@ -22,6 +22,28 @@ static struct queued_message *new_queued_message(tal_t *ctx, return msg; } +bool replace_broadcast(struct broadcast_state *bstate, u64 *index, + const int type, const u8 *tag, const u8 *payload) +{ + struct queued_message *msg; + bool evicted = false; + + msg = uintmap_get(&bstate->broadcasts, *index); + if (msg && msg->type == type && + 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, type, tag, payload); + uintmap_add(&bstate->broadcasts, *index, msg); + bstate->next_index++; + return evicted; +} + bool queue_broadcast(struct broadcast_state *bstate, const int type, const u8 *tag, @@ -37,7 +59,8 @@ bool queue_broadcast(struct broadcast_state *bstate, for (msg = uintmap_first(&bstate->broadcasts, &index); msg; msg = uintmap_after(&bstate->broadcasts, &index)) { - if (msg->type == type && memcmp(msg->tag, tag, tal_len(tag)) == 0) { + if (msg->type == type && + memeq(msg->tag, tal_len(msg->tag), tag, tal_len(tag))) { uintmap_del(&bstate->broadcasts, index); tal_free(msg); evicted = true; diff --git a/gossipd/broadcast.h b/gossipd/broadcast.h index cb8747d61..296b03a7f 100644 --- a/gossipd/broadcast.h +++ b/gossipd/broadcast.h @@ -20,8 +20,8 @@ struct queued_message { }; struct broadcast_state { -u32 next_index; -UINTMAP(struct queued_message *) broadcasts; + u32 next_index; + UINTMAP(struct queued_message *) broadcasts; }; struct broadcast_state *new_broadcast_state(tal_t *ctx); @@ -36,6 +36,17 @@ bool queue_broadcast(struct broadcast_state *bstate, const u8 *tag, const u8 *payload); +/* Replace a queued message with @index, if it matches the type and + * 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, + u64 *index, + const int type, + const u8 *tag, + const u8 *payload); + + struct queued_message *next_broadcast_message(struct broadcast_state *bstate, u64 last_index); #endif /* LIGHTNING_LIGHTNINGD_GOSSIP_BROADCAST_H */