Skip to content

Commit b4a8cbf

Browse files
committed
pyln-client: fix mypy warnings, fix and test deletion of a channel.
This only happens when a deletion is added by a running gossipd, so we put a deletion at the end of the store to test it. mypy noticed that this code was nonsensical, so clearly untested. The testing noticed that making a nodeid from a string was also buggy. Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
1 parent 5187a1e commit b4a8cbf

File tree

3 files changed

+19
-14
lines changed

3 files changed

+19
-14
lines changed

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

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ def __repr__(self):
5454

5555

5656
class GossmapNodeId(object):
57-
def __init__(self, buf: bytes):
57+
def __init__(self, buf: Union[bytes, str]):
5858
if isinstance(buf, str):
5959
buf = bytes.fromhex(buf)
6060
if len(buf) != 33 or (buf[0] != 2 and buf[0] != 3):
@@ -84,7 +84,7 @@ def __repr__(self):
8484
def from_str(cls, s: str):
8585
if s.startswith('0x'):
8686
s = s[2:]
87-
if len(s) != 67:
87+
if len(s) != 66:
8888
raise ValueError(f"{s} is not a valid hexstring of a node_id")
8989
return cls(bytes.fromhex(s))
9090

@@ -140,7 +140,7 @@ class GossmapNode(object):
140140
141141
.channels is a list of the GossmapChannels attached to this node.
142142
"""
143-
def __init__(self, node_id: GossmapNodeId):
143+
def __init__(self, node_id: Union[GossmapNodeId, bytes, str]):
144144
if isinstance(node_id, bytes) or isinstance(node_id, str):
145145
node_id = GossmapNodeId(node_id)
146146
self.announce_fields: Optional[Dict[str, Any]] = None
@@ -194,15 +194,14 @@ def _new_channel(self,
194194

195195
def _del_channel(self, scid: ShortChannelId):
196196
c = self.channels[scid]
197-
n1 = self.nodes[c.node1_id]
198-
n2 = self.nodes[c.node2_id]
199-
n1.channels.remove(c)
200-
n2.channels.remove(c)
197+
del self.channels[scid]
198+
c.node1.channels.remove(c)
199+
c.node2.channels.remove(c)
201200
# Beware self-channels n1-n1!
202-
if len(n1.channels) == 0 and n1 != n2:
203-
del self.nodes[c.node1_id]
204-
if len(n2.channels):
205-
del self.nodes[c.node2_id]
201+
if len(c.node1.channels) == 0 and c.node1 != c.node2:
202+
del self.nodes[c.node1.node_id]
203+
if len(c.node2.channels) == 0:
204+
del self.nodes[c.node2.node_id]
206205

207206
def _add_channel(self, rec: bytes, off: int, is_private: bool):
208207
fields = channel_announcement.read(io.BytesIO(rec[2:]), {})
@@ -252,7 +251,8 @@ def reopen_store(self):
252251
assert False
253252

254253
def _remove_channel_by_deletemsg(self, rec: bytes):
255-
scid, = struct.unpack(">Q", rec[2:])
254+
scidint, = struct.unpack(">Q", rec[2:])
255+
scid = ShortChannelId.from_int(scidint)
256256
# It might have already been deleted when we skipped it.
257257
if scid in self.channels:
258258
self._del_channel(scid)
0 Bytes
Binary file not shown.

contrib/pyln-client/tests/test_gossmap.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,12 @@ def test_gossmap(tmp_path):
2828

2929
g.refresh()
3030

31+
# This actually deletes a channel, which deletes a node.
32+
assert g.get_channel("686386x1093x1") is None
33+
assert g.get_node('029deaf9d2fba868fe0a124050f0a13e021519a12f41bea34f391fe7533fb3166d') is None
34+
# The other node is untouched
35+
assert g.get_node('02e0af3c70bf42343316513e54683b10c01d906c04a05dfcd9479b90d7beed9129')
36+
3137
# It will notice the new ones.
3238
assert chans < len(g.channels)
3339
assert nodes < len(g.nodes)
@@ -38,9 +44,8 @@ def test_gossmap(tmp_path):
3844
assert set(g.nodes.keys()) == set(g2.nodes.keys())
3945

4046
# Check some details
41-
channel1 = g.get_channel("686386x1093x1")
4247
channel2 = g.get_channel("686200x1137x0")
43-
assert channel1.satoshis == 1000000
48+
assert g.get_channel("686386x1093x1") is None
4449
assert channel2.satoshis == 3000000
4550

4651

0 commit comments

Comments
 (0)