forked from ElementsProject/lightning
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwait.c
220 lines (189 loc) · 5.29 KB
/
wait.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/* Code to be notified when various standardized events happen. */
#include "config.h"
#include <ccan/array_size/array_size.h>
#include <ccan/tal/str/str.h>
#include <common/json_command.h>
#include <common/overflows.h>
#include <db/exec.h>
#include <lightningd/jsonrpc.h>
#include <lightningd/lightningd.h>
#include <lightningd/wait.h>
struct waiter {
struct list_node list;
struct command *cmd;
/* These are pointers because of how param_ works */
enum wait_subsystem *subsystem;
enum wait_index *index;
u64 *nextval;
};
static const char *subsystem_names[] = {
"forwards",
"sendpays",
"invoices",
};
static const char *index_names[] = {
"created",
"updated",
"deleted",
};
/* This is part of the API, so no changing! */
const char *wait_index_name(enum wait_index index)
{
switch (index) {
case WAIT_INDEX_CREATED:
case WAIT_INDEX_UPDATED:
case WAIT_INDEX_DELETED:
return index_names[index];
}
abort();
}
const char *wait_subsystem_name(enum wait_subsystem subsystem)
{
switch (subsystem) {
case WAIT_SUBSYSTEM_FORWARD:
case WAIT_SUBSYSTEM_SENDPAY:
case WAIT_SUBSYSTEM_INVOICE:
return subsystem_names[subsystem];
}
abort();
}
static u64 *wait_index_ptr(struct lightningd *ld,
enum wait_subsystem subsystem,
enum wait_index index)
{
struct indexes *indexes;
assert(subsystem < ARRAY_SIZE(ld->indexes));
indexes = &ld->indexes[subsystem];
assert(index < ARRAY_SIZE(indexes->i));
return &indexes->i[index];
}
static void json_add_index(struct json_stream *response,
enum wait_subsystem subsystem,
enum wait_index index,
u64 val,
va_list *ap)
{
const char *name, *value;
json_add_string(response, "subsystem", wait_subsystem_name(subsystem));
json_add_u64(response, wait_index_name(index), val);
if (!ap)
return;
json_object_start(response, "details");
while ((name = va_arg(*ap, const char *)) != NULL) {
value = va_arg(*ap, const char *);
if (!value)
continue;
/* This is a hack! */
if (name[0] == '=') {
/* Copy in literallty! */
json_add_jsonstr(response, name + 1, value, strlen(value));
} else {
json_add_string(response, name, value);
}
}
json_object_end(response);
}
u64 wait_index_increment(struct lightningd *ld,
enum wait_subsystem subsystem,
enum wait_index index,
...)
{
struct waiter *i, *n;
va_list ap;
u64 *idxval = wait_index_ptr(ld, subsystem, index);
assert(!add_overflows_u64(*idxval, 1));
(*idxval)++;
/* FIXME: We can optimize this! It's always the max of the fields in
* the table, *unless* we delete one. So we can lazily write this on
* delete, and fix it up to MAX() when we startup. */
db_set_intvar(ld->wallet->db,
tal_fmt(tmpctx, "last_%s_%s_index",
wait_subsystem_name(subsystem),
wait_index_name(index)),
*idxval);
list_for_each_safe(&ld->wait_commands, i, n, list) {
struct json_stream *response;
if (*i->subsystem != subsystem)
continue;
if (*i->index != index)
continue;
if (*idxval < *i->nextval)
continue;
response = json_stream_success(i->cmd);
va_start(ap, index);
json_add_index(response, subsystem, index, *idxval, &ap);
va_end(ap);
/* Delete before freeing */
list_del_from(&ld->wait_commands, &i->list);
was_pending(command_success(i->cmd, response));
}
return *idxval;
}
static struct command_result *param_subsystem(struct command *cmd,
const char *name,
const char *buffer,
const jsmntok_t *tok,
enum wait_subsystem **subsystem)
{
for (size_t i = 0; i < ARRAY_SIZE(subsystem_names); i++) {
if (json_tok_streq(buffer, tok, subsystem_names[i])) {
*subsystem = tal(cmd, enum wait_subsystem);
**subsystem = i;
return NULL;
}
}
return command_fail_badparam(cmd, name, buffer, tok,
"unknown subsystem");
}
struct command_result *param_index(struct command *cmd,
const char *name,
const char *buffer,
const jsmntok_t *tok,
enum wait_index **index)
{
for (size_t i = 0; i < ARRAY_SIZE(index_names); i++) {
if (json_tok_streq(buffer, tok, index_names[i])) {
*index = tal(cmd, enum wait_index);
**index = i;
return NULL;
}
}
return command_fail_badparam(cmd, name, buffer, tok,
"unknown index");
}
static struct command_result *json_wait(struct command *cmd,
const char *buffer,
const jsmntok_t *obj UNNEEDED,
const jsmntok_t *params)
{
struct waiter *waiter = tal(cmd, struct waiter);
u64 val;
if (!param(cmd, buffer, params,
p_req("subsystem", param_subsystem,
&waiter->subsystem),
p_req("indexname", param_index, &waiter->index),
p_req("nextvalue", param_u64, &waiter->nextval),
NULL))
return command_param_failed();
/* Are we there already? Return immediately. */
val = *wait_index_ptr(cmd->ld, *waiter->subsystem, *waiter->index);
if (val >= *waiter->nextval) {
struct json_stream *response;
response = json_stream_success(cmd);
json_add_index(response,
*waiter->subsystem,
*waiter->index,
val, NULL);
return command_success(cmd, response);
}
waiter->cmd = cmd;
list_add_tail(&cmd->ld->wait_commands, &waiter->list);
return command_still_pending(cmd);
}
static const struct json_command wait_command = {
"wait",
"utility",
json_wait,
"Wait for {subsystem} {indexname} to reach or exceed {value})"
};
AUTODATA(json_command, &wait_command);