[tests] don't override __init__() in individual tests
[bitcoinplatinum.git] / test / functional / p2p-versionbits-warning.py
blob5dfac6dd10e016c95460d125c8df33b3872da79a
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.
7 Generate chains with block versions that appear to be signalling unknown
8 soft-forks, and test that warning alerts are generated.
9 """
11 from test_framework.mininode import *
12 from test_framework.test_framework import BitcoinTestFramework
13 from test_framework.util import *
14 import re
15 from test_framework.blocktools import create_block, create_coinbase
17 VB_PERIOD = 144 # versionbits period length for regtest
18 VB_THRESHOLD = 108 # versionbits activation threshold for regtest
19 VB_TOP_BITS = 0x20000000
20 VB_UNKNOWN_BIT = 27 # Choose a bit unassigned to any deployment
22 WARN_UNKNOWN_RULES_MINED = "Unknown block versions being mined! It's possible unknown rules are in effect"
23 WARN_UNKNOWN_RULES_ACTIVE = "unknown new rules activated (versionbit {})".format(VB_UNKNOWN_BIT)
24 VB_PATTERN = re.compile("^Warning.*versionbit")
26 class TestNode(NodeConnCB):
27 def on_inv(self, conn, message):
28 pass
30 class VersionBitsWarningTest(BitcoinTestFramework):
31 def set_test_params(self):
32 self.setup_clean_chain = True
33 self.num_nodes = 1
35 def setup_network(self):
36 self.alert_filename = os.path.join(self.options.tmpdir, "alert.txt")
37 # Open and close to create zero-length file
38 with open(self.alert_filename, 'w', encoding='utf8') as _:
39 pass
40 self.extra_args = [["-alertnotify=echo %s >> \"" + self.alert_filename + "\""]]
41 self.setup_nodes()
43 # Send numblocks blocks via peer with nVersionToUse set.
44 def send_blocks_with_version(self, peer, numblocks, nVersionToUse):
45 tip = self.nodes[0].getbestblockhash()
46 height = self.nodes[0].getblockcount()
47 block_time = self.nodes[0].getblockheader(tip)["time"]+1
48 tip = int(tip, 16)
50 for _ in range(numblocks):
51 block = create_block(tip, create_coinbase(height+1), block_time)
52 block.nVersion = nVersionToUse
53 block.solve()
54 peer.send_message(msg_block(block))
55 block_time += 1
56 height += 1
57 tip = block.sha256
58 peer.sync_with_ping()
60 def test_versionbits_in_alert_file(self):
61 with open(self.alert_filename, 'r', encoding='utf8') as f:
62 alert_text = f.read()
63 assert(VB_PATTERN.match(alert_text))
65 def run_test(self):
66 # Setup the p2p connection and start up the network thread.
67 test_node = TestNode()
69 connections = []
70 connections.append(NodeConn('127.0.0.1', p2p_port(0), self.nodes[0], test_node))
71 test_node.add_connection(connections[0])
73 NetworkThread().start() # Start up network handling in another thread
75 # Test logic begins here
76 test_node.wait_for_verack()
78 # 1. Have the node mine one period worth of blocks
79 self.nodes[0].generate(VB_PERIOD)
81 # 2. Now build one period of blocks on the tip, with < VB_THRESHOLD
82 # blocks signaling some unknown bit.
83 nVersion = VB_TOP_BITS | (1<<VB_UNKNOWN_BIT)
84 self.send_blocks_with_version(test_node, VB_THRESHOLD-1, nVersion)
86 # Fill rest of period with regular version blocks
87 self.nodes[0].generate(VB_PERIOD - VB_THRESHOLD + 1)
88 # Check that we're not getting any versionbit-related errors in
89 # get*info()
90 assert(not VB_PATTERN.match(self.nodes[0].getinfo()["errors"]))
91 assert(not VB_PATTERN.match(self.nodes[0].getmininginfo()["errors"]))
92 assert(not VB_PATTERN.match(self.nodes[0].getnetworkinfo()["warnings"]))
94 # 3. Now build one period of blocks with >= VB_THRESHOLD blocks signaling
95 # some unknown bit
96 self.send_blocks_with_version(test_node, VB_THRESHOLD, nVersion)
97 self.nodes[0].generate(VB_PERIOD - VB_THRESHOLD)
98 # Might not get a versionbits-related alert yet, as we should
99 # have gotten a different alert due to more than 51/100 blocks
100 # being of unexpected version.
101 # Check that get*info() shows some kind of error.
102 assert(WARN_UNKNOWN_RULES_MINED in self.nodes[0].getinfo()["errors"])
103 assert(WARN_UNKNOWN_RULES_MINED in self.nodes[0].getmininginfo()["errors"])
104 assert(WARN_UNKNOWN_RULES_MINED in self.nodes[0].getnetworkinfo()["warnings"])
106 # Mine a period worth of expected blocks so the generic block-version warning
107 # is cleared, and restart the node. This should move the versionbit state
108 # to ACTIVE.
109 self.nodes[0].generate(VB_PERIOD)
110 self.stop_nodes()
111 # Empty out the alert file
112 with open(self.alert_filename, 'w', encoding='utf8') as _:
113 pass
114 self.start_nodes()
116 # Connecting one block should be enough to generate an error.
117 self.nodes[0].generate(1)
118 assert(WARN_UNKNOWN_RULES_ACTIVE in self.nodes[0].getinfo()["errors"])
119 assert(WARN_UNKNOWN_RULES_ACTIVE in self.nodes[0].getmininginfo()["errors"])
120 assert(WARN_UNKNOWN_RULES_ACTIVE in self.nodes[0].getnetworkinfo()["warnings"])
121 self.stop_nodes()
122 self.test_versionbits_in_alert_file()
124 # Test framework expects the node to still be running...
125 self.start_nodes()
127 if __name__ == '__main__':
128 VersionBitsWarningTest().main()