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 6.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. # TestNode: bare-bones "peer". Used mostly as a conduit for a test to sending
  22. # p2p messages to a node, generating the messages in the main testing logic.
  23. class TestNode(NodeConnCB):
  24. def __init__(self):
  25. super().__init__()
  26. self.connection = None
  27. self.ping_counter = 1
  28. self.last_pong = msg_pong()
  29. def add_connection(self, conn):
  30. self.connection = conn
  31. def on_inv(self, conn, message):
  32. pass
  33. # Wrapper for the NodeConn's send_message function
  34. def send_message(self, message):
  35. self.connection.send_message(message)
  36. def on_pong(self, conn, message):
  37. self.last_pong = message
  38. class VersionBitsWarningTest(BitcoinTestFramework):
  39. def __init__(self):
  40. super().__init__()
  41. self.setup_clean_chain = True
  42. self.num_nodes = 1
  43. def setup_network(self):
  44. self.alert_filename = os.path.join(self.options.tmpdir, "alert.txt")
  45. # Open and close to create zero-length file
  46. with open(self.alert_filename, 'w', encoding='utf8') as _:
  47. pass
  48. self.extra_args = [["-alertnotify=echo %s >> \"" + self.alert_filename + "\""]]
  49. self.nodes = start_nodes(self.num_nodes, self.options.tmpdir, self.extra_args)
  50. # Send numblocks blocks via peer with nVersionToUse set.
  51. def send_blocks_with_version(self, peer, numblocks, nVersionToUse):
  52. tip = self.nodes[0].getbestblockhash()
  53. height = self.nodes[0].getblockcount()
  54. block_time = self.nodes[0].getblockheader(tip)["time"]+1
  55. tip = int(tip, 16)
  56. for _ in range(numblocks):
  57. block = create_block(tip, create_coinbase(height+1), block_time)
  58. block.nVersion = nVersionToUse
  59. block.solve()
  60. peer.send_message(msg_block(block))
  61. block_time += 1
  62. height += 1
  63. tip = block.sha256
  64. peer.sync_with_ping()
  65. def test_versionbits_in_alert_file(self):
  66. with open(self.alert_filename, 'r', encoding='utf8') as f:
  67. alert_text = f.read()
  68. assert(VB_PATTERN.match(alert_text))
  69. def run_test(self):
  70. # Setup the p2p connection and start up the network thread.
  71. test_node = TestNode()
  72. connections = []
  73. connections.append(NodeConn('127.0.0.1', p2p_port(0), self.nodes[0], test_node))
  74. test_node.add_connection(connections[0])
  75. NetworkThread().start() # Start up network handling in another thread
  76. # Test logic begins here
  77. test_node.wait_for_verack()
  78. # 1. Have the node mine one period worth of blocks
  79. self.nodes[0].generate(VB_PERIOD)
  80. # 2. Now build one period of blocks on the tip, with < VB_THRESHOLD
  81. # blocks signaling some unknown bit.
  82. nVersion = VB_TOP_BITS | (1<<VB_UNKNOWN_BIT)
  83. self.send_blocks_with_version(test_node, VB_THRESHOLD-1, nVersion)
  84. # Fill rest of period with regular version blocks
  85. self.nodes[0].generate(VB_PERIOD - VB_THRESHOLD + 1)
  86. # Check that we're not getting any versionbit-related errors in
  87. # get*info()
  88. assert(not VB_PATTERN.match(self.nodes[0].getinfo()["errors"]))
  89. assert(not VB_PATTERN.match(self.nodes[0].getmininginfo()["errors"]))
  90. assert(not VB_PATTERN.match(self.nodes[0].getnetworkinfo()["warnings"]))
  91. # 3. Now build one period of blocks with >= VB_THRESHOLD blocks signaling
  92. # some unknown bit
  93. self.send_blocks_with_version(test_node, VB_THRESHOLD, nVersion)
  94. self.nodes[0].generate(VB_PERIOD - VB_THRESHOLD)
  95. # Might not get a versionbits-related alert yet, as we should
  96. # have gotten a different alert due to more than 51/100 blocks
  97. # being of unexpected version.
  98. # Check that get*info() shows some kind of error.
  99. assert(WARN_UNKNOWN_RULES_MINED in self.nodes[0].getinfo()["errors"])
  100. assert(WARN_UNKNOWN_RULES_MINED in self.nodes[0].getmininginfo()["errors"])
  101. assert(WARN_UNKNOWN_RULES_MINED in self.nodes[0].getnetworkinfo()["warnings"])
  102. # Mine a period worth of expected blocks so the generic block-version warning
  103. # is cleared, and restart the node. This should move the versionbit state
  104. # to ACTIVE.
  105. self.nodes[0].generate(VB_PERIOD)
  106. stop_nodes(self.nodes)
  107. # Empty out the alert file
  108. with open(self.alert_filename, 'w', encoding='utf8') as _:
  109. pass
  110. self.nodes = start_nodes(self.num_nodes, self.options.tmpdir, self.extra_args)
  111. # Connecting one block should be enough to generate an error.
  112. self.nodes[0].generate(1)
  113. assert(WARN_UNKNOWN_RULES_ACTIVE in self.nodes[0].getinfo()["errors"])
  114. assert(WARN_UNKNOWN_RULES_ACTIVE in self.nodes[0].getmininginfo()["errors"])
  115. assert(WARN_UNKNOWN_RULES_ACTIVE in self.nodes[0].getnetworkinfo()["warnings"])
  116. stop_nodes(self.nodes)
  117. self.test_versionbits_in_alert_file()
  118. # Test framework expects the node to still be running...
  119. self.nodes = start_nodes(self.num_nodes, self.options.tmpdir, self.extra_args)
  120. if __name__ == '__main__':
  121. VersionBitsWarningTest().main()