Skip to content

Commit

Permalink
pyln-client/gossmap: init GossmapNode and Id also with hexstring
Browse files Browse the repository at this point in the history
also improves test coverage
  • Loading branch information
m-schmoock authored and rustyrussell committed Sep 2, 2021
1 parent 9564166 commit 4b6938e
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
4 changes: 4 additions & 0 deletions contrib/pyln-client/pyln/client/gossmap.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ def __repr__(self):

class GossmapNodeId(object):
def __init__(self, buf: bytes):
if isinstance(buf, str):
buf = bytes.fromhex(buf)
if len(buf) != 33 or (buf[0] != 2 and buf[0] != 3):
raise ValueError("{} is not a valid node_id".format(buf.hex()))
self.nodeid = buf
Expand Down Expand Up @@ -140,6 +142,8 @@ class GossmapNode(object):
.channels is a list of the GossmapChannels attached to this node.
"""
def __init__(self, node_id: GossmapNodeId):
if isinstance(node_id, bytes) or isinstance(node_id, str):
node_id = GossmapNodeId(node_id)
self.announce_fields: Optional[Dict[str, Any]] = None
self.announce_offset: Optional[int] = None
self.channels: List[GossmapChannel] = []
Expand Down
27 changes: 26 additions & 1 deletion contrib/pyln-client/tests/test_gossmap.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from pyln.client import Gossmap
from pyln.client import Gossmap, GossmapNode, GossmapNodeId

import os.path
import lzma
Expand Down Expand Up @@ -42,3 +42,28 @@ def test_gossmap(tmp_path):
channel2 = g.get_channel("686200x1137x0")
assert channel1.satoshis == 1000000
assert channel2.satoshis == 3000000


def test_objects():
boltz = "026165850492521f4ac8abd9bd8088123446d126f648ca35e60f88177dc149ceb2"
acinq = "03864ef025fde8fb587d989186ce6a4a186895ee44a926bfc370e2c366597a3f8f"

boltz_id = GossmapNodeId(bytes.fromhex(boltz))
acinq_id = GossmapNodeId(bytes.fromhex(acinq))
assert boltz_id == GossmapNodeId(boltz)

assert boltz_id < acinq_id
assert acinq_id > boltz_id
assert boltz_id != acinq_id
assert acinq_id != boltz_id
assert not boltz_id > acinq_id
assert not acinq_id < boltz_id
assert not boltz_id == acinq_id
assert not acinq_id == boltz_id

boltz_node = GossmapNode(boltz_id)
acinq_node = GossmapNode(acinq_id)
assert boltz_node == GossmapNode(boltz)
assert boltz_node < acinq_node
assert acinq_node > boltz_node
assert boltz_node != acinq_node

0 comments on commit 4b6938e

Please sign in to comment.