MCPcopy Create free account
hub / github.com/ElementsProject/lightning / json_datastore

Function json_datastore

lightningd/datastore.c:120–223  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

118}
119
120static struct command_result *json_datastore(struct command *cmd,
121 const char *buffer,
122 const jsmntok_t *obj UNNEEDED,
123 const jsmntok_t *params)
124{
125 struct json_stream *response;
126 const char **key, *strdata, **k;
127 u8 *data;
128 const u8 *prevdata;
129 enum ds_mode *mode;
130 u64 *generation, actual_gen;
131 struct db_stmt *stmt;
132
133 if (!param(cmd, buffer, params,
134 p_req("key", param_list_or_string, &key),
135 p_opt("string", param_string, &strdata),
136 p_opt("hex", param_bin_from_hex, &data),
137 p_opt_def("mode", param_mode, &mode, DS_MUST_NOT_EXIST),
138 p_opt("generation", param_u64, &generation),
139 NULL))
140 return command_param_failed();
141
142 if (strdata) {
143 if (data)
144 return command_fail(cmd, JSONRPC2_INVALID_PARAMS,
145 "Cannot have both hex and string");
146 data = tal_dup_arr(cmd, u8, (u8 *)strdata, strlen(strdata), 0);
147 } else {
148 if (!data)
149 return command_fail(cmd, JSONRPC2_INVALID_PARAMS,
150 "Must have either hex or string");
151 }
152
153 if (generation && !(*mode & DS_MUST_EXIST))
154 return command_fail(cmd, JSONRPC2_INVALID_PARAMS,
155 "generation only valid with must-replace"
156 " or must-append");
157
158 /* Fetch, and make sure we don't have children! */
159 stmt = wallet_datastore_first(cmd, cmd->ld->wallet, key,
160 &k, &prevdata, &actual_gen);
161 tal_free(stmt);
162
163 /* We use prevdata as a "does it exist?" flag */
164 if (!stmt)
165 prevdata = NULL;
166 else if (!datastore_key_eq(k, key)) {
167 prevdata = tal_free(prevdata);
168 /* Make sure we don't have a child! */
169 if (datastore_key_startswith(k, key))
170 return command_fail(cmd, DATASTORE_UPDATE_HAS_CHILDREN,
171 "Key has children already");
172 }
173
174 /* We have to make sure that parents don't exist. */
175 if (!prevdata) {
176 for (size_t i = 1; i < tal_count(key); i++) {
177 const char **parent;

Callers

nothing calls this directly

Calls 14

wallet_datastore_firstFunction · 0.85
tal_freeFunction · 0.85
datastore_key_eqFunction · 0.85
datastore_key_startswithFunction · 0.85
datastore_key_fmtFunction · 0.85
tal_bytelenFunction · 0.85
wallet_datastore_updateFunction · 0.85
wallet_datastore_createFunction · 0.85
json_add_datastoreFunction · 0.85
command_param_failedFunction · 0.70
command_failFunction · 0.70
json_stream_successFunction · 0.70

Tested by

no test coverage detected