forked from ElementsProject/lightning
-
Notifications
You must be signed in to change notification settings - Fork 0
/
txout_failures.c
59 lines (49 loc) · 1.46 KB
/
txout_failures.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include "config.h"
#include <common/timeout.h>
#include <gossipd/gossipd.h>
#include <gossipd/txout_failures.h>
/* Convenience to tell the two failure maps apart */
#define RECENT 0
#define AGED 1
#define MAX_ENTRIES 10000
/* Once an hour, or at 10000 entries, we expire old ones */
static void txout_failures_age(struct txout_failures *txf)
{
uintmap_clear(&txf->failures[AGED]);
txf->failures[AGED] = txf->failures[RECENT];
uintmap_init(&txf->failures[RECENT]);
txf->num = 0;
txf->age_timer = new_reltimer(&txf->daemon->timers, txf,
time_from_sec(3600),
txout_failures_age, txf);
}
struct txout_failures *txout_failures_new(const tal_t *ctx, struct daemon *daemon)
{
struct txout_failures *txf = tal(ctx, struct txout_failures);
txf->daemon = daemon;
uintmap_init(&txf->failures[RECENT]);
uintmap_init(&txf->failures[AGED]);
txout_failures_age(txf);
return txf;
}
void txout_failures_add(struct txout_failures *txf,
const struct short_channel_id scid)
{
if (uintmap_add(&txf->failures[RECENT], scid.u64, true)
&& ++txf->num == MAX_ENTRIES) {
tal_free(txf->age_timer);
txout_failures_age(txf);
}
}
bool in_txout_failures(struct txout_failures *txf,
const struct short_channel_id scid)
{
if (uintmap_get(&txf->failures[RECENT], scid.u64))
return true;
/* If we were going to expire it, we no longer are. */
if (uintmap_get(&txf->failures[AGED], scid.u64)) {
txout_failures_add(txf, scid);
return true;
}
return false;
}