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.6KB

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