electrum

Electrum Bitcoin wallet
git clone https://git.parazyd.org/electrum
Log | Files | Refs | Submodules

commit 35f1f2905b79550fe062fe9db8ec1fa160eda121
parent 3eba26b3980719647d826dfb664e39fd51ef0893
Author: SomberNight <somber.night@protonmail.com>
Date:   Tue, 15 Sep 2020 18:35:16 +0200

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

related: #6583

Diffstat:
Melectrum/lnpeer.py | 1+
Melectrum/lnworker.py | 14+++++++++++---
2 files changed, 12 insertions(+), 3 deletions(-)

diff --git a/electrum/lnpeer.py b/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 diff --git a/electrum/lnworker.py b/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: