Skip to content

Commit e7315ff

Browse files
committed
lightningd: allow a more general wait_index increase.
We're not going to increment one at a time for bulk deletion of htlcs when a channel closes. There could be millions of HTLCs! Signed-off-by: Rusty Russell <[email protected]>
1 parent db104aa commit e7315ff

File tree

2 files changed

+56
-8
lines changed

2 files changed

+56
-8
lines changed

lightningd/wait.c

Lines changed: 40 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -119,17 +119,17 @@ static void json_add_index(struct command *cmd,
119119
json_object_end(response);
120120
}
121121

122-
u64 wait_index_increment(struct lightningd *ld,
123-
enum wait_subsystem subsystem,
124-
enum wait_index index,
125-
...)
122+
static u64 wait_index_bump(struct lightningd *ld,
123+
enum wait_subsystem subsystem,
124+
enum wait_index index,
125+
u64 num,
126+
va_list ap_master)
126127
{
127128
struct waiter *i, *n;
128-
va_list ap;
129129
u64 *idxval = wait_index_ptr(ld, subsystem, index);
130130

131-
assert(!add_overflows_u64(*idxval, 1));
132-
(*idxval)++;
131+
assert(!add_overflows_u64(*idxval, num));
132+
(*idxval) += num;
133133

134134
/* FIXME: We can optimize this! It's always the max of the fields in
135135
* the table, *unless* we delete one. So we can lazily write this on
@@ -142,6 +142,7 @@ u64 wait_index_increment(struct lightningd *ld,
142142

143143
list_for_each_safe(&ld->wait_commands, i, n, list) {
144144
struct json_stream *response;
145+
va_list ap;
145146

146147
if (*i->subsystem != subsystem)
147148
continue;
@@ -151,7 +152,7 @@ u64 wait_index_increment(struct lightningd *ld,
151152
continue;
152153

153154
response = json_stream_success(i->cmd);
154-
va_start(ap, index);
155+
va_copy(ap, ap_master);
155156
json_add_index(i->cmd, response, subsystem, index, *idxval, &ap);
156157
va_end(ap);
157158
/* Delete before freeing */
@@ -162,6 +163,37 @@ u64 wait_index_increment(struct lightningd *ld,
162163
return *idxval;
163164
}
164165

166+
u64 wait_index_increment(struct lightningd *ld,
167+
enum wait_subsystem subsystem,
168+
enum wait_index index,
169+
...)
170+
{
171+
va_list ap;
172+
u64 ret;
173+
174+
va_start(ap, index);
175+
ret = wait_index_bump(ld, subsystem, index, 1, ap);
176+
va_end(ap);
177+
178+
return ret;
179+
}
180+
181+
void wait_index_increase(struct lightningd *ld,
182+
enum wait_subsystem subsystem,
183+
enum wait_index index,
184+
u64 num,
185+
...)
186+
{
187+
va_list ap;
188+
189+
if (num == 0)
190+
return;
191+
192+
va_start(ap, num);
193+
wait_index_bump(ld, subsystem, index, num, ap);
194+
va_end(ap);
195+
}
196+
165197
static struct command_result *param_subsystem(struct command *cmd,
166198
const char *name,
167199
const char *buffer,

lightningd/wait.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,22 @@ u64 LAST_ARG_NULL wait_index_increment(struct lightningd *ld,
4949
enum wait_index index,
5050
...);
5151

52+
/**
53+
* wait_index_increase - increase an index, tell waiters.
54+
* @ld: the lightningd
55+
* @subsystem: subsystem for index
56+
* @index: which index
57+
* @num: number to add (if > 0).
58+
* ...: name/value pairs, followed by NULL.
59+
*
60+
* A more generic version if wait_index_increment: if num is 0 it's a noop.
61+
*/
62+
void LAST_ARG_NULL wait_index_increase(struct lightningd *ld,
63+
enum wait_subsystem subsystem,
64+
enum wait_index index,
65+
u64 num,
66+
...);
67+
5268
/* For passing in index parameters. */
5369
struct command_result *param_index(struct command *cmd, const char *name,
5470
const char *buffer,

0 commit comments

Comments
 (0)