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.

feature_versionbits_warning.py 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. #!/usr/bin/env python3
  2. # Copyright (c) 2016-2017 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 version bits warning system.
  6. Generate chains with block versions that appear to be signalling unknown
  7. soft-forks, and test that warning alerts are generated.
  8. """
  9. import os
  10. import re
  11. from test_framework.blocktools import create_block, create_coinbase
  12. from test_framework.messages import msg_block
  13. from test_framework.mininode import P2PInterface, network_thread_start
  14. from test_framework.test_framework import BitcoinTestFramework
  15. VB_PERIOD = 144 # versionbits period length for regtest
  16. VB_THRESHOLD = 108 # versionbits activation threshold for regtest
  17. VB_TOP_BITS = 0x20000000
  18. VB_UNKNOWN_BIT = 27 # Choose a bit unassigned to any deployment
  19. VB_UNKNOWN_VERSION = VB_TOP_BITS | (1 << VB_UNKNOWN_BIT)
  20. WARN_UNKNOWN_RULES_MINED = "Unknown block versions being mined! It's possible unknown rules are in effect"
  21. WARN_UNKNOWN_RULES_ACTIVE = "unknown new rules activated (versionbit {})".format(VB_UNKNOWN_BIT)
  22. VB_PATTERN = re.compile("^Warning.*versionbit")
  23. class TestNode(P2PInterface):
  24. def on_inv(self, message):
  25. pass
  26. class VersionBitsWarningTest(BitcoinTestFramework):
  27. def set_test_params(self):
  28. self.setup_clean_chain = True
  29. self.num_nodes = 1
  30. def setup_network(self):
  31. self.alert_filename = os.path.join(self.options.tmpdir, "alert.txt")
  32. # Open and close to create zero-length file
  33. with open(self.alert_filename, 'w', encoding='utf8'):
  34. pass
  35. self.extra_args = [["-alertnotify=echo %s >> \"" + self.alert_filename + "\""]]
  36. self.setup_nodes()
  37. def send_blocks_with_version(self, peer, numblocks, version):
  38. """Send numblocks blocks to peer with version set"""
  39. tip = self.nodes[0].getbestblockhash()
  40. height = self.nodes[0].getblockcount()
  41. block_time = self.nodes[0].getblockheader(tip)["time"] + 1
  42. tip = int(tip, 16)
  43. for _ in range(numblocks):
  44. block = create_block(tip, create_coinbase(height + 1), block_time)
  45. block.nVersion = version
  46. block.solve()
  47. peer.send_message(msg_block(block))
  48. block_time += 1
  49. height += 1
  50. tip = block.sha256
  51. peer.sync_with_ping()
  52. def test_versionbits_in_alert_file(self):
  53. """Test that the versionbits warning has been written to the alert file.
  54. Note that this is only called after the node is shutdown, so doesn't need
  55. a wait_until wrapper."""
  56. with open(self.alert_filename, 'r', encoding='utf8') as f:
  57. alert_text = f.read()
  58. assert(VB_PATTERN.match(alert_text))
  59. def run_test(self):
  60. self.nodes[0].add_p2p_connection(TestNode())
  61. network_thread_start()
  62. self.nodes[0].p2p.wait_for_verack()
  63. # Mine one period worth of blocks
  64. self.nodes[0].generate(VB_PERIOD)
  65. self.log.info("Check that there is no warning if previous VB_BLOCKS have <VB_THRESHOLD blocks with unknown versionbits version.")
  66. # Build one period of blocks with < VB_THRESHOLD blocks signaling some unknown bit
  67. self.send_blocks_with_version(self.nodes[0].p2p, VB_THRESHOLD - 1, VB_UNKNOWN_VERSION)
  68. self.nodes[0].generate(VB_PERIOD - VB_THRESHOLD + 1)
  69. # Check that we're not getting any versionbit-related errors in get*info()
  70. assert(not VB_PATTERN.match(self.nodes[0].getmininginfo()["warnings"]))
  71. assert(not VB_PATTERN.match(self.nodes[0].getnetworkinfo()["warnings"]))
  72. # Build one period of blocks with VB_THRESHOLD blocks signaling some unknown bit
  73. self.send_blocks_with_version(self.nodes[0].p2p, VB_THRESHOLD, VB_UNKNOWN_VERSION)
  74. self.nodes[0].generate(VB_PERIOD - VB_THRESHOLD)
  75. self.log.info("Check that there is a warning if <50 blocks in the last 100 were an unknown version")
  76. # Check that get*info() shows the 51/100 unknown block version error.
  77. assert(WARN_UNKNOWN_RULES_MINED in self.nodes[0].getmininginfo()["warnings"])
  78. assert(WARN_UNKNOWN_RULES_MINED in self.nodes[0].getnetworkinfo()["warnings"])
  79. # Mine a period worth of expected blocks so the generic block-version warning
  80. # is cleared, and restart the node. This will move the versionbit state
  81. # to ACTIVE.
  82. self.nodes[0].generate(VB_PERIOD)
  83. self.stop_nodes()
  84. # Empty out the alert file
  85. with open(self.alert_filename, 'w', encoding='utf8'):
  86. pass
  87. self.start_nodes()
  88. self.log.info("Check that there is a warning if previous VB_BLOCKS have >=VB_THRESHOLD blocks with unknown versionbits version.")
  89. # Connecting one block should be enough to generate an error.
  90. self.nodes[0].generate(1)
  91. assert(WARN_UNKNOWN_RULES_ACTIVE in self.nodes[0].getmininginfo()["warnings"])
  92. assert(WARN_UNKNOWN_RULES_ACTIVE in self.nodes[0].getnetworkinfo()["warnings"])
  93. self.stop_nodes()
  94. self.test_versionbits_in_alert_file()
  95. # Test framework expects the node to still be running...
  96. self.start_nodes()
  97. if __name__ == '__main__':
  98. VersionBitsWarningTest().main()