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-06-08 08:31:51 +02:00
|
|
|
void broadcast_del(struct broadcast_state *bstate, u64 index, const u8 *payload)
|
|
|
|
{
|
|
|
|
const struct queued_message *q = uintmap_del(&bstate->broadcasts, index);
|
|
|
|
assert(q->payload == payload);
|
|
|
|
}
|
|
|
|
|
2018-03-13 16:38:55 +01:00
|
|
|
static void destroy_queued_message(struct queued_message *msg,
|
|
|
|
struct broadcast_state *bstate)
|
|
|
|
{
|
2018-06-08 08:31:51 +02:00
|
|
|
broadcast_del(bstate, msg->index, msg->payload);
|
2018-03-13 16:38:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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-08 08:31:49 +02:00
|
|
|
u64 insert_broadcast(struct broadcast_state *bstate,
|
2018-06-04 06:24:25 +02:00
|
|
|
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,
|
2018-06-08 08:31:49 +02:00
|
|
|
bstate->next_index);
|
|
|
|
return bstate->next_index++;
|
2018-01-31 17:52:12 +01:00
|
|
|
}
|
|
|
|
|
2018-06-04 06:26:25 +02:00
|
|
|
const u8 *next_broadcast(struct broadcast_state *bstate,
|
|
|
|
u32 timestamp_min, u32 timestamp_max,
|
|
|
|
u64 *last_index)
|
2017-01-26 22:47:52 +01:00
|
|
|
{
|
2018-03-13 16:45:55 +01:00
|
|
|
struct queued_message *m;
|
|
|
|
|
2018-06-04 06:26:25 +02:00
|
|
|
while ((m = uintmap_after(&bstate->broadcasts, last_index)) != NULL) {
|
|
|
|
if (m->timestamp >= timestamp_min
|
|
|
|
&& m->timestamp <= timestamp_max)
|
|
|
|
return m->payload;
|
|
|
|
}
|
2018-03-13 16:45:55 +01:00
|
|
|
return NULL;
|
2017-01-26 22:47:52 +01:00
|
|
|
}
|