Browse Source

lnpeer: forbid creating Peer with ourselves (self-connect)

related: #6583
patch-4
SomberNight 4 years ago
parent
commit
35f1f2905b
No known key found for this signature in database GPG Key ID: B33B5F232C6271E9
  1. 1
      electrum/lnpeer.py
  2. 14
      electrum/lnworker.py

1
electrum/lnpeer.py

@ -82,6 +82,7 @@ class Peer(Logger):
self.features = self.lnworker.features
self.their_features = 0
self.node_ids = [self.pubkey, privkey_to_pubkey(self.privkey)]
assert self.node_ids[0] != self.node_ids[1]
self.network = lnworker.network
self.channel_db = lnworker.network.channel_db
self.ping_time = 0

14
electrum/lnworker.py

@ -144,6 +144,9 @@ class NoPathFound(PaymentFailure):
return _('No path found')
class ErrorAddingPeer(Exception): pass
class LNWorker(Logger, NetworkRetryManager[LNPeerAddr]):
def __init__(self, xprv):
@ -226,17 +229,22 @@ class LNWorker(Logger, NetworkRetryManager[LNPeerAddr]):
peers = await self._get_next_peers_to_try()
for peer in peers:
if self._can_retry_addr(peer, now=now):
await self._add_peer(peer.host, peer.port, peer.pubkey)
try:
await self._add_peer(peer.host, peer.port, peer.pubkey)
except ErrorAddingPeer as e:
self.logger.info(f"failed to add peer: {peer}. exc: {e!r}")
async def _add_peer(self, host: str, port: int, node_id: bytes) -> Peer:
if node_id in self._peers:
return self._peers[node_id]
port = int(port)
peer_addr = LNPeerAddr(host, port, node_id)
transport = LNTransport(self.node_keypair.privkey, peer_addr,
proxy=self.network.proxy)
self._trying_addr_now(peer_addr)
self.logger.info(f"adding peer {peer_addr}")
if node_id == self.node_keypair.pubkey:
raise ErrorAddingPeer("cannot connect to self")
transport = LNTransport(self.node_keypair.privkey, peer_addr,
proxy=self.network.proxy)
peer = Peer(self, node_id, transport)
await self.taskgroup.spawn(peer.main_loop())
with self.lock:

Loading…
Cancel
Save