Skip to content

Commit

Permalink
pylightning: translate msat input to class Millisatoshi
Browse files Browse the repository at this point in the history
Rather than using LightningJSONDecoder's implicit "field name and
value ends in msat, try converting to Millisatoshi", we do it to
parameters using type annotations.

If you had a parameter which was an array or dict itself, we don't
delve into that, but that's probably OK.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
  • Loading branch information
rustyrussell committed Feb 25, 2019
1 parent 690064f commit 6e63d79
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
15 changes: 12 additions & 3 deletions contrib/pylightning/lightning/plugin.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from collections import OrderedDict
from enum import Enum
from lightning import LightningRpc
from lightning import LightningRpc, Millisatoshi
from threading import RLock

import inspect
Expand Down Expand Up @@ -293,7 +293,11 @@ def _exec_func(self, func, request):
if isinstance(params, dict):
for k, v in params.items():
if k in arguments:
arguments[k] = v
# Explicitly (try to) interpret as Millisatoshi if annotated
if func.__annotations__.get(k) == Millisatoshi:
arguments[k] = Millisatoshi(v)
else:
arguments[k] = v
else:
kwargs[k] = v
else:
Expand All @@ -305,7 +309,10 @@ def _exec_func(self, func, request):

if pos < len(params):
# Apply positional args if we have them
arguments[k] = params[pos]
if func.__annotations__.get(k) == Millisatoshi:
arguments[k] = Millisatoshi(params[pos])
else:
arguments[k] = params[pos]
elif sig.parameters[k].default is inspect.Signature.empty:
# This is a positional arg with no value passed
raise TypeError("Missing required parameter: %s" % sig.parameters[k])
Expand Down Expand Up @@ -406,6 +413,8 @@ def _multi_dispatch(self, msgs):
Returns the last partial message that was not complete yet.
"""
for payload in msgs[:-1]:
# Note that we use function annotations to do Millisatoshi conversions
# in _exec_func, so we don't use LightningJSONDecoder here.
request = self._parse_request(json.loads(payload))

# If this has an 'id'-field, it's a request and returns a
Expand Down
1 change: 0 additions & 1 deletion tests/test_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ def test_option_passthrough(node_factory):
n.stop()


@pytest.mark.xfail(strict=True)
def test_millisatoshi_passthrough(node_factory):
""" Ensure that Millisatoshi arguments and return work.
"""
Expand Down

0 comments on commit 6e63d79

Please sign in to comment.