diff --git a/gossipd/broadcast.c b/gossipd/broadcast.c index 624ce2431..650f7a609 100644 --- a/gossipd/broadcast.c +++ b/gossipd/broadcast.c @@ -63,6 +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, struct broadcastable *bcast) { u32 offset; @@ -71,7 +72,8 @@ void insert_broadcast(struct broadcast_state **bstate, if (!bcast->index) { u64 idx; - bcast->index = idx = gossip_store_add((*bstate)->gs, msg); + bcast->index = idx = gossip_store_add((*bstate)->gs, msg, + channel_announce_sat); 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 b4c0b4830..5e9d6cd54 100644 --- a/gossipd/broadcast.h +++ b/gossipd/broadcast.h @@ -37,9 +37,13 @@ struct broadcast_state *new_broadcast_state(struct routing_state *rstate, struct list_head *peers); /* 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. + */ void insert_broadcast(struct broadcast_state **bstate, const u8 *msg, + const struct amount_sat *channel_announce_sat, 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 b7d6210a4..57f94f4a8 100644 --- a/gossipd/gossip_store.c +++ b/gossipd/gossip_store.c @@ -200,47 +200,18 @@ struct gossip_store *gossip_store_new(struct routing_state *rstate) return gs; } -static u8 *make_store_channel_amount(const tal_t *ctx, - struct routing_state *rstate, - const u8 *gossip_msg) -{ - secp256k1_ecdsa_signature node_signature_1, node_signature_2; - secp256k1_ecdsa_signature bitcoin_signature_1, bitcoin_signature_2; - u8 *features; - struct bitcoin_blkid chain_hash; - struct short_channel_id scid; - struct node_id node_id_1; - struct node_id node_id_2; - struct pubkey bitcoin_key_1; - struct pubkey bitcoin_key_2; - - /* Which channel are we talking about here? */ - if (!fromwire_channel_announcement( - tmpctx, gossip_msg, &node_signature_1, &node_signature_2, - &bitcoin_signature_1, &bitcoin_signature_2, &features, - &chain_hash, &scid, &node_id_1, &node_id_2, &bitcoin_key_1, - &bitcoin_key_2)) - status_failed(STATUS_FAIL_INTERNAL_ERROR, - "Error parsing channel_announcement"); - - struct chan *chan = get_channel(rstate, &scid); - assert(chan && amount_sat_greater(chan->sat, AMOUNT_SAT(0))); - - return towire_gossip_store_channel_amount(ctx, chan->sat); -} - /** * Wrap the raw gossip message and write it to fd * * @param fd File descriptor to write the wrapped message into - * @param rstate Routing state if we need to look up channel capacity * @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, - struct routing_state *rstate, const u8 *gossip_msg, + const struct amount_sat *channel_announce_sat, u64 *lenp) { if (!append_msg(fd, gossip_msg, lenp)) @@ -248,7 +219,8 @@ static bool gossip_store_append(int fd, if (fromwire_peektype(gossip_msg) == WIRE_CHANNEL_ANNOUNCEMENT) { /* This gives the channel amount. */ - u8 *msg = make_store_channel_amount(tmpctx, rstate, gossip_msg); + u8 *msg = towire_gossip_store_channel_amount(tmpctx, + *channel_announce_sat); if (!append_msg(fd, msg, lenp)) return false; } @@ -305,7 +277,6 @@ static size_t copy_message(int in_fd, int out_fd, unsigned offset) * the broadcast map count. */ static bool add_local_unnannounced(int in_fd, int out_fd, - struct routing_state *rstate, struct node *self, u64 *len) { @@ -322,7 +293,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, rstate, msg, len)) + if (!gossip_store_append(out_fd, msg, NULL, len)) return false; for (size_t i = 0; i < 2; i++) { @@ -469,8 +440,7 @@ bool gossip_store_compact(struct gossip_store *gs, /* Local unannounced channels are not in the store! */ self = get_node(gs->rstate, &gs->rstate->local_id); - if (self && !add_local_unnannounced(gs->fd, fd, gs->rstate, self, - &len)) { + if (self && !add_local_unnannounced(gs->fd, fd, self, &len)) { status_broken("Failed writing unannounced to gossip store: %s", strerror(errno)); goto unlink_disable; @@ -523,14 +493,16 @@ void gossip_store_maybe_compact(struct gossip_store *gs, gossip_store_compact(gs, bs, offset); } -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) { u64 off = gs->len; /* Should never get here during loading! */ assert(gs->writable); - if (!gossip_store_append(gs->fd, gs->rstate, gossip_msg, &gs->len)) { + if (!gossip_store_append(gs->fd, gossip_msg, channel_announce_sat, + &gs->len)) { status_broken("Failed writing to gossip store: %s", strerror(errno)); return 0; @@ -548,7 +520,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, gs->rstate, msg, &gs->len)) + if (!gossip_store_append(gs->fd, msg, NULL, &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 b2e0cc59a..e4fcc4d6c 100644 --- a/gossipd/gossip_store.h +++ b/gossipd/gossip_store.h @@ -30,7 +30,8 @@ void gossip_store_load(struct routing_state *rstate, struct gossip_store *gs); /** * Add a gossip message to the gossip_store */ -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); /** * Remember that we deleted a channel as a result of its outpoint being spent diff --git a/gossipd/gossipd.c b/gossipd/gossipd.c index edd25a8c5..856e2d030 100644 --- a/gossipd/gossipd.c +++ b/gossipd/gossipd.c @@ -1661,7 +1661,7 @@ static struct io_plan *peer_msg_in(struct io_conn *conn, ok = handle_local_add_channel(peer->daemon->rstate, msg); if (ok) gossip_store_add(peer->daemon->rstate->broadcasts->gs, - msg); + msg, NULL); goto handled_cmd; case WIRE_GOSSIPD_LOCAL_CHANNEL_UPDATE: ok = handle_local_channel_update(peer, msg); diff --git a/gossipd/routing.c b/gossipd/routing.c index 0870ea089..45be51af2 100644 --- a/gossipd/routing.c +++ b/gossipd/routing.c @@ -340,7 +340,8 @@ static void remove_chan_from_node(struct routing_state *rstate, * channel_announce, but we don't care that much about spurious * retransmissions in this corner case */ broadcast_del(rstate->broadcasts, &node->bcast); - insert_broadcast(&rstate->broadcasts, announce, &node->bcast); + insert_broadcast(&rstate->broadcasts, announce, NULL, + &node->bcast); } } @@ -1336,7 +1337,8 @@ static void add_channel_announce_to_broadcast(struct routing_state *rstate, chan->bcast.timestamp = timestamp; /* 0, unless we're loading from store */ chan->bcast.index = index; - insert_broadcast(&rstate->broadcasts, channel_announce, &chan->bcast); + insert_broadcast(&rstate->broadcasts, channel_announce, &chan->sat, + &chan->bcast); rstate->local_channel_announced |= is_local_channel(rstate, chan); } @@ -1843,7 +1845,7 @@ bool routing_add_channel_update(struct routing_state *rstate, assert(is_local_channel(rstate, chan)); if (!index) { hc->bcast.index = gossip_store_add(rstate->broadcasts->gs, - update); + update, NULL); } else hc->bcast.index = index; return true; @@ -1853,7 +1855,7 @@ bool routing_add_channel_update(struct routing_state *rstate, chan->half[direction].bcast.index = index; insert_broadcast(&rstate->broadcasts, - update, + update, NULL, &chan->half[direction].bcast); if (uc) { @@ -2061,7 +2063,7 @@ bool routing_add_node_announcement(struct routing_state *rstate, node->bcast.timestamp = timestamp; node->bcast.index = index; - insert_broadcast(&rstate->broadcasts, msg, &node->bcast); + insert_broadcast(&rstate->broadcasts, msg, NULL, &node->bcast); return true; }