pyln-client/gossmap: adds helpers to get channels and nodes

This commit is contained in:
Michael Schmoock 2021-09-07 13:39:16 +09:30 committed by Rusty Russell
parent fd16535f68
commit c5c909acd3

View File

@ -79,6 +79,12 @@ class GossmapChannel(object):
fields['fee_proportional_millionths'])
self.half_channels[direction] = half
def get_half_channel(self, direction: int):
""" returns the GossmapHalfchannel if known by channel_update """
if not 0 <= direction <= 1:
raise ValueError("direction can only be 0 or 1")
return self.half_channels[direction]
class GossmapNodeId(object):
def __init__(self, buf: bytes):
@ -101,6 +107,13 @@ class GossmapNodeId(object):
def __repr__(self):
return "GossmapNodeId[0x{}]".format(self.nodeid.hex())
def from_str(self, s: str):
if s.startswith('0x'):
s = s[2:]
if len(s) != 67:
raise ValueError(f"{s} is not a valid hexstring of a node_id")
return GossmapNodeId(bytes.fromhex(s))
class GossmapNode(object):
"""A node: fields of node_announcement are in .announce_fields, which can be None of there has been no node announcement.
@ -166,6 +179,18 @@ class Gossmap(object):
GossmapNodeId(fields['node_id_1']), GossmapNodeId(fields['node_id_2']),
is_private)
def get_channel(self, short_channel_id: ShortChannelId):
""" Resolves a channel by its short channel id """
if type(short_channel_id) == str:
short_channel_id = ShortChannelId.from_str(short_channel_id)
return self.channels.get(short_channel_id)
def get_node(self, node_id: GossmapNodeId):
""" Resolves a node by its public key node_id """
if type(node_id) == str:
node_id = GossmapNodeId.from_str(node_id)
return self.nodes.get(node_id)
def update_channel(self, rec: bytes, off: int):
fields = channel_update.read(io.BytesIO(rec[2:]), {})
direction = fields['channel_flags'] & 1