pyln.proto.message: expose fundamental MessageTypes as variables.

Suggested-by: Christian Decker
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
Rusty Russell 2020-06-04 12:11:45 +09:30 committed by Christian Decker
parent 47631cc23c
commit 42aab89b9f
3 changed files with 27 additions and 30 deletions

View File

@ -7,4 +7,20 @@ __all__ = [
"MessageType",
"Message",
"SubtypeType",
# fundamental_types
'byte',
'u16',
'u32',
'u64',
'tu16',
'tu32',
'tu64',
'chain_hash',
'channel_id',
'sha256',
'point',
'short_channel_id',
'signature',
'bigsize',
]

View File

@ -1,5 +1,6 @@
import struct
import io
import sys
from typing import Optional
@ -235,3 +236,9 @@ def fundamental_types():
# FIXME: See https://github.com/lightningnetwork/lightning-rfc/pull/778
BigSizeType('varint'),
]
# Expose these as native types.
mod = sys.modules[FieldType.__module__]
for m in fundamental_types():
setattr(mod, m.name, m)

View File

@ -1,18 +1,10 @@
#! /usr/bin/python3
from pyln.proto.message.fundamental_types import fundamental_types
from pyln.proto.message.fundamental_types import byte, u16, short_channel_id
from pyln.proto.message.array_types import SizedArrayType, DynamicArrayType, EllipsisArrayType, LengthFieldType
import io
def test_sized_array():
# Steal two fundamental types for testing
for t in fundamental_types():
if t.name == 'byte':
byte = t
if t.name == 'u16':
u16 = t
if t.name == 'short_channel_id':
scid = t
# Simple class to make outer work.
class dummy:
@ -25,7 +17,7 @@ def test_sized_array():
[SizedArrayType(dummy("test2"), "test_arr", u16, 4),
"[0,1,2,256]",
bytes([0, 0, 0, 1, 0, 2, 1, 0])],
[SizedArrayType(dummy("test3"), "test_arr", scid, 4),
[SizedArrayType(dummy("test3"), "test_arr", short_channel_id, 4),
"[1x2x3,4x5x6,7x8x9,10x11x12]",
bytes([0, 0, 1, 0, 0, 2, 0, 3]
+ [0, 0, 4, 0, 0, 5, 0, 6]
@ -41,15 +33,6 @@ def test_sized_array():
def test_ellipsis_array():
# Steal two fundamental types for testing
for t in fundamental_types():
if t.name == 'byte':
byte = t
if t.name == 'u16':
u16 = t
if t.name == 'short_channel_id':
scid = t
# Simple class to make outer work.
class dummy:
def __init__(self, name):
@ -61,7 +44,7 @@ def test_ellipsis_array():
[EllipsisArrayType(dummy("test2"), "test_arr", u16),
"[0,1,2,256]",
bytes([0, 0, 0, 1, 0, 2, 1, 0])],
[EllipsisArrayType(dummy("test3"), "test_arr", scid),
[EllipsisArrayType(dummy("test3"), "test_arr", short_channel_id),
"[1x2x3,4x5x6,7x8x9,10x11x12]",
bytes([0, 0, 1, 0, 0, 2, 0, 3]
+ [0, 0, 4, 0, 0, 5, 0, 6]
@ -77,15 +60,6 @@ def test_ellipsis_array():
def test_dynamic_array():
# Steal two fundamental types for testing
for t in fundamental_types():
if t.name == 'byte':
byte = t
if t.name == 'u16':
u16 = t
if t.name == 'short_channel_id':
scid = t
# Simple class to make outer.
class dummy:
def __init__(self, name):
@ -106,7 +80,7 @@ def test_dynamic_array():
lenfield),
"[0,1,2,256]",
bytes([0, 0, 0, 1, 0, 2, 1, 0])],
[DynamicArrayType(dummy("test3"), "test_arr", scid,
[DynamicArrayType(dummy("test3"), "test_arr", short_channel_id,
lenfield),
"[1x2x3,4x5x6,7x8x9,10x11x12]",
bytes([0, 0, 1, 0, 0, 2, 0, 3]