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

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