Skip to content

Commit 626a6d3

Browse files
m-schmoockrustyrussell
authored andcommitted
pyln-client/gossmap: Don't mix bytes and GossmapNodeId
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.
1 parent 8a8a5c3 commit 626a6d3

File tree

1 file changed

+32
-32
lines changed

1 file changed

+32
-32
lines changed

contrib/pyln-client/pyln/client/gossmap.py

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -50,14 +50,43 @@ def __repr__(self):
5050
return "GossmapHalfchannel[{}x{}]".format(str(self.channel.scid), self.direction)
5151

5252

53+
class GossmapNodeId(object):
54+
def __init__(self, buf: bytes):
55+
if len(buf) != 33 or (buf[0] != 2 and buf[0] != 3):
56+
raise ValueError("{} is not a valid node_id".format(buf.hex()))
57+
self.nodeid = buf
58+
59+
def to_pubkey(self) -> PublicKey:
60+
return PublicKey(self.nodeid)
61+
62+
def __eq__(self, other):
63+
if not isinstance(other, GossmapNodeId):
64+
return False
65+
66+
return self.nodeid == other.nodeid
67+
68+
def __hash__(self):
69+
return self.nodeid.__hash__()
70+
71+
def __repr__(self):
72+
return "GossmapNodeId[{}]".format(self.nodeid.hex())
73+
74+
def from_str(self, s: str):
75+
if s.startswith('0x'):
76+
s = s[2:]
77+
if len(s) != 67:
78+
raise ValueError(f"{s} is not a valid hexstring of a node_id")
79+
return GossmapNodeId(bytes.fromhex(s))
80+
81+
5382
class GossmapChannel(object):
5483
"""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."""
5584
def __init__(self,
5685
fields: Dict[str, Any],
5786
announce_offset: int,
5887
scid,
59-
node1_id: bytes,
60-
node2_id: bytes,
88+
node1_id: GossmapNodeId,
89+
node2_id: GossmapNodeId,
6190
is_private: bool):
6291
self.fields = fields
6392
self.announce_offset = announce_offset
@@ -96,35 +125,6 @@ def __repr__(self):
96125
return "GossmapChannel[{}]".format(str(self.scid))
97126

98127

99-
class GossmapNodeId(object):
100-
def __init__(self, buf: bytes):
101-
if len(buf) != 33 or (buf[0] != 2 and buf[0] != 3):
102-
raise ValueError("{} is not a valid node_id".format(buf.hex()))
103-
self.nodeid = buf
104-
105-
def to_pubkey(self) -> PublicKey:
106-
return PublicKey(self.nodeid)
107-
108-
def __eq__(self, other):
109-
if not isinstance(other, GossmapNodeId):
110-
return False
111-
112-
return self.nodeid == other.nodeid
113-
114-
def __hash__(self):
115-
return self.nodeid.__hash__()
116-
117-
def __repr__(self):
118-
return "GossmapNodeId[{}]".format(self.nodeid.hex())
119-
120-
def from_str(self, s: str):
121-
if s.startswith('0x'):
122-
s = s[2:]
123-
if len(s) != 67:
124-
raise ValueError(f"{s} is not a valid hexstring of a node_id")
125-
return GossmapNodeId(bytes.fromhex(s))
126-
127-
128128
class GossmapNode(object):
129129
"""A node: fields of node_announcement are in .announce_fields, which can be None of there has been no node announcement.
130130
@@ -146,7 +146,7 @@ def __init__(self, store_filename: str = "gossip_store"):
146146
self.store_filename = store_filename
147147
self.store_file = open(store_filename, "rb")
148148
self.store_buf = bytes()
149-
self.nodes: Dict[bytes, GossmapNode] = {}
149+
self.nodes: Dict[GossmapNodeId, GossmapNode] = {}
150150
self.channels: Dict[ShortChannelId, GossmapChannel] = {}
151151
self._last_scid: str = None
152152
version = self.store_file.read(1)

0 commit comments

Comments
 (0)