From 85d8848ede35cb05dab114cd4a1d6ca5851afb4b Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Fri, 17 May 2019 05:25:17 +0930 Subject: [PATCH] gossipd: neaten insert_broadcast a little. Suggested-by: @cdecker. Signed-off-by: Rusty Russell --- gossipd/broadcast.c | 4 ++-- gossipd/broadcast.h | 5 +++-- gossipd/gossip_store.c | 41 +++++++++-------------------------------- gossipd/gossip_store.h | 5 +++-- gossipd/routing.c | 5 ++++- 5 files changed, 21 insertions(+), 39 deletions(-) diff --git a/gossipd/broadcast.c b/gossipd/broadcast.c index 3e769dbeb..edf080168 100644 --- a/gossipd/broadcast.c +++ b/gossipd/broadcast.c @@ -63,7 +63,7 @@ void insert_broadcast_nostore(struct broadcast_state *bstate, void insert_broadcast(struct broadcast_state **bstate, const u8 *msg, - const struct amount_sat *channel_announce_sat, + const u8 *addendum, struct broadcastable *bcast) { u32 offset; @@ -73,7 +73,7 @@ void insert_broadcast(struct broadcast_state **bstate, u64 idx; bcast->index = idx = gossip_store_add((*bstate)->gs, msg, - channel_announce_sat); + addendum); if (!idx) status_failed(STATUS_FAIL_INTERNAL_ERROR, "Could not add to gossip store: %s", diff --git a/gossipd/broadcast.h b/gossipd/broadcast.h index e36e0363a..167821a03 100644 --- a/gossipd/broadcast.h +++ b/gossipd/broadcast.h @@ -39,11 +39,12 @@ struct broadcast_state *new_broadcast_state(struct routing_state *rstate, /* Append a queued message for broadcast. Must be explicitly deleted. * Also adds it to the gossip store. * - * If it's a channel_announcement, channel_announce_sat must be set. + * If it's a channel_announcement, caller sets addendum to the + * WIRE_GOSSIP_STORE_CHANNEL_AMOUNT to immediately follow the announcement. */ void insert_broadcast(struct broadcast_state **bstate, const u8 *msg, - const struct amount_sat *channel_announce_sat, + const u8 *addendum, struct broadcastable *bcast); /* Add to broadcast, but not store: for gossip store compaction. */ diff --git a/gossipd/gossip_store.c b/gossipd/gossip_store.c index c585a123b..16da5b339 100644 --- a/gossipd/gossip_store.c +++ b/gossipd/gossip_store.c @@ -201,33 +201,6 @@ struct gossip_store *gossip_store_new(struct routing_state *rstate) return gs; } -/** - * Wrap the raw gossip message and write it to fd - * - * @param fd File descriptor to write the wrapped message into - * @param gossip_msg The message to write - * @param channel_announce_sat Amount iff gossip_msg is a channel_announcement - * @param lenp The length to increase by amount written. - * @return true if the message was written - */ -static bool gossip_store_append(int fd, - const u8 *gossip_msg, - const struct amount_sat *channel_announce_sat, - u64 *lenp) -{ - if (!append_msg(fd, gossip_msg, lenp)) - return false; - - if (fromwire_peektype(gossip_msg) == WIRE_CHANNEL_ANNOUNCEMENT) { - /* This gives the channel amount. */ - u8 *msg = towire_gossip_store_channel_amount(tmpctx, - *channel_announce_sat); - if (!append_msg(fd, msg, lenp)) - return false; - } - return true; -} - /* Copy a whole message from one gossip_store to another. Returns * total msg length including header, or 0 on error. */ static size_t copy_message(int in_fd, int out_fd, unsigned offset) @@ -294,7 +267,7 @@ static bool add_local_unnannounced(int in_fd, int out_fd, msg = towire_gossipd_local_add_channel(tmpctx, &c->scid, &peer->id, c->sat); - if (!gossip_store_append(out_fd, msg, NULL, len)) + if (!append_msg(out_fd, msg, len)) return false; for (size_t i = 0; i < 2; i++) { @@ -495,19 +468,23 @@ bool gossip_store_maybe_compact(struct gossip_store *gs, } u64 gossip_store_add(struct gossip_store *gs, const u8 *gossip_msg, - const struct amount_sat *channel_announce_sat) + const u8 *addendum) { u64 off = gs->len; /* Should never get here during loading! */ assert(gs->writable); - if (!gossip_store_append(gs->fd, gossip_msg, channel_announce_sat, - &gs->len)) { + if (!append_msg(gs->fd, gossip_msg, &gs->len)) { status_broken("Failed writing to gossip store: %s", strerror(errno)); return 0; } + if (addendum && !append_msg(gs->fd, addendum, &gs->len)) { + status_broken("Failed writing addendum to gossip store: %s", + strerror(errno)); + return 0; + } gs->count++; return off; @@ -521,7 +498,7 @@ void gossip_store_add_channel_delete(struct gossip_store *gs, /* Should never get here during loading! */ assert(gs->writable); - if (!gossip_store_append(gs->fd, msg, NULL, &gs->len)) + if (!append_msg(gs->fd, msg, &gs->len)) status_failed(STATUS_FAIL_INTERNAL_ERROR, "Failed writing channel_delete to gossip store: %s", strerror(errno)); diff --git a/gossipd/gossip_store.h b/gossipd/gossip_store.h index 6cebedbed..317bd35e8 100644 --- a/gossipd/gossip_store.h +++ b/gossipd/gossip_store.h @@ -27,10 +27,11 @@ struct gossip_store *gossip_store_new(struct routing_state *rstate); void gossip_store_load(struct routing_state *rstate, struct gossip_store *gs); /** - * Add a gossip message to the gossip_store + * Add a gossip message to the gossip_store (and optional addendum) */ u64 gossip_store_add(struct gossip_store *gs, const u8 *gossip_msg, - const struct amount_sat *channel_announce_sat); + const u8 *addendum); + /** * Remember that we deleted a channel as a result of its outpoint being spent diff --git a/gossipd/routing.c b/gossipd/routing.c index 45be51af2..cd56a9f68 100644 --- a/gossipd/routing.c +++ b/gossipd/routing.c @@ -14,6 +14,7 @@ #include #include #include +#include #include #include #include @@ -1334,10 +1335,12 @@ static void add_channel_announce_to_broadcast(struct routing_state *rstate, u32 timestamp, u32 index) { + u8 *addendum = towire_gossip_store_channel_amount(tmpctx, chan->sat); + chan->bcast.timestamp = timestamp; /* 0, unless we're loading from store */ chan->bcast.index = index; - insert_broadcast(&rstate->broadcasts, channel_announce, &chan->sat, + insert_broadcast(&rstate->broadcasts, channel_announce, addendum, &chan->bcast); rstate->local_channel_announced |= is_local_channel(rstate, chan); }