|
10 | 10 | if uploadtarget has been reached.
|
11 | 11 | * Verify that the upload counters are reset after 24 hours.
|
12 | 12 | """
|
| 13 | +from collections import defaultdict |
| 14 | +import time |
13 | 15 |
|
14 | 16 | from test_framework.mininode import *
|
15 | 17 | from test_framework.test_framework import BitcoinTestFramework
|
16 | 18 | from test_framework.util import *
|
17 |
| -import time |
18 | 19 |
|
19 |
| -# TestNode: bare-bones "peer". Used mostly as a conduit for a test to sending |
20 |
| -# p2p messages to a node, generating the messages in the main testing logic. |
21 | 20 | class TestNode(NodeConnCB):
|
22 | 21 | def __init__(self):
|
23 | 22 | super().__init__()
|
24 |
| - self.connection = None |
25 |
| - self.ping_counter = 1 |
26 |
| - self.last_pong = msg_pong() |
27 |
| - self.block_receive_map = {} |
28 |
| - |
29 |
| - def add_connection(self, conn): |
30 |
| - self.connection = conn |
31 |
| - self.peer_disconnected = False |
| 23 | + self.block_receive_map = defaultdict(int) |
32 | 24 |
|
33 | 25 | def on_inv(self, conn, message):
|
34 | 26 | pass
|
35 | 27 |
|
36 |
| - # Track the last getdata message we receive (used in the test) |
37 |
| - def on_getdata(self, conn, message): |
38 |
| - self.last_getdata = message |
39 |
| - |
40 | 28 | def on_block(self, conn, message):
|
41 | 29 | message.block.calc_sha256()
|
42 |
| - try: |
43 |
| - self.block_receive_map[message.block.sha256] += 1 |
44 |
| - except KeyError as e: |
45 |
| - self.block_receive_map[message.block.sha256] = 1 |
46 |
| - |
47 |
| - # Spin until verack message is received from the node. |
48 |
| - # We use this to signal that our test can begin. This |
49 |
| - # is called from the testing thread, so it needs to acquire |
50 |
| - # the global lock. |
51 |
| - def wait_for_verack(self): |
52 |
| - def veracked(): |
53 |
| - return self.verack_received |
54 |
| - return wait_until(veracked, timeout=10) |
55 |
| - |
56 |
| - def wait_for_disconnect(self): |
57 |
| - def disconnected(): |
58 |
| - return self.peer_disconnected |
59 |
| - return wait_until(disconnected, timeout=10) |
60 |
| - |
61 |
| - # Wrapper for the NodeConn's send_message function |
62 |
| - def send_message(self, message): |
63 |
| - self.connection.send_message(message) |
64 |
| - |
65 |
| - def on_pong(self, conn, message): |
66 |
| - self.last_pong = message |
67 |
| - |
68 |
| - def on_close(self, conn): |
69 |
| - self.peer_disconnected = True |
| 30 | + self.block_receive_map[message.block.sha256] += 1 |
70 | 31 |
|
71 | 32 | class MaxUploadTest(BitcoinTestFramework):
|
72 | 33 |
|
@@ -192,33 +153,26 @@ def run_test(self):
|
192 | 153 | stop_node(self.nodes[0], 0)
|
193 | 154 | self.nodes[0] = start_node(0, self.options.tmpdir, ["-whitelist=127.0.0.1", "-maxuploadtarget=1", "-blockmaxsize=999000"])
|
194 | 155 |
|
195 |
| - #recreate/reconnect 3 test nodes |
196 |
| - test_nodes = [] |
197 |
| - connections = [] |
198 |
| - |
199 |
| - for i in range(3): |
200 |
| - test_nodes.append(TestNode()) |
201 |
| - connections.append(NodeConn('127.0.0.1', p2p_port(0), self.nodes[0], test_nodes[i])) |
202 |
| - test_nodes[i].add_connection(connections[i]) |
| 156 | + #recreate/reconnect a test node |
| 157 | + test_nodes = [TestNode()] |
| 158 | + connections = [NodeConn('127.0.0.1', p2p_port(0), self.nodes[0], test_nodes[0])] |
| 159 | + test_nodes[0].add_connection(connections[0]) |
203 | 160 |
|
204 | 161 | NetworkThread().start() # Start up network handling in another thread
|
205 |
| - [x.wait_for_verack() for x in test_nodes] |
| 162 | + test_nodes[0].wait_for_verack() |
206 | 163 |
|
207 | 164 | #retrieve 20 blocks which should be enough to break the 1MB limit
|
208 | 165 | getdata_request.inv = [CInv(2, big_new_block)]
|
209 | 166 | for i in range(20):
|
210 |
| - test_nodes[1].send_message(getdata_request) |
211 |
| - test_nodes[1].sync_with_ping() |
212 |
| - assert_equal(test_nodes[1].block_receive_map[big_new_block], i+1) |
| 167 | + test_nodes[0].send_message(getdata_request) |
| 168 | + test_nodes[0].sync_with_ping() |
| 169 | + assert_equal(test_nodes[0].block_receive_map[big_new_block], i+1) |
213 | 170 |
|
214 | 171 | getdata_request.inv = [CInv(2, big_old_block)]
|
215 |
| - test_nodes[1].send_message(getdata_request) |
216 |
| - test_nodes[1].wait_for_disconnect() |
217 |
| - assert_equal(len(self.nodes[0].getpeerinfo()), 3) #node is still connected because of the whitelist |
| 172 | + test_nodes[0].send_and_ping(getdata_request) |
| 173 | + assert_equal(len(self.nodes[0].getpeerinfo()), 1) #node is still connected because of the whitelist |
218 | 174 |
|
219 |
| - self.log.info("Peer 1 still connected after trying to download old block (whitelisted)") |
220 |
| - |
221 |
| - [c.disconnect_node() for c in connections] |
| 175 | + self.log.info("Peer still connected after trying to download old block (whitelisted)") |
222 | 176 |
|
223 | 177 | if __name__ == '__main__':
|
224 | 178 | MaxUploadTest().main()
|
0 commit comments