Browse Source

lnworker: fix confusion re what is in self.peers

dependabot/pip/contrib/deterministic-build/ecdsa-0.13.3
SomberNight 7 years ago
committed by ThomasV
parent
commit
362a3a5a44
  1. 13
      electrum/lnworker.py

13
electrum/lnworker.py

@ -42,7 +42,7 @@ class LNWorker(PrintError):
self.privkey = bfh(pk) self.privkey = bfh(pk)
self.pubkey = privkey_to_pubkey(self.privkey) self.pubkey = privkey_to_pubkey(self.privkey)
self.config = network.config self.config = network.config
self.peers = {} self.peers = {} # pubkey -> Peer
self.channels = {x.channel_id: x for x in map(HTLCStateMachine, wallet.storage.get("channels", []))} self.channels = {x.channel_id: x for x in map(HTLCStateMachine, wallet.storage.get("channels", []))}
self.invoices = wallet.storage.get('lightning_invoices', {}) self.invoices = wallet.storage.get('lightning_invoices', {})
for chan_id, chan in self.channels.items(): for chan_id, chan in self.channels.items():
@ -77,11 +77,12 @@ class LNWorker(PrintError):
def add_peer(self, host, port, node_id): def add_peer(self, host, port, node_id):
port = int(port) port = int(port)
peer_addr = LNPeerAddr(host, port, node_id) peer_addr = LNPeerAddr(host, port, node_id)
if peer_addr in self.peers: if node_id in self.peers:
return return
if peer_addr in self._last_tried_peer: if peer_addr in self._last_tried_peer:
return return
self._last_tried_peer[peer_addr] = time.time() self._last_tried_peer[peer_addr] = time.time()
self.print_error("adding peer", peer_addr)
peer = Peer(self, host, port, node_id, request_initial_sync=self.config.get("request_initial_sync", True)) peer = Peer(self, host, port, node_id, request_initial_sync=self.config.get("request_initial_sync", True))
self.network.futures.append(asyncio.run_coroutine_threadsafe(peer.main_loop(), asyncio.get_event_loop())) self.network.futures.append(asyncio.run_coroutine_threadsafe(peer.main_loop(), asyncio.get_event_loop()))
self.peers[node_id] = peer self.peers[node_id] = peer
@ -243,7 +244,7 @@ class LNWorker(PrintError):
del self._last_tried_peer[peer] del self._last_tried_peer[peer]
# first try from recent peers # first try from recent peers
for peer in recent_peers: for peer in recent_peers:
if peer in self.peers: continue if peer.pubkey in self.peers: continue
if peer in self._last_tried_peer: continue if peer in self._last_tried_peer: continue
return [peer] return [peer]
# try random peer from graph # try random peer from graph
@ -260,7 +261,7 @@ class LNWorker(PrintError):
if not addresses: continue if not addresses: continue
host, port = addresses[0] host, port = addresses[0]
peer = LNPeerAddr(host, port, node_id) peer = LNPeerAddr(host, port, node_id)
if peer in self.peers: continue if peer.pubkey in self.peers: continue
if peer in self._last_tried_peer: continue if peer in self._last_tried_peer: continue
self.print_error('taking random ln peer from our channel db') self.print_error('taking random ln peer from our channel db')
return [peer] return [peer]
@ -312,10 +313,10 @@ class LNWorker(PrintError):
async def main_loop(self): async def main_loop(self):
while True: while True:
await asyncio.sleep(1) await asyncio.sleep(1)
for k, peer in list(self.peers.items()): for node_id, peer in list(self.peers.items()):
if peer.exception: if peer.exception:
self.print_error("removing peer", peer.host) self.print_error("removing peer", peer.host)
self.peers.pop(k) self.peers.pop(node_id)
if len(self.peers) >= NUM_PEERS_TARGET: if len(self.peers) >= NUM_PEERS_TARGET:
continue continue
peers = self._get_next_peers_to_try() peers = self._get_next_peers_to_try()

Loading…
Cancel
Save