Browse Source

gossipd: neaten insert_broadcast a little.

Suggested-by: @cdecker.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
htlc_accepted_hook
Rusty Russell 6 years ago
parent
commit
85d8848ede
  1. 4
      gossipd/broadcast.c
  2. 5
      gossipd/broadcast.h
  3. 41
      gossipd/gossip_store.c
  4. 5
      gossipd/gossip_store.h
  5. 5
      gossipd/routing.c

4
gossipd/broadcast.c

@ -63,7 +63,7 @@ void insert_broadcast_nostore(struct broadcast_state *bstate,
void insert_broadcast(struct broadcast_state **bstate, void insert_broadcast(struct broadcast_state **bstate,
const u8 *msg, const u8 *msg,
const struct amount_sat *channel_announce_sat, const u8 *addendum,
struct broadcastable *bcast) struct broadcastable *bcast)
{ {
u32 offset; u32 offset;
@ -73,7 +73,7 @@ void insert_broadcast(struct broadcast_state **bstate,
u64 idx; u64 idx;
bcast->index = idx = gossip_store_add((*bstate)->gs, msg, bcast->index = idx = gossip_store_add((*bstate)->gs, msg,
channel_announce_sat); addendum);
if (!idx) if (!idx)
status_failed(STATUS_FAIL_INTERNAL_ERROR, status_failed(STATUS_FAIL_INTERNAL_ERROR,
"Could not add to gossip store: %s", "Could not add to gossip store: %s",

5
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. /* Append a queued message for broadcast. Must be explicitly deleted.
* Also adds it to the gossip store. * 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, void insert_broadcast(struct broadcast_state **bstate,
const u8 *msg, const u8 *msg,
const struct amount_sat *channel_announce_sat, const u8 *addendum,
struct broadcastable *bcast); struct broadcastable *bcast);
/* Add to broadcast, but not store: for gossip store compaction. */ /* Add to broadcast, but not store: for gossip store compaction. */

41
gossipd/gossip_store.c

@ -201,33 +201,6 @@ struct gossip_store *gossip_store_new(struct routing_state *rstate)
return gs; 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 /* Copy a whole message from one gossip_store to another. Returns
* total msg length including header, or 0 on error. */ * total msg length including header, or 0 on error. */
static size_t copy_message(int in_fd, int out_fd, unsigned offset) 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, msg = towire_gossipd_local_add_channel(tmpctx, &c->scid,
&peer->id, c->sat); &peer->id, c->sat);
if (!gossip_store_append(out_fd, msg, NULL, len)) if (!append_msg(out_fd, msg, len))
return false; return false;
for (size_t i = 0; i < 2; i++) { 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, 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; u64 off = gs->len;
/* Should never get here during loading! */ /* Should never get here during loading! */
assert(gs->writable); assert(gs->writable);
if (!gossip_store_append(gs->fd, gossip_msg, channel_announce_sat, if (!append_msg(gs->fd, gossip_msg, &gs->len)) {
&gs->len)) {
status_broken("Failed writing to gossip store: %s", status_broken("Failed writing to gossip store: %s",
strerror(errno)); strerror(errno));
return 0; 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++; gs->count++;
return off; return off;
@ -521,7 +498,7 @@ void gossip_store_add_channel_delete(struct gossip_store *gs,
/* Should never get here during loading! */ /* Should never get here during loading! */
assert(gs->writable); 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, status_failed(STATUS_FAIL_INTERNAL_ERROR,
"Failed writing channel_delete to gossip store: %s", "Failed writing channel_delete to gossip store: %s",
strerror(errno)); strerror(errno));

5
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); 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, 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 * Remember that we deleted a channel as a result of its outpoint being spent

5
gossipd/routing.c

@ -14,6 +14,7 @@
#include <common/wire_error.h> #include <common/wire_error.h>
#include <common/wireaddr.h> #include <common/wireaddr.h>
#include <gossipd/gen_gossip_peerd_wire.h> #include <gossipd/gen_gossip_peerd_wire.h>
#include <gossipd/gen_gossip_store.h>
#include <gossipd/gen_gossip_wire.h> #include <gossipd/gen_gossip_wire.h>
#include <inttypes.h> #include <inttypes.h>
#include <wire/gen_peer_wire.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 timestamp,
u32 index) u32 index)
{ {
u8 *addendum = towire_gossip_store_channel_amount(tmpctx, chan->sat);
chan->bcast.timestamp = timestamp; chan->bcast.timestamp = timestamp;
/* 0, unless we're loading from store */ /* 0, unless we're loading from store */
chan->bcast.index = index; chan->bcast.index = index;
insert_broadcast(&rstate->broadcasts, channel_announce, &chan->sat, insert_broadcast(&rstate->broadcasts, channel_announce, addendum,
&chan->bcast); &chan->bcast);
rstate->local_channel_announced |= is_local_channel(rstate, chan); rstate->local_channel_announced |= is_local_channel(rstate, chan);
} }

Loading…
Cancel
Save