[tests] don't override __init__() in individual tests
[bitcoinplatinum.git] / test / functional / maxuploadtarget.py
blob1f402798e7e01e05a60e5565d77867ea3101efec
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.
5 """Test behavior of -maxuploadtarget.
7 * Verify that getdata requests for old blocks (>1week) are dropped
8 if uploadtarget has been reached.
9 * Verify that getdata requests for recent blocks are respecteved even
10 if uploadtarget has been reached.
11 * Verify that the upload counters are reset after 24 hours.
12 """
13 from collections import defaultdict
14 import time
16 from test_framework.mininode import *
17 from test_framework.test_framework import BitcoinTestFramework
18 from test_framework.util import *
20 class TestNode(NodeConnCB):
21 def __init__(self):
22 super().__init__()
23 self.block_receive_map = defaultdict(int)
25 def on_inv(self, conn, message):
26 pass
28 def on_block(self, conn, message):
29 message.block.calc_sha256()
30 self.block_receive_map[message.block.sha256] += 1
32 class MaxUploadTest(BitcoinTestFramework):
34 def set_test_params(self):
35 self.setup_clean_chain = True
36 self.num_nodes = 1
37 self.extra_args = [["-maxuploadtarget=800", "-blockmaxsize=999000"]]
39 # Cache for utxos, as the listunspent may take a long time later in the test
40 self.utxo_cache = []
42 def run_test(self):
43 # Before we connect anything, we first set the time on the node
44 # to be in the past, otherwise things break because the CNode
45 # time counters can't be reset backward after initialization
46 old_time = int(time.time() - 2*60*60*24*7)
47 self.nodes[0].setmocktime(old_time)
49 # Generate some old blocks
50 self.nodes[0].generate(130)
52 # test_nodes[0] will only request old blocks
53 # test_nodes[1] will only request new blocks
54 # test_nodes[2] will test resetting the counters
55 test_nodes = []
56 connections = []
58 for i in range(3):
59 test_nodes.append(TestNode())
60 connections.append(NodeConn('127.0.0.1', p2p_port(0), self.nodes[0], test_nodes[i]))
61 test_nodes[i].add_connection(connections[i])
63 NetworkThread().start() # Start up network handling in another thread
64 [x.wait_for_verack() for x in test_nodes]
66 # Test logic begins here
68 # Now mine a big block
69 mine_large_block(self.nodes[0], self.utxo_cache)
71 # Store the hash; we'll request this later
72 big_old_block = self.nodes[0].getbestblockhash()
73 old_block_size = self.nodes[0].getblock(big_old_block, True)['size']
74 big_old_block = int(big_old_block, 16)
76 # Advance to two days ago
77 self.nodes[0].setmocktime(int(time.time()) - 2*60*60*24)
79 # Mine one more block, so that the prior block looks old
80 mine_large_block(self.nodes[0], self.utxo_cache)
82 # We'll be requesting this new block too
83 big_new_block = self.nodes[0].getbestblockhash()
84 big_new_block = int(big_new_block, 16)
86 # test_nodes[0] will test what happens if we just keep requesting the
87 # the same big old block too many times (expect: disconnect)
89 getdata_request = msg_getdata()
90 getdata_request.inv.append(CInv(2, big_old_block))
92 max_bytes_per_day = 800*1024*1024
93 daily_buffer = 144 * 4000000
94 max_bytes_available = max_bytes_per_day - daily_buffer
95 success_count = max_bytes_available // old_block_size
97 # 576MB will be reserved for relaying new blocks, so expect this to
98 # succeed for ~235 tries.
99 for i in range(success_count):
100 test_nodes[0].send_message(getdata_request)
101 test_nodes[0].sync_with_ping()
102 assert_equal(test_nodes[0].block_receive_map[big_old_block], i+1)
104 assert_equal(len(self.nodes[0].getpeerinfo()), 3)
105 # At most a couple more tries should succeed (depending on how long
106 # the test has been running so far).
107 for i in range(3):
108 test_nodes[0].send_message(getdata_request)
109 test_nodes[0].wait_for_disconnect()
110 assert_equal(len(self.nodes[0].getpeerinfo()), 2)
111 self.log.info("Peer 0 disconnected after downloading old block too many times")
113 # Requesting the current block on test_nodes[1] should succeed indefinitely,
114 # even when over the max upload target.
115 # We'll try 800 times
116 getdata_request.inv = [CInv(2, big_new_block)]
117 for i in range(800):
118 test_nodes[1].send_message(getdata_request)
119 test_nodes[1].sync_with_ping()
120 assert_equal(test_nodes[1].block_receive_map[big_new_block], i+1)
122 self.log.info("Peer 1 able to repeatedly download new block")
124 # But if test_nodes[1] tries for an old block, it gets disconnected too.
125 getdata_request.inv = [CInv(2, big_old_block)]
126 test_nodes[1].send_message(getdata_request)
127 test_nodes[1].wait_for_disconnect()
128 assert_equal(len(self.nodes[0].getpeerinfo()), 1)
130 self.log.info("Peer 1 disconnected after trying to download old block")
132 self.log.info("Advancing system time on node to clear counters...")
134 # If we advance the time by 24 hours, then the counters should reset,
135 # and test_nodes[2] should be able to retrieve the old block.
136 self.nodes[0].setmocktime(int(time.time()))
137 test_nodes[2].sync_with_ping()
138 test_nodes[2].send_message(getdata_request)
139 test_nodes[2].sync_with_ping()
140 assert_equal(test_nodes[2].block_receive_map[big_old_block], 1)
142 self.log.info("Peer 2 able to download old block")
144 [c.disconnect_node() for c in connections]
146 #stop and start node 0 with 1MB maxuploadtarget, whitelist 127.0.0.1
147 self.log.info("Restarting nodes with -whitelist=127.0.0.1")
148 self.stop_node(0)
149 self.start_node(0, ["-whitelist=127.0.0.1", "-maxuploadtarget=1", "-blockmaxsize=999000"])
151 #recreate/reconnect a test node
152 test_nodes = [TestNode()]
153 connections = [NodeConn('127.0.0.1', p2p_port(0), self.nodes[0], test_nodes[0])]
154 test_nodes[0].add_connection(connections[0])
156 NetworkThread().start() # Start up network handling in another thread
157 test_nodes[0].wait_for_verack()
159 #retrieve 20 blocks which should be enough to break the 1MB limit
160 getdata_request.inv = [CInv(2, big_new_block)]
161 for i in range(20):
162 test_nodes[0].send_message(getdata_request)
163 test_nodes[0].sync_with_ping()
164 assert_equal(test_nodes[0].block_receive_map[big_new_block], i+1)
166 getdata_request.inv = [CInv(2, big_old_block)]
167 test_nodes[0].send_and_ping(getdata_request)
168 assert_equal(len(self.nodes[0].getpeerinfo()), 1) #node is still connected because of the whitelist
170 self.log.info("Peer still connected after trying to download old block (whitelisted)")
172 if __name__ == '__main__':
173 MaxUploadTest().main()