From 819044221b5e8090d36de5c5f9d5245f49c2988e Mon Sep 17 00:00:00 2001 From: SomberNight Date: Thu, 13 Sep 2018 19:00:21 +0200 Subject: [PATCH] verifier: need to wait for reorg fixes race between verifier and block header download. scenario: client starts, connects to server. while client was offline, there was a reorg. txn A was not mined in the old chain, but is mined after reorg. client subscribes to addresses and starts downloading headers, concurrently. server tells client txn A is mined at height H >= reorg height. client sees it has block header at height H, asks for SPV proof for txn A. but the header the client has is still the old one, the verifier was faster than the block header download (race...). client receives proof. proof is incorrect for old header. client disconnects. --- electrum/verifier.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/electrum/verifier.py b/electrum/verifier.py index 3b502e7af..0f13195e6 100644 --- a/electrum/verifier.py +++ b/electrum/verifier.py @@ -90,7 +90,9 @@ class SPV(ThreadJob): tx_height = merkle.get('block_height') pos = merkle.get('pos') merkle_branch = merkle.get('merkle') - header = self.network.blockchain().read_header(tx_height) + # we need to wait if header sync/reorg is still ongoing, hence lock: + async with self.network.bhi_lock: + header = self.network.blockchain().read_header(tx_height) try: verify_tx_is_in_block(tx_hash, merkle_branch, pos, header, tx_height) except MerkleVerificationFailure as e: