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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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, mininode_lock
  14. from test_framework.test_framework import BitcoinTestFramework
  15. from test_framework.util import wait_until
  16. VB_PERIOD = 144 # versionbits period length for regtest
  17. VB_THRESHOLD = 108 # versionbits activation threshold for regtest
  18. VB_TOP_BITS = 0x20000000
  19. VB_UNKNOWN_BIT = 27 # Choose a bit unassigned to any deployment
  20. VB_UNKNOWN_VERSION = VB_TOP_BITS | (1 << VB_UNKNOWN_BIT)
  21. WARN_UNKNOWN_RULES_MINED = "Unknown block versions being mined! It's possible unknown rules are in effect"
  22. WARN_UNKNOWN_RULES_ACTIVE = "unknown new rules activated (versionbit {})".format(VB_UNKNOWN_BIT)
  23. VB_PATTERN = re.compile("Warning: unknown new rules activated.*versionbit")
  24. class VersionBitsWarningTest(BitcoinTestFramework):
  25. def set_test_params(self):
  26. self.setup_clean_chain = True
  27. self.num_nodes = 1
  28. def setup_network(self):
  29. self.alert_filename = os.path.join(self.options.tmpdir, "alert.txt")
  30. # Open and close to create zero-length file
  31. with open(self.alert_filename, 'w', encoding='utf8'):
  32. pass
  33. self.extra_args = [["-alertnotify=echo %s >> \"" + self.alert_filename + "\""]]
  34. self.setup_nodes()
  35. def send_blocks_with_version(self, peer, numblocks, version):
  36. """Send numblocks blocks to peer with version set"""
  37. tip = self.nodes[0].getbestblockhash()
  38. height = self.nodes[0].getblockcount()
  39. block_time = self.nodes[0].getblockheader(tip)["time"] + 1
  40. tip = int(tip, 16)
  41. for _ in range(numblocks):
  42. block = create_block(tip, create_coinbase(height + 1), block_time)
  43. block.nVersion = version
  44. block.solve()
  45. peer.send_message(msg_block(block))
  46. block_time += 1
  47. height += 1
  48. tip = block.sha256
  49. peer.sync_with_ping()
  50. def versionbits_in_alert_file(self):
  51. """Test that the versionbits warning has been written to the alert file."""
  52. alert_text = open(self.alert_filename, 'r', encoding='utf8').read()
  53. return VB_PATTERN.search(alert_text) is not None
  54. def run_test(self):
  55. # Handy alias
  56. node = self.nodes[0]
  57. node.add_p2p_connection(P2PInterface())
  58. network_thread_start()
  59. node.p2p.wait_for_verack()
  60. # Mine one period worth of blocks
  61. node.generate(VB_PERIOD)
  62. self.log.info("Check that there is no warning if previous VB_BLOCKS have <VB_THRESHOLD blocks with unknown versionbits version.")
  63. # Build one period of blocks with < VB_THRESHOLD blocks signaling some unknown bit
  64. self.send_blocks_with_version(node.p2p, VB_THRESHOLD - 1, VB_UNKNOWN_VERSION)
  65. node.generate(VB_PERIOD - VB_THRESHOLD + 1)
  66. # Check that we're not getting any versionbit-related errors in get*info()
  67. assert(not VB_PATTERN.match(node.getmininginfo()["warnings"]))
  68. assert(not VB_PATTERN.match(node.getnetworkinfo()["warnings"]))
  69. self.log.info("Check that there is a warning if >50 blocks in the last 100 were an unknown version")
  70. # Build one period of blocks with VB_THRESHOLD blocks signaling some unknown bit
  71. self.send_blocks_with_version(node.p2p, VB_THRESHOLD, VB_UNKNOWN_VERSION)
  72. node.generate(VB_PERIOD - VB_THRESHOLD)
  73. # Check that get*info() shows the 51/100 unknown block version error.
  74. assert(WARN_UNKNOWN_RULES_MINED in node.getmininginfo()["warnings"])
  75. assert(WARN_UNKNOWN_RULES_MINED in node.getnetworkinfo()["warnings"])
  76. self.log.info("Check that there is a warning if previous VB_BLOCKS have >=VB_THRESHOLD blocks with unknown versionbits version.")
  77. # Mine a period worth of expected blocks so the generic block-version warning
  78. # is cleared. This will move the versionbit state to ACTIVE.
  79. node.generate(VB_PERIOD)
  80. # Stop-start the node. This is required because bitcoind will only warn once about unknown versions or unknown rules activating.
  81. self.restart_node(0)
  82. # Generating one block guarantees that we'll get out of IBD
  83. node.generate(1)
  84. wait_until(lambda: not node.getblockchaininfo()['initialblockdownload'], timeout=10, lock=mininode_lock)
  85. # Generating one more block will be enough to generate an error.
  86. node.generate(1)
  87. # Check that get*info() shows the versionbits unknown rules warning
  88. assert(WARN_UNKNOWN_RULES_ACTIVE in node.getmininginfo()["warnings"])
  89. assert(WARN_UNKNOWN_RULES_ACTIVE in node.getnetworkinfo()["warnings"])
  90. # Check that the alert file shows the versionbits unknown rules warning
  91. wait_until(lambda: self.versionbits_in_alert_file(), timeout=60)
  92. if __name__ == '__main__':
  93. VersionBitsWarningTest().main()