You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

invalidateblock.py 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. # Test InvalidateBlock code
  7. #
  8. from test_framework.test_framework import BitcoinTestFramework
  9. from test_framework.util import *
  10. class InvalidateTest(BitcoinTestFramework):
  11. def setup_chain(self):
  12. print("Initializing test directory "+self.options.tmpdir)
  13. initialize_chain_clean(self.options.tmpdir, 3)
  14. def setup_network(self):
  15. self.nodes = []
  16. self.is_network_split = False
  17. self.nodes.append(start_node(0, self.options.tmpdir, ["-debug"]))
  18. self.nodes.append(start_node(1, self.options.tmpdir, ["-debug"]))
  19. self.nodes.append(start_node(2, self.options.tmpdir, ["-debug"]))
  20. def run_test(self):
  21. print "Make sure we repopulate setBlockIndexCandidates after InvalidateBlock:"
  22. print "Mine 4 blocks on Node 0"
  23. self.nodes[0].generate(4)
  24. assert(self.nodes[0].getblockcount() == 4)
  25. besthash = self.nodes[0].getbestblockhash()
  26. print "Mine competing 6 blocks on Node 1"
  27. self.nodes[1].generate(6)
  28. assert(self.nodes[1].getblockcount() == 6)
  29. print "Connect nodes to force a reorg"
  30. connect_nodes_bi(self.nodes,0,1)
  31. sync_blocks(self.nodes[0:2])
  32. assert(self.nodes[0].getblockcount() == 6)
  33. badhash = self.nodes[1].getblockhash(2)
  34. print "Invalidate block 2 on node 0 and verify we reorg to node 0's original chain"
  35. self.nodes[0].invalidateblock(badhash)
  36. newheight = self.nodes[0].getblockcount()
  37. newhash = self.nodes[0].getbestblockhash()
  38. if (newheight != 4 or newhash != besthash):
  39. raise AssertionError("Wrong tip for node0, hash %s, height %d"%(newhash,newheight))
  40. print "\nMake sure we won't reorg to a lower work chain:"
  41. connect_nodes_bi(self.nodes,1,2)
  42. print "Sync node 2 to node 1 so both have 6 blocks"
  43. sync_blocks(self.nodes[1:3])
  44. assert(self.nodes[2].getblockcount() == 6)
  45. print "Invalidate block 5 on node 1 so its tip is now at 4"
  46. self.nodes[1].invalidateblock(self.nodes[1].getblockhash(5))
  47. assert(self.nodes[1].getblockcount() == 4)
  48. print "Invalidate block 3 on node 2, so its tip is now 2"
  49. self.nodes[2].invalidateblock(self.nodes[2].getblockhash(3))
  50. assert(self.nodes[2].getblockcount() == 2)
  51. print "..and then mine a block"
  52. self.nodes[2].generate(1)
  53. print "Verify all nodes are at the right height"
  54. time.sleep(5)
  55. for i in xrange(3):
  56. print i,self.nodes[i].getblockcount()
  57. assert(self.nodes[2].getblockcount() == 3)
  58. assert(self.nodes[0].getblockcount() == 4)
  59. node1height = self.nodes[1].getblockcount()
  60. if node1height < 4:
  61. raise AssertionError("Node 1 reorged to a lower height: %d"%node1height)
  62. if __name__ == '__main__':
  63. InvalidateTest().main()