Dash Core  0.12.2.1
P2P Digital Currency
invalidateblock.py
Go to the documentation of this file.
1 #!/usr/bin/env python2
2 # Copyright (c) 2014-2015 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.test_framework import BitcoinTestFramework
11 from test_framework.util import *
12 
14 
15 
16  def setup_chain(self):
17  print("Initializing test directory "+self.options.tmpdir)
18  initialize_chain_clean(self.options.tmpdir, 3)
19 
20  def setup_network(self):
21  self.nodes = []
22  self.is_network_split = False
23  self.nodes.append(start_node(0, self.options.tmpdir, ["-debug"]))
24  self.nodes.append(start_node(1, self.options.tmpdir, ["-debug"]))
25  self.nodes.append(start_node(2, self.options.tmpdir, ["-debug"]))
26 
27  def run_test(self):
28  print "Make sure we repopulate setBlockIndexCandidates after InvalidateBlock:"
29  print "Mine 4 blocks on Node 0"
30  self.nodes[0].generate(4)
31  assert(self.nodes[0].getblockcount() == 4)
32  besthash = self.nodes[0].getbestblockhash()
33 
34  print "Mine competing 6 blocks on Node 1"
35  self.nodes[1].generate(6)
36  assert(self.nodes[1].getblockcount() == 6)
37 
38  print "Connect nodes to force a reorg"
39  connect_nodes_bi(self.nodes,0,1)
40  sync_blocks(self.nodes[0:2])
41  assert(self.nodes[0].getblockcount() == 6)
42  badhash = self.nodes[1].getblockhash(2)
43 
44  print "Invalidate block 2 on node 0 and verify we reorg to node 0's original chain"
45  self.nodes[0].invalidateblock(badhash)
46  newheight = self.nodes[0].getblockcount()
47  newhash = self.nodes[0].getbestblockhash()
48  if (newheight != 4 or newhash != besthash):
49  raise AssertionError("Wrong tip for node0, hash %s, height %d"%(newhash,newheight))
50 
51  print "\nMake sure we won't reorg to a lower work chain:"
52  connect_nodes_bi(self.nodes,1,2)
53  print "Sync node 2 to node 1 so both have 6 blocks"
54  sync_blocks(self.nodes[1:3])
55  assert(self.nodes[2].getblockcount() == 6)
56  print "Invalidate block 5 on node 1 so its tip is now at 4"
57  self.nodes[1].invalidateblock(self.nodes[1].getblockhash(5))
58  assert(self.nodes[1].getblockcount() == 4)
59  print "Invalidate block 3 on node 2, so its tip is now 2"
60  self.nodes[2].invalidateblock(self.nodes[2].getblockhash(3))
61  assert(self.nodes[2].getblockcount() == 2)
62  print "..and then mine a block"
63  self.nodes[2].generate(1)
64  print "Verify all nodes are at the right height"
65  time.sleep(5)
66  for i in xrange(3):
67  print i,self.nodes[i].getblockcount()
68  assert(self.nodes[2].getblockcount() == 3)
69  assert(self.nodes[0].getblockcount() == 4)
70  node1height = self.nodes[1].getblockcount()
71  if node1height < 4:
72  raise AssertionError("Node 1 reorged to a lower height: %d"%node1height)
73 
74 if __name__ == '__main__':
UniValue getblockhash(const UniValue &params, bool fHelp)
Definition: blockchain.cpp:311
UniValue getblockcount(const UniValue &params, bool fHelp)
Definition: blockchain.cpp:131
def initialize_chain_clean(test_dir, num_nodes)
Definition: util.py:252
def start_node(i, dirname, extra_args=None, rpchost=None, timewait=None, binary=None)
Definition: util.py:281
UniValue generate(const UniValue &params, bool fHelp)
Definition: mining.cpp:122
def sync_blocks(rpc_connections, wait=1)
Definition: util.py:117
UniValue getbestblockhash(const UniValue &params, bool fHelp)
Definition: blockchain.cpp:148
def connect_nodes_bi(nodes, a, b)
Definition: util.py:351