Browse Source

pylightning: translate msat input to class Millisatoshi

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>
pr-2391
Rusty Russell 6 years ago
parent
commit
6e63d79159
  1. 11
      contrib/pylightning/lightning/plugin.py
  2. 1
      tests/test_plugin.py

11
contrib/pylightning/lightning/plugin.py

@ -1,6 +1,6 @@
from collections import OrderedDict from collections import OrderedDict
from enum import Enum from enum import Enum
from lightning import LightningRpc from lightning import LightningRpc, Millisatoshi
from threading import RLock from threading import RLock
import inspect import inspect
@ -293,6 +293,10 @@ class Plugin(object):
if isinstance(params, dict): if isinstance(params, dict):
for k, v in params.items(): for k, v in params.items():
if k in arguments: if k in arguments:
# Explicitly (try to) interpret as Millisatoshi if annotated
if func.__annotations__.get(k) == Millisatoshi:
arguments[k] = Millisatoshi(v)
else:
arguments[k] = v arguments[k] = v
else: else:
kwargs[k] = v kwargs[k] = v
@ -305,6 +309,9 @@ class Plugin(object):
if pos < len(params): if pos < len(params):
# Apply positional args if we have them # Apply positional args if we have them
if func.__annotations__.get(k) == Millisatoshi:
arguments[k] = Millisatoshi(params[pos])
else:
arguments[k] = params[pos] arguments[k] = params[pos]
elif sig.parameters[k].default is inspect.Signature.empty: elif sig.parameters[k].default is inspect.Signature.empty:
# This is a positional arg with no value passed # This is a positional arg with no value passed
@ -406,6 +413,8 @@ class Plugin(object):
Returns the last partial message that was not complete yet. Returns the last partial message that was not complete yet.
""" """
for payload in msgs[:-1]: 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)) request = self._parse_request(json.loads(payload))
# If this has an 'id'-field, it's a request and returns a # If this has an 'id'-field, it's a request and returns a

1
tests/test_plugin.py

@ -34,7 +34,6 @@ def test_option_passthrough(node_factory):
n.stop() n.stop()
@pytest.mark.xfail(strict=True)
def test_millisatoshi_passthrough(node_factory): def test_millisatoshi_passthrough(node_factory):
""" Ensure that Millisatoshi arguments and return work. """ Ensure that Millisatoshi arguments and return work.
""" """

Loading…
Cancel
Save