Skip to content

Commit 1e2e09e

Browse files
committed
Fix intermittent failure in p2p-versionbits-warning.py
Makes following changes to fix and tidy up p2p-versionbits-warning.py: - add node alias in the run() method - call versionbits_in_alert_file() in a wait_until loop. - don't clear out the alert.txt file - explicitly comment why the node needs to be stop-started - Verify that the node is out of IBD after stop-start (nodes in IBD do not generate alert messages) - no need to subclass P2PInterface
1 parent 3bbd843 commit 1e2e09e

File tree

1 file changed

+38
-45
lines changed

1 file changed

+38
-45
lines changed

test/functional/feature_versionbits_warning.py

Lines changed: 38 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@
1212

1313
from test_framework.blocktools import create_block, create_coinbase
1414
from test_framework.messages import msg_block
15-
from test_framework.mininode import P2PInterface, network_thread_start
15+
from test_framework.mininode import P2PInterface, network_thread_start, mininode_lock
1616
from test_framework.test_framework import BitcoinTestFramework
17+
from test_framework.util import wait_until
1718

1819
VB_PERIOD = 144 # versionbits period length for regtest
1920
VB_THRESHOLD = 108 # versionbits activation threshold for regtest
@@ -23,11 +24,7 @@
2324

2425
WARN_UNKNOWN_RULES_MINED = "Unknown block versions being mined! It's possible unknown rules are in effect"
2526
WARN_UNKNOWN_RULES_ACTIVE = "unknown new rules activated (versionbit {})".format(VB_UNKNOWN_BIT)
26-
VB_PATTERN = re.compile("^Warning.*versionbit")
27-
28-
class TestNode(P2PInterface):
29-
def on_inv(self, message):
30-
pass
27+
VB_PATTERN = re.compile("Warning: unknown new rules activated.*versionbit")
3128

3229
class VersionBitsWarningTest(BitcoinTestFramework):
3330
def set_test_params(self):
@@ -59,61 +56,57 @@ def send_blocks_with_version(self, peer, numblocks, version):
5956
tip = block.sha256
6057
peer.sync_with_ping()
6158

62-
def test_versionbits_in_alert_file(self):
63-
"""Test that the versionbits warning has been written to the alert file.
64-
65-
Note that this is only called after the node is shutdown, so doesn't need
66-
a wait_until wrapper."""
67-
with open(self.alert_filename, 'r', encoding='utf8') as f:
68-
alert_text = f.read()
69-
assert(VB_PATTERN.match(alert_text))
59+
def versionbits_in_alert_file(self):
60+
"""Test that the versionbits warning has been written to the alert file."""
61+
alert_text = open(self.alert_filename, 'r', encoding='utf8').read()
62+
return VB_PATTERN.search(alert_text) is not None
7063

7164
def run_test(self):
72-
self.nodes[0].add_p2p_connection(TestNode())
65+
# Handy alias
66+
node = self.nodes[0]
67+
node.add_p2p_connection(P2PInterface())
7368
network_thread_start()
74-
self.nodes[0].p2p.wait_for_verack()
69+
node.p2p.wait_for_verack()
7570

7671
# Mine one period worth of blocks
77-
self.nodes[0].generate(VB_PERIOD)
72+
node.generate(VB_PERIOD)
7873

7974
self.log.info("Check that there is no warning if previous VB_BLOCKS have <VB_THRESHOLD blocks with unknown versionbits version.")
8075
# Build one period of blocks with < VB_THRESHOLD blocks signaling some unknown bit
81-
self.send_blocks_with_version(self.nodes[0].p2p, VB_THRESHOLD - 1, VB_UNKNOWN_VERSION)
82-
self.nodes[0].generate(VB_PERIOD - VB_THRESHOLD + 1)
76+
self.send_blocks_with_version(node.p2p, VB_THRESHOLD - 1, VB_UNKNOWN_VERSION)
77+
node.generate(VB_PERIOD - VB_THRESHOLD + 1)
8378

