Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
- JSON API: `getinfo` now returns `num_peers` `num_pending_channels`,
`num_active_channels` and `num_inactive_channels` fields.
- JSON API: use `\n\n` to terminate responses, for simplified parsing (pylightning now relies on this)
- Plugins: Added plugins to `lightningd` and implemented the option passthrough.
- Plugins: Added plugins to `lightningd`, including option passthrough and JSON-RPC passthrough.

### Changed

Expand Down
12 changes: 12 additions & 0 deletions common/utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,15 @@ void clean_tmpctx(void)
while ((p = tal_first(tmpctx)) != NULL)
tal_free(p);
}

void tal_arr_remove_(void *p, size_t elemsize, size_t n)
{
// p is a pointer-to-pointer for tal_resize.
char *objp = *(char **)p;
size_t len = tal_bytelen(objp);
assert(len % elemsize == 0);
assert((n + 1) * elemsize <= len);
memmove(objp + elemsize * n, objp + elemsize * (n+1),
len - (elemsize * (n+1)));
tal_resize((char **)p, len - elemsize);
}
9 changes: 9 additions & 0 deletions common/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,15 @@ u8 *tal_hexdata(const tal_t *ctx, const void *str, size_t len);
(tal_resize((p), tal_count(*(p))+1), (*p) + tal_count(*(p))-1)
#endif

/**
* Remove an element from an array
*
* This will shift the elements past the removed element, changing
* their position in memory, so only use this for arrays of pointers.
*/
#define tal_arr_remove(p, n) tal_arr_remove_((p), sizeof(**p), (n))
void tal_arr_remove_(void *p, size_t elemsize, size_t n);

/* Use the POSIX C locale. */
void setup_locale(void);

Expand Down
58 changes: 58 additions & 0 deletions contrib/plugins/fail/failtimeout.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#!/usr/bin/env python3
"""An example plugin that fails to answer to `getmanifest`

Used to test the `getmanifest` timeout.
"""
import json
import sys
import time


def json_getmanifest(request):
# Timeout is 10 seconds, so wait 11
time.sleep(11)
return {
"options": [
],
"rpcmethods": [
]
}


methods = {
'getmanifest': json_getmanifest,
}


partial = ""
for l in sys.stdin:
try:
partial += l
request = json.loads(partial)
except Exception:
continue

result = None
method = methods[request['method']]
params = request['params']
try:
if isinstance(params, dict):
result = method(request, **params)
else:
result = method(request, *params)
result = {
"jsonrpc": "2.0",
"result": result,
"id": request['id']
}
except Exception as e:
result = {
"jsonrpc": "2.0",
"error": "Error while processing {}".format(request['method']),
"id": request['id']
}

json.dump(result, fp=sys.stdout)
sys.stdout.write('\n')
sys.stdout.flush()
partial = ""
16 changes: 6 additions & 10 deletions lightningd/jsonrpc.c
Original file line number Diff line number Diff line change
Expand Up @@ -347,11 +347,8 @@ static const struct json_command *find_cmd(const struct jsonrpc *rpc,
{
struct json_command **commands = rpc->commands;

/* commands[i] can be NULL if the plugin that registered it
* was killed, commands[i]->name can be NULL in test code. */
for (size_t i = 0; i < tal_count(commands); i++)
if (commands[i] && commands[i]->name &&
json_tok_streq(buffer, tok, commands[i]->name))
if (json_tok_streq(buffer, tok, commands[i]->name))
return commands[i];
return NULL;
}
Expand Down Expand Up @@ -731,8 +728,7 @@ bool jsonrpc_command_add(struct jsonrpc *rpc, struct json_command *command)

/* Check that we don't clobber a method */
for (size_t i = 0; i < count; i++)
if (rpc->commands[i] != NULL &&
streq(rpc->commands[i]->name, command->name))
if (streq(rpc->commands[i]->name, command->name))
return false;

*tal_arr_expand(&rpc->commands) = command;
Expand All @@ -741,12 +737,12 @@ bool jsonrpc_command_add(struct jsonrpc *rpc, struct json_command *command)

void jsonrpc_command_remove(struct jsonrpc *rpc, const char *method)
{
// FIXME: Currently leaves NULL entries in the table, if we
// restart plugins we should shift them out.
for (size_t i=0; i<tal_count(rpc->commands); i++) {
struct json_command *cmd = rpc->commands[i];
if (cmd && streq(cmd->name, method)) {
rpc->commands[i] = tal_free(cmd);
if (streq(cmd->name, method)) {
tal_arr_remove(&rpc->commands, i);
tal_free(cmd);
Comment thread
cdecker marked this conversation as resolved.
break;
}
}
}
Expand Down
Loading