gossip: Avoid storing messages from gossip_store twice

Signed-off-by: Christian Decker <decker.christian@gmail.com>
This commit is contained in:
Christian Decker
2018-03-18 15:54:34 +01:00
committed by Rusty Russell
parent 1a5a4f5853
commit 8de6d9a7e2

View File

@@ -4,11 +4,16 @@
#include <ccan/read_write_all/read_write_all.h>
#include <common/status.h>
#include <fcntl.h>
#include <unistd.h>
#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);
}