From 8de6d9a7e27e81b65e4faa932b68fcd1d7519657 Mon Sep 17 00:00:00 2001 From: Christian Decker Date: Sun, 18 Mar 2018 15:54:34 +0100 Subject: [PATCH] gossip: Avoid storing messages from gossip_store twice Signed-off-by: Christian Decker --- gossipd/gossip_store.c | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/gossipd/gossip_store.c b/gossipd/gossip_store.c index 02791cc47..c8c39be2b 100644 --- a/gossipd/gossip_store.c +++ b/gossipd/gossip_store.c @@ -4,11 +4,16 @@ #include #include #include +#include #define GOSSIP_STORE_FILENAME "gossip_store" struct gossip_store { int read_fd, write_fd; + + /* What was the size of the gossip_store when we started replaying + * it? */ + __off_t replaysize; }; struct gossip_store *gossip_store_new(const tal_t *ctx) @@ -16,6 +21,8 @@ struct gossip_store *gossip_store_new(const tal_t *ctx) struct gossip_store *gs = tal(ctx, struct gossip_store); gs->write_fd = open(GOSSIP_STORE_FILENAME, O_RDWR|O_APPEND|O_CREAT, 0600); gs->read_fd = open(GOSSIP_STORE_FILENAME, O_RDONLY); + gs->replaysize = lseek(gs->write_fd, 0, SEEK_END); + return gs; } @@ -24,6 +31,15 @@ void gossip_store_append(struct gossip_store *gs, const u8 *msg) u16 msglen = tal_len(msg); beint16_t belen = cpu_to_be16(msglen); + /* FIXME: this method of detecting replayed messages is best effort + * only. It should avoid doubling the store file size on every start, + * but it'll allow the last few messages to be duplicated since replay + * and write are async, and we'll think we are done replaying a bit too + * early. */ + /* Check if we are replaying the store */ + if (lseek(gs->read_fd, 0, SEEK_CUR) < gs->replaysize) + return; + write_all(gs->write_fd, &belen, sizeof(belen)); write_all(gs->write_fd, msg, msglen); }