gossipd: neaten insert_broadcast a little.

Suggested-by: @cdecker.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
Rusty Russell 2019-05-17 05:25:17 +09:30
parent 3d2af2237c
commit 85d8848ede
5 changed files with 21 additions and 39 deletions

View File

@ -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",

View File

@ -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. */

View File

@ -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));

View File

@ -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

View File

@ -14,6 +14,7 @@
#include <common/wire_error.h>
#include <common/wireaddr.h>
#include <gossipd/gen_gossip_peerd_wire.h>
#include <gossipd/gen_gossip_store.h>
#include <gossipd/gen_gossip_wire.h>
#include <inttypes.h>
#include <wire/gen_peer_wire.h>
@ -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);
}