From 1f443df428fa78f3942ecee33e0593261f0c5053 Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Wed, 14 Mar 2018 02:10:55 +1030 Subject: [PATCH] gossipd: use the broadcast structure to hold gossip messages. We currently keep two copies; one in the broadcast structure to send in order, and one in the routing information. Since we already keep the broadcast index in the routing information, use that. Conveniently, a zero index is the same as the old NULL test. Rename struct node's announcement_idx to node_announce_msgidx to make it match the other users. Signed-off-by: Rusty Russell --- gossipd/broadcast.c | 10 ++++++++++ gossipd/broadcast.h | 1 + gossipd/gossip.c | 33 +++++++++++++++++++++++---------- gossipd/routing.c | 21 +++++---------------- gossipd/routing.h | 14 ++++---------- 5 files changed, 43 insertions(+), 36 deletions(-) diff --git a/gossipd/broadcast.c b/gossipd/broadcast.c index 41e611ff5..91c449eba 100644 --- a/gossipd/broadcast.c +++ b/gossipd/broadcast.c @@ -53,3 +53,13 @@ struct queued_message *next_broadcast_message(struct broadcast_state *bstate, u6 { return uintmap_after(&bstate->broadcasts, &last_index); } + +const u8 *get_broadcast(struct broadcast_state *bstate, u64 msgidx) +{ + struct queued_message *m; + + m = uintmap_get(&bstate->broadcasts, msgidx); + if (m) + return m->payload; + return NULL; +} diff --git a/gossipd/broadcast.h b/gossipd/broadcast.h index 08c61eee4..ef00f456f 100644 --- a/gossipd/broadcast.h +++ b/gossipd/broadcast.h @@ -36,4 +36,5 @@ bool replace_broadcast(const tal_t *ctx, struct queued_message *next_broadcast_message(struct broadcast_state *bstate, u64 last_index); +const u8 *get_broadcast(struct broadcast_state *bstate, u64 msgidx); #endif /* LIGHTNING_LIGHTNINGD_GOSSIP_BROADCAST_H */ diff --git a/gossipd/gossip.c b/gossipd/gossip.c index 21b5b1710..1961b42f5 100644 --- a/gossipd/gossip.c +++ b/gossipd/gossip.c @@ -734,6 +734,7 @@ static void handle_get_update(struct peer *peer, const u8 *msg) struct short_channel_id scid; struct chan *chan; const u8 *update; + struct routing_state *rstate = peer->daemon->rstate; if (!fromwire_gossip_get_update(msg, &scid)) { status_trace("peer %s sent bad gossip_get_update %s", @@ -742,7 +743,7 @@ static void handle_get_update(struct peer *peer, const u8 *msg) return; } - chan = get_channel(peer->daemon->rstate, &scid); + chan = get_channel(rstate, &scid); if (!chan) { status_unusual("peer %s scid %s: unknown channel", type_to_string(trc, struct pubkey, &peer->id), @@ -752,9 +753,13 @@ static void handle_get_update(struct peer *peer, const u8 *msg) } else { /* We want update that comes from our end. */ if (pubkey_eq(&chan->nodes[0]->id, &peer->daemon->id)) - update = chan->half[0].channel_update; + update = get_broadcast(rstate->broadcasts, + chan->half[0] + .channel_update_msgidx); else if (pubkey_eq(&chan->nodes[1]->id, &peer->daemon->id)) - update = chan->half[1].channel_update; + update = get_broadcast(rstate->broadcasts, + chan->half[1] + .channel_update_msgidx); else { status_unusual("peer %s scid %s: not our channel?", type_to_string(trc, struct pubkey, @@ -1132,7 +1137,7 @@ static void append_half_channel(struct gossip_getchannels_entry **entries, return; /* Don't mention non-public inactive channels. */ - if (!c->active && !c->channel_update) + if (!c->active && !c->channel_update_msgidx) return; n = tal_count(*entries); @@ -1144,9 +1149,9 @@ static void append_half_channel(struct gossip_getchannels_entry **entries, e->satoshis = chan->satoshis; e->active = c->active; e->flags = c->flags; - e->public = (c->channel_update != NULL); + e->public = (c->channel_update_msgidx != 0); e->short_channel_id = chan->scid; - e->last_update_timestamp = c->channel_update ? c->last_timestamp : -1; + e->last_update_timestamp = c->channel_update_msgidx ? c->last_timestamp : -1; if (e->last_update_timestamp >= 0) { e->base_fee_msat = c->base_fee; e->fee_per_millionth = c->proportional_fee; @@ -1335,10 +1340,14 @@ static void gossip_send_keepalive_update(struct routing_state *rstate, u64 htlc_minimum_msat; u16 flags, cltv_expiry_delta; u8 *update, *msg, *err; + const u8 *old_update; /* Parse old update */ + old_update = get_broadcast(rstate->broadcasts, + hc->channel_update_msgidx); + if (!fromwire_channel_update( - hc->channel_update, &sig, &chain_hash, &scid, ×tamp, + old_update, &sig, &chain_hash, &scid, ×tamp, &flags, &cltv_expiry_delta, &htlc_minimum_msat, &fee_base_msat, &fee_proportional_millionths)) { status_failed( @@ -1398,7 +1407,7 @@ static void gossip_refresh_network(struct daemon *daemon) for (size_t i = 0; i < tal_count(n->chans); i++) { struct half_chan *hc = half_chan_from(n, n->chans[i]); - if (!hc->channel_update) { + if (!hc->channel_update_msgidx) { /* Connection is not public yet, so don't even * try to re-announce it */ continue; @@ -1869,6 +1878,7 @@ static struct io_plan *handle_disable_channel(struct io_conn *conn, secp256k1_ecdsa_signature sig; u64 htlc_minimum_msat; u8 *err; + const u8 *old_update; if (!fromwire_gossip_disable_channel(msg, &scid, &direction, &active) ) { status_unusual("Unable to parse %s", @@ -1891,7 +1901,7 @@ static struct io_plan *handle_disable_channel(struct io_conn *conn, hc->active = active; - if (!hc->channel_update) { + if (!hc->channel_update_msgidx) { status_trace( "Channel %s/%d doesn't have a channel_update yet, can't " "disable", @@ -1900,8 +1910,11 @@ static struct io_plan *handle_disable_channel(struct io_conn *conn, goto fail; } + old_update = get_broadcast(daemon->rstate->broadcasts, + hc->channel_update_msgidx); + if (!fromwire_channel_update( - hc->channel_update, &sig, &chain_hash, &scid, ×tamp, + old_update, &sig, &chain_hash, &scid, ×tamp, &flags, &cltv_expiry_delta, &htlc_minimum_msat, &fee_base_msat, &fee_proportional_millionths)) { status_failed( diff --git a/gossipd/routing.c b/gossipd/routing.c index 7e474b9e6..b39f2494b 100644 --- a/gossipd/routing.c +++ b/gossipd/routing.c @@ -144,8 +144,7 @@ static struct node *new_node(struct routing_state *rstate, n->id = *id; n->chans = tal_arr(n, struct chan *, 0); n->alias = NULL; - n->node_announcement = NULL; - n->announcement_idx = 0; + n->node_announce_msgidx = 0; n->last_timestamp = -1; n->addresses = tal_arr(n, struct wireaddr, 0); node_map_add(rstate->nodes, n); @@ -191,7 +190,6 @@ static void init_half_chan(struct routing_state *rstate, { struct half_chan *c = &chan->half[idx]; - c->channel_update = NULL; c->channel_update_msgidx = 0; c->unroutable_until = 0; c->active = false; @@ -223,7 +221,6 @@ struct chan *new_chan(struct routing_state *rstate, chan->nodes[n1idx] = n1; chan->nodes[!n1idx] = n2; chan->txout_script = NULL; - chan->channel_announcement = NULL; chan->channel_announce_msgidx = 0; chan->public = false; chan->satoshis = 0; @@ -808,13 +805,9 @@ bool handle_pending_cannouncement(struct routing_state *rstate, chan->public = true; chan->satoshis = satoshis; - /* Save channel_announcement. */ - tal_free(chan->channel_announcement); - chan->channel_announcement = tal_steal(chan, pending->announce); - if (replace_broadcast(chan, rstate->broadcasts, &chan->channel_announce_msgidx, - pending->announce)) + take(pending->announce))) status_failed(STATUS_FAIL_INTERNAL_ERROR, "Announcement %s was replaced?", tal_hex(trc, pending->announce)); @@ -997,10 +990,8 @@ u8 *handle_channel_update(struct routing_state *rstate, const u8 *update) replace_broadcast(chan, rstate->broadcasts, &chan->half[direction].channel_update_msgidx, - serialized); + take(serialized)); - tal_free(c->channel_update); - c->channel_update = tal_steal(chan, serialized); tal_free(tmpctx); return NULL; } @@ -1181,10 +1172,8 @@ u8 *handle_node_announcement(struct routing_state *rstate, const u8 *node_ann) node->alias = tal_dup_arr(node, u8, alias, 32, 0); replace_broadcast(node, rstate->broadcasts, - &node->announcement_idx, - serialized); - tal_free(node->node_announcement); - node->node_announcement = tal_steal(node, serialized); + &node->node_announce_msgidx, + take(serialized)); tal_free(tmpctx); return NULL; } diff --git a/gossipd/routing.h b/gossipd/routing.h index 0d2e5d9e6..505d16656 100644 --- a/gossipd/routing.h +++ b/gossipd/routing.h @@ -33,8 +33,7 @@ struct half_chan { * things indicated direction wrt the `channel_id` */ u16 flags; - /* Cached `channel_update` we might forward to new peers*/ - u8 *channel_update; + /* Cached `channel_update` we might forward to new peers (or 0) */ u64 channel_update_msgidx; /* If greater than current time, this connection should not @@ -54,9 +53,7 @@ struct chan { /* node[0].id < node[1].id */ struct node *nodes[2]; - /* Cached `channel_announcement` we might forward to new peers*/ - const u8 *channel_announcement; - + /* Cached `channel_announcement` we might forward to new peers (or 0) */ u64 channel_announce_msgidx; /* Is this a public channel, or was it only added locally? */ @@ -93,11 +90,8 @@ struct node { /* Color to be used when displaying the name */ u8 rgb_color[3]; - /* Cached `node_announcement` we might forward to new peers. */ - u8 *node_announcement; - - /* What index does the announcement broadcast have? */ - u64 announcement_idx; + /* Cached `node_announcement` we might forward to new peers (or 0). */ + u64 node_announce_msgidx; }; const secp256k1_pubkey *node_map_keyof_node(const struct node *n);