|
| 1 | +#!/usr/bin/env python2 |
| 2 | +# Copyright (c) 2014 The Bitcoin Core developers |
| 3 | +# Distributed under the MIT software license, see the accompanying |
| 4 | +# file COPYING or http://www.opensource.org/licenses/mit-license.php. |
| 5 | + |
| 6 | +# |
| 7 | +# Test InvalidateBlock code |
| 8 | +# |
| 9 | + |
| 10 | +from test_framework import BitcoinTestFramework |
| 11 | +from bitcoinrpc.authproxy import AuthServiceProxy, JSONRPCException |
| 12 | +from util import * |
| 13 | + |
| 14 | +class InvalidateTest(BitcoinTestFramework): |
| 15 | + |
| 16 | + |
| 17 | + def setup_chain(self): |
| 18 | + print("Initializing test directory "+self.options.tmpdir) |
| 19 | + initialize_chain_clean(self.options.tmpdir, 2) |
| 20 | + |
| 21 | + def setup_network(self): |
| 22 | + self.nodes = [] |
| 23 | + self.is_network_split = False |
| 24 | + self.nodes.append(start_node(0, self.options.tmpdir, ["-debug"])) |
| 25 | + self.nodes.append(start_node(1, self.options.tmpdir, ["-debug"])) |
| 26 | + |
| 27 | + def run_test(self): |
| 28 | + print "Mine 4 blocks on Node 0" |
| 29 | + self.nodes[0].setgenerate(True, 4) |
| 30 | + assert(self.nodes[0].getblockcount() == 4) |
| 31 | + besthash = self.nodes[0].getbestblockhash() |
| 32 | + |
| 33 | + print "Mine competing 6 blocks on Node 1" |
| 34 | + self.nodes[1].setgenerate(True, 6) |
| 35 | + assert(self.nodes[1].getblockcount() == 6) |
| 36 | + |
| 37 | + print "Connect nodes to force a reorg" |
| 38 | + connect_nodes_bi(self.nodes,0,1) |
| 39 | + sync_blocks(self.nodes) |
| 40 | + assert(self.nodes[0].getblockcount() == 6) |
| 41 | + badhash = self.nodes[1].getblockhash(2) |
| 42 | + |
| 43 | + print "Invalidate block 2 on node 0 and verify we reorg to node 0's original chain" |
| 44 | + self.nodes[0].invalidateblock(badhash) |
| 45 | + newheight = self.nodes[0].getblockcount() |
| 46 | + newhash = self.nodes[0].getbestblockhash() |
| 47 | + if (newheight != 4 or newhash != besthash): |
| 48 | + raise AssertionError("Wrong tip for node0, hash %s, height %d"%(newhash,newheight)) |
| 49 | + |
| 50 | +if __name__ == '__main__': |
| 51 | + InvalidateTest().main() |
0 commit comments