2018-01-11 14:57:00 +01:00
|
|
|
#include <ccan/mem/mem.h>
|
2017-08-29 06:12:04 +02:00
|
|
|
#include <gossipd/broadcast.h>
|
2017-01-26 22:47:52 +01:00
|
|
|
|
2018-03-13 16:45:55 +01:00
|
|
|
struct queued_message {
|
|
|
|
/* Broadcast index. */
|
|
|
|
u64 index;
|
|
|
|
|
2018-06-04 06:24:25 +02:00
|
|
|
/* Timestamp, for filtering. */
|
|
|
|
u32 timestamp;
|
|
|
|
|
2018-03-13 16:45:55 +01:00
|
|
|
/* Serialized payload */
|
|
|
|
const u8 *payload;
|
|
|
|
};
|
|
|
|
|
2017-01-26 22:47:52 +01:00
|
|
|
struct broadcast_state *new_broadcast_state(tal_t *ctx)
|
|
|
|
{
|
|
|
|
struct broadcast_state *bstate = tal(ctx, struct broadcast_state);
|
|
|
|
uintmap_init(&bstate->broadcasts);
|
|
|
|
/* Skip 0 because we initialize peers with 0 */
|
|
|
|
bstate->next_index = 1;
|
|
|
|
return bstate;
|
|
|
|
}
|
|
|
|
|
2018-03-13 16:38:55 +01:00
|
|
|
static void destroy_queued_message(struct queued_message *msg,
|
|
|
|
struct broadcast_state *bstate)
|
|
|
|
{
|
|
|
|
uintmap_del(&bstate->broadcasts, msg->index);
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct queued_message *new_queued_message(const tal_t *ctx,
|
|
|
|
struct broadcast_state *bstate,
|
2018-05-10 14:22:37 +02:00
|
|
|
const u8 *payload,
|
2018-06-04 06:24:25 +02:00
|
|
|
u32 timestamp,
|
2018-03-13 16:38:55 +01:00
|
|
|
u64 index)
|
2017-01-26 22:47:52 +01:00
|
|
|
{
|
|
|
|
struct queued_message *msg = tal(ctx, struct queued_message);
|
2018-06-04 06:24:25 +02:00
|
|
|
assert(payload);
|
2018-05-10 14:22:37 +02:00
|
|
|
msg->payload = payload;
|
2018-03-13 16:38:55 +01:00
|
|
|
msg->index = index;
|
2018-06-04 06:24:25 +02:00
|
|
|
msg->timestamp = timestamp;
|
2018-03-13 16:38:55 +01:00
|
|
|
uintmap_add(&bstate->broadcasts, index, msg);
|
|
|
|
tal_add_destructor2(msg, destroy_queued_message, bstate);
|
2017-01-26 22:47:52 +01:00
|
|
|
return msg;
|
|
|
|
}
|
|
|
|
|
2018-06-04 06:24:25 +02:00
|
|
|
void insert_broadcast(struct broadcast_state *bstate,
|
|
|
|
const u8 *payload, u32 timestamp)
|
2018-01-31 17:52:12 +01:00
|
|
|
{
|
2018-05-10 14:22:37 +02:00
|
|
|
/* Free payload, free index. */
|
2018-06-04 06:24:25 +02:00
|
|
|
new_queued_message(payload, bstate, payload, timestamp,
|
|
|
|
bstate->next_index++);
|
2018-01-31 17:52:12 +01:00
|
|
|
}
|
|
|
|
|
2018-03-13 16:45:55 +01:00
|
|
|
const u8 *next_broadcast(struct broadcast_state *bstate, u64 *last_index)
|
2017-01-26 22:47:52 +01:00
|
|
|
{
|
2018-03-13 16:45:55 +01:00
|
|
|
struct queued_message *m;
|
|
|
|
|
|
|
|
m = uintmap_after(&bstate->broadcasts, last_index);
|
|
|
|
if (m)
|
|
|
|
return m->payload;
|
|
|
|
return NULL;
|
2017-01-26 22:47:52 +01:00
|
|
|
}
|