def reorg_back(self):
    assert_raises_rpc_error((- 1), 'Block not available (pruned data)', self.nodes[2].getblock, self.forkhash)
    with self.nodes[2].assert_debug_log(expected_msgs=['block verification stopping at height', '(pruning, no data)']):
        self.nodes[2].verifychain(checklevel=4, nblocks=0)
    self.log.info(f'Will need to redownload block {self.forkheight}')
    self.nodes[2].getblock(self.nodes[2].getblockhash(self.forkheight))
    first_reorg_height = self.nodes[2].getblockcount()
    curchainhash = self.nodes[2].getblockhash(self.mainchainheight)
    self.nodes[2].invalidateblock(curchainhash)
    goalbestheight = self.mainchainheight
    goalbesthash = self.mainchainhash2
    if (self.nodes[2].getblockcount() < self.mainchainheight):
        blocks_to_mine = ((first_reorg_height + 1) - self.mainchainheight)
        self.log.info(f'Rewind node 0 to prev main chain to mine longer chain to trigger redownload. Blocks needed: {blocks_to_mine}')
        self.nodes[0].invalidateblock(curchainhash)
        assert_equal(self.nodes[0].getblockcount(), self.mainchainheight)
        assert_equal(self.nodes[0].getbestblockhash(), self.mainchainhash2)
        goalbesthash = self.generate(self.nodes[0], blocks_to_mine)[(- 1)]
        goalbestheight = (first_reorg_height + 1)
    self.log.info('Verify node 2 reorged back to the main chain, some blocks of which it had to redownload')
    self.wait_until((lambda : (self.nodes[2].getblockcount() >= goalbestheight)), timeout=900)
    assert_equal(self.nodes[2].getbestblockhash(), goalbesthash)
    assert_equal(self.nodes[2].getblock(self.forkhash)['height'], self.forkheight)
def reorg_back(self):
    assert_raises_rpc_error((- 1), 'Block not available (pruned data)', self.nodes[2].getblock, self.forkhash)
    with self.nodes[2].assert_debug_log(expected_msgs=['block verification stopping at height', '(pruning, no data)']):
        self.nodes[2].verifychain(checklevel=4, nblocks=0)
    self.log.info(f'Will need to redownload block {self.forkheight}')
    self.nodes[2].getblock(self.nodes[2].getblockhash(self.forkheight))
    first_reorg_height = self.nodes[2].getblockcount()
    curchainhash = self.nodes[2].getblockhash(self.mainchainheight)
    self.nodes[2].invalidateblock(curchainhash)
    goalbestheight = self.mainchainheight
    goalbesthash = self.mainchainhash2
    if (self.nodes[2].getblockcount() < self.mainchainheight):
        blocks_to_mine = ((first_reorg_height + 1) - self.mainchainheight)
        self.log.info(f'Rewind node 0 to prev main chain to mine longer chain to trigger redownload. Blocks needed: {blocks_to_mine}')
        self.nodes[0].invalidateblock(curchainhash)
        assert_equal(self.nodes[0].getblockcount(), self.mainchainheight)
        assert_equal(self.nodes[0].getbestblockhash(), self.mainchainhash2)
        goalbesthash = self.generate(self.nodes[0], blocks_to_mine)[(- 1)]
        goalbestheight = (first_reorg_height + 1)
    self.log.info('Verify node 2 reorged back to the main chain, some blocks of which it had to redownload')
    self.wait_until((lambda : (self.nodes[2].getblockcount() >= goalbestheight)), timeout=900)
    assert_equal(self.nodes[2].getbestblockhash(), goalbesthash)
    assert_equal(self.nodes[2].getblock(self.forkhash)['height'], self.forkheight)