mirror of
https://github.com/aljazceru/lightning.git
synced 2025-12-19 15:14:23 +01:00
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>
This commit is contained in:
@@ -54,7 +54,7 @@ class GossmapHalfchannel(object):
|
|||||||
|
|
||||||
|
|
||||||
class GossmapNodeId(object):
|
class GossmapNodeId(object):
|
||||||
def __init__(self, buf: bytes):
|
def __init__(self, buf: Union[bytes, str]):
|
||||||
if isinstance(buf, str):
|
if isinstance(buf, str):
|
||||||
buf = bytes.fromhex(buf)
|
buf = bytes.fromhex(buf)
|
||||||
if len(buf) != 33 or (buf[0] != 2 and buf[0] != 3):
|
if len(buf) != 33 or (buf[0] != 2 and buf[0] != 3):
|
||||||
@@ -84,7 +84,7 @@ class GossmapNodeId(object):
|
|||||||
def from_str(cls, s: str):
|
def from_str(cls, s: str):
|
||||||
if s.startswith('0x'):
|
if s.startswith('0x'):
|
||||||
s = s[2:]
|
s = s[2:]
|
||||||
if len(s) != 67:
|
if len(s) != 66:
|
||||||
raise ValueError(f"{s} is not a valid hexstring of a node_id")
|
raise ValueError(f"{s} is not a valid hexstring of a node_id")
|
||||||
return cls(bytes.fromhex(s))
|
return cls(bytes.fromhex(s))
|
||||||
|
|
||||||
@@ -140,7 +140,7 @@ class GossmapNode(object):
|
|||||||
|
|
||||||
.channels is a list of the GossmapChannels attached to this node.
|
.channels is a list of the GossmapChannels attached to this node.
|
||||||
"""
|
"""
|
||||||
def __init__(self, node_id: GossmapNodeId):
|
def __init__(self, node_id: Union[GossmapNodeId, bytes, str]):
|
||||||
if isinstance(node_id, bytes) or isinstance(node_id, str):
|
if isinstance(node_id, bytes) or isinstance(node_id, str):
|
||||||
node_id = GossmapNodeId(node_id)
|
node_id = GossmapNodeId(node_id)
|
||||||
self.announce_fields: Optional[Dict[str, Any]] = None
|
self.announce_fields: Optional[Dict[str, Any]] = None
|
||||||
@@ -194,15 +194,14 @@ class Gossmap(object):
|
|||||||
|
|
||||||
def _del_channel(self, scid: ShortChannelId):
|
def _del_channel(self, scid: ShortChannelId):
|
||||||
c = self.channels[scid]
|
c = self.channels[scid]
|
||||||
n1 = self.nodes[c.node1_id]
|
del self.channels[scid]
|
||||||
n2 = self.nodes[c.node2_id]
|
c.node1.channels.remove(c)
|
||||||
n1.channels.remove(c)
|
c.node2.channels.remove(c)
|
||||||
n2.channels.remove(c)
|
|
||||||
# Beware self-channels n1-n1!
|
# Beware self-channels n1-n1!
|
||||||
if len(n1.channels) == 0 and n1 != n2:
|
if len(c.node1.channels) == 0 and c.node1 != c.node2:
|
||||||
del self.nodes[c.node1_id]
|
del self.nodes[c.node1.node_id]
|
||||||
if len(n2.channels):
|
if len(c.node2.channels) == 0:
|
||||||
del self.nodes[c.node2_id]
|
del self.nodes[c.node2.node_id]
|
||||||
|
|
||||||
def _add_channel(self, rec: bytes, off: int, is_private: bool):
|
def _add_channel(self, rec: bytes, off: int, is_private: bool):
|
||||||
fields = channel_announcement.read(io.BytesIO(rec[2:]), {})
|
fields = channel_announcement.read(io.BytesIO(rec[2:]), {})
|
||||||
@@ -252,7 +251,8 @@ class Gossmap(object):
|
|||||||
assert False
|
assert False
|
||||||
|
|
||||||
def _remove_channel_by_deletemsg(self, rec: bytes):
|
def _remove_channel_by_deletemsg(self, rec: bytes):
|
||||||
scid, = struct.unpack(">Q", rec[2:])
|
scidint, = struct.unpack(">Q", rec[2:])
|
||||||
|
scid = ShortChannelId.from_int(scidint)
|
||||||
# It might have already been deleted when we skipped it.
|
# It might have already been deleted when we skipped it.
|
||||||
if scid in self.channels:
|
if scid in self.channels:
|
||||||
self._del_channel(scid)
|
self._del_channel(scid)
|
||||||
|
|||||||
Binary file not shown.
@@ -28,6 +28,12 @@ def test_gossmap(tmp_path):
|
|||||||
|
|
||||||
g.refresh()
|
g.refresh()
|
||||||
|
|
||||||
|
# This actually deletes a channel, which deletes a node.
|
||||||
|
assert g.get_channel("686386x1093x1") is None
|
||||||
|
assert g.get_node('029deaf9d2fba868fe0a124050f0a13e021519a12f41bea34f391fe7533fb3166d') is None
|
||||||
|
# The other node is untouched
|
||||||
|
assert g.get_node('02e0af3c70bf42343316513e54683b10c01d906c04a05dfcd9479b90d7beed9129')
|
||||||
|
|
||||||
# It will notice the new ones.
|
# It will notice the new ones.
|
||||||
assert chans < len(g.channels)
|
assert chans < len(g.channels)
|
||||||
assert nodes < len(g.nodes)
|
assert nodes < len(g.nodes)
|
||||||
@@ -38,9 +44,8 @@ def test_gossmap(tmp_path):
|
|||||||
assert set(g.nodes.keys()) == set(g2.nodes.keys())
|
assert set(g.nodes.keys()) == set(g2.nodes.keys())
|
||||||
|
|
||||||
# Check some details
|
# Check some details
|
||||||
channel1 = g.get_channel("686386x1093x1")
|
|
||||||
channel2 = g.get_channel("686200x1137x0")
|
channel2 = g.get_channel("686200x1137x0")
|
||||||
assert channel1.satoshis == 1000000
|
assert g.get_channel("686386x1093x1") is None
|
||||||
assert channel2.satoshis == 3000000
|
assert channel2.satoshis == 3000000
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user