gossipd: remove tag.

We only access via index.  We do, however, want to clean up when we
delete nodes and channels, so we tie lifetimes to that.  This leads
us to put the index into 'struct queued_message'.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
Rusty Russell 2018-03-14 02:08:55 +10:30
parent 882f9f258f
commit 1290f305a2
6 changed files with 33 additions and 67 deletions

View File

@ -10,35 +10,42 @@ struct broadcast_state *new_broadcast_state(tal_t *ctx)
return bstate; return bstate;
} }
static struct queued_message *new_queued_message(tal_t *ctx, static void destroy_queued_message(struct queued_message *msg,
const u8 *tag, struct broadcast_state *bstate)
const u8 *payload) {
uintmap_del(&bstate->broadcasts, msg->index);
}
static struct queued_message *new_queued_message(const tal_t *ctx,
struct broadcast_state *bstate,
const u8 *payload,
u64 index)
{ {
struct queued_message *msg = tal(ctx, struct queued_message); struct queued_message *msg = tal(ctx, struct queued_message);
msg->tag = tal_dup_arr(msg, u8, tag, tal_len(tag), 0);
msg->payload = tal_dup_arr(msg, u8, payload, tal_len(payload), 0); msg->payload = tal_dup_arr(msg, u8, payload, tal_len(payload), 0);
msg->index = index;
uintmap_add(&bstate->broadcasts, index, msg);
tal_add_destructor2(msg, destroy_queued_message, bstate);
return msg; return msg;
} }
bool replace_broadcast(struct broadcast_state *bstate, u64 *index, bool replace_broadcast(const tal_t *ctx,
const u8 *tag, const u8 *payload) struct broadcast_state *bstate,
u64 *index,
const u8 *payload)
{ {
struct queued_message *msg; struct queued_message *msg;
bool evicted = false; bool evicted = false;
msg = uintmap_get(&bstate->broadcasts, *index); msg = uintmap_get(&bstate->broadcasts, *index);
if (msg) { if (msg) {
assert(memeq(msg->tag, tal_len(msg->tag), tag, tal_len(tag)));
uintmap_del(&bstate->broadcasts, *index);
tal_free(msg); tal_free(msg);
evicted = true; evicted = true;
} }
*index = bstate->next_index;
/* Now add the message to the queue */ /* Now add the message to the queue */
msg = new_queued_message(bstate, tag, payload); msg = new_queued_message(ctx, bstate, payload, bstate->next_index++);
uintmap_add(&bstate->broadcasts, *index, msg); *index = msg->index;
bstate->next_index++;
return evicted; return evicted;
} }

View File