8479
# Check that we're not getting any versionbit-related errors in get*info()
85-
assert(not VB_PATTERN.match(self.nodes[0].getmininginfo()["warnings"]))
86-
assert(not VB_PATTERN.match(self.nodes[0].getnetworkinfo()["warnings"]))
80+
assert(not VB_PATTERN.match(node.getmininginfo()["warnings"]))
81+
assert(not VB_PATTERN.match(node.getnetworkinfo()["warnings"]))
8782

83+
self.log.info("Check that there is a warning if >50 blocks in the last 100 were an unknown version")
8884
# Build one period of blocks with VB_THRESHOLD blocks signaling some unknown bit
89-
self.send_blocks_with_version(self.nodes[0].p2p, VB_THRESHOLD, VB_UNKNOWN_VERSION)
90-
self.nodes[0].generate(VB_PERIOD - VB_THRESHOLD)
85+
self.send_blocks_with_version(node.p2p, VB_THRESHOLD, VB_UNKNOWN_VERSION)
86+
node.generate(VB_PERIOD - VB_THRESHOLD)
9187

92-
self.log.info("Check that there is a warning if <50 blocks in the last 100 were an unknown version")
9388
# Check that get*info() shows the 51/100 unknown block version error.
94-
assert(WARN_UNKNOWN_RULES_MINED in self.nodes[0].getmininginfo()["warnings"])
95-
assert(WARN_UNKNOWN_RULES_MINED in self.nodes[0].getnetworkinfo()["warnings"])
96-
97-
# Mine a period worth of expected blocks so the generic block-version warning
98-
# is cleared, and restart the node. This will move the versionbit state
99-
# to ACTIVE.
100-
self.nodes[0].generate(VB_PERIOD)
101-
self.stop_nodes()
102-
# Empty out the alert file
103-
with open(self.alert_filename, 'w', encoding='utf8'):
104-
pass
105-
self.start_nodes()
89+
assert(WARN_UNKNOWN_RULES_MINED in node.getmininginfo()["warnings"])
90+
assert(WARN_UNKNOWN_RULES_MINED in node.getnetworkinfo()["warnings"])
10691

10792
self.log.info("Check that there is a warning if previous VB_BLOCKS have >=VB_THRESHOLD blocks with unknown versionbits version.")
108-
# Connecting one block should be enough to generate an error.
109-
self.nodes[0].generate(1)
110-
assert(WARN_UNKNOWN_RULES_ACTIVE in self.nodes[0].getmininginfo()["warnings"])
111-
assert(WARN_UNKNOWN_RULES_ACTIVE in self.nodes[0].getnetworkinfo()["warnings"])
112-
self.stop_nodes()
113-
self.test_versionbits_in_alert_file()
114-
115-
# Test framework expects the node to still be running...
116-
self.start_nodes()
93+
# Mine a period worth of expected blocks so the generic block-version warning
94+
# is cleared. This will move the versionbit state to ACTIVE.
95+
node.generate(VB_PERIOD)
96+
97+
# Stop-start the node. This is required because bitcoind will only warn once about unknown versions or unknown rules activating.
98+
self.restart_node(0)
99+
100+
# Generating one block guarantees that we'll get out of IBD
101+
node.generate(1)
102+
wait_until(lambda: not node.getblockchaininfo()['initialblockdownload'], timeout=10, lock=mininode_lock)
103+
# Generating one more block will be enough to generate an error.
104+
node.generate(1)
105+
# Check that get*info() shows the versionbits unknown rules warning
106+
assert(WARN_UNKNOWN_RULES_ACTIVE in node.getmininginfo()["warnings"])
107+
assert(WARN_UNKNOWN_RULES_ACTIVE in node.getnetworkinfo()["warnings"])
108+
# Check that the alert file shows the versionbits unknown rules warning
109+
wait_until(lambda: self.versionbits_in_alert_file(), timeout=60)
117110

118111
if __name__ == '__main__':
119112
VersionBitsWarningTest().main()

0 commit comments

Comments
 (0)