Improve comments/logging in p2p-versionbits-warning.py

This commit is contained in:
John Newbery 2018-01-24 12:57:34 -05:00
parent ef2beb2c13
commit 3bbd843708

View file

@ -19,6 +19,7 @@ VB_PERIOD = 144 # versionbits period length for regtest
VB_THRESHOLD = 108 # versionbits activation threshold for regtest
VB_TOP_BITS = 0x20000000
VB_UNKNOWN_BIT = 27 # Choose a bit unassigned to any deployment
VB_UNKNOWN_VERSION = VB_TOP_BITS | (1 << VB_UNKNOWN_BIT)
WARN_UNKNOWN_RULES_MINED = "Unknown block versions being mined! It's possible unknown rules are in effect"
WARN_UNKNOWN_RULES_ACTIVE = "unknown new rules activated (versionbit {})".format(VB_UNKNOWN_BIT)
@ -41,8 +42,8 @@ class VersionBitsWarningTest(BitcoinTestFramework):
self.extra_args = [["-alertnotify=echo %s >> \"" + self.alert_filename + "\""]]
self.setup_nodes()
# Send numblocks blocks via peer with version set.
def send_blocks_with_version(self, peer, numblocks, version):
"""Send numblocks blocks to peer with version set"""
tip = self.nodes[0].getbestblockhash()
height = self.nodes[0].getblockcount()
block_time = self.nodes[0].getblockheader(tip)["time"] + 1
@ -59,47 +60,42 @@ class VersionBitsWarningTest(BitcoinTestFramework):
peer.sync_with_ping()
def test_versionbits_in_alert_file(self):
"""Test that the versionbits warning has been written to the alert file.
Note that this is only called after the node is shutdown, so doesn't need
a wait_until wrapper."""
with open(self.alert_filename, 'r', encoding='utf8') as f:
alert_text = f.read()
assert(VB_PATTERN.match(alert_text))
def run_test(self):
# Setup the p2p connection and start up the network thread.
self.nodes[0].add_p2p_connection(TestNode())
network_thread_start()
# Test logic begins here
self.nodes[0].p2p.wait_for_verack()
# 1. Have the node mine one period worth of blocks
# Mine one period worth of blocks
self.nodes[0].generate(VB_PERIOD)
# 2. Now build one period of blocks on the tip, with < VB_THRESHOLD
# blocks signaling some unknown bit.
version = VB_TOP_BITS | (1 << VB_UNKNOWN_BIT)
self.send_blocks_with_version(self.nodes[0].p2p, VB_THRESHOLD - 1, version)
# Fill rest of period with regular version blocks
self.log.info("Check that there is no warning if previous VB_BLOCKS have <VB_THRESHOLD blocks with unknown versionbits version.")
# Build one period of blocks with < VB_THRESHOLD blocks signaling some unknown bit
self.send_blocks_with_version(self.nodes[0].p2p, VB_THRESHOLD - 1, VB_UNKNOWN_VERSION)
self.nodes[0].generate(VB_PERIOD - VB_THRESHOLD + 1)
# Check that we're not getting any versionbit-related errors in
# get*info()
# Check that we're not getting any versionbit-related errors in get*info()
assert(not VB_PATTERN.match(self.nodes[0].getmininginfo()["warnings"]))
assert(not VB_PATTERN.match(self.nodes[0].getnetworkinfo()["warnings"]))
# 3. Now build one period of blocks with >= VB_THRESHOLD blocks signaling
# some unknown bit
self.send_blocks_with_version(self.nodes[0].p2p, VB_THRESHOLD, version)
# Build one period of blocks with VB_THRESHOLD blocks signaling some unknown bit
self.send_blocks_with_version(self.nodes[0].p2p, VB_THRESHOLD, VB_UNKNOWN_VERSION)
self.nodes[0].generate(VB_PERIOD - VB_THRESHOLD)
# Might not get a versionbits-related alert yet, as we should
# have gotten a different alert due to more than 51/100 blocks
# being of unexpected version.
# Check that get*info() shows some kind of error.
self.log.info("Check that there is a warning if <50 blocks in the last 100 were an unknown version")
# Check that get*info() shows the 51/100 unknown block version error.
assert(WARN_UNKNOWN_RULES_MINED in self.nodes[0].getmininginfo()["warnings"])
assert(WARN_UNKNOWN_RULES_MINED in self.nodes[0].getnetworkinfo()["warnings"])
# Mine a period worth of expected blocks so the generic block-version warning
# is cleared, and restart the node. This should move the versionbit state
# is cleared, and restart the node. This will move the versionbit state
# to ACTIVE.
self.nodes[0].generate(VB_PERIOD)
self.stop_nodes()
@ -108,6 +104,7 @@ class VersionBitsWarningTest(BitcoinTestFramework):
pass
self.start_nodes()
self.log.info("Check that there is a warning if previous VB_BLOCKS have >=VB_THRESHOLD blocks with unknown versionbits version.")
# Connecting one block should be enough to generate an error.
self.nodes[0].generate(1)
assert(WARN_UNKNOWN_RULES_ACTIVE in self.nodes[0].getmininginfo()["warnings"])