@ -10,8 +10,8 @@
/* Common functionality to implement staggered broadcasts with replacement. */ /* Common functionality to implement staggered broadcasts with replacement. */
struct queued_message { struct queued_message {
/* Unique tag specifying the msg origin */ /* Broadcast index. */
void *tag; u64 index;
/* Serialized payload */ /* Serialized payload */
u8 *payload; u8 *payload;
@ -28,9 +28,9 @@ struct broadcast_state *new_broadcast_state(tal_t *ctx);
* tag for the new message. The new message will be queued with the * tag for the new message. The new message will be queued with the
* next highest index. @index is updated to hold the index of the * next highest index. @index is updated to hold the index of the
* newly queued message*/ * newly queued message*/
bool replace_broadcast(struct broadcast_state *bstate, bool replace_broadcast(const tal_t *ctx,
struct broadcast_state *bstate,
u64 *index, u64 *index,
const u8 *tag,
const u8 *payload); const u8 *payload);

View File

@ -753,7 +753,6 @@ bool handle_pending_cannouncement(struct routing_state *rstate,
const u8 *outscript) const u8 *outscript)
{ {
bool local; bool local;
u8 *tag;
const u8 *s; const u8 *s;
struct pending_cannouncement *pending; struct pending_cannouncement *pending;
struct chan *chan; struct chan *chan;
@ -762,9 +761,6 @@ bool handle_pending_cannouncement(struct routing_state *rstate,
if (!pending) if (!pending)
return false; return false;
tag = tal_arr(pending, u8, 0);
towire_short_channel_id(&tag, scid);
/* BOLT #7: /* BOLT #7:
* *
* The receiving node MUST ignore the message if this output is spent. * The receiving node MUST ignore the message if this output is spent.
@ -816,9 +812,9 @@ bool handle_pending_cannouncement(struct routing_state *rstate,
tal_free(chan->channel_announcement); tal_free(chan->channel_announcement);
chan->channel_announcement = tal_steal(chan, pending->announce); chan->channel_announcement = tal_steal(chan, pending->announce);
if (replace_broadcast(rstate->broadcasts, if (replace_broadcast(chan, rstate->broadcasts,
&chan->channel_announce_msgidx, &chan->channel_announce_msgidx,
tag, pending->announce)) pending->announce))
status_failed(STATUS_FAIL_INTERNAL_ERROR, status_failed(STATUS_FAIL_INTERNAL_ERROR,
"Announcement %s was replaced?", "Announcement %s was replaced?",
tal_hex(trc, pending->announce)); tal_hex(trc, pending->announce));
@ -999,12 +995,8 @@ u8 *handle_channel_update(struct routing_state *rstate, const u8 *update)
timestamp, timestamp,
htlc_minimum_msat); htlc_minimum_msat);
u8 *tag = tal_arr(tmpctx, u8, 0); replace_broadcast(chan, rstate->broadcasts,
towire_short_channel_id(&tag, &short_channel_id);
towire_u16(&tag, direction);
replace_broadcast(rstate->broadcasts,
&chan->half[direction].channel_update_msgidx, &chan->half[direction].channel_update_msgidx,
tag,
serialized); serialized);
tal_free(c->channel_update); tal_free(c->channel_update);
@ -1188,11 +1180,8 @@ u8 *handle_node_announcement(struct routing_state *rstate, const u8 *node_ann)
tal_free(node->alias); tal_free(node->alias);
node->alias = tal_dup_arr(node, u8, alias, 32, 0); node->alias = tal_dup_arr(node, u8, alias, 32, 0);
u8 *tag = tal_arr(tmpctx, u8, 0); replace_broadcast(node, rstate->broadcasts,
towire_pubkey(&tag, &node_id);
replace_broadcast(rstate->broadcasts,
&node->announcement_idx, &node->announcement_idx,
tag,
serialized); serialized);
tal_free(node->node_announcement); tal_free(node->node_announcement);
node->node_announcement = tal_steal(node, serialized); node->node_announcement = tal_steal(node, serialized);

View File

@ -74,9 +74,9 @@ bool fromwire_wireaddr(const u8 **cursor UNNEEDED, size_t *max UNNEEDED, struct
const char *onion_type_name(int e UNNEEDED) const char *onion_type_name(int e UNNEEDED)
{ fprintf(stderr, "onion_type_name called!\n"); abort(); } { fprintf(stderr, "onion_type_name called!\n"); abort(); }
/* Generated stub for replace_broadcast */ /* Generated stub for replace_broadcast */
bool replace_broadcast(struct broadcast_state *bstate UNNEEDED, bool replace_broadcast(const tal_t *ctx UNNEEDED,
struct broadcast_state *bstate UNNEEDED,
u64 *index UNNEEDED, u64 *index UNNEEDED,
const u8 *tag UNNEEDED,
const u8 *payload UNNEEDED) const u8 *payload UNNEEDED)
{ fprintf(stderr, "replace_broadcast called!\n"); abort(); } { fprintf(stderr, "replace_broadcast called!\n"); abort(); }
/* Generated stub for sanitize_error */ /* Generated stub for sanitize_error */
@ -92,16 +92,6 @@ u8 *towire_errorfmt(const tal_t *ctx UNNEEDED,
const struct channel_id *channel UNNEEDED, const struct channel_id *channel UNNEEDED,
const char *fmt UNNEEDED, ...) const char *fmt UNNEEDED, ...)
{ fprintf(stderr, "towire_errorfmt called!\n"); abort(); } { fprintf(stderr, "towire_errorfmt called!\n"); abort(); }
/* Generated stub for towire_pubkey */
void towire_pubkey(u8 **pptr UNNEEDED, const struct pubkey *pubkey UNNEEDED)
{ fprintf(stderr, "towire_pubkey called!\n"); abort(); }
/* Generated stub for towire_short_channel_id */
void towire_short_channel_id(u8 **pptr UNNEEDED,
const struct short_channel_id *short_channel_id UNNEEDED)
{ fprintf(stderr, "towire_short_channel_id called!\n"); abort(); }
/* Generated stub for towire_u16 */
void towire_u16(u8 **pptr UNNEEDED, u16 v UNNEEDED)
{ fprintf(stderr, "towire_u16 called!\n"); abort(); }
/* AUTOGENERATED MOCKS END */ /* AUTOGENERATED MOCKS END */
const void *trc; const void *trc;

View File

@ -38,9 +38,9 @@ bool fromwire_wireaddr(const u8 **cursor UNNEEDED, size_t *max UNNEEDED, struct
const char *onion_type_name(int e UNNEEDED) const char *onion_type_name(int e UNNEEDED)
{ fprintf(stderr, "onion_type_name called!\n"); abort(); } { fprintf(stderr, "onion_type_name called!\n"); abort(); }
/* Generated stub for replace_broadcast */ /* Generated stub for replace_broadcast */
bool replace_broadcast(struct broadcast_state *bstate UNNEEDED, bool replace_broadcast(const tal_t *ctx UNNEEDED,
struct broadcast_state *bstate UNNEEDED,
u64 *index UNNEEDED, u64 *index UNNEEDED,
const u8 *tag UNNEEDED,
const u8 *payload UNNEEDED) const u8 *payload UNNEEDED)
{ fprintf(stderr, "replace_broadcast called!\n"); abort(); } { fprintf(stderr, "replace_broadcast called!\n"); abort(); }
/* Generated stub for sanitize_error */ /* Generated stub for sanitize_error */
@ -56,16 +56,6 @@ u8 *towire_errorfmt(const tal_t *ctx UNNEEDED,
const struct channel_id *channel UNNEEDED, const struct channel_id *channel UNNEEDED,
const char *fmt UNNEEDED, ...) const char *fmt UNNEEDED, ...)
{ fprintf(stderr, "towire_errorfmt called!\n"); abort(); } { fprintf(stderr, "towire_errorfmt called!\n"); abort(); }
/* Generated stub for towire_pubkey */
void towire_pubkey(u8 **pptr UNNEEDED, const struct pubkey *pubkey UNNEEDED)
{ fprintf(stderr, "towire_pubkey called!\n"); abort(); }
/* Generated stub for towire_short_channel_id */
void towire_short_channel_id(u8 **pptr UNNEEDED,
const struct short_channel_id *short_channel_id UNNEEDED)
{ fprintf(stderr, "towire_short_channel_id called!\n"); abort(); }
/* Generated stub for towire_u16 */
void towire_u16(u8 **pptr UNNEEDED, u16 v UNNEEDED)
{ fprintf(stderr, "towire_u16 called!\n"); abort(); }
/* AUTOGENERATED MOCKS END */ /* AUTOGENERATED MOCKS END */
const void *trc; const void *trc;

View File

@ -36,9 +36,9 @@ bool fromwire_wireaddr(const u8 **cursor UNNEEDED, size_t *max UNNEEDED, struct
const char *onion_type_name(int e UNNEEDED) const char *onion_type_name(int e UNNEEDED)
{ fprintf(stderr, "onion_type_name called!\n"); abort(); } { fprintf(stderr, "onion_type_name called!\n"); abort(); }
/* Generated stub for replace_broadcast */ /* Generated stub for replace_broadcast */
bool replace_broadcast(struct broadcast_state *bstate UNNEEDED, bool replace_broadcast(const tal_t *ctx UNNEEDED,
struct broadcast_state *bstate UNNEEDED,
u64 *index UNNEEDED, u64 *index UNNEEDED,
const u8 *tag UNNEEDED,
const u8 *payload UNNEEDED) const u8 *payload UNNEEDED)
{ fprintf(stderr, "replace_broadcast called!\n"); abort(); } { fprintf(stderr, "replace_broadcast called!\n"); abort(); }
/* Generated stub for sanitize_error */ /* Generated stub for sanitize_error */
@ -54,16 +54,6 @@ u8 *towire_errorfmt(const tal_t *ctx UNNEEDED,
const struct channel_id *channel UNNEEDED, const struct channel_id *channel UNNEEDED,
const char *fmt UNNEEDED, ...) const char *fmt UNNEEDED, ...)
{ fprintf(stderr, "towire_errorfmt called!\n"); abort(); } { fprintf(stderr, "towire_errorfmt called!\n"); abort(); }
/* Generated stub for towire_pubkey */
void towire_pubkey(u8 **pptr UNNEEDED, const struct pubkey *pubkey UNNEEDED)
{ fprintf(stderr, "towire_pubkey called!\n"); abort(); }
/* Generated stub for towire_short_channel_id */
void towire_short_channel_id(u8 **pptr UNNEEDED,
const struct short_channel_id *short_channel_id UNNEEDED)
{ fprintf(stderr, "towire_short_channel_id called!\n"); abort(); }
/* Generated stub for towire_u16 */
void towire_u16(u8 **pptr UNNEEDED, u16 v UNNEEDED)
{ fprintf(stderr, "towire_u16 called!\n"); abort(); }
/* AUTOGENERATED MOCKS END */ /* AUTOGENERATED MOCKS END */
const void *trc; const void *trc;