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 2.7KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. #!/usr/bin/env python3
  2. # Copyright (c) 2014-2016 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. """Test the invalidateblock RPC."""
  6. from test_framework.test_framework import BitcoinTestFramework
  7. from test_framework.util import *
  8. class InvalidateTest(BitcoinTestFramework):
  9. def set_test_params(self):
  10. self.setup_clean_chain = True
  11. self.num_nodes = 3
  12. def setup_network(self):
  13. self.setup_nodes()
  14. def run_test(self):
  15. self.log.info("Make sure we repopulate setBlockIndexCandidates after InvalidateBlock:")
  16. self.log.info("Mine 4 blocks on Node 0")
  17. self.nodes[0].generate(4)
  18. assert(self.nodes[0].getblockcount() == 4)
  19. besthash = self.nodes[0].getbestblockhash()
  20. self.log.info("Mine competing 6 blocks on Node 1")
  21. self.nodes[1].generate(6)
  22. assert(self.nodes[1].getblockcount() == 6)
  23. self.log.info("Connect nodes to force a reorg")
  24. connect_nodes_bi(self.nodes,0,1)
  25. sync_blocks(self.nodes[0:2])
  26. assert(self.nodes[0].getblockcount() == 6)
  27. badhash = self.nodes[1].getblockhash(2)
  28. self.log.info("Invalidate block 2 on node 0 and verify we reorg to node 0's original chain")
  29. self.nodes[0].invalidateblock(badhash)
  30. newheight = self.nodes[0].getblockcount()
  31. newhash = self.nodes[0].getbestblockhash()
  32. if (newheight != 4 or newhash != besthash):
  33. raise AssertionError("Wrong tip for node0, hash %s, height %d"%(newhash,newheight))
  34. self.log.info("Make sure we won't reorg to a lower work chain:")
  35. connect_nodes_bi(self.nodes,1,2)
  36. self.log.info("Sync node 2 to node 1 so both have 6 blocks")
  37. sync_blocks(self.nodes[1:3])
  38. assert(self.nodes[2].getblockcount() == 6)
  39. self.log.info("Invalidate block 5 on node 1 so its tip is now at 4")
  40. self.nodes[1].invalidateblock(self.nodes[1].getblockhash(5))
  41. assert(self.nodes[1].getblockcount() == 4)
  42. self.log.info("Invalidate block 3 on node 2, so its tip is now 2")
  43. self.nodes[2].invalidateblock(self.nodes[2].getblockhash(3))
  44. assert(self.nodes[2].getblockcount() == 2)
  45. self.log.info("..and then mine a block")
  46. self.nodes[2].generate(1)
  47. self.log.info("Verify all nodes are at the right height")
  48. time.sleep(5)
  49. assert_equal(self.nodes[2].getblockcount(), 3)
  50. assert_equal(self.nodes[0].getblockcount(), 4)
  51. node1height = self.nodes[1].getblockcount()
  52. if node1height < 4:
  53. raise AssertionError("Node 1 reorged to a lower height: %d"%node1height)
  54. if __name__ == '__main__':
  55. InvalidateTest().main()