add test for -walletrejectlongchains
[bitcoinplatinum.git] / qa / rpc-tests / maxuploadtarget.py
blob9340e899ebc7ad44d693744e554d231b9bcec7e9
1 #!/usr/bin/env python3
2 # Copyright (c) 2015-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.
6 from test_framework.mininode import *
7 from test_framework.test_framework import BitcoinTestFramework
8 from test_framework.util import *
9 import time
11 '''
12 Test behavior of -maxuploadtarget.
14 * Verify that getdata requests for old blocks (>1week) are dropped
15 if uploadtarget has been reached.
16 * Verify that getdata requests for recent blocks are respecteved even
17 if uploadtarget has been reached.
18 * Verify that the upload counters are reset after 24 hours.
19 '''
21 # TestNode: bare-bones "peer". Used mostly as a conduit for a test to sending
22 # p2p messages to a node, generating the messages in the main testing logic.
23 class TestNode(NodeConnCB):
24 def __init__(self):
25 NodeConnCB.__init__(self)
26 self.connection = None
27 self.ping_counter = 1
28 self.last_pong = msg_pong()
29 self.block_receive_map = {}
31 def add_connection(self, conn):
32 self.connection = conn
33 self.peer_disconnected = False
35 def on_inv(self, conn, message):
36 pass
38 # Track the last getdata message we receive (used in the test)
39 def on_getdata(self, conn, message):
40 self.last_getdata = message
42 def on_block(self, conn, message):
43 message.block.calc_sha256()
44 try:
45 self.block_receive_map[message.block.sha256] += 1
46 except KeyError as e:
47 self.block_receive_map[message.block.sha256] = 1
49 # Spin until verack message is received from the node.
50 # We use this to signal that our test can begin. This
51 # is called from the testing thread, so it needs to acquire
52 # the global lock.
53 def wait_for_verack(self):
54 def veracked():
55 return self.verack_received
56 return wait_until(veracked, timeout=10)
58 def wait_for_disconnect(self):
59 def disconnected():
60 return self.peer_disconnected
61 return wait_until(disconnected, timeout=10)
63 # Wrapper for the NodeConn's send_message function
64 def send_message(self, message):
65 self.connection.send_message(message)
67 def on_pong(self, conn, message):
68 self.last_pong = message
70 def on_close(self, conn):
71 self.peer_disconnected = True
73 # Sync up with the node after delivery of a block
74 def sync_with_ping(self, timeout=30):
75 def received_pong():
76 return (self.last_pong.nonce == self.ping_counter)
77 self.connection.send_message(msg_ping(nonce=self.ping_counter))
78 success = wait_until(received_pong, timeout=timeout)
79 self.ping_counter += 1
80 return success
82 class MaxUploadTest(BitcoinTestFramework):
84 def __init__(self):
85 super().__init__()
86 self.setup_clean_chain = True
87 self.num_nodes = 1
89 # Cache for utxos, as the listunspent may take a long time later in the test
90 self.utxo_cache = []
92 def setup_network(self):
93 # Start a node with maxuploadtarget of 200 MB (/24h)
94 self.nodes = []
95 self.nodes.append(start_node(0, self.options.tmpdir, ["-debug", "-maxuploadtarget=800", "-blockmaxsize=999000"]))
97 def run_test(self):
98 # Before we connect anything, we first set the time on the node
99 # to be in the past, otherwise things break because the CNode
100 # time counters can't be reset backward after initialization
101 old_time = int(time.time() - 2*60*60*24*7)
102 self.nodes[0].setmocktime(old_time)
104 # Generate some old blocks
105 self.nodes[0].generate(130)
107 # test_nodes[0] will only request old blocks
108 # test_nodes[1] will only request new blocks
109 # test_nodes[2] will test resetting the counters
110 test_nodes = []
111 connections = []
113 for i in range(3):
114 test_nodes.append(TestNode())
115 connections.append(NodeConn('127.0.0.1', p2p_port(0), self.nodes[0], test_nodes[i]))
116 test_nodes[i].add_connection(connections[i])
118 NetworkThread().start() # Start up network handling in another thread
119 [x.wait_for_verack() for x in test_nodes]
121 # Test logic begins here
123 # Now mine a big block
124 mine_large_block(self.nodes[0], self.utxo_cache)
126 # Store the hash; we'll request this later
127 big_old_block = self.nodes[0].getbestblockhash()
128 old_block_size = self.nodes[0].getblock(big_old_block, True)['size']
129 big_old_block = int(big_old_block, 16)
131 # Advance to two days ago
132 self.nodes[0].setmocktime(int(time.time()) - 2*60*60*24)
134 # Mine one more block, so that the prior block looks old
135 mine_large_block(self.nodes[0], self.utxo_cache)
137 # We'll be requesting this new block too
138 big_new_block = self.nodes[0].getbestblockhash()
139 big_new_block = int(big_new_block, 16)
141 # test_nodes[0] will test what happens if we just keep requesting the
142 # the same big old block too many times (expect: disconnect)
144 getdata_request = msg_getdata()
145 getdata_request.inv.append(CInv(2, big_old_block))
147 max_bytes_per_day = 800*1024*1024
148 daily_buffer = 144 * 4000000
149 max_bytes_available = max_bytes_per_day - daily_buffer
150 success_count = max_bytes_available // old_block_size
152 # 576MB will be reserved for relaying new blocks, so expect this to
153 # succeed for ~235 tries.
154 for i in range(success_count):
155 test_nodes[0].send_message(getdata_request)
156 test_nodes[0].sync_with_ping()
157 assert_equal(test_nodes[0].block_receive_map[big_old_block], i+1)
159 assert_equal(len(self.nodes[0].getpeerinfo()), 3)
160 # At most a couple more tries should succeed (depending on how long
161 # the test has been running so far).
162 for i in range(3):
163 test_nodes[0].send_message(getdata_request)
164 test_nodes[0].wait_for_disconnect()
165 assert_equal(len(self.nodes[0].getpeerinfo()), 2)
166 print("Peer 0 disconnected after downloading old block too many times")
168 # Requesting the current block on test_nodes[1] should succeed indefinitely,
169 # even when over the max upload target.
170 # We'll try 800 times
171 getdata_request.inv = [CInv(2, big_new_block)]
172 for i in range(800):
173 test_nodes[1].send_message(getdata_request)
174 test_nodes[1].sync_with_ping()
175 assert_equal(test_nodes[1].block_receive_map[big_new_block], i+1)
177 print("Peer 1 able to repeatedly download new block")
179 # But if test_nodes[1] tries for an old block, it gets disconnected too.
180 getdata_request.inv = [CInv(2, big_old_block)]
181 test_nodes[1].send_message(getdata_request)
182 test_nodes[1].wait_for_disconnect()
183 assert_equal(len(self.nodes[0].getpeerinfo()), 1)
185 print("Peer 1 disconnected after trying to download old block")
187 print("Advancing system time on node to clear counters...")
189 # If we advance the time by 24 hours, then the counters should reset,
190 # and test_nodes[2] should be able to retrieve the old block.
191 self.nodes[0].setmocktime(int(time.time()))
192 test_nodes[2].sync_with_ping()
193 test_nodes[2].send_message(getdata_request)
194 test_nodes[2].sync_with_ping()
195 assert_equal(test_nodes[2].block_receive_map[big_old_block], 1)
197 print("Peer 2 able to download old block")
199 [c.disconnect_node() for c in connections]
201 #stop and start node 0 with 1MB maxuploadtarget, whitelist 127.0.0.1
202 print("Restarting nodes with -whitelist=127.0.0.1")
203 stop_node(self.nodes[0], 0)
204 self.nodes[0] = start_node(0, self.options.tmpdir, ["-debug", "-whitelist=127.0.0.1", "-maxuploadtarget=1", "-blockmaxsize=999000"])
206 #recreate/reconnect 3 test nodes
207 test_nodes = []
208 connections = []
210 for i in range(3):
211 test_nodes.append(TestNode())
212 connections.append(NodeConn('127.0.0.1', p2p_port(0), self.nodes[0], test_nodes[i]))
213 test_nodes[i].add_connection(connections[i])
215 NetworkThread().start() # Start up network handling in another thread
216 [x.wait_for_verack() for x in test_nodes]
218 #retrieve 20 blocks which should be enough to break the 1MB limit
219 getdata_request.inv = [CInv(2, big_new_block)]
220 for i in range(20):
221 test_nodes[1].send_message(getdata_request)
222 test_nodes[1].sync_with_ping()
223 assert_equal(test_nodes[1].block_receive_map[big_new_block], i+1)
225 getdata_request.inv = [CInv(2, big_old_block)]
226 test_nodes[1].send_message(getdata_request)
227 test_nodes[1].wait_for_disconnect()
228 assert_equal(len(self.nodes[0].getpeerinfo()), 3) #node is still connected because of the whitelist
230 print("Peer 1 still connected after trying to download old block (whitelisted)")
232 [c.disconnect_node() for c in connections]
234 if __name__ == '__main__':
235 MaxUploadTest().main()