From 77d86f074f5134bf496867991d1043445d087a26 Mon Sep 17 00:00:00 2001 From: SomberNight Date: Sat, 8 Sep 2018 19:11:02 +0200 Subject: [PATCH] verifier: don't try to request same chunk multiple times --- electrum/network.py | 11 +++++++++-- electrum/verifier.py | 2 +- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/electrum/network.py b/electrum/network.py index bf1709c61..d51b8ab15 100644 --- a/electrum/network.py +++ b/electrum/network.py @@ -751,14 +751,21 @@ class Network(PrintError): return False, "error: " + out return True, out - async def request_chunk(self, height, tip, session=None): + async def request_chunk(self, height, tip, session=None, can_return_early=False): if session is None: session = self.interface.session index = height // 2016 + if can_return_early and index in self.requested_chunks: + return size = 2016 if tip is not None: size = min(size, tip - index * 2016) size = max(size, 0) - res = await asyncio.wait_for(session.send_request('blockchain.block.headers', [index * 2016, size]), 20) + try: + self.requested_chunks.add(index) + res = await asyncio.wait_for(session.send_request('blockchain.block.headers', [index * 2016, size]), 20) + finally: + try: self.requested_chunks.remove(index) + except KeyError: pass conn = self.blockchain().connect_chunk(index, res['hex']) if not conn: return conn, 0 diff --git a/electrum/verifier.py b/electrum/verifier.py index 1013648b5..493d402f0 100644 --- a/electrum/verifier.py +++ b/electrum/verifier.py @@ -70,7 +70,7 @@ class SPV(ThreadJob): if header is None: index = tx_height // 2016 if index < len(blockchain.checkpoints): - await interface.group.spawn(self.network.request_chunk, tx_height, None) + await interface.group.spawn(self.network.request_chunk(tx_height, None, can_return_early=True)) elif (tx_hash not in self.requested_merkle and tx_hash not in self.merkle_roots): self.print_error('requested merkle', tx_hash)