gossipd: clean up gossip_store routines.

We don't use the dying flag, and we can manually append the
addendum rather than having gossip_store_add present a
bizarre interface.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
Rusty Russell 2024-01-31 16:14:14 +10:30
parent f7b7cf3719
commit 7efa0a46a9
3 changed files with 21 additions and 44 deletions

View file

@ -64,8 +64,7 @@ static ssize_t gossip_pwritev(int fd, const struct iovec *iov, int iovcnt,
}
#endif /* !HAVE_PWRITEV */
static bool append_msg(int fd, const u8 *msg, u32 timestamp,
bool dying, u64 *len)
static bool append_msg(int fd, const u8 *msg, u32 timestamp, u64 *len)
{
struct gossip_hdr hdr;
u32 msglen;
@ -77,8 +76,6 @@ static bool append_msg(int fd, const u8 *msg, u32 timestamp,
msglen = tal_count(msg);
hdr.len = cpu_to_be16(msglen);
hdr.flags = 0;
if (dying)
hdr.flags |= CPU_TO_BE16(GOSSIP_STORE_DYING_BIT);
hdr.crc = cpu_to_be32(crc32c(timestamp, msg, msglen));
hdr.timestamp = cpu_to_be32(timestamp);
@ -310,7 +307,7 @@ static u32 gossip_store_compact_offline(struct daemon *daemon)
oldlen = lseek(old_fd, SEEK_END, 0);
newlen = lseek(new_fd, SEEK_END, 0);
append_msg(old_fd, towire_gossip_store_ended(tmpctx, newlen),
0, false, &oldlen);
0, &oldlen);
close(old_fd);
status_debug("gossip_store_compact_offline: %zu deleted, %zu copied",
deleted, count);
@ -367,25 +364,18 @@ struct gossip_store *gossip_store_new(struct daemon *daemon)
return gs;
}
u64 gossip_store_add(struct gossip_store *gs, const u8 *gossip_msg,
u32 timestamp,
bool dying, const u8 *addendum)
u64 gossip_store_add(struct gossip_store *gs, const u8 *gossip_msg, u32 timestamp)
{
u64 off = gs->len;
/* Should never get here during loading! */
assert(gs->writable);
if (!append_msg(gs->fd, gossip_msg, timestamp, dying, &gs->len)) {
if (!append_msg(gs->fd, gossip_msg, timestamp, &gs->len)) {
status_broken("Failed writing to gossip store: %s",
strerror(errno));
return 0;
}
if (addendum && !append_msg(gs->fd, addendum, 0, false, &gs->len)) {
status_broken("Failed writing addendum to gossip store: %s",
strerror(errno));
return 0;
}
return off;
}
@ -514,13 +504,6 @@ void gossip_store_flag(struct gossip_store *gs,
flag_by_index(gs, offset - sizeof(struct gossip_hdr), flag, type);
}
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),
0, false, NULL);
}
u32 gossip_store_get_timestamp(struct gossip_store *gs, u64 offset)
{
struct gossip_hdr hdr;

View file

@ -29,17 +29,14 @@ struct gossip_store *gossip_store_new(struct daemon *daemon);
u32 gossip_store_load(struct gossip_store *gs);
/**
* Add a gossip message to the gossip_store (and optional addendum)
* 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.
* @dying: true if this message is for a dying channel.
* @addendum: another message to append immediately after this
* (for appending amounts to channel_announcements for internal use).
*/
u64 gossip_store_add(struct gossip_store *gs, const u8 *gossip_msg,
u32 timestamp, bool dying,
const u8 *addendum);
u64 gossip_store_add(struct gossip_store *gs,
const u8 *gossip_msg,
u32 timestamp);
/**
@ -63,13 +60,6 @@ void gossip_store_flag(struct gossip_store *gs,
u16 flag,
int type);
/**
* Mark that the channel is about to be deleted, for convenience of
* others mapping the gossip_store.
*/
void gossip_store_mark_channel_deleted(struct gossip_store *gs,
const struct short_channel_id *scid);
/**
* Direct store accessor: get timestamp header for a record.
*

View file

@ -246,7 +246,9 @@ static void remove_channel(struct gossmap_manage *gm,
tal_free(map_del(&gm->early_ann_map, scid));
/* Put in tombstone marker. */
gossip_store_mark_channel_deleted(gm->daemon->gs, &scid);
gossip_store_add(gm->daemon->gs,
towire_gossip_store_delete_chan(tmpctx, &scid),
0);
/* Delete from store */
gossip_store_del(gm->daemon->gs, chan->cann_off, WIRE_CHANNEL_ANNOUNCEMENT);
@ -284,7 +286,7 @@ static void remove_channel(struct gossmap_manage *gm,
nannounce = gossmap_node_get_announce(tmpctx, gossmap, node);
timestamp = gossip_store_get_timestamp(gm->daemon->gs, node->nann_off);
gossip_store_del(gm->daemon->gs, node->nann_off, WIRE_NODE_ANNOUNCEMENT);
gossip_store_add(gm->daemon->gs, nannounce, timestamp, false, NULL);
gossip_store_add(gm->daemon->gs, nannounce, timestamp);
}
}
@ -508,8 +510,9 @@ const char *gossmap_manage_channel_announcement(const tal_t *ctx,
&& !map_get(&gm->pending_ann_map, scid)
&& !map_get(&gm->early_ann_map, scid)) {
/* Set with timestamp 0 (we will update once we have a channel_update) */
gossip_store_add(gm->daemon->gs, announce, 0, false,
towire_gossip_store_channel_amount(tmpctx, *known_amount));
gossip_store_add(gm->daemon->gs, announce, 0);
gossip_store_add(gm->daemon->gs,
towire_gossip_store_channel_amount(tmpctx, *known_amount), 0);
tal_free(pca);
return NULL;
}
@ -603,8 +606,9 @@ void gossmap_manage_handle_get_txout_reply(struct gossmap_manage *gm, const u8 *
}
/* Set with timestamp 0 (we will update once we have a channel_update) */
gossip_store_add(gm->daemon->gs, pca->channel_announcement, 0, false,
towire_gossip_store_channel_amount(tmpctx, sat));
gossip_store_add(gm->daemon->gs, pca->channel_announcement, 0);
gossip_store_add(gm->daemon->gs,
towire_gossip_store_channel_amount(tmpctx, sat), 0);
tal_free(pca);
/* If we looking specifically for this, we no longer are. */
@ -692,7 +696,7 @@ static const char *process_channel_update(const tal_t *ctx,
}
/* OK, apply the new one */
gossip_store_add(gm->daemon->gs, update, timestamp, false, NULL);
gossip_store_add(gm->daemon->gs, update, timestamp);
/* Now delete old */
if (gossmap_chan_set(chan, dir))
@ -833,7 +837,7 @@ static void process_node_announcement(struct gossmap_manage *gm,
}
/* OK, apply the new one */
gossip_store_add(gm->daemon->gs, nannounce, timestamp, false, NULL);
gossip_store_add(gm->daemon->gs, nannounce, timestamp);
/* Now delete old */
if (gossmap_node_announced(node))
@ -1152,7 +1156,7 @@ void gossmap_manage_channel_spent(struct gossmap_manage *gm,
/* Save to gossip_store in case we restart */
msg = towire_gossip_store_chan_dying(tmpctx, &scid, deadline);
off = gossip_store_add(gm->daemon->gs, msg, 0, false, NULL);
off = gossip_store_add(gm->daemon->gs, msg, 0);
gossmap_manage_channel_dying(gm, off, deadline, scid);
/* Mark it dying, so we don't gossip it */