Skip to content

Commit

Permalink
fixup! gossmap: add NodeId class.
Browse files Browse the repository at this point in the history
Do not mix bytes and GossmapNodeId when accessing Gossmap.nodes dicts.

Therefore the definion got GossmapNodeId also needed to be pulled to the
beginning of the file.
  • Loading branch information
m-schmoock committed Aug 25, 2021
1 parent 81fbbd9 commit 4b7b65e
Showing 1 changed file with 32 additions and 32 deletions.
64 changes: 32 additions & 32 deletions contrib/pyln-client/pyln/client/gossmap.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,43 @@ def __init__(self, buf: bytes):
self.length = (length & GOSSIP_STORE_LEN_MASK)


class GossmapNodeId(object):
def __init__(self, buf: bytes):
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

def to_pubkey(self) -> PublicKey:
return PublicKey(self.nodeid)

def __eq__(self, other):
if not isinstance(other, GossmapNodeId):
return False

return self.nodeid == other.nodeid

def __hash__(self):
return self.nodeid.__hash__()

def __repr__(self):
return "GossmapNodeId[{}]".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 GossmapChannel(object):
"""A channel: fields of channel_announcement are in .fields, optional updates are in .updates_fields, which can be None if there has been no channel update."""
def __init__(self,
fields: Dict[str, Any],
announce_offset: int,
scid,
node1_id: bytes,
node2_id: bytes,
node1_id: GossmapNodeId,
node2_id: GossmapNodeId,
is_private: bool):
self.fields = fields
self.announce_offset = announce_offset
Expand Down Expand Up @@ -96,35 +125,6 @@ def __repr__(self):
return "GossmapHalfChannel[{}x{}]".format(str(self.channel.scid), self.direction)


class GossmapNodeId(object):
def __init__(self, buf: bytes):
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

def to_pubkey(self) -> PublicKey:
return PublicKey(self.nodeid)

def __eq__(self, other):
if not isinstance(other, GossmapNodeId):
return False

return self.nodeid == other.nodeid

def __hash__(self):
return self.nodeid.__hash__()

def __repr__(self):
return "GossmapNodeId[{}]".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.
Expand All @@ -146,7 +146,7 @@ def __init__(self, store_filename: str = "gossip_store"):
self.store_filename = store_filename
self.store_file = open(store_filename, "rb")
self.store_buf = bytes()
self.nodes: Dict[bytes, GossmapNode] = {}
self.nodes: Dict[GossmapNodeId, GossmapNode] = {}
self.channels: Dict[ShortChannelId, GossmapChannel] = {}
self._last_scid: str = None
version = self.store_file.read(1)
Expand Down

0 comments on commit 4b7b65e

Please sign in to comment.