Skip to content

Commit 6e63d79

Browse files
committed
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 <[email protected]>
1 parent 690064f commit 6e63d79

File tree

2 files changed

+12
-4
lines changed

2 files changed

+12
-4
lines changed

contrib/pylightning/lightning/plugin.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from collections import OrderedDict
22
from enum import Enum
3-
from lightning import LightningRpc
3+
from lightning import LightningRpc, Millisatoshi
44
from threading import RLock
55

66
import inspect
@@ -293,7 +293,11 @@ def _exec_func(self, func, request):
293293
if isinstance(params, dict):
294294
for k, v in params.items():
295295
if k in arguments:
296-
arguments[k] = v
296+
# Explicitly (try to) interpret as Millisatoshi if annotated
297+
if func.__annotations__.get(k) == Millisatoshi:
298+
arguments[k] = Millisatoshi(v)
299+
else:
300+
arguments[k] = v
297301
else:
298302
kwargs[k] = v
299303
else:
@@ -305,7 +309,10 @@ def _exec_func(self, func, request):
305309

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

411420
# If this has an 'id'-field, it's a request and returns a

tests/test_plugin.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ def test_option_passthrough(node_factory):
3434
n.stop()
3535

3636

37-
@pytest.mark.xfail(strict=True)
3837
def test_millisatoshi_passthrough(node_factory):
3938
""" Ensure that Millisatoshi arguments and return work.
4039
"""

0 commit comments

Comments
 (0)