Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pass typed plugin option data to plugin in init #3582

Merged
merged 7 commits into from
Mar 10, 2020
Merged
Show file tree
Hide file tree
Changes from 4 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
3 changes: 3 additions & 0 deletions contrib/pyln-client/pyln/client/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,9 @@ def add_option(self, name, default, description, opt_type="string"):
"Name {} is already used by another option".format(name)
)

if opt_type not in ["string", "int", "bool"]:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[commit msg]

not sure if this is totally necessary, should we jsut let clightning
enforce the input?

Might be a bit redundant but an advantage is that one can see its mistake just by trying its plugin with python3 myplugin.py..

raise ValueError('{} not in supported type set (string, int, bool)')

self.options[name] = {
'name': name,
'default': default,
Expand Down
32 changes: 28 additions & 4 deletions contrib/pyln-testing/pyln/testing/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ def __init__(self, outputDir=None, verbose=True):
self.proc = None
self.outputDir = outputDir
self.logsearch_start = 0
self.err_logs = []

# Should we be logging lines we read from stdout?
self.verbose = verbose
Expand Down Expand Up @@ -210,6 +211,10 @@ def tail(self):
self.running = False
self.proc.stdout.close()
if self.proc.stderr:
for line in iter(self.proc.stderr.readline, ''):
if len(line) == 0:
break
self.err_logs.append(line.rstrip().decode('ASCII'))
self.proc.stderr.close()

def is_in_log(self, regex, start=0):
Expand All @@ -224,6 +229,18 @@ def is_in_log(self, regex, start=0):
logging.debug("Did not find '%s' in logs", regex)
return None

def is_in_stderr(self, regex):
"""Look for `regex` in stderr."""

ex = re.compile(regex)
for l in self.err_logs:
if ex.search(l):
logging.debug("Found '%s' in stderr", regex)
return l

logging.debug("Did not find '%s' in stderr", regex)
return None

def wait_for_logs(self, regexs, timeout=TIMEOUT):
"""Look for `regexs` in the logs.

Expand Down Expand Up @@ -637,8 +654,8 @@ def is_synced_with_bitcoin(self, info=None):
info = self.rpc.getinfo()
return 'warning_bitcoind_sync' not in info and 'warning_lightningd_sync' not in info

def start(self, wait_for_bitcoind_sync=True):
self.daemon.start()
def start(self, wait_for_bitcoind_sync=True, stderr=None):
self.daemon.start(stderr=stderr)
# Cache `getinfo`, we'll be using it a lot
self.info = self.rpc.getinfo()
# This shortcut is sufficient for our simple tests.
Expand Down Expand Up @@ -973,7 +990,7 @@ def get_nodes(self, num_nodes, opts=None):

def get_node(self, node_id=None, options=None, dbfile=None,
feerates=(15000, 7500, 3750), start=True,
wait_for_bitcoind_sync=True, **kwargs):
wait_for_bitcoind_sync=True, expect_fail=False, **kwargs):

node_id = self.get_node_id() if not node_id else node_id
port = self.get_next_port()
Expand Down Expand Up @@ -1004,8 +1021,15 @@ def get_node(self, node_id=None, options=None, dbfile=None,

if start:
try:
node.start(wait_for_bitcoind_sync)
# Capture stderr if we're failing
if expect_fail:
stderr = subprocess.PIPE
else:
stderr = None
node.start(wait_for_bitcoind_sync, stderr=stderr)
except Exception:
if expect_fail:
return node
node.daemon.stop()
raise
return node
Expand Down
8 changes: 8 additions & 0 deletions lightningd/plugin.c
Original file line number Diff line number Diff line change
Expand Up @@ -1102,6 +1102,14 @@ plugin_populate_init_request(struct plugin *plugin, struct jsonrpc_request *req)
list_for_each(&plugin->plugin_opts, opt, list) {
/* Trim the `--` that we added before */
name = opt->name + 2;
if (opt->value->as_bool) {
json_add_bool(req->stream, name, *opt->value->as_bool);
continue;
}
if (opt->value->as_int) {
json_add_s64(req->stream, name, *opt->value->as_int);
continue;
}
if (opt->value->as_str) {
json_add_string(req->stream, name, opt->value->as_str);
}
Expand Down