2021-12-04 12:23:56 +01:00
|
|
|
#include "config.h"
|
2019-06-03 20:13:25 +02:00
|
|
|
#include <ccan/array_size/array_size.h>
|
2019-06-11 07:30:54 +02:00
|
|
|
#include <ccan/crc32c/crc32c.h>
|
2019-06-03 20:15:25 +02:00
|
|
|
#include <ccan/noerr/noerr.h>
|
2018-03-13 12:08:03 +01:00
|
|
|
#include <ccan/read_write_all/read_write_all.h>
|
2019-06-20 04:58:52 +02:00
|
|
|
#include <ccan/tal/str/str.h>
|
2019-05-04 07:53:13 +02:00
|
|
|
#include <common/gossip_store.h>
|
2020-10-20 05:59:30 +02:00
|
|
|
#include <common/private_channel_announcement.h>
|
2018-03-13 12:08:03 +01:00
|
|
|
#include <common/status.h>
|
2018-04-11 01:03:35 +02:00
|
|
|
#include <errno.h>
|
2018-03-13 12:08:03 +01:00
|
|
|
#include <fcntl.h>
|
2021-12-04 12:23:56 +01:00
|
|
|
#include <gossipd/gossip_store.h>
|
2020-08-25 04:05:45 +02:00
|
|
|
#include <gossipd/gossip_store_wiregen.h>
|
2019-10-08 03:11:24 +02:00
|
|
|
#include <sys/stat.h>
|
2019-06-03 20:13:25 +02:00
|
|
|
#include <sys/uio.h>
|
2018-03-18 15:54:34 +01:00
|
|
|
#include <unistd.h>
|
2020-09-07 23:06:50 +02:00
|
|
|
#include <wire/peer_wire.h>
|
2018-03-13 12:08:03 +01:00
|
|
|
|
2018-05-30 16:06:23 +02:00
|
|
|
#define GOSSIP_STORE_TEMP_FILENAME "gossip_store.tmp"
|
2018-03-13 12:08:03 +01:00
|
|
|
|
|
|
|
struct gossip_store {
|
2019-04-11 07:15:22 +02:00
|
|
|
/* This is false when we're loading */
|
|
|
|
bool writable;
|
|
|
|
|
2018-03-20 16:18:18 +01:00
|
|
|
int fd;
|
2018-03-23 15:51:17 +01:00
|
|
|
u8 version;
|
2018-05-30 14:27:32 +02:00
|
|
|
|
2019-04-10 09:31:29 +02:00
|
|
|
/* Offset of current EOF */
|
|
|
|
u64 len;
|
|
|
|
|
2018-05-29 14:39:37 +02:00
|
|
|
/* Counters for entries in the gossip_store entries. This is used to
|
2019-06-03 20:20:25 +02:00
|
|
|
* decide whether we should rewrite the on-disk store or not.
|
|
|
|
* Note: count includes deleted. */
|
|
|
|
size_t count, deleted;
|
2018-05-29 14:39:37 +02:00
|
|
|
|
2018-05-30 19:24:56 +02:00
|
|
|
/* Handle to the routing_state to retrieve additional information,
|
|
|
|
* should it be needed */
|
|
|
|
struct routing_state *rstate;
|
2018-05-31 16:58:04 +02:00
|
|
|
|
2019-06-03 20:22:25 +02:00
|
|
|
/* This is daemon->peers for handling to update_peers_broadcast_index */
|
|
|
|
struct list_head *peers;
|
|
|
|
|
2018-05-31 16:58:04 +02:00
|
|
|
/* Disable compaction if we encounter an error during a prior
|
|
|
|
* compaction */
|
|
|
|
bool disable_compaction;
|
2019-10-08 03:11:24 +02:00
|
|
|
|
|
|
|
/* Timestamp of store when we opened it (0 if we created it) */
|
|
|
|
u32 timestamp;
|
2018-03-13 12:08:03 +01:00
|
|
|
};
|
|
|
|
|
2018-03-20 14:17:07 +01:00
|
|
|
static void gossip_store_destroy(struct gossip_store *gs)
|
|
|
|
{
|
2018-03-20 16:18:18 +01:00
|
|
|
close(gs->fd);
|
2018-03-20 14:17:07 +01:00
|
|
|
}
|
|
|
|
|
2019-09-11 01:53:41 +02:00
|
|
|
#if HAVE_PWRITEV
|
2021-03-17 02:54:51 +01:00
|
|
|
/* One fewer syscall for the win! */
|
2019-09-11 01:53:41 +02:00
|
|
|
static ssize_t gossip_pwritev(int fd, const struct iovec *iov, int iovcnt,
|
|
|
|
off_t offset)
|
|
|
|
{
|
|
|
|
return pwritev(fd, iov, iovcnt, offset);
|
|
|
|
}
|
|
|
|
#else /* Hello MacOS! */
|
|
|
|
static ssize_t gossip_pwritev(int fd, const struct iovec *iov, int iovcnt,
|
|
|
|
off_t offset)
|
|
|
|
{
|
2021-03-17 02:54:51 +01:00
|
|
|
if (lseek(fd, offset, SEEK_SET) != offset)
|
|
|
|
return -1;
|
|
|
|
return writev(fd, iov, iovcnt);
|
2019-09-11 01:53:41 +02:00
|
|
|
}
|
|
|
|
#endif /* !HAVE_PWRITEV */
|
|
|
|
|
2019-11-04 01:37:05 +01:00
|
|
|
static bool append_msg(int fd, const u8 *msg, u32 timestamp,
|
2022-05-04 15:48:59 +02:00
|
|
|
bool push, bool spam, u64 *len)
|
2019-05-04 07:53:12 +02:00
|
|
|
{
|
2019-06-03 20:17:25 +02:00
|
|
|
struct gossip_hdr hdr;
|
2019-05-04 07:53:12 +02:00
|
|
|
u32 msglen;
|
2019-06-03 20:13:25 +02:00
|
|
|
struct iovec iov[2];
|
2019-05-04 07:53:12 +02:00
|
|
|
|
2021-03-17 02:57:27 +01:00
|
|
|
/* Don't ever overwrite the version header! */
|
|
|
|
assert(*len);
|
|
|
|
|
2019-05-04 07:53:12 +02:00
|
|
|
msglen = tal_count(msg);
|
2019-06-03 20:17:25 +02:00
|
|
|
hdr.len = cpu_to_be32(msglen);
|
2019-11-04 01:37:05 +01:00
|
|
|
if (push)
|
|
|
|
hdr.len |= CPU_TO_BE32(GOSSIP_STORE_LEN_PUSH_BIT);
|
2022-05-04 15:48:59 +02:00
|
|
|
if (spam)
|
|
|
|
hdr.len |= CPU_TO_BE32(GOSSIP_STORE_LEN_RATELIMIT_BIT);
|
2019-06-03 20:17:25 +02:00
|
|
|
hdr.crc = cpu_to_be32(crc32c(timestamp, msg, msglen));
|
|
|
|
hdr.timestamp = cpu_to_be32(timestamp);
|
2019-05-04 07:53:12 +02:00
|
|
|
|
2021-03-17 02:54:51 +01:00
|
|
|
/* pwritev makes it more likely to appear at once, plus it's
|
|
|
|
* exactly what we want. */
|
2019-06-03 20:17:25 +02:00
|
|
|
iov[0].iov_base = &hdr;
|
2019-06-03 20:13:25 +02:00
|
|
|
iov[0].iov_len = sizeof(hdr);
|
|
|
|
iov[1].iov_base = (void *)msg;
|
|
|
|
iov[1].iov_len = msglen;
|
2019-09-11 01:53:41 +02:00
|
|
|
if (gossip_pwritev(fd, iov, ARRAY_SIZE(iov), *len) != sizeof(hdr) + msglen)
|
2019-07-30 03:10:09 +02:00
|
|
|
return false;
|
|
|
|
*len += sizeof(hdr) + msglen;
|
|
|
|
return true;
|
2019-05-04 07:53:12 +02:00
|
|
|
}
|
|
|
|
|
2020-05-04 02:18:25 +02:00
|
|
|
#ifdef COMPAT_V082
|
2020-10-20 05:59:30 +02:00
|
|
|
static u8 *mk_private_channelmsg(const tal_t *ctx,
|
|
|
|
struct routing_state *rstate,
|
|
|
|
const struct short_channel_id *scid,
|
|
|
|
const struct node_id *remote_node_id,
|
|
|
|
struct amount_sat sat,
|
|
|
|
const u8 *features)
|
|
|
|
{
|
|
|
|
const u8 *ann = private_channel_announcement(tmpctx, scid,
|
|
|
|
&rstate->local_id,
|
|
|
|
remote_node_id,
|
|
|
|
features);
|
|
|
|
|
|
|
|
return towire_gossip_store_private_channel(ctx, sat, ann);
|
|
|
|
}
|
|
|
|
|
2020-05-04 02:18:25 +02:00
|
|
|
/* The upgrade from version 7 is trivial */
|
|
|
|
static bool can_upgrade(u8 oldversion)
|
|
|
|
{
|
2022-05-03 19:42:31 +02:00
|
|
|
return oldversion == 7 || oldversion == 8 || oldversion == 9;
|
2020-05-04 02:18:25 +02:00
|
|
|
}
|
|
|
|
|
2020-10-20 05:59:30 +02:00
|
|
|
static bool upgrade_field(u8 oldversion,
|
|
|
|
struct routing_state *rstate,
|
|
|
|
u8 **msg)
|
2020-05-04 02:18:25 +02:00
|
|
|
{
|
|
|
|
assert(can_upgrade(oldversion));
|
|
|
|
|
2020-10-20 05:59:30 +02:00
|
|
|
if (fromwire_peektype(*msg) == WIRE_GOSSIPD_LOCAL_ADD_CHANNEL_OBS
|
|
|
|
&& oldversion == 7) {
|
2020-05-04 02:18:25 +02:00
|
|
|
/* Append two 0 bytes, for (empty) feature bits */
|
|
|
|
tal_resizez(msg, tal_bytelen(*msg) + 2);
|
|
|
|
}
|
2020-10-20 05:59:30 +02:00
|
|
|
|
|
|
|
/* We turn these (v8) into a WIRE_GOSSIP_STORE_PRIVATE_CHANNEL */
|
|
|
|
if (fromwire_peektype(*msg) == WIRE_GOSSIPD_LOCAL_ADD_CHANNEL_OBS) {
|
|
|
|
struct short_channel_id scid;
|
|
|
|
struct node_id remote_node_id;
|
|
|
|
struct amount_sat satoshis;
|
|
|
|
u8 *features;
|
|
|
|
u8 *storemsg;
|
|
|
|
|
|
|
|
if (!fromwire_gossipd_local_add_channel_obs(tmpctx, *msg,
|
|
|
|
&scid,
|
|
|
|
&remote_node_id,
|
|
|
|
&satoshis,
|
|
|
|
&features))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
storemsg = mk_private_channelmsg(tal_parent(*msg),
|
|
|
|
rstate,
|
|
|
|
&scid,
|
|
|
|
&remote_node_id,
|
|
|
|
satoshis,
|
|
|
|
features);
|
|
|
|
tal_free(*msg);
|
|
|
|
*msg = storemsg;
|
|
|
|
}
|
2020-05-04 02:18:25 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
static bool can_upgrade(u8 oldversion)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-10-20 05:59:30 +02:00
|
|
|
static bool upgrade_field(u8 oldversion,
|
|
|
|
struct routing_state *rstate,
|
|
|
|
u8 **msg)
|
2020-05-04 02:18:25 +02:00
|
|
|
{
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
#endif /* !COMPAT_V082 */
|
|
|
|
|
2019-06-22 02:18:13 +02:00
|
|
|
/* Read gossip store entries, copy non-deleted ones. This code is written
|
|
|
|
* as simply and robustly as possible! */
|
2020-10-20 05:59:30 +02:00
|
|
|
static u32 gossip_store_compact_offline(struct routing_state *rstate)
|
2019-06-22 02:18:13 +02:00
|
|
|
{
|
|
|
|
size_t count = 0, deleted = 0;
|
|
|
|
int old_fd, new_fd;
|
2021-05-22 07:00:22 +02:00
|
|
|
u64 oldlen, newlen;
|
2019-06-22 02:18:13 +02:00
|
|
|
struct gossip_hdr hdr;
|
2020-05-04 02:18:25 +02:00
|
|
|
u8 oldversion, version = GOSSIP_STORE_VERSION;
|
2019-10-08 03:11:24 +02:00
|
|
|
struct stat st;
|
2019-06-22 02:18:13 +02:00
|
|
|
|
2021-05-22 07:00:22 +02:00
|
|
|
old_fd = open(GOSSIP_STORE_FILENAME, O_RDWR);
|
2019-06-22 02:18:13 +02:00
|
|
|
if (old_fd == -1)
|
2019-10-08 03:11:24 +02:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (fstat(old_fd, &st) != 0) {
|
|
|
|
status_broken("Could not stat gossip_store: %s",
|
|
|
|
strerror(errno));
|
|
|
|
goto close_old;
|
|
|
|
}
|
|
|
|
|
2019-06-22 02:18:13 +02:00
|
|
|
new_fd = open(GOSSIP_STORE_TEMP_FILENAME, O_RDWR|O_TRUNC|O_CREAT, 0600);
|
|
|
|
if (new_fd < 0) {
|
|
|
|
status_broken(
|
|
|
|
"Could not open file for gossip_store compaction");
|
|
|
|
goto close_old;
|
|
|
|
}
|
|
|
|
|
2020-05-04 02:18:25 +02:00
|
|
|
if (!read_all(old_fd, &oldversion, sizeof(oldversion))
|
|
|
|
|| (oldversion != version && !can_upgrade(oldversion))) {
|
2019-06-22 02:18:13 +02:00
|
|
|
status_broken("gossip_store_compact: bad version");
|
|
|
|
goto close_and_delete;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!write_all(new_fd, &version, sizeof(version))) {
|
|
|
|
status_broken("gossip_store_compact_offline: writing version to store: %s",
|
|
|
|
strerror(errno));
|
|
|
|
goto close_and_delete;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Read everything, write non-deleted ones to new_fd */
|
|
|
|
while (read_all(old_fd, &hdr, sizeof(hdr))) {
|
|
|
|
size_t msglen;
|
|
|
|
u8 *msg;
|
|
|
|
|
2019-11-04 01:37:02 +01:00
|
|
|
msglen = (be32_to_cpu(hdr.len) & GOSSIP_STORE_LEN_MASK);
|
2019-06-22 02:18:13 +02:00
|
|
|
msg = tal_arr(NULL, u8, msglen);
|
|
|
|
if (!read_all(old_fd, msg, msglen)) {
|
|
|
|
status_broken("gossip_store_compact_offline: reading msg len %zu from store: %s",
|
|
|
|
msglen, strerror(errno));
|
|
|
|
tal_free(msg);
|
|
|
|
goto close_and_delete;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (be32_to_cpu(hdr.len) & GOSSIP_STORE_LEN_DELETED_BIT) {
|
|
|
|
deleted++;
|
|
|
|
tal_free(msg);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-05-04 02:18:25 +02:00
|
|
|
if (oldversion != version) {
|
2020-10-20 05:59:30 +02:00
|
|
|
if (!upgrade_field(oldversion, rstate, &msg)) {
|
2020-05-04 02:18:25 +02:00
|
|
|
tal_free(msg);
|
|
|
|
goto close_and_delete;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Recalc msglen and header */
|
|
|
|
msglen = tal_bytelen(msg);
|
|
|
|
hdr.len = cpu_to_be32(msglen);
|
|
|
|
hdr.crc = cpu_to_be32(crc32c(be32_to_cpu(hdr.timestamp),
|
|
|
|
msg, msglen));
|
|
|
|
}
|
|
|
|
|
2020-10-20 05:58:06 +02:00
|
|
|
/* Don't write out old tombstones */
|
|
|
|
if (fromwire_peektype(msg) == WIRE_GOSSIP_STORE_DELETE_CHAN) {
|
|
|
|
deleted++;
|
|
|
|
tal_free(msg);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2019-06-22 02:18:13 +02:00
|
|
|
if (!write_all(new_fd, &hdr, sizeof(hdr))
|
|
|
|
|| !write_all(new_fd, msg, msglen)) {
|
|
|
|
status_broken("gossip_store_compact_offline: writing msg len %zu to new store: %s",
|
|
|
|
msglen, strerror(errno));
|
|
|
|
tal_free(msg);
|
|
|
|
goto close_and_delete;
|
|
|
|
}
|
|
|
|
tal_free(msg);
|
|
|
|
count++;
|
|
|
|
}
|
|
|
|
if (close(new_fd) != 0) {
|
|
|
|
status_broken("gossip_store_compact_offline: closing new store: %s",
|
|
|
|
strerror(errno));
|
|
|
|
goto close_old;
|
|
|
|
}
|
|
|
|
if (rename(GOSSIP_STORE_TEMP_FILENAME, GOSSIP_STORE_FILENAME) != 0) {
|
|
|
|
status_broken("gossip_store_compact_offline: rename failed: %s",
|
|
|
|
strerror(errno));
|
|
|
|
}
|
2021-05-22 07:00:22 +02:00
|
|
|
|
|
|
|
/* Create end marker now new file exists. */
|
|
|
|
oldlen = lseek(old_fd, SEEK_END, 0);
|
|
|
|
newlen = lseek(new_fd, SEEK_END, 0);
|
|
|
|
append_msg(old_fd, towire_gossip_store_ended(tmpctx, newlen),
|
2022-05-04 15:48:59 +02:00
|
|
|
0, true, false, &oldlen);
|
2021-05-22 07:00:22 +02:00
|
|
|
close(old_fd);
|
2019-06-22 02:18:13 +02:00
|
|
|
status_debug("gossip_store_compact_offline: %zu deleted, %zu copied",
|
|
|
|
deleted, count);
|
2019-10-08 03:11:24 +02:00
|
|
|
return st.st_mtime;
|
2019-06-22 02:18:13 +02:00
|
|
|
|
|
|
|
close_and_delete:
|
|
|
|
close(new_fd);
|
|
|
|
close_old:
|
|
|
|
close(old_fd);
|
|
|
|
unlink(GOSSIP_STORE_TEMP_FILENAME);
|
2019-10-08 03:11:24 +02:00
|
|
|
return 0;
|
2019-06-22 02:18:13 +02:00
|
|
|
}
|
|
|
|
|
2019-06-03 20:22:25 +02:00
|
|
|
struct gossip_store *gossip_store_new(struct routing_state *rstate,
|
|
|
|
struct list_head *peers)
|
2018-03-13 12:08:03 +01:00
|
|
|
{
|
2019-04-10 09:31:29 +02:00
|
|
|
struct gossip_store *gs = tal(rstate, struct gossip_store);
|
2019-06-03 20:20:25 +02:00
|
|
|
gs->count = gs->deleted = 0;
|
2019-04-11 07:15:22 +02:00
|
|
|
gs->writable = true;
|
2020-10-20 05:59:30 +02:00
|
|
|
gs->timestamp = gossip_store_compact_offline(rstate);
|
2019-07-30 03:10:09 +02:00
|
|
|
gs->fd = open(GOSSIP_STORE_FILENAME, O_RDWR|O_CREAT, 0600);
|
2019-07-04 01:23:07 +02:00
|
|
|
if (gs->fd < 0)
|
|
|
|
status_failed(STATUS_FAIL_INTERNAL_ERROR,
|
|
|
|
"Opening gossip_store store: %s",
|
|
|
|
strerror(errno));
|
2018-05-30 19:24:56 +02:00
|
|
|
gs->rstate = rstate;
|
2018-05-31 16:58:04 +02:00
|
|
|
gs->disable_compaction = false;
|
2019-04-10 09:31:29 +02:00
|
|
|
gs->len = sizeof(gs->version);
|
2019-06-03 20:22:25 +02:00
|
|
|
gs->peers = peers;
|
2018-04-11 01:03:35 +02:00
|
|
|
|
|
|
|
tal_add_destructor(gs, gossip_store_destroy);
|
2018-03-18 15:54:34 +01:00
|
|
|
|
2018-03-23 15:51:17 +01:00
|
|
|
/* Try to read the version, write it if this is a new file, or truncate
|
|
|
|
* if the version doesn't match */
|
2018-04-11 01:03:35 +02:00
|
|
|
if (read(gs->fd, &gs->version, sizeof(gs->version))
|
|
|
|
== sizeof(gs->version)) {
|
|
|
|
/* Version match? All good */
|
2018-08-30 02:04:28 +02:00
|
|
|
if (gs->version == GOSSIP_STORE_VERSION)
|
2018-04-11 01:03:35 +02:00
|
|
|
return gs;
|
|
|
|
|
|
|
|
status_unusual("Gossip store version %u not %u: removing",
|
2018-08-30 02:04:28 +02:00
|
|
|
gs->version, GOSSIP_STORE_VERSION);
|
2018-04-11 01:03:35 +02:00
|
|
|
if (ftruncate(gs->fd, 0) != 0)
|
|
|
|
status_failed(STATUS_FAIL_INTERNAL_ERROR,
|
|
|
|
"Truncating store: %s", strerror(errno));
|
2021-03-25 10:46:49 +01:00
|
|
|
/* Subtle: we are at offset 1, move back to start! */
|
|
|
|
if (lseek(gs->fd, 0, SEEK_SET) != 0)
|
|
|
|
status_failed(STATUS_FAIL_INTERNAL_ERROR,
|
|
|
|
"Seeking to start of store: %s",
|
|
|
|
strerror(errno));
|
2018-03-23 15:51:17 +01:00
|
|
|
}
|
2018-04-11 01:03:35 +02:00
|
|
|
/* Empty file, write version byte */
|
2018-08-30 02:04:28 +02:00
|
|
|
gs->version = GOSSIP_STORE_VERSION;
|
2018-04-11 01:03:35 +02:00
|
|
|
if (write(gs->fd, &gs->version, sizeof(gs->version))
|
|
|
|
!= sizeof(gs->version))
|
|
|
|
status_failed(STATUS_FAIL_INTERNAL_ERROR,
|
|
|
|
"Writing version to store: %s", strerror(errno));
|
2018-03-13 12:08:03 +01:00
|
|
|
return gs;
|
|
|
|
}
|
2018-05-30 22:18:45 +02:00
|
|
|
|
2019-06-03 20:16:25 +02:00
|
|
|
/* Returns bytes transferred, or 0 on error */
|
2019-07-30 03:10:09 +02:00
|
|
|
static size_t transfer_store_msg(int from_fd, size_t from_off,
|
|
|
|
int to_fd, size_t to_off,
|
2019-06-03 20:16:25 +02:00
|
|
|
int *type)
|
2019-04-11 07:16:30 +02:00
|
|
|
{
|
2019-06-03 20:17:25 +02:00
|
|
|
struct gossip_hdr hdr;
|
2019-04-11 07:16:30 +02:00
|
|
|
u32 msglen;
|
|
|
|
u8 *msg;
|
2019-06-03 20:16:25 +02:00
|
|
|
const u8 *p;
|
|
|
|
size_t tmplen;
|
2019-04-11 07:16:30 +02:00
|
|
|
|
2019-06-03 20:16:25 +02:00
|
|
|
*type = -1;
|
2019-06-03 20:17:25 +02:00
|
|
|
if (pread(from_fd, &hdr, sizeof(hdr), from_off) != sizeof(hdr)) {
|
2019-06-03 20:16:25 +02:00
|
|
|
status_broken("Failed reading header from to gossip store @%zu"
|
2019-04-11 07:16:30 +02:00
|
|
|
": %s",
|
2019-06-03 20:16:25 +02:00
|
|
|
from_off, strerror(errno));
|
2019-04-11 07:16:30 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-06-03 20:17:25 +02:00
|
|
|
msglen = be32_to_cpu(hdr.len);
|
2019-06-03 20:16:25 +02:00
|
|
|
if (msglen & GOSSIP_STORE_LEN_DELETED_BIT) {
|
|
|
|
status_broken("Can't transfer deleted msg from gossip store @%zu",
|
|
|
|
from_off);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-11-04 01:37:02 +01:00
|
|
|
/* Ignore any non-length bits (e.g. push) */
|
|
|
|
msglen &= GOSSIP_STORE_LEN_MASK;
|
|
|
|
|
2019-06-03 20:16:25 +02:00
|
|
|
/* FIXME: Reuse buffer? */
|
|
|
|
msg = tal_arr(tmpctx, u8, sizeof(hdr) + msglen);
|
2019-06-03 20:17:25 +02:00
|
|
|
memcpy(msg, &hdr, sizeof(hdr));
|
2019-06-03 20:16:25 +02:00
|
|
|
if (pread(from_fd, msg + sizeof(hdr), msglen, from_off + sizeof(hdr))
|
2019-04-11 07:16:30 +02:00
|
|
|
!= msglen) {
|
2019-06-03 20:16:25 +02:00
|
|
|
status_broken("Failed reading %u from to gossip store @%zu"
|
2019-04-11 07:16:30 +02:00
|
|
|
": %s",
|
2019-06-03 20:16:25 +02:00
|
|
|
msglen, from_off, strerror(errno));
|
2019-04-11 07:16:30 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-07-30 03:10:09 +02:00
|
|
|
if (pwrite(to_fd, msg, msglen + sizeof(hdr), to_off)
|
|
|
|
!= msglen + sizeof(hdr)) {
|
2019-04-11 07:16:30 +02:00
|
|
|
status_broken("Failed writing to gossip store: %s",
|
|
|
|
strerror(errno));
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-06-03 20:16:25 +02:00
|
|
|
/* Can't use peektype here, since we have header on front */
|
|
|
|
p = msg + sizeof(hdr);
|
|
|
|
tmplen = msglen;
|
|
|
|
*type = fromwire_u16(&p, &tmplen);
|
|
|
|
if (!p)
|
|
|
|
*type = -1;
|
2019-04-11 07:16:30 +02:00
|
|
|
tal_free(msg);
|
2019-06-03 20:16:25 +02:00
|
|
|
return sizeof(hdr) + msglen;
|
2019-04-11 07:16:30 +02:00
|
|
|
}
|
|
|
|
|
2019-06-03 20:21:25 +02:00
|
|
|
/* We keep a htable map of old gossip_store offsets to new ones. */
|
|
|
|
struct offset_map {
|
|
|
|
size_t from, to;
|
|
|
|
};
|
2019-04-08 01:52:19 +02:00
|
|
|
|
2019-06-03 20:21:25 +02:00
|
|
|
static size_t offset_map_key(const struct offset_map *omap)
|
|
|
|
{
|
|
|
|
return omap->from;
|
|
|
|
}
|
2019-04-11 07:16:30 +02:00
|
|
|
|
2019-06-03 20:21:25 +02:00
|
|
|
static size_t hash_offset(size_t from)
|
|
|
|
{
|
|
|
|
/* Crappy fast hash is "good enough" */
|
|
|
|
return (from >> 5) ^ from;
|
|
|
|
}
|
2019-04-11 07:16:30 +02:00
|
|
|
|
2019-06-03 20:21:25 +02:00
|
|
|
static bool offset_map_eq(const struct offset_map *omap, const size_t from)
|
|
|
|
{
|
|
|
|
return omap->from == from;
|
|
|
|
}
|
|
|
|
HTABLE_DEFINE_TYPE(struct offset_map,
|
|
|
|
offset_map_key, hash_offset, offset_map_eq, offmap);
|
|
|
|
|
|
|
|
static void move_broadcast(struct offmap *offmap,
|
|
|
|
struct broadcastable *bcast,
|
|
|
|
const char *what)
|
|
|
|
{
|
|
|
|
struct offset_map *omap;
|
2019-04-11 07:16:30 +02:00
|
|
|
|
2019-06-03 20:21:25 +02:00
|
|
|
if (!bcast->index)
|
|
|
|
return;
|
2019-04-11 07:16:30 +02:00
|
|
|
|
2019-06-03 20:21:25 +02:00
|
|
|
omap = offmap_get(offmap, bcast->index);
|
|
|
|
if (!omap)
|
|
|
|
status_failed(STATUS_FAIL_INTERNAL_ERROR,
|
|
|
|
"Could not relocate %s at offset %u",
|
|
|
|
what, bcast->index);
|
|
|
|
bcast->index = omap->to;
|
|
|
|
offmap_del(offmap, omap);
|
|
|
|
}
|
2019-04-08 01:52:19 +02:00
|
|
|
|
2019-06-03 20:21:25 +02:00
|
|
|
static void destroy_offmap(struct offmap *offmap)
|
|
|
|
{
|
|
|
|
offmap_clear(offmap);
|
2019-04-08 01:52:19 +02:00
|
|
|
}
|
|
|
|
|
2018-05-30 16:06:23 +02:00
|
|
|
/**
|
|
|
|
* Rewrite the on-disk gossip store, compacting it along the way
|
|
|
|
*
|
|
|
|
* Creates a new file, writes all the updates from the `broadcast_state`, and
|
|
|
|
* then atomically swaps the files.
|
|
|
|
*/
|
2019-06-03 20:22:25 +02:00
|
|
|
bool gossip_store_compact(struct gossip_store *gs)
|
2018-05-30 16:06:23 +02:00
|
|
|
{
|
2019-06-03 20:21:25 +02:00
|
|
|
size_t count = 0, deleted = 0;
|
2018-05-30 16:06:23 +02:00
|
|
|
int fd;
|
2021-05-22 09:09:53 +02:00
|
|
|
u64 off, len = sizeof(gs->version), idx;
|
2019-06-03 20:21:25 +02:00
|
|
|
struct offmap *offmap;
|
|
|
|
struct gossip_hdr hdr;
|
|
|
|
struct offmap_iter oit;
|
|
|
|
struct node_map_iter nit;
|
|
|
|
struct offset_map *omap;
|
2019-04-10 09:31:29 +02:00
|
|
|
|
|
|
|
if (gs->disable_compaction)
|
|
|
|
return false;
|
2018-05-30 16:06:23 +02:00
|
|
|
|
2019-09-08 18:39:26 +02:00
|
|
|
status_debug(
|
2018-05-30 16:06:23 +02:00
|
|
|
"Compacting gossip_store with %zu entries, %zu of which are stale",
|
2019-06-03 20:20:25 +02:00
|
|
|
gs->count, gs->deleted);
|
2018-05-30 16:06:23 +02:00
|
|
|
|
2019-06-20 02:51:12 +02:00
|
|
|
fd = open(GOSSIP_STORE_TEMP_FILENAME, O_RDWR|O_TRUNC|O_CREAT, 0600);
|
2018-05-30 16:06:23 +02:00
|
|
|
|
2018-05-31 16:58:04 +02:00
|
|
|
if (fd < 0) {
|
|
|
|
status_broken(
|
2018-05-30 16:06:23 +02:00
|
|
|
"Could not open file for gossip_store compaction");
|
2018-05-31 16:58:04 +02:00
|
|
|
goto disable;
|
|
|
|
}
|
2018-05-30 16:06:23 +02:00
|
|
|
|
2018-08-30 02:04:28 +02:00
|
|
|
if (write(fd, &gs->version, sizeof(gs->version))
|
|
|
|
!= sizeof(gs->version)) {
|
2018-05-31 16:58:04 +02:00
|
|
|
status_broken("Writing version to store: %s", strerror(errno));
|
|
|
|
goto unlink_disable;
|
|
|
|
}
|
2018-05-30 16:06:23 +02:00
|
|
|
|
2019-06-03 20:21:25 +02:00
|
|
|
/* Walk old file, copy everything and remember new offsets. */
|
|
|
|
offmap = tal(tmpctx, struct offmap);
|
|
|
|
offmap_init_sized(offmap, gs->count);
|
|
|
|
tal_add_destructor(offmap, destroy_offmap);
|
|
|
|
|
|
|
|
/* Start by writing all channel announcements and updates. */
|
|
|
|
off = 1;
|
|
|
|
while (pread(gs->fd, &hdr, sizeof(hdr), off) == sizeof(hdr)) {
|
|
|
|
u32 msglen, wlen;
|
2019-05-04 07:53:12 +02:00
|
|
|
int msgtype;
|
2019-04-10 09:31:29 +02:00
|
|
|
|
2019-11-04 01:37:02 +01:00
|
|
|
msglen = (be32_to_cpu(hdr.len) & GOSSIP_STORE_LEN_MASK);
|
2019-06-03 20:21:25 +02:00
|
|
|
if (be32_to_cpu(hdr.len) & GOSSIP_STORE_LEN_DELETED_BIT) {
|
|
|
|
off += sizeof(hdr) + msglen;
|
|
|
|
deleted++;
|
|
|
|
continue;
|
|
|
|
}
|
2019-04-10 09:31:29 +02:00
|
|
|
|
2019-05-04 07:53:12 +02:00
|
|
|
count++;
|
2019-07-30 03:10:09 +02:00
|
|
|
wlen = transfer_store_msg(gs->fd, off, fd, len, &msgtype);
|
2019-06-03 20:21:25 +02:00
|
|
|
if (wlen == 0)
|
|
|
|
goto unlink_disable;
|
2019-04-10 09:31:29 +02:00
|
|
|
|
2019-06-03 20:21:25 +02:00
|
|
|
/* We track location of all these message types. */
|
2020-10-20 05:59:30 +02:00
|
|
|
if (msgtype == WIRE_GOSSIP_STORE_PRIVATE_CHANNEL
|
2019-06-03 20:21:25 +02:00
|
|
|
|| msgtype == WIRE_GOSSIP_STORE_PRIVATE_UPDATE
|
|
|
|
|| msgtype == WIRE_CHANNEL_ANNOUNCEMENT
|
|
|
|
|| msgtype == WIRE_CHANNEL_UPDATE
|
|
|
|
|| msgtype == WIRE_NODE_ANNOUNCEMENT) {
|
|
|
|
omap = tal(offmap, struct offset_map);
|
|
|
|
omap->from = off;
|
|
|
|
omap->to = len;
|
|
|
|
offmap_add(offmap, omap);
|
2018-05-30 16:06:23 +02:00
|
|
|
}
|
2019-06-03 20:21:25 +02:00
|
|
|
len += wlen;
|
|
|
|
off += wlen;
|
2018-05-30 16:06:23 +02:00
|
|
|
}
|
|
|
|
|
2019-06-03 20:22:25 +02:00
|
|
|
/* OK, now we've written file successfully, we can move broadcasts. */
|
2019-06-03 20:21:25 +02:00
|
|
|
/* Remap node announcements. */
|
|
|
|
for (struct node *n = node_map_first(gs->rstate->nodes, &nit);
|
|
|
|
n;
|
|
|
|
n = node_map_next(gs->rstate->nodes, &nit)) {
|
2019-06-03 20:22:25 +02:00
|
|
|
move_broadcast(offmap, &n->bcast, "node_announce");
|
2019-04-08 01:52:19 +02:00
|
|
|
}
|
|
|
|
|
2019-06-03 20:21:25 +02:00
|
|
|
/* Remap channel announcements and updates */
|
|
|
|
for (struct chan *c = uintmap_first(&gs->rstate->chanmap, &idx);
|
|
|
|
c;
|
|
|
|
c = uintmap_after(&gs->rstate->chanmap, &idx)) {
|
2019-06-03 20:22:25 +02:00
|
|
|
move_broadcast(offmap, &c->bcast, "channel_announce");
|
|
|
|
move_broadcast(offmap, &c->half[0].bcast, "channel_update");
|
|
|
|
move_broadcast(offmap, &c->half[1].bcast, "channel_update");
|
2019-06-03 20:21:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* That should be everything. */
|
|
|
|
omap = offmap_first(offmap, &oit);
|
|
|
|
if (omap)
|
|
|
|
status_failed(STATUS_FAIL_INTERNAL_ERROR,
|
|
|
|
"gossip_store: Entry at %zu->%zu not updated?",
|
|
|
|
omap->from, omap->to);
|
|
|
|
|
2019-06-14 02:35:12 +02:00
|
|
|
if (count != gs->count - gs->deleted)
|
|
|
|
status_failed(STATUS_FAIL_INTERNAL_ERROR,
|
|
|
|
"gossip_store: Expected %zu msgs in new"
|
|
|
|
" gossip store, got %zu",
|
2019-06-03 20:20:25 +02:00
|
|
|
gs->count - gs->deleted, count);
|
|
|
|
|
2019-06-14 02:35:12 +02:00
|
|
|
if (deleted != gs->deleted)
|
|
|
|
status_failed(STATUS_FAIL_INTERNAL_ERROR,
|
|
|
|
"gossip_store: Expected %zu deleted msgs in old"
|
|
|
|
" gossip store, got %zu",
|
2019-06-03 20:21:25 +02:00
|
|
|
gs->deleted, deleted);
|
|
|
|
|
2019-06-14 02:35:12 +02:00
|
|
|
if (rename(GOSSIP_STORE_TEMP_FILENAME, GOSSIP_STORE_FILENAME) == -1)
|
|
|
|
status_failed(STATUS_FAIL_INTERNAL_ERROR,
|
|
|
|
"Error swapping compacted gossip_store into place:"
|
|
|
|
" %s",
|
|
|
|
strerror(errno));
|
2018-05-30 16:06:23 +02:00
|
|
|
|
2019-09-08 18:39:26 +02:00
|
|
|
status_debug(
|
2019-04-10 09:31:29 +02:00
|
|
|
"Compaction completed: dropped %zu messages, new count %zu, len %"PRIu64,
|
2019-06-03 20:21:25 +02:00
|
|
|
deleted, count, len);
|
2021-05-22 07:00:22 +02:00
|
|
|
|
|
|
|
/* Write end marker now new one is ready */
|
|
|
|
append_msg(gs->fd, towire_gossip_store_ended(tmpctx, len),
|
2022-05-04 15:48:59 +02:00
|
|
|
0, true, false, &gs->len);
|
2021-05-22 07:00:22 +02:00
|
|
|
|
2018-05-30 16:06:23 +02:00
|
|
|
gs->count = count;
|
2019-06-03 20:20:25 +02:00
|
|
|
gs->deleted = 0;
|
2019-04-10 09:31:29 +02:00
|
|
|
gs->len = len;
|
2018-05-30 16:06:23 +02:00
|
|
|
close(gs->fd);
|
|
|
|
gs->fd = fd;
|
2019-04-10 09:31:29 +02:00
|
|
|
|
2019-04-08 01:52:19 +02:00
|
|
|
return true;
|
2018-05-31 16:58:04 +02:00
|
|
|
|
|
|
|
unlink_disable:
|
|
|
|
unlink(GOSSIP_STORE_TEMP_FILENAME);
|
|
|
|
disable:
|
2019-09-08 18:39:26 +02:00
|
|
|
status_debug("Encountered an error while compacting, disabling "
|
2018-05-31 16:58:04 +02:00
|
|
|
"future compactions.");
|
|
|
|
gs->disable_compaction = true;
|
2019-04-08 01:52:19 +02:00
|
|
|
return false;
|
2018-05-30 16:06:23 +02:00
|
|
|
}
|
|
|
|
|
2019-05-04 07:53:12 +02:00
|
|
|
u64 gossip_store_add(struct gossip_store *gs, const u8 *gossip_msg,
|
2019-11-04 01:37:05 +01:00
|
|
|
u32 timestamp, bool push,
|
2022-05-04 15:48:59 +02:00
|
|
|
bool spam, const u8 *addendum)
|
2019-04-10 09:31:29 +02:00
|
|
|
{
|
|
|
|
u64 off = gs->len;
|
|
|
|
|
|
|
|
/* Should never get here during loading! */
|
2019-04-11 07:15:22 +02:00
|
|
|
assert(gs->writable);
|
2019-04-10 09:31:29 +02:00
|
|
|
|
2022-05-04 15:48:59 +02:00
|
|
|
if (!append_msg(gs->fd, gossip_msg, timestamp, push, spam, &gs->len)) {
|
2018-05-31 11:43:25 +02:00
|
|
|
status_broken("Failed writing to gossip store: %s",
|
|
|
|
strerror(errno));
|
2019-04-10 09:31:29 +02:00
|
|
|
return 0;
|
2018-05-31 11:43:25 +02:00
|
|
|
}
|
2022-05-04 15:48:59 +02:00
|
|
|
if (addendum && !append_msg(gs->fd, addendum, 0, false, false, &gs->len)) {
|
2019-05-16 21:55:17 +02:00
|
|
|
status_broken("Failed writing addendum to gossip store: %s",
|
|
|
|
strerror(errno));
|
|
|
|
return 0;
|
|
|
|
}
|
2018-05-31 11:43:25 +02:00
|
|
|
|
|
|
|
gs->count++;
|
2019-06-03 20:20:25 +02:00
|
|
|
if (addendum)
|
|
|
|
gs->count++;
|
2019-04-10 09:31:29 +02:00
|
|
|
return off;
|
2018-03-25 18:23:10 +02:00
|
|
|
}
|
|
|
|
|
2019-06-03 20:05:25 +02:00
|
|
|
u64 gossip_store_add_private_update(struct gossip_store *gs, const u8 *update)
|
|
|
|
{
|
|
|
|
/* A local update for an unannounced channel: not broadcastable, but
|
|
|
|
* otherwise the same as a normal channel_update */
|
|
|
|
const u8 *pupdate = towire_gossip_store_private_update(tmpctx, update);
|
2022-05-04 15:48:59 +02:00
|
|
|
return gossip_store_add(gs, pupdate, 0, false, false, NULL);
|
2019-06-03 20:05:25 +02:00
|
|
|
}
|
|
|
|
|
2019-06-14 05:41:39 +02:00
|
|
|
/* Returns index of following entry. */
|
|
|
|
static u32 delete_by_index(struct gossip_store *gs, u32 index, int type)
|
2018-03-28 12:54:09 +02:00
|
|
|
{
|
2019-06-03 20:09:25 +02:00
|
|
|
beint32_t belen;
|
|
|
|
|
2019-04-11 07:15:22 +02:00
|
|
|
/* Should never get here during loading! */
|
|
|
|
assert(gs->writable);
|
|
|
|
|
2021-03-17 02:57:27 +01:00
|
|
|
/* Should never try to overwrite version */
|
|
|
|
assert(index);
|
|
|
|
|
2019-06-03 20:09:25 +02:00
|
|
|
#if DEVELOPER
|
2019-06-14 05:41:39 +02:00
|
|
|
const u8 *msg = gossip_store_get(tmpctx, gs, index);
|
2019-06-03 20:09:25 +02:00
|
|
|
assert(fromwire_peektype(msg) == type);
|
|
|
|
#endif
|
2019-06-14 05:41:39 +02:00
|
|
|
|
|
|
|
if (pread(gs->fd, &belen, sizeof(belen), index) != sizeof(belen))
|
2019-04-10 09:31:29 +02:00
|
|
|
status_failed(STATUS_FAIL_INTERNAL_ERROR,
|
2019-06-03 20:09:25 +02:00
|
|
|
"Failed reading len to delete @%u: %s",
|
2019-06-14 05:41:39 +02:00
|
|
|
index, strerror(errno));
|
2019-06-03 20:09:25 +02:00
|
|
|
|
|
|
|
assert((be32_to_cpu(belen) & GOSSIP_STORE_LEN_DELETED_BIT) == 0);
|
|
|
|
belen |= cpu_to_be32(GOSSIP_STORE_LEN_DELETED_BIT);
|
2019-06-14 05:41:39 +02:00
|
|
|
if (pwrite(gs->fd, &belen, sizeof(belen), index) != sizeof(belen))
|
2019-06-03 20:09:25 +02:00
|
|
|
status_failed(STATUS_FAIL_INTERNAL_ERROR,
|
|
|
|
"Failed writing len to delete @%u: %s",
|
2019-06-14 05:41:39 +02:00
|
|
|
index, strerror(errno));
|
2019-06-03 20:20:25 +02:00
|
|
|
gs->deleted++;
|
2019-06-03 20:22:25 +02:00
|
|
|
|
2019-06-14 05:41:39 +02:00
|
|
|
return index + sizeof(struct gossip_hdr)
|
2019-11-04 01:37:02 +01:00
|
|
|
+ (be32_to_cpu(belen) & GOSSIP_STORE_LEN_MASK);
|
2019-06-14 05:41:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void gossip_store_delete(struct gossip_store *gs,
|
|
|
|
struct broadcastable *bcast,
|
|
|
|
int type)
|
|
|
|
{
|
|
|
|
u32 next_index;
|
|
|
|
|
|
|
|
if (!bcast->index)
|
|
|
|
return;
|
|
|
|
|
|
|
|
next_index = delete_by_index(gs, bcast->index, type);
|
|
|
|
|
2019-06-03 20:22:25 +02:00
|
|
|
/* Reset index. */
|
|
|
|
bcast->index = 0;
|
|
|
|
|
2019-06-14 05:41:39 +02:00
|
|
|
/* For a channel_announcement, we need to delete amount too */
|
|
|
|
if (type == WIRE_CHANNEL_ANNOUNCEMENT)
|
|
|
|
delete_by_index(gs, next_index,
|
|
|
|
WIRE_GOSSIP_STORE_CHANNEL_AMOUNT);
|
2018-05-30 20:29:17 +02:00
|
|
|
}
|
2018-04-21 12:17:14 +02:00
|
|
|
|
2020-08-11 07:06:56 +02:00
|
|
|
void gossip_store_mark_channel_deleted(struct gossip_store *gs,
|
|
|
|
const struct short_channel_id *scid)
|
|
|
|
{
|
|
|
|
gossip_store_add(gs, towire_gossip_store_delete_chan(tmpctx, scid),
|
2022-05-04 15:48:59 +02:00
|
|
|
0, false, false, NULL);
|
2020-08-11 07:06:56 +02:00
|
|
|
}
|
|
|
|
|
2019-04-10 09:31:29 +02:00
|
|
|
const u8 *gossip_store_get(const tal_t *ctx,
|
|
|
|
struct gossip_store *gs,
|
|
|
|
u64 offset)
|
|
|
|
{
|
2019-06-03 20:17:25 +02:00
|
|
|
struct gossip_hdr hdr;
|
2019-06-03 20:15:25 +02:00
|
|
|
u32 msglen, checksum;
|
|
|
|
u8 *msg;
|
|
|
|
|
|
|
|
if (offset == 0)
|
|
|
|
status_failed(STATUS_FAIL_INTERNAL_ERROR,
|
|
|
|
"gossip_store: can't access offset %"PRIu64,
|
|
|
|
offset);
|
2019-06-03 20:17:25 +02:00
|
|
|
if (pread(gs->fd, &hdr, sizeof(hdr), offset) != sizeof(hdr)) {
|
2019-06-03 20:15:25 +02:00
|
|
|
status_failed(STATUS_FAIL_INTERNAL_ERROR,
|
|
|
|
"gossip_store: can't read hdr offset %"PRIu64
|
|
|
|
"/%"PRIu64": %s",
|
|
|
|
offset, gs->len, strerror(errno));
|
|
|
|
}
|
|
|
|
|
2019-08-07 15:14:17 +02:00
|
|
|
if (be32_to_cpu(hdr.len) & GOSSIP_STORE_LEN_DELETED_BIT)
|
|
|
|
status_failed(STATUS_FAIL_INTERNAL_ERROR,
|
|
|
|
"gossip_store: get delete entry offset %"PRIu64
|
|
|
|
"/%"PRIu64"",
|
|
|
|
offset, gs->len);
|
|
|
|
|
2019-11-04 01:37:02 +01:00
|
|
|
msglen = (be32_to_cpu(hdr.len) & GOSSIP_STORE_LEN_MASK);
|
2019-06-03 20:17:25 +02:00
|
|
|
checksum = be32_to_cpu(hdr.crc);
|
2019-06-03 20:15:25 +02:00
|
|
|
msg = tal_arr(ctx, u8, msglen);
|
|
|
|
if (pread(gs->fd, msg, msglen, offset + sizeof(hdr)) != msglen)
|
|
|
|
status_failed(STATUS_FAIL_INTERNAL_ERROR,
|
|
|
|
"gossip_store: can't read len %u offset %"PRIu64
|
|
|
|
"/%"PRIu64, msglen, offset, gs->len);
|
|
|
|
|
2019-06-03 20:17:25 +02:00
|
|
|
if (checksum != crc32c(be32_to_cpu(hdr.timestamp), msg, msglen))
|
2019-06-03 20:15:25 +02:00
|
|
|
status_failed(STATUS_FAIL_INTERNAL_ERROR,
|
|
|
|
"gossip_store: bad checksum offset %"PRIu64": %s",
|
|
|
|
offset, tal_hex(tmpctx, msg));
|
|
|
|
|
|
|
|
return msg;
|
2019-04-10 09:31:29 +02:00
|
|
|
}
|
|
|
|
|
2019-06-03 20:05:25 +02:00
|
|
|
const u8 *gossip_store_get_private_update(const tal_t *ctx,
|
|
|
|
struct gossip_store *gs,
|
|
|
|
u64 offset)
|
|
|
|
{
|
|
|
|
const u8 *pmsg = gossip_store_get(tmpctx, gs, offset);
|
|
|
|
u8 *msg;
|
|
|
|
|
|
|
|
if (!fromwire_gossip_store_private_update(ctx, pmsg, &msg))
|
|
|
|
status_failed(STATUS_FAIL_INTERNAL_ERROR,
|
|
|
|
"Failed to decode private update @%"PRIu64": %s",
|
|
|
|
offset, tal_hex(tmpctx, pmsg));
|
|
|
|
return msg;
|
|
|
|
}
|
|
|
|
|
2019-05-04 07:53:13 +02:00
|
|
|
int gossip_store_readonly_fd(struct gossip_store *gs)
|
|
|
|
{
|
2019-06-03 20:15:25 +02:00
|
|
|
int fd = open(GOSSIP_STORE_FILENAME, O_RDONLY);
|
|
|
|
|
|
|
|
/* Skip over version header */
|
|
|
|
if (fd != -1 && lseek(fd, 1, SEEK_SET) != 1) {
|
|
|
|
close_noerr(fd);
|
|
|
|
fd = -1;
|
|
|
|
}
|
|
|
|
return fd;
|
2019-05-04 07:53:13 +02:00
|
|
|
}
|
|
|
|
|
2019-10-08 03:11:24 +02:00
|
|
|
u32 gossip_store_load(struct routing_state *rstate, struct gossip_store *gs)
|
2018-03-13 12:08:03 +01:00
|
|
|
{
|
2019-06-03 20:17:25 +02:00
|
|
|
struct gossip_hdr hdr;
|
2018-05-26 12:44:27 +02:00
|
|
|
u32 msglen, checksum;
|
2019-05-04 07:53:12 +02:00
|
|
|
u8 *msg;
|
2019-02-21 04:45:55 +01:00
|
|
|
struct amount_sat satoshis;
|
2018-04-11 01:03:35 +02:00
|
|
|
const char *bad;
|
2018-09-21 02:31:45 +02:00
|
|
|
size_t stats[] = {0, 0, 0, 0};
|
tools/bench-gossipd.sh: rough benchmark for gossipd and the million channels project
Outputs CSV. We add some stats for load times in developer mode, so we can
easily read them out.
peer_read_all_sec doesn't work, since we seem to reject about half the
updates for having bad signatures. It's also very slow...
routing fails, for unknown reasons, so that failure is ignored in routing_sec.
Results from 5 runs, min-max(mean +/- stddev):
store_load_msec,vsz_kb,store_rewrite_sec,listnodes_sec,listchannels_sec,routing_sec,peer_write_all_sec
39275-44779(40466.8+/-2.2e+03),2899248,41.010000-44.970000(41.972+/-1.5),2.280000-2.350000(2.304+/-0.025),49.770000-63.390000(59.178+/-5),33.310000-34.260000(33.62+/-0.35),42.100000-44.080000(43.082+/-0.67)
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
Header from folded patch 'fixup!_tools-bench-gossipd.sh__rough_benchmark_for_gossipd_and_the_million_channels_project-2.patch':
fixup! tools/bench-gossipd.sh: rough benchmark for gossipd and the million channels project
Suggested-by: @niftynei
Header from folded patch 'fixup!_tools-bench-gossipd.sh__rough_benchmark_for_gossipd_and_the_million_channels_project-1.patch':
fixup! tools/bench-gossipd.sh: rough benchmark for gossipd and the million channels project
MCP filename change.
Header from folded patch 'tools-bench-gossipd.sh__dont_print_csv_by_default.patch':
tools/bench-gossipd.sh: don't print CSV by default.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
Header from folded patch 'fixup!_tools-bench-gossipd.sh__rough_benchmark_for_gossipd_and_the_million_channels_project.patch':
fixup! tools/bench-gossipd.sh: rough benchmark for gossipd and the million channels project
Make shellcheck happy.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2019-04-08 01:52:54 +02:00
|
|
|
struct timeabs start = time_now();
|
2020-10-20 05:59:30 +02:00
|
|
|
u8 *chan_ann = NULL;
|
2019-05-26 06:18:16 +02:00
|
|
|
u64 chan_ann_off = 0; /* Spurious gcc-9 (Ubuntu 9-20190402-1ubuntu1) 9.0.1 20190402 (experimental) warning */
|
2018-04-11 01:03:35 +02:00
|
|
|
|
2019-04-11 07:15:22 +02:00
|
|
|
gs->writable = false;
|
2019-06-03 20:17:25 +02:00
|
|
|
while (pread(gs->fd, &hdr, sizeof(hdr), gs->len) == sizeof(hdr)) {
|
2019-11-04 01:37:02 +01:00
|
|
|
msglen = be32_to_cpu(hdr.len) & GOSSIP_STORE_LEN_MASK;
|
2019-06-03 20:17:25 +02:00
|
|
|
checksum = be32_to_cpu(hdr.crc);
|
2019-04-11 07:15:22 +02:00
|
|
|
msg = tal_arr(tmpctx, u8, msglen);
|
2018-04-11 01:03:35 +02:00
|
|
|
|
2019-04-11 07:15:22 +02:00
|
|
|
if (pread(gs->fd, msg, msglen, gs->len+sizeof(hdr)) != msglen) {
|
2019-06-20 04:58:52 +02:00
|
|
|
bad = "gossip_store: truncated file?";
|
|
|
|
goto corrupt;
|
2018-04-11 01:03:35 +02:00
|
|
|
}
|
|
|
|
|
2019-06-03 20:17:25 +02:00
|
|
|
if (checksum != crc32c(be32_to_cpu(hdr.timestamp), msg, msglen)) {
|
2018-05-26 12:44:27 +02:00
|
|
|
bad = "Checksum verification failed";
|
2019-06-20 04:58:52 +02:00
|
|
|
goto badmsg;
|
2018-05-26 12:44:27 +02:00
|
|
|
}
|
|
|
|
|
2019-06-03 20:09:25 +02:00
|
|
|
/* Skip deleted entries */
|
2019-06-03 20:20:25 +02:00
|
|
|
if (be32_to_cpu(hdr.len) & GOSSIP_STORE_LEN_DELETED_BIT) {
|
2019-06-14 02:32:05 +02:00
|
|
|
/* Count includes deleted! */
|
|
|
|
gs->count++;
|
2019-06-03 20:20:25 +02:00
|
|
|
gs->deleted++;
|
2019-06-03 20:09:25 +02:00
|
|
|
goto next;
|
2019-06-03 20:20:25 +02:00
|
|
|
}
|
2019-06-03 20:09:25 +02:00
|
|
|
|
2019-05-04 07:53:12 +02:00
|
|
|
switch (fromwire_peektype(msg)) {
|
2022-01-24 21:03:52 +01:00
|
|
|
case WIRE_GOSSIP_STORE_PRIVATE_CHANNEL: {
|
|
|
|
u8 *chan_ann;
|
|
|
|
struct amount_sat sat;
|
|
|
|
if (!fromwire_gossip_store_private_channel(msg, msg,
|
|
|
|
&sat,
|
|
|
|
&chan_ann)) {
|
|
|
|
bad = "Bad private_channel";
|
|
|
|
goto badmsg;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!routing_add_private_channel(rstate, NULL,
|
|
|
|
sat, chan_ann,
|
2020-10-20 05:59:30 +02:00
|
|
|
gs->len)) {
|
|
|
|
bad = "Bad add_private_channel";
|
|
|
|
goto badmsg;
|
|
|
|
}
|
|
|
|
stats[0]++;
|
|
|
|
break;
|
2022-01-24 21:03:52 +01:00
|
|
|
}
|
2019-05-04 07:53:12 +02:00
|
|
|
case WIRE_GOSSIP_STORE_CHANNEL_AMOUNT:
|
|
|
|
if (!fromwire_gossip_store_channel_amount(msg,
|
|
|
|
&satoshis)) {
|
|
|
|
bad = "Bad gossip_store_channel_amount";
|
2019-06-20 04:58:52 +02:00
|
|
|
goto badmsg;
|
2019-05-04 07:53:12 +02:00
|
|
|
}
|
2019-06-03 20:09:25 +02:00
|
|
|
/* Previous channel_announcement may have been deleted */
|
|
|
|
if (!chan_ann)
|
|
|
|
break;
|
2018-04-11 01:03:35 +02:00
|
|
|
if (!routing_add_channel_announcement(rstate,
|
2019-05-04 07:53:12 +02:00
|
|
|
take(chan_ann),
|
2019-04-10 09:31:29 +02:00
|
|
|
satoshis,
|
2019-10-08 03:13:24 +02:00
|
|
|
chan_ann_off,
|
|
|
|
NULL)) {
|
2018-04-11 01:03:35 +02:00
|
|
|
bad = "Bad channel_announcement";
|
2019-06-20 04:58:52 +02:00
|
|
|
goto badmsg;
|
2018-04-11 01:03:35 +02:00
|
|
|
}
|
2019-05-04 07:53:12 +02:00
|
|
|
chan_ann = NULL;
|
2018-04-11 01:03:35 +02:00
|
|
|
stats[0]++;
|
2019-05-04 07:53:12 +02:00
|
|
|
break;
|
|
|
|
case WIRE_CHANNEL_ANNOUNCEMENT:
|
|
|
|
if (chan_ann) {
|
|
|
|
bad = "channel_announcement without amount";
|
2019-06-20 04:58:52 +02:00
|
|
|
goto badmsg;
|
2019-05-04 07:53:12 +02:00
|
|
|
}
|
|
|
|
/* Save for channel_amount (next msg) */
|
|
|
|
chan_ann = tal_steal(gs, msg);
|
|
|
|
chan_ann_off = gs->len;
|
|
|
|
break;
|
2019-06-03 20:05:25 +02:00
|
|
|
case WIRE_GOSSIP_STORE_PRIVATE_UPDATE:
|
|
|
|
if (!fromwire_gossip_store_private_update(tmpctx, msg, &msg)) {
|
|
|
|
bad = "invalid gossip_store_private_update";
|
2019-06-20 04:58:52 +02:00
|
|
|
goto badmsg;
|
2019-06-03 20:05:25 +02:00
|
|
|
}
|
|
|
|
/* fall thru */
|
2019-05-04 07:53:12 +02:00
|
|
|
case WIRE_CHANNEL_UPDATE:
|
2019-04-10 09:31:18 +02:00
|
|
|
if (!routing_add_channel_update(rstate,
|
2019-10-08 03:13:24 +02:00
|
|
|
take(msg), gs->len,
|
2022-07-11 00:13:09 +02:00
|
|
|
NULL, false,
|
|
|
|
be32_to_cpu(hdr.len) & GOSSIP_STORE_LEN_RATELIMIT_BIT)) {
|
2018-04-11 01:03:35 +02:00
|
|
|
bad = "Bad channel_update";
|
2019-06-20 04:58:52 +02:00
|
|
|
goto badmsg;
|
2018-04-11 01:03:35 +02:00
|
|
|
}
|
2018-04-11 01:03:35 +02:00
|
|
|
stats[1]++;
|
2019-05-04 07:53:12 +02:00
|
|
|
break;
|
|
|
|
case WIRE_NODE_ANNOUNCEMENT:
|
2019-04-10 09:31:18 +02:00
|
|
|
if (!routing_add_node_announcement(rstate,
|
2019-10-08 03:13:24 +02:00
|
|
|
take(msg), gs->len,
|
2022-07-11 00:13:09 +02:00
|
|
|
NULL, NULL,
|
|
|
|
be32_to_cpu(hdr.len) & GOSSIP_STORE_LEN_RATELIMIT_BIT)) {
|
2018-09-21 02:31:45 +02:00
|
|
|
bad = "Bad node_announcement";
|
2019-06-20 04:58:52 +02:00
|
|
|
goto badmsg;
|
2018-09-21 02:31:45 +02:00
|
|
|
}
|
|
|
|
stats[2]++;
|
2019-05-04 07:53:12 +02:00
|
|
|
break;
|
|
|
|
default:
|
2018-04-11 01:03:35 +02:00
|
|
|
bad = "Unknown message";
|
2019-06-20 04:58:52 +02:00
|
|
|
goto badmsg;
|
2018-04-11 01:03:35 +02:00
|
|
|
}
|
2019-05-04 07:53:12 +02:00
|
|
|
|
2019-06-03 20:20:25 +02:00
|
|
|
gs->count++;
|
2019-06-03 20:09:25 +02:00
|
|
|
next:
|
|
|
|
gs->len += sizeof(hdr) + msglen;
|
2019-04-11 07:15:22 +02:00
|
|
|
clean_tmpctx();
|
2018-03-23 17:34:11 +01:00
|
|
|
}
|
2019-06-12 01:25:07 +02:00
|
|
|
|
2019-06-20 04:57:52 +02:00
|
|
|
if (chan_ann) {
|
2019-06-20 04:58:52 +02:00
|
|
|
bad = "dangling channel_announcement";
|
|
|
|
goto corrupt;
|
2019-06-20 04:57:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bad = unfinalized_entries(tmpctx, rstate);
|
2019-06-20 04:58:52 +02:00
|
|
|
if (bad)
|
|
|
|
goto corrupt;
|
2019-06-20 04:57:52 +02:00
|
|
|
|
2018-04-11 01:03:35 +02:00
|
|
|
goto out;
|
2018-04-11 01:03:35 +02:00
|
|
|
|
2019-06-20 04:58:52 +02:00
|
|
|
badmsg:
|
|
|
|
bad = tal_fmt(tmpctx, "%s (%s)", bad, tal_hex(tmpctx, msg));
|
|
|
|
|
|
|
|
corrupt:
|
|
|
|
status_broken("gossip_store: %s. Moving to %s.corrupt and truncating",
|
|
|
|
bad, GOSSIP_STORE_FILENAME);
|
2019-06-20 04:57:52 +02:00
|
|
|
|
|
|
|
/* FIXME: Debug partial truncate case. */
|
2019-06-20 04:58:52 +02:00
|
|
|
rename(GOSSIP_STORE_FILENAME, GOSSIP_STORE_FILENAME ".corrupt");
|
|
|
|
close(gs->fd);
|
2019-07-30 03:10:09 +02:00
|
|
|
gs->fd = open(GOSSIP_STORE_FILENAME, O_RDWR|O_TRUNC|O_CREAT, 0600);
|
2019-06-20 04:58:52 +02:00
|
|
|
if (gs->fd < 0 || !write_all(gs->fd, &gs->version, sizeof(gs->version)))
|
2018-04-11 01:03:35 +02:00
|
|
|
status_failed(STATUS_FAIL_INTERNAL_ERROR,
|
2019-06-20 04:58:52 +02:00
|
|
|
"Truncating new store file: %s", strerror(errno));
|
2019-06-20 04:57:52 +02:00
|
|
|
remove_all_gossip(rstate);
|
|
|
|
gs->count = gs->deleted = 0;
|
|
|
|
gs->len = 1;
|
2019-10-08 03:11:24 +02:00
|
|
|
gs->timestamp = 0;
|
2018-04-11 01:03:35 +02:00
|
|
|
out:
|
2019-06-03 20:25:25 +02:00
|
|
|
gs->writable = true;
|
2019-09-08 18:39:26 +02:00
|
|
|
status_debug("total store load time: %"PRIu64" msec",
|
tools/bench-gossipd.sh: rough benchmark for gossipd and the million channels project
Outputs CSV. We add some stats for load times in developer mode, so we can
easily read them out.
peer_read_all_sec doesn't work, since we seem to reject about half the
updates for having bad signatures. It's also very slow...
routing fails, for unknown reasons, so that failure is ignored in routing_sec.
Results from 5 runs, min-max(mean +/- stddev):
store_load_msec,vsz_kb,store_rewrite_sec,listnodes_sec,listchannels_sec,routing_sec,peer_write_all_sec
39275-44779(40466.8+/-2.2e+03),2899248,41.010000-44.970000(41.972+/-1.5),2.280000-2.350000(2.304+/-0.025),49.770000-63.390000(59.178+/-5),33.310000-34.260000(33.62+/-0.35),42.100000-44.080000(43.082+/-0.67)
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
Header from folded patch 'fixup!_tools-bench-gossipd.sh__rough_benchmark_for_gossipd_and_the_million_channels_project-2.patch':
fixup! tools/bench-gossipd.sh: rough benchmark for gossipd and the million channels project
Suggested-by: @niftynei
Header from folded patch 'fixup!_tools-bench-gossipd.sh__rough_benchmark_for_gossipd_and_the_million_channels_project-1.patch':
fixup! tools/bench-gossipd.sh: rough benchmark for gossipd and the million channels project
MCP filename change.
Header from folded patch 'tools-bench-gossipd.sh__dont_print_csv_by_default.patch':
tools/bench-gossipd.sh: don't print CSV by default.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
Header from folded patch 'fixup!_tools-bench-gossipd.sh__rough_benchmark_for_gossipd_and_the_million_channels_project.patch':
fixup! tools/bench-gossipd.sh: rough benchmark for gossipd and the million channels project
Make shellcheck happy.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2019-04-08 01:52:54 +02:00
|
|
|
time_to_msec(time_between(time_now(), start)));
|
2019-09-08 18:39:26 +02:00
|
|
|
status_debug("gossip_store: Read %zu/%zu/%zu/%zu cannounce/cupdate/nannounce/cdelete from store (%zu deleted) in %"PRIu64" bytes",
|
2019-06-03 20:20:25 +02:00
|
|
|
stats[0], stats[1], stats[2], stats[3], gs->deleted,
|
2019-04-10 09:31:29 +02:00
|
|
|
gs->len);
|
2019-06-12 01:25:07 +02:00
|
|
|
|
2019-10-08 03:11:24 +02:00
|
|
|
return gs->timestamp;
|
2018-03-13 12:08:03 +01:00
|
|
|
}
|