# Buggy code
def run(self):
    lh = self.network.get_local_height()
    unverified = self.wallet.get_unverified_txs()
    for tx_hash, tx_height in unverified.items():
        # do not request merkle branch before headers are available
        if (tx_height > 0) and (tx_height <= lh):
            header = self.network.blockchain().read_header(tx_height)
            index = tx_height // 2016
            # print(index, header)
            if header is None:
                if index not in self.requested_chunks and self.network.interface:
                    print("requesting chunk", index)
                    # request = ('blockchain.block.get_chunk', [index])
                    # self.network.send([request], self.verify_merkle)
                    self.requested_chunks[index] = None
                    self.network.request_chunk(self.network.interface, index)
            else:
                if tx_hash not in self.merkle_roots:
                    request = (
                        "blockchain.transaction.get_merkle",
                        [tx_hash, tx_height],
                    )
                    self.network.send([request], self.verify_merkle)
                    self.print_error("requested merkle", tx_hash)
                    self.merkle_roots[tx_hash] = None

    if self.network.blockchain() != self.blockchain:
        self.blockchain = self.network.blockchain()
        self.undo_verifications()


# Traceback (AssertionError)
Exception in thread Thread-3:
Traceback (most recent call last):
File "/usr/lib/python3.5/threading.py", line 914, in _bootstrap_inner
self.run()
File "/home/user/wspace/electrum/lib/network.py", line 966, in run
self.wait_on_sockets()
File "/home/user/wspace/electrum/lib/network.py", line 948, in wait_on_sockets
self.process_responses(interface)
File "/home/user/wspace/electrum/lib/network.py", line 612, in process_responses
self.process_response(interface, response, callbacks)
File "/home/user/wspace/electrum/lib/network.py", line 558, in process_response
self.on_get_header(interface, response)
File "/home/user/wspace/electrum/lib/network.py", line 807, in on_get_header
chain = blockchain.check_header(header)
File "/home/user/wspace/electrum/lib/blockchain.py", line 85, in check_header
if b.check_header(header):
File "/home/user/wspace/electrum/lib/blockchain.py", line 131, in check_header
return header_hash == self.get_hash(height)
File "/home/user/wspace/electrum/lib/blockchain.py", line 266, in get_hash
assert (height+1) % 2016 == 0
AssertionError

# Fixed code
def run(self):
    lh = self.network.get_local_height()
    unverified = self.wallet.get_unverified_txs()
    for tx_hash, tx_height in unverified.items():
        # do not request merkle branch before headers are available
        if (tx_height > 0) and (tx_height <= lh):
            header = self.network.blockchain().read_header(tx_height)
            if header is None and self.network.interface:
                index = tx_height // 2016
                self.network.request_chunk(self.network.interface, index)
            else:
                if tx_hash not in self.merkle_roots:
                    request = (
                        "blockchain.transaction.get_merkle",
                        [tx_hash, tx_height],
                    )
                    self.network.send([request], self.verify_merkle)
                    self.print_error("requested merkle", tx_hash)
                    self.merkle_roots[tx_hash] = None

    if self.network.blockchain() != self.blockchain:
        self.blockchain = self.network.blockchain()
        self.undo_verifications()