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.

getchaintips.py 2.1KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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 getchaintips RPC.
  6. - introduce a network split
  7. - work on chains of different lengths
  8. - join the network together again
  9. - verify that getchaintips now returns two chain tips.
  10. """
  11. from test_framework.test_framework import BitcoinTestFramework
  12. from test_framework.util import assert_equal
  13. class GetChainTipsTest (BitcoinTestFramework):
  14. def set_test_params(self):
  15. self.num_nodes = 4
  16. def run_test (self):
  17. tips = self.nodes[0].getchaintips ()
  18. assert_equal (len (tips), 1)
  19. assert_equal (tips[0]['branchlen'], 0)
  20. assert_equal (tips[0]['height'], 200)
  21. assert_equal (tips[0]['status'], 'active')
  22. # Split the network and build two chains of different lengths.
  23. self.split_network ()
  24. self.nodes[0].generate(10)
  25. self.nodes[2].generate(20)
  26. self.sync_all([self.nodes[:2], self.nodes[2:]])
  27. tips = self.nodes[1].getchaintips ()
  28. assert_equal (len (tips), 1)
  29. shortTip = tips[0]
  30. assert_equal (shortTip['branchlen'], 0)
  31. assert_equal (shortTip['height'], 210)
  32. assert_equal (tips[0]['status'], 'active')
  33. tips = self.nodes[3].getchaintips ()
  34. assert_equal (len (tips), 1)
  35. longTip = tips[0]
  36. assert_equal (longTip['branchlen'], 0)
  37. assert_equal (longTip['height'], 220)
  38. assert_equal (tips[0]['status'], 'active')
  39. # Join the network halves and check that we now have two tips
  40. # (at least at the nodes that previously had the short chain).
  41. self.join_network ()
  42. tips = self.nodes[0].getchaintips ()
  43. assert_equal (len (tips), 2)
  44. assert_equal (tips[0], longTip)
  45. assert_equal (tips[1]['branchlen'], 10)
  46. assert_equal (tips[1]['status'], 'valid-fork')
  47. tips[1]['branchlen'] = 0
  48. tips[1]['status'] = 'active'
  49. assert_equal (tips[1], shortTip)
  50. if __name__ == '__main__':
  51. GetChainTipsTest ().main ()