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.

forknotify.py 2.1KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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 -alertnotify option."""
  6. import os
  7. import time
  8. from test_framework.test_framework import BitcoinTestFramework
  9. class ForkNotifyTest(BitcoinTestFramework):
  10. def set_test_params(self):
  11. self.num_nodes = 2
  12. def setup_network(self):
  13. self.alert_filename = os.path.join(self.options.tmpdir, "alert.txt")
  14. with open(self.alert_filename, 'w', encoding='utf8'):
  15. pass # Just open then close to create zero-length file
  16. self.extra_args = [["-blockversion=2", "-alertnotify=echo %s >> \"" + self.alert_filename + "\""],
  17. ["-blockversion=211"]]
  18. super().setup_network()
  19. def run_test(self):
  20. # Mine 51 up-version blocks
  21. self.nodes[1].generate(51)
  22. self.sync_all()
  23. # -alertnotify should trigger on the 51'st,
  24. # but mine and sync another to give
  25. # -alertnotify time to write
  26. self.nodes[1].generate(1)
  27. self.sync_all()
  28. # Give bitcoind 10 seconds to write the alert notification
  29. timeout = 10.0
  30. while timeout > 0:
  31. if os.path.exists(self.alert_filename) and os.path.getsize(self.alert_filename):
  32. break
  33. time.sleep(0.1)
  34. timeout -= 0.1
  35. else:
  36. assert False, "-alertnotify did not warn of up-version blocks"
  37. with open(self.alert_filename, 'r', encoding='utf8') as f:
  38. alert_text = f.read()
  39. # Mine more up-version blocks, should not get more alerts:
  40. self.nodes[1].generate(1)
  41. self.sync_all()
  42. self.nodes[1].generate(1)
  43. self.sync_all()
  44. with open(self.alert_filename, 'r', encoding='utf8') as f:
  45. alert_text2 = f.read()
  46. if alert_text != alert_text2:
  47. raise AssertionError("-alertnotify excessive warning of up-version blocks")
  48. if __name__ == '__main__':
  49. ForkNotifyTest().main()