19
19
VB_THRESHOLD = 108 # versionbits activation threshold for regtest
20
20
VB_TOP_BITS = 0x20000000
21
21
VB_UNKNOWN_BIT = 27 # Choose a bit unassigned to any deployment
22
+ VB_UNKNOWN_VERSION = VB_TOP_BITS | (1 << VB_UNKNOWN_BIT )
22
23
23
24
WARN_UNKNOWN_RULES_MINED = "Unknown block versions being mined! It's possible unknown rules are in effect"
24
25
WARN_UNKNOWN_RULES_ACTIVE = "unknown new rules activated (versionbit {})" .format (VB_UNKNOWN_BIT )
@@ -41,8 +42,8 @@ def setup_network(self):
41
42
self .extra_args = [["-alertnotify=echo %s >> \" " + self .alert_filename + "\" " ]]
42
43
self .setup_nodes ()
43
44
44
- # Send numblocks blocks via peer with version set.
45
45
def send_blocks_with_version (self , peer , numblocks , version ):
46
+ """Send numblocks blocks to peer with version set"""
46
47
tip = self .nodes [0 ].getbestblockhash ()
47
48
height = self .nodes [0 ].getblockcount ()
48
49
block_time = self .nodes [0 ].getblockheader (tip )["time" ] + 1
@@ -59,47 +60,42 @@ def send_blocks_with_version(self, peer, numblocks, version):
59
60
peer .sync_with_ping ()
60
61
61
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."""
62
67
with open (self .alert_filename , 'r' , encoding = 'utf8' ) as f :
63
68
alert_text = f .read ()
64
69
assert (VB_PATTERN .match (alert_text ))
65
70
66
71
def run_test (self ):
67
- # Setup the p2p connection and start up the network thread.
68
72
self .nodes [0 ].add_p2p_connection (TestNode ())
69
-
70
73
network_thread_start ()
71
-
72
- # Test logic begins here
73
74
self .nodes [0 ].p2p .wait_for_verack ()
74
75
75
- # 1. Have the node mine one period worth of blocks
76
+ # Mine one period worth of blocks
76
77
self .nodes [0 ].generate (VB_PERIOD )
77
78
78
- # 2. Now build one period of blocks on the tip, with < VB_THRESHOLD
79
- # blocks signaling some unknown bit.
80
- version = VB_TOP_BITS | (1 << VB_UNKNOWN_BIT )
81
- self .send_blocks_with_version (self .nodes [0 ].p2p , VB_THRESHOLD - 1 , version )
82
-
83
- # Fill rest of period with regular version blocks
79
+ self .log .info ("Check that there is no warning if previous VB_BLOCKS have <VB_THRESHOLD blocks with unknown versionbits version." )
80
+ # 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 )
84
82
self .nodes [0 ].generate (VB_PERIOD - VB_THRESHOLD + 1 )
85
- # Check that we're not getting any versionbit-related errors in
86
- # get*info()
83
+
84
+ # Check that we're not getting any versionbit-related errors in get*info()
87
85
assert (not VB_PATTERN .match (self .nodes [0 ].getmininginfo ()["warnings" ]))
88
86
assert (not VB_PATTERN .match (self .nodes [0 ].getnetworkinfo ()["warnings" ]))
89
87
90
- # 3. Now build one period of blocks with >= VB_THRESHOLD blocks signaling
91
- # some unknown bit
92
- self .send_blocks_with_version (self .nodes [0 ].p2p , VB_THRESHOLD , version )
88
+ # 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 )
93
90
self .nodes [0 ].generate (VB_PERIOD - VB_THRESHOLD )
94
- # Might not get a versionbits-related alert yet, as we should
95
- # have gotten a different alert due to more than 51/100 blocks
96
- # being of unexpected version.
97
- # Check that get*info() shows some kind of error.
91
+
92
+ self .log .info ("Check that there is a warning if <50 blocks in the last 100 were an unknown version" )
93
+ # Check that get*info() shows the 51/100 unknown block version error.
98
94
assert (WARN_UNKNOWN_RULES_MINED in self .nodes [0 ].getmininginfo ()["warnings" ])
99
95
assert (WARN_UNKNOWN_RULES_MINED in self .nodes [0 ].getnetworkinfo ()["warnings" ])
100
96
101
97
# Mine a period worth of expected blocks so the generic block-version warning
102
- # is cleared, and restart the node. This should move the versionbit state
98
+ # is cleared, and restart the node. This will move the versionbit state
103
99
# to ACTIVE.
104
100
self .nodes [0 ].generate (VB_PERIOD )
105
101
self .stop_nodes ()
@@ -108,6 +104,7 @@ def run_test(self):
108
104
pass
109
105
self .start_nodes ()
110
106
107
+ self .log .info ("Check that there is a warning if previous VB_BLOCKS have >=VB_THRESHOLD blocks with unknown versionbits version." )
111
108
# Connecting one block should be enough to generate an error.
112
109
self .nodes [0 ].generate (1 )
113
110
assert (WARN_UNKNOWN_RULES_ACTIVE in self .nodes [0 ].getmininginfo ()["warnings" ])
0 commit comments