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.2KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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 __init__(self):
  15. super().__init__()
  16. self.num_nodes = 4
  17. self.setup_clean_chain = False
  18. def run_test (self):
  19. tips = self.nodes[0].getchaintips ()
  20. assert_equal (len (tips), 1)
  21. assert_equal (tips[0]['branchlen'], 0)
  22. assert_equal (tips[0]['height'], 200)
  23. assert_equal (tips[0]['status'], 'active')
  24. # Split the network and build two chains of different lengths.
  25. self.split_network ()
  26. self.nodes[0].generate(10)
  27. self.nodes[2].generate(20)
  28. self.sync_all ()
  29. tips = self.nodes[1].getchaintips ()
  30. assert_equal (len (tips), 1)
  31. shortTip = tips[0]
  32. assert_equal (shortTip['branchlen'], 0)
  33. assert_equal (shortTip['height'], 210)
  34. assert_equal (tips[0]['status'], 'active')
  35. tips = self.nodes[3].getchaintips ()
  36. assert_equal (len (tips), 1)
  37. longTip = tips[0]
  38. assert_equal (longTip['branchlen'], 0)
  39. assert_equal (longTip['height'], 220)
  40. assert_equal (tips[0]['status'], 'active')
  41. # Join the network halves and check that we now have two tips
  42. # (at least at the nodes that previously had the short chain).
  43. self.join_network ()
  44. tips = self.nodes[0].getchaintips ()
  45. assert_equal (len (tips), 2)
  46. assert_equal (tips[0], longTip)
  47. assert_equal (tips[1]['branchlen'], 10)
  48. assert_equal (tips[1]['status'], 'valid-fork')
  49. tips[1]['branchlen'] = 0
  50. tips[1]['status'] = 'active'
  51. assert_equal (tips[1], shortTip)
  52. if __name__ == '__main__':
  53. GetChainTipsTest ().main ()