Browse Source

gossip: Introduce bookkeeping into gossip_store for rewrite

Signed-off-by: Christian Decker <decker.christian@gmail.com>
ppa-0.6.1
Christian Decker 7 years ago
parent
commit
eaba5a249a
  1. 18
      gossipd/gossip_store.c

18
gossipd/gossip_store.c

@ -13,15 +13,21 @@
#include <wire/wire.h> #include <wire/wire.h>
#define GOSSIP_STORE_FILENAME "gossip_store" #define GOSSIP_STORE_FILENAME "gossip_store"
#define MAX_COUNT_TO_STALE_RATE 10
static u8 gossip_store_version = 0x02; static u8 gossip_store_version = 0x02;
struct gossip_store { struct gossip_store {
int fd; int fd;
u8 version; u8 version;
/* Counters for entries in the gossip_store entries. This is used to
* decide whether we should rewrite the on-disk store or not */
size_t count;
/* The broadcast struct we source messages from when rewriting the /* The broadcast struct we source messages from when rewriting the
* gossip_store */ * gossip_store */
struct broadcast_state *broadcast; struct broadcast_state *broadcast;
}; };
static void gossip_store_destroy(struct gossip_store *gs) static void gossip_store_destroy(struct gossip_store *gs)
@ -33,6 +39,7 @@ struct gossip_store *gossip_store_new(const tal_t *ctx,
struct broadcast_state *broadcast) struct broadcast_state *broadcast)
{ {
struct gossip_store *gs = tal(ctx, struct gossip_store); struct gossip_store *gs = tal(ctx, struct gossip_store);
gs->count = 0;
gs->fd = open(GOSSIP_STORE_FILENAME, O_RDWR|O_APPEND|O_CREAT, 0600); gs->fd = open(GOSSIP_STORE_FILENAME, O_RDWR|O_APPEND|O_CREAT, 0600);
gs->broadcast = broadcast; gs->broadcast = broadcast;
@ -71,6 +78,7 @@ static void gossip_store_append(struct gossip_store *gs, const u8 *msg)
{ {
u32 msglen = tal_len(msg); u32 msglen = tal_len(msg);
beint32_t checksum, belen = cpu_to_be32(msglen); beint32_t checksum, belen = cpu_to_be32(msglen);
size_t stale;
/* Only give error message once. */ /* Only give error message once. */
if (gs->fd == -1) if (gs->fd == -1)
@ -86,9 +94,16 @@ static void gossip_store_append(struct gossip_store *gs, const u8 *msg)
strerror(errno)); strerror(errno));
gs->fd = -1; gs->fd = -1;
} }
gs->count++;
stale = gs->count - gs->broadcast->count;
if (gs->count >= 100 && stale * MAX_COUNT_TO_STALE_RATE > gs->count) {
/* FIXME(cdecker) Implement rewriting of gossip_store */
}
} }
void gossip_store_add_channel_announcement(struct gossip_store *gs, const u8 *gossip_msg, u64 satoshis) void gossip_store_add_channel_announcement(struct gossip_store *gs,
const u8 *gossip_msg, u64 satoshis)
{ {
u8 *msg = towire_gossip_store_channel_announcement(NULL, gossip_msg, satoshis); u8 *msg = towire_gossip_store_channel_announcement(NULL, gossip_msg, satoshis);
gossip_store_append(gs, msg); gossip_store_append(gs, msg);
@ -200,6 +215,7 @@ void gossip_store_load(struct routing_state *rstate, struct gossip_store *gs)
goto truncate; goto truncate;
} }
known_good += sizeof(belen) + msglen; known_good += sizeof(belen) + msglen;
gs->count++;
tal_free(msg); tal_free(msg);
} }
goto out; goto out;

Loading…
Cancel
Save