[qa] Improve prioritisetransaction functional test
[bitcoinplatinum.git] / test / functional / p2p-versionbits-warning.py
blobd29d43ebedf396c04a0b053a4801968d4b46e67f
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(P2PInterface):
27 def on_inv(self, 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 self.nodes[0].add_p2p_connection(TestNode())
69 network_thread_start()
71 # Test logic begins here
72 self.nodes[0].p2p.wait_for_verack()
74 # 1. Have the node mine one period worth of blocks
75 self.nodes[0].generate(VB_PERIOD)
77 # 2. Now build one period of blocks on the tip, with < VB_THRESHOLD
78 # blocks signaling some unknown bit.
79 nVersion = VB_TOP_BITS | (1<<VB_UNKNOWN_BIT)
80 self.send_blocks_with_version(self.nodes[0].p2p, VB_THRESHOLD-1, nVersion)
82 # Fill rest of period with regular version blocks
83 self.nodes[0].generate(VB_PERIOD - VB_THRESHOLD + 1)
84 # Check that we're not getting any versionbit-related errors in
85 # get*info()
86 assert(not VB_PATTERN.match(self.nodes[0].getmininginfo()["warnings"]))
87 assert(not VB_PATTERN.match(self.nodes[0].getnetworkinfo()["warnings"]))
89 # 3. Now build one period of blocks with >= VB_THRESHOLD blocks signaling
90 # some unknown bit
91 self.send_blocks_with_version(self.nodes[0].p2p, VB_THRESHOLD, nVersion)
92 self.nodes[0].generate(VB_PERIOD - VB_THRESHOLD)
93 # Might not get a versionbits-related alert yet, as we should
94 # have gotten a different alert due to more than 51/100 blocks
95 # being of unexpected version.
96 # Check that get*info() shows some kind of error.
97 assert(WARN_UNKNOWN_RULES_MINED in self.nodes[0].getmininginfo()["warnings"])
98 assert(WARN_UNKNOWN_RULES_MINED in self.nodes[0].getnetworkinfo()["warnings"])
100 # Mine a period worth of expected blocks so the generic block-version warning
101 # is cleared, and restart the node. This should move the versionbit state
102 # to ACTIVE.
103 self.nodes[0].generate(VB_PERIOD)
104 self.stop_nodes()
105 # Empty out the alert file
106 with open(self.alert_filename, 'w', encoding='utf8') as _:
107 pass
108 self.start_nodes()
110 # Connecting one block should be enough to generate an error.
111 self.nodes[0].generate(1)
112 assert(WARN_UNKNOWN_RULES_ACTIVE in self.nodes[0].getmininginfo()["warnings"])
113 assert(WARN_UNKNOWN_RULES_ACTIVE in self.nodes[0].getnetworkinfo()["warnings"])
114 self.stop_nodes()
115 self.test_versionbits_in_alert_file()
117 # Test framework expects the node to still be running...
118 self.start_nodes()
120 if __name__ == '__main__':
121 VersionBitsWarningTest().main()