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.

p2p-versionbits-warning.py 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. #!/usr/bin/env python3
  2. # Copyright (c) 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 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. from test_framework.mininode import *
  10. from test_framework.test_framework import BitcoinTestFramework
  11. from test_framework.util import *
  12. import re
  13. from test_framework.blocktools import create_block, create_coinbase
  14. VB_PERIOD = 144 # versionbits period length for regtest
  15. VB_THRESHOLD = 108 # versionbits activation threshold for regtest
  16. VB_TOP_BITS = 0x20000000
  17. VB_UNKNOWN_BIT = 27 # Choose a bit unassigned to any deployment
  18. WARN_UNKNOWN_RULES_MINED = "Unknown block versions being mined! It's possible unknown rules are in effect"
  19. WARN_UNKNOWN_RULES_ACTIVE = "unknown new rules activated (versionbit {})".format(VB_UNKNOWN_BIT)
  20. VB_PATTERN = re.compile("^Warning.*versionbit")
  21. class TestNode(NodeConnCB):
  22. def on_inv(self, conn, message):
  23. pass
  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') as _:
  32. pass
  33. self.extra_args = [["-alertnotify=echo %s >> \"" + self.alert_filename + "\""]]
  34. self.setup_nodes()
  35. # Send numblocks blocks via peer with nVersionToUse set.
  36. def send_blocks_with_version(self, peer, numblocks, nVersionToUse):
  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 = nVersionToUse
  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 test_versionbits_in_alert_file(self):
  51. with open(self.alert_filename, 'r', encoding='utf8') as f:
  52. alert_text = f.read()
  53. assert(VB_PATTERN.match(alert_text))
  54. def run_test(self):
  55. # Setup the p2p connection and start up the network thread.
  56. test_node = TestNode()
  57. connections = []
  58. connections.append(NodeConn('127.0.0.1', p2p_port(0), self.nodes[0], test_node))
  59. test_node.add_connection(connections[0])
  60. NetworkThread().start() # Start up network handling in another thread
  61. # Test logic begins here
  62. test_node.wait_for_verack()
  63. # 1. Have the node mine one period worth of blocks
  64. self.nodes[0].generate(VB_PERIOD)
  65. # 2. Now build one period of blocks on the tip, with < VB_THRESHOLD
  66. # blocks signaling some unknown bit.
  67. nVersion = VB_TOP_BITS | (1<<VB_UNKNOWN_BIT)
  68. self.send_blocks_with_version(test_node, VB_THRESHOLD-1, nVersion)
  69. # Fill rest of period with regular version blocks
  70. self.nodes[0].generate(VB_PERIOD - VB_THRESHOLD + 1)
  71. # Check that we're not getting any versionbit-related errors in
  72. # get*info()
  73. assert(not VB_PATTERN.match(self.nodes[0].getinfo()["errors"]))
  74. assert(not VB_PATTERN.match(self.nodes[0].getmininginfo()["errors"]))
  75. assert(not VB_PATTERN.match(self.nodes[0].getnetworkinfo()["warnings"]))
  76. # 3. Now build one period of blocks with >= VB_THRESHOLD blocks signaling
  77. # some unknown bit
  78. self.send_blocks_with_version(test_node, VB_THRESHOLD, nVersion)
  79. self.nodes[0].generate(VB_PERIOD - VB_THRESHOLD)
  80. # Might not get a versionbits-related alert yet, as we should
  81. # have gotten a different alert due to more than 51/100 blocks
  82. # being of unexpected version.
  83. # Check that get*info() shows some kind of error.
  84. assert(WARN_UNKNOWN_RULES_MINED in self.nodes[0].getinfo()["errors"])
  85. assert(WARN_UNKNOWN_RULES_MINED in self.nodes[0].getmininginfo()["errors"])
  86. assert(WARN_UNKNOWN_RULES_MINED in self.nodes[0].getnetworkinfo()["warnings"])
  87. # Mine a period worth of expected blocks so the generic block-version warning
  88. # is cleared, and restart the node. This should move the versionbit state
  89. # to ACTIVE.
  90. self.nodes[0].generate(VB_PERIOD)
  91. self.stop_nodes()
  92. # Empty out the alert file
  93. with open(self.alert_filename, 'w', encoding='utf8') as _:
  94. pass
  95. self.start_nodes()
  96. # Connecting one block should be enough to generate an error.
  97. self.nodes[0].generate(1)
  98. assert(WARN_UNKNOWN_RULES_ACTIVE in self.nodes[0].getinfo()["errors"])
  99. assert(WARN_UNKNOWN_RULES_ACTIVE in self.nodes[0].getmininginfo()["errors"])
  100. assert(WARN_UNKNOWN_RULES_ACTIVE in self.nodes[0].getnetworkinfo()["warnings"])
  101. self.stop_nodes()
  102. self.test_versionbits_in_alert_file()
  103. # Test framework expects the node to still be running...
  104. self.start_nodes()
  105. if __name__ == '__main__':
  106. VersionBitsWarningTest().main()