forked from ElementsProject/lightning
-
Notifications
You must be signed in to change notification settings - Fork 0
/
datastore.c
314 lines (279 loc) · 8.92 KB
/
datastore.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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
#include "config.h"
#include <ccan/tal/str/str.h>
#include <common/json_command.h>
#include <common/json_param.h>
#include <common/json_stream.h>
#include <lightningd/jsonrpc.h>
#include <lightningd/lightningd.h>
static void json_add_datastore(struct json_stream *response,
const char **key, const u8 *data,
u64 generation)
{
json_array_start(response, "key");
for (size_t i = 0; i < tal_count(key); i++)
json_add_string(response, NULL, key[i]);
json_array_end(response);
if (data) {
const char *str;
json_add_u64(response, "generation", generation);
json_add_hex(response, "hex", data, tal_bytelen(data));
str = utf8_str(response, data, tal_bytelen(data));
if (str)
json_add_string(response, "string", str);
}
}
enum ds_mode {
DS_MUST_EXIST = 1,
DS_MUST_NOT_EXIST = 2,
DS_APPEND = 4
};
static struct command_result *param_mode(struct command *cmd,
const char *name,
const char *buffer,
const jsmntok_t *tok,
enum ds_mode **mode)
{
*mode = tal(cmd, enum ds_mode);
if (json_tok_streq(buffer, tok, "must-create"))
**mode = DS_MUST_NOT_EXIST;
else if (json_tok_streq(buffer, tok, "must-replace"))
**mode = DS_MUST_EXIST;
else if (json_tok_streq(buffer, tok, "create-or-replace"))
**mode = 0;
else if (json_tok_streq(buffer, tok, "must-append"))
**mode = DS_MUST_EXIST | DS_APPEND;
else if (json_tok_streq(buffer, tok, "create-or-append"))
**mode = DS_APPEND;
else
return command_fail_badparam(cmd, name, buffer, tok,
"should be 'must-create',"
" 'must-replace',"
" 'create-or-replace',"
" 'must-append',"
" or 'create-or-append'");
return NULL;
}
static struct command_result *param_list_or_string(struct command *cmd,
const char *name,
const char *buffer,
const jsmntok_t *tok,
const char ***str)
{
if (tok->type == JSMN_ARRAY && tok->size <= 0) {
return command_fail_badparam(cmd, name,
buffer, tok,
"should not be empty");
} else if (tok->type == JSMN_ARRAY) {
size_t i;
const jsmntok_t *t;
*str = tal_arr(cmd, const char *, tok->size);
json_for_each_arr(i, t, tok) {
if (t->type != JSMN_STRING && t->type != JSMN_PRIMITIVE)
return command_fail_badparam(cmd, name,
buffer, t,
"should be string");
(*str)[i] = json_strdup(*str, buffer, t);
}
} else if (tok->type == JSMN_STRING || tok->type == JSMN_PRIMITIVE) {
*str = tal_arr(cmd, const char *, 1);
(*str)[0] = json_strdup(*str, buffer, tok);
} else
return command_fail_badparam(cmd, name,
buffer, tok,
"should be string or array");
return NULL;
}
static char *datastore_key_fmt(const tal_t *ctx, const char **key)
{
char *ret = tal_strdup(ctx, "[");
for (size_t i = 0; i < tal_count(key); i++)
tal_append_fmt(&ret, "%s%s", i ? "," : "", key[i]);
tal_append_fmt(&ret, "]");
return ret;
}
static struct command_result *json_datastore(struct command *cmd,
const char *buffer,
const jsmntok_t *obj UNNEEDED,
const jsmntok_t *params)
{
struct json_stream *response;
const char **key, *strdata, **k;
u8 *data;
const u8 *prevdata;
enum ds_mode *mode;
u64 *generation, actual_gen;
struct db_stmt *stmt;
if (!param(cmd, buffer, params,
p_req("key", param_list_or_string, &key),
p_opt("string", param_escaped_string, &strdata),
p_opt("hex", param_bin_from_hex, &data),
p_opt_def("mode", param_mode, &mode, DS_MUST_NOT_EXIST),
p_opt("generation", param_u64, &generation),
NULL))
return command_param_failed();
if (strdata) {
if (data)
return command_fail(cmd, JSONRPC2_INVALID_PARAMS,
"Cannot have both hex and string");
data = tal_dup_arr(cmd, u8, (u8 *)strdata, strlen(strdata), 0);
} else {
if (!data)
return command_fail(cmd, JSONRPC2_INVALID_PARAMS,
"Must have either hex or string");
}
if (generation && !(*mode & DS_MUST_EXIST))
return command_fail(cmd, JSONRPC2_INVALID_PARAMS,
"generation only valid with must-replace"
" or must-append");
/* Fetch, and make sure we don't have children! */
stmt = wallet_datastore_first(cmd, cmd->ld->wallet, key,
&k, &prevdata, &actual_gen);
tal_free(stmt);
/* We use prevdata as a "does it exist?" flag */
if (!stmt)
prevdata = NULL;
else if (!datastore_key_eq(k, key)) {
prevdata = tal_free(prevdata);
/* Make sure we don't have a child! */
if (datastore_key_startswith(k, key))
return command_fail(cmd, DATASTORE_UPDATE_HAS_CHILDREN,
"Key has children already");
}
/* We have to make sure that parents don't exist. */
if (!prevdata) {
for (size_t i = 1; i < tal_count(key); i++) {
const char **parent;
parent = tal_dup_arr(cmd, const char *, key, i, 0);
if (wallet_datastore_get(cmd, cmd->ld->wallet, parent,
NULL)) {
return command_fail(cmd,
DATASTORE_UPDATE_NO_CHILDREN,
"Parent key %s exists",
datastore_key_fmt(tmpctx,
parent));
}
}
}
if ((*mode & DS_MUST_NOT_EXIST) && prevdata)
return command_fail(cmd, DATASTORE_UPDATE_ALREADY_EXISTS,
"Key already exists");
if ((*mode & DS_MUST_EXIST) && !prevdata)
return command_fail(cmd, DATASTORE_UPDATE_DOES_NOT_EXIST,
"Key does not exist");
if (generation && actual_gen != *generation)
return command_fail(cmd, DATASTORE_UPDATE_WRONG_GENERATION,
"generation is different");
if ((*mode & DS_APPEND) && prevdata) {
size_t prevlen = tal_bytelen(prevdata);
u8 *newdata = tal_arr(cmd, u8, prevlen + tal_bytelen(data));
memcpy(newdata, prevdata, prevlen);
memcpy(newdata + prevlen, data, tal_bytelen(data));
data = newdata;
}
if (prevdata) {
wallet_datastore_update(cmd->ld->wallet, key, data);
actual_gen++;
} else {
wallet_datastore_create(cmd->ld->wallet, key, data);
actual_gen = 0;
}
response = json_stream_success(cmd);
json_add_datastore(response, key, data, actual_gen);
return command_success(cmd, response);
}
static struct command_result *json_listdatastore(struct command *cmd,
const char *buffer,
const jsmntok_t *obj UNNEEDED,
const jsmntok_t *params)
{
struct json_stream *response;
const char **key, **k, **prev_k = NULL;
const u8 *data;
u64 generation;
struct db_stmt *stmt;
if (!param(cmd, buffer, params,
p_opt("key", param_list_or_string, &key),
NULL))
return command_param_failed();
if (key)
log_debug(cmd->ld->log, "Looking for %s",
datastore_key_fmt(tmpctx, key));
response = json_stream_success(cmd);
json_array_start(response, "datastore");
for (stmt = wallet_datastore_first(cmd, cmd->ld->wallet, key,
&k, &data, &generation);
stmt;
stmt = wallet_datastore_next(cmd, key,
stmt, &k, &data,
&generation)) {
log_debug(cmd->ld->log, "Got %s",
datastore_key_fmt(tmpctx, k));
/* Don't list sub-children, except as summary to show it exists. */
if (tal_count(k) > tal_count(key) + 1) {
log_debug(cmd->ld->log, "Too long");
if (!prev_k || !datastore_key_startswith(k, prev_k)) {
prev_k = tal_dup_arr(cmd, const char *, k,
tal_count(key) + 1, 0);
json_object_start(response, NULL);
json_add_datastore(response, prev_k, NULL, 0);
json_object_end(response);
}
} else {
log_debug(cmd->ld->log, "Printing");
json_object_start(response, NULL);
json_add_datastore(response, k, data, generation);
json_object_end(response);
}
}
json_array_end(response);
return command_success(cmd, response);
}
static struct command_result *json_deldatastore(struct command *cmd,
const char *buffer,
const jsmntok_t *obj UNNEEDED,
const jsmntok_t *params)
{
struct json_stream *response;
const char **key;
const u8 *data;
u64 *generation;
u64 actual_gen;
if (!param(cmd, buffer, params,
p_req("key", param_list_or_string, &key),
p_opt("generation", param_u64, &generation),
NULL))
return command_param_failed();
data = wallet_datastore_get(cmd, cmd->ld->wallet, key, &actual_gen);
if (!data) {
return command_fail(cmd, DATASTORE_DEL_DOES_NOT_EXIST,
"Key does not exist");
}
if (generation && actual_gen != *generation)
return command_fail(cmd, DATASTORE_DEL_WRONG_GENERATION,
"generation is different");
wallet_datastore_remove(cmd->ld->wallet, key);
response = json_stream_success(cmd);
json_add_datastore(response, key, data, actual_gen);
return command_success(cmd, response);
}
static const struct json_command datastore_command = {
"datastore",
"utility",
json_datastore,
"Add a {key} and {hex}/{string} data to the data store",
};
AUTODATA(json_command, &datastore_command);
static const struct json_command deldatastore_command = {
"deldatastore",
"utility",
json_deldatastore,
"Remove a {key} from the data store",
};
AUTODATA(json_command, &deldatastore_command);
static const struct json_command listdatastore_command = {
"listdatastore",
"utility",
json_listdatastore,
"List the datastore, optionally only {key}",
};
AUTODATA(json_command, &listdatastore_command);