#ifndef LIGHTNING_GOSSIPD_GOSSIP_STORE_H
#define LIGHTNING_GOSSIPD_GOSSIP_STORE_H

#include "config.h"

#include <bitcoin/short_channel_id.h>

/**
 * gossip_store -- On-disk storage related information
 */

struct gossip_store;
struct daemon;

/* For channels we find dying in the store, on load.  They are < 12
 * blocks closed. */
struct chan_dying {
	struct short_channel_id scid;
	/* Offset of dying marker in the gossip_store */
	u64 gossmap_offset;
	/* Blockheight where it's supposed to be deleted. */
	u32 deadline;
};

/**
 * Load the gossip_store
 * @ctx: the context to allocate from
 * @daemon: the daemon context
 * @populated: set to false if store is empty/obviously partial.
 *
 * Returns NULL on error.
 */
struct gossip_store *gossip_store_new(const tal_t *ctx,
				      struct daemon *daemon,
				      bool *populated);

/**
 * Reopen the gossip_store
 * @gs: the gossip store.
 *
 * We've rewritten it, so reopen the file.
 */
void gossip_store_reopen(struct gossip_store *gs);

/**
 * Move the old gossip store out the way.  Log a broken message about it.
 */
void gossip_store_corrupt(void);

/**
 * Extra paranoia: make sure it's on disk.  Don't call often!\
 */
void gossip_store_fsync(const struct gossip_store *gs);

/**
 * Append a gossip message to the gossip_store
 * @gs: gossip store
 * @gossip_msg: the gossip message to insert.
 * @timestamp: the timestamp for filtering of this messsage.
 *
 * Returns the offset (after the gossip_hdr).
 */
u64 gossip_store_add(struct gossip_store *gs,
		     const u8 *gossip_msg,
		     u32 timestamp);

/**
 * Delete the record at this offset (offset is that of
 * record, not header, unlike bcast->index!).
 *
 * In developer mode, checks that type is correct.
 */
void gossip_store_del(struct gossip_store *gs,
		      u64 offset,
		      int type);

/**
 * Add a flag the record at this offset (offset is that of
 * record!).  Returns offset of *next* record.
 *
 * In developer mode, checks that type is correct.
 */
u64 gossip_store_set_flag(struct gossip_store *gs,
		       u64 offset, u16 flag, int type);

/**
 * Clear a flag the record at this offset (offset is that of
 * record!).  OK if it's not currently set.
 *
 * In developer mode, checks that type is correct.
 */
void gossip_store_clear_flag(struct gossip_store *gs,
			     u64 offset, u16 flag, int type);

/**
 * Get flags from the record at this offset (offset is that of
 * record!).
 *
 * In developer mode, checks that type is correct.
 */
u16 gossip_store_get_flags(struct gossip_store *gs,
			   u64 offset, int type);

/**
 * Direct store accessor: get timestamp header for a record.
 *
 * Offset is *after* the header.
 */
u32 gossip_store_get_timestamp(struct gossip_store *gs, u64 offset);

/**
 * Direct store accessor: set timestamp header for a record.
 *
 * Offset is *after* the header.
 */
void gossip_store_set_timestamp(struct gossip_store *gs, u64 offset, u32 timestamp);

/**
 * We've seen (ZFS on Linux) writes not show up in the gossip store.
 * This lets us rewrite the last bytes. */
void gossip_store_rewrite_end(struct gossip_store *gs);

/**
 * Once we've checked the contents are good, the last_writes storage can
 * be reset. */
void gossip_store_writes_confirmed(struct gossip_store *gs);

/**
 * For debugging.
 */
u64 gossip_store_len_written(const struct gossip_store *gs);

#endif /* LIGHTNING_GOSSIPD_GOSSIP_STORE_H */
