Skip to content

Commit 2613c54

Browse files
committed
[tests] fix flake8 warnings in sendheaders.py
1 parent 99bc0b4 commit 2613c54

File tree

1 file changed

+54
-38
lines changed

1 file changed

+54
-38
lines changed

test/functional/sendheaders.py

Lines changed: 54 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
55
"""Test behavior of headers messages to announce blocks.
66
7-
Setup:
7+
Setup:
88
99
- Two nodes, two p2p connections to node0. One p2p connection should only ever
1010
receive inv's (omitted from testing description below, this is our control).
@@ -83,16 +83,32 @@
8383
e. Announce one more that doesn't connect.
8484
Expect: disconnect.
8585
"""
86-
87-
from test_framework.mininode import *
88-
from test_framework.test_framework import BitcoinTestFramework
89-
from test_framework.util import *
9086
from test_framework.blocktools import create_block, create_coinbase
87+
from test_framework.mininode import (
88+
CBlockHeader,
89+
CInv,
90+
NODE_WITNESS,
91+
NetworkThread,
92+
NodeConnCB,
93+
mininode_lock,
94+
msg_block,
95+
msg_getblocks,
96+
msg_getdata,
97+
msg_getheaders,
98+
msg_headers,
99+
msg_inv,
100+
msg_sendheaders,
101+
)
102+
from test_framework.test_framework import BitcoinTestFramework
103+
from test_framework.util import (
104+
assert_equal,
105+
sync_blocks,
106+
wait_until,
107+
)
91108

109+
DIRECT_FETCH_RESPONSE_TIME = 0.05
92110

93-
direct_fetch_response_time = 0.05
94-
95-
class TestNode(NodeConnCB):
111+
class BaseNode(NodeConnCB):
96112
def __init__(self):
97113
super().__init__()
98114
self.block_announced = False
@@ -136,8 +152,8 @@ def on_headers(self, conn, message):
136152
# right header or the right inv
137153
# inv and headers should be lists of block hashes
138154
def check_last_announcement(self, headers=None, inv=None):
139-
expect_headers = headers if headers != None else []
140-
expect_inv = inv if inv != None else []
155+
expect_headers = headers if headers is not None else []
156+
expect_inv = inv if inv is not None else []
141157
test_function = lambda: self.block_announced
142158
wait_until(test_function, timeout=60, lock=mininode_lock)
143159
with mininode_lock:
@@ -153,7 +169,7 @@ def check_last_announcement(self, headers=None, inv=None):
153169
hash_headers = []
154170
if "headers" in self.last_message:
155171
# treat headers as a list of block hashes
156-
hash_headers = [ x.sha256 for x in self.last_message["headers"].headers ]
172+
hash_headers = [x.sha256 for x in self.last_message["headers"].headers]
157173
if hash_headers != expect_headers:
158174
success = False
159175

@@ -176,7 +192,7 @@ def wait_for_block_announcement(self, block_hash, timeout=60):
176192

177193
def send_header_for_blocks(self, new_blocks):
178194
headers_message = msg_headers()
179-
headers_message.headers = [ CBlockHeader(b) for b in new_blocks ]
195+
headers_message.headers = [CBlockHeader(b) for b in new_blocks]
180196
self.send_message(headers_message)
181197

182198
def send_getblocks(self, locator):
@@ -202,27 +218,27 @@ def mine_blocks(self, count):
202218
# to-be-reorged-out blocks are mined, so that we don't break later tests.
203219
# return the list of block hashes newly mined
204220
def mine_reorg(self, length):
205-
self.nodes[0].generate(length) # make sure all invalidated blocks are node0's
221+
self.nodes[0].generate(length) # make sure all invalidated blocks are node0's
206222
sync_blocks(self.nodes, wait=0.1)
207223
for x in self.nodes[0].p2ps:
208224
x.wait_for_block_announcement(int(self.nodes[0].getbestblockhash(), 16))
209225
x.clear_last_announcement()
210226

211227
tip_height = self.nodes[1].getblockcount()
212-
hash_to_invalidate = self.nodes[1].getblockhash(tip_height-(length-1))
228+
hash_to_invalidate = self.nodes[1].getblockhash(tip_height - (length - 1))
213229
self.nodes[1].invalidateblock(hash_to_invalidate)
214-
all_hashes = self.nodes[1].generate(length+1) # Must be longer than the orig chain
230+
all_hashes = self.nodes[1].generate(length + 1) # Must be longer than the orig chain
215231
sync_blocks(self.nodes, wait=0.1)
216232
return [int(x, 16) for x in all_hashes]
217233

218234
def run_test(self):
219235
# Setup the p2p connections and start up the network thread.
220-
inv_node = self.nodes[0].add_p2p_connection(TestNode())
236+
inv_node = self.nodes[0].add_p2p_connection(BaseNode())
221237
# Set nServices to 0 for test_node, so no block download will occur outside of
222238
# direct fetching
223-
test_node = self.nodes[0].add_p2p_connection(TestNode(), services=NODE_WITNESS)
239+
test_node = self.nodes[0].add_p2p_connection(BaseNode(), services=NODE_WITNESS)
224240

225-
NetworkThread().start() # Start up network handling in another thread
241+
NetworkThread().start() # Start up network handling in another thread
226242

227243
# Test logic begins here
228244
inv_node.wait_for_verack()
@@ -275,18 +291,18 @@ def test_nonnull_locators(self, test_node, inv_node):
275291
test_node.get_headers(locator=[old_tip], hashstop=tip)
276292
test_node.get_data([tip])
277293
test_node.wait_for_block(tip)
278-
test_node.clear_last_announcement() # since we requested headers...
294+
test_node.clear_last_announcement() # since we requested headers...
279295
elif i == 2:
280296
# this time announce own block via headers
281297
height = self.nodes[0].getblockcount()
282298
last_time = self.nodes[0].getblock(self.nodes[0].getbestblockhash())['time']
283299
block_time = last_time + 1
284-
new_block = create_block(tip, create_coinbase(height+1), block_time)
300+
new_block = create_block(tip, create_coinbase(height + 1), block_time)
285301
new_block.solve()
286302
test_node.send_header_for_blocks([new_block])
287303
test_node.wait_for_getdata([new_block.sha256])
288304
test_node.send_message(msg_block(new_block))
289-
test_node.sync_with_ping() # make sure this block is processed
305+
test_node.sync_with_ping() # make sure this block is processed
290306
inv_node.clear_last_announcement()
291307
test_node.clear_last_announcement()
292308

@@ -305,7 +321,7 @@ def test_nonnull_locators(self, test_node, inv_node):
305321
assert_equal(inv_node.check_last_announcement(inv=[tip]), True)
306322
assert_equal(test_node.check_last_announcement(headers=[tip]), True)
307323

308-
height = self.nodes[0].getblockcount()+1
324+
height = self.nodes[0].getblockcount() + 1
309325
block_time += 10 # Advance far enough ahead
310326
for i in range(10):
311327
# Mine i blocks, and alternate announcing either via
@@ -314,7 +330,7 @@ def test_nonnull_locators(self, test_node, inv_node):
314330
# with block header, even though the blocks are never requested
315331
for j in range(2):
316332
blocks = []
317-
for b in range(i+1):
333+
for b in range(i + 1):
318334
blocks.append(create_block(tip, create_coinbase(height), block_time))
319335
blocks[-1].solve()
320336
tip = blocks[-1].sha256
@@ -328,7 +344,7 @@ def test_nonnull_locators(self, test_node, inv_node):
328344
test_node.send_header_for_blocks(blocks)
329345
# Test that duplicate inv's won't result in duplicate
330346
# getdata requests, or duplicate headers announcements
331-
[ inv_node.send_block_inv(x.sha256) for x in blocks ]
347+
[inv_node.send_block_inv(x.sha256) for x in blocks]
332348
test_node.wait_for_getdata([x.sha256 for x in blocks])
333349
inv_node.sync_with_ping()
334350
else:
@@ -339,7 +355,7 @@ def test_nonnull_locators(self, test_node, inv_node):
339355
# getdata requests (the check is further down)
340356
inv_node.send_header_for_blocks(blocks)
341357
inv_node.sync_with_ping()
342-
[ test_node.send_message(msg_block(x)) for x in blocks ]
358+
[test_node.send_message(msg_block(x)) for x in blocks]
343359
test_node.sync_with_ping()
344360
inv_node.sync_with_ping()
345361
# This block should not be announced to the inv node (since it also
@@ -365,7 +381,7 @@ def test_nonnull_locators(self, test_node, inv_node):
365381
assert_equal(inv_node.check_last_announcement(inv=[tip]), True)
366382
assert_equal(test_node.check_last_announcement(headers=new_block_hashes), True)
367383

368-
block_time += 8
384+
block_time += 8
369385

370386
# Mine a too-large reorg, which should be announced with a single inv
371387
new_block_hashes = self.mine_reorg(length=8)
@@ -379,7 +395,7 @@ def test_nonnull_locators(self, test_node, inv_node):
379395
fork_point = int(fork_point, 16)
380396

381397
# Use getblocks/getdata
382-
test_node.send_getblocks(locator = [fork_point])
398+
test_node.send_getblocks(locator=[fork_point])
383399
assert_equal(test_node.check_last_announcement(inv=new_block_hashes), True)
384400
test_node.get_data(new_block_hashes)
385401
test_node.wait_for_block(new_block_hashes[-1])
@@ -403,7 +419,7 @@ def test_nonnull_locators(self, test_node, inv_node):
403419
test_node.get_data([tip])
404420
test_node.wait_for_block(tip)
405421
# This time, try sending either a getheaders to trigger resumption
406-
# of headers announcements, or mine a new block and inv it, also
422+
# of headers announcements, or mine a new block and inv it, also
407423
# triggering resumption of headers announcements.
408424
if j == 0:
409425
test_node.get_headers(locator=[tip], hashstop=0)
@@ -434,7 +450,7 @@ def test_nonnull_locators(self, test_node, inv_node):
434450
height += 1
435451
inv_node.send_message(msg_block(blocks[-1]))
436452

437-
inv_node.sync_with_ping() # Make sure blocks are processed
453+
inv_node.sync_with_ping() # Make sure blocks are processed
438454
test_node.last_message.pop("getdata", None)
439455
test_node.send_header_for_blocks(blocks)
440456
test_node.sync_with_ping()
@@ -453,9 +469,9 @@ def test_nonnull_locators(self, test_node, inv_node):
453469

454470
test_node.send_header_for_blocks(blocks)
455471
test_node.sync_with_ping()
456-
test_node.wait_for_getdata([x.sha256 for x in blocks], timeout=direct_fetch_response_time)
472+
test_node.wait_for_getdata([x.sha256 for x in blocks], timeout=DIRECT_FETCH_RESPONSE_TIME)
457473

458-
[ test_node.send_message(msg_block(x)) for x in blocks ]
474+
[test_node.send_message(msg_block(x)) for x in blocks]
459475

460476
test_node.sync_with_ping()
461477

@@ -484,13 +500,13 @@ def test_nonnull_locators(self, test_node, inv_node):
484500
# both blocks (same work as tip)
485501
test_node.send_header_for_blocks(blocks[1:2])
486502
test_node.sync_with_ping()
487-
test_node.wait_for_getdata([x.sha256 for x in blocks[0:2]], timeout=direct_fetch_response_time)
503+
test_node.wait_for_getdata([x.sha256 for x in blocks[0:2]], timeout=DIRECT_FETCH_RESPONSE_TIME)
488504

489505
# Announcing 16 more headers should trigger direct fetch for 14 more
490506
# blocks
491507
test_node.send_header_for_blocks(blocks[2:18])
492508
test_node.sync_with_ping()
493-
test_node.wait_for_getdata([x.sha256 for x in blocks[2:16]], timeout=direct_fetch_response_time)
509+
test_node.wait_for_getdata([x.sha256 for x in blocks[2:16]], timeout=DIRECT_FETCH_RESPONSE_TIME)
494510

495511
# Announcing 1 more header should not trigger any response
496512
test_node.last_message.pop("getdata", None)
@@ -502,7 +518,7 @@ def test_nonnull_locators(self, test_node, inv_node):
502518
self.log.info("Part 4: success!")
503519

504520
# Now deliver all those blocks we announced.
505-
[ test_node.send_message(msg_block(x)) for x in blocks ]
521+
[test_node.send_message(msg_block(x)) for x in blocks]
506522

507523
self.log.info("Part 5: Testing handling of unconnecting headers")
508524
# First we test that receipt of an unconnecting header doesn't prevent
@@ -524,15 +540,15 @@ def test_nonnull_locators(self, test_node, inv_node):
524540
test_node.wait_for_getheaders()
525541
test_node.send_header_for_blocks(blocks)
526542
test_node.wait_for_getdata([x.sha256 for x in blocks])
527-
[ test_node.send_message(msg_block(x)) for x in blocks ]
543+
[test_node.send_message(msg_block(x)) for x in blocks]
528544
test_node.sync_with_ping()
529545
assert_equal(int(self.nodes[0].getbestblockhash(), 16), blocks[1].sha256)
530546

531547
blocks = []
532548
# Now we test that if we repeatedly don't send connecting headers, we
533549
# don't go into an infinite loop trying to get them to connect.
534550
MAX_UNCONNECTING_HEADERS = 10
535-
for j in range(MAX_UNCONNECTING_HEADERS+1):
551+
for j in range(MAX_UNCONNECTING_HEADERS + 1):
536552
blocks.append(create_block(tip, create_coinbase(height), block_time))
537553
blocks[-1].solve()
538554
tip = blocks[-1].sha256
@@ -554,11 +570,11 @@ def test_nonnull_locators(self, test_node, inv_node):
554570

555571
# Now try to see how many unconnecting headers we can send
556572
# before we get disconnected. Should be 5*MAX_UNCONNECTING_HEADERS
557-
for i in range(5*MAX_UNCONNECTING_HEADERS - 1):
573+
for i in range(5 * MAX_UNCONNECTING_HEADERS - 1):
558574
# Send a header that doesn't connect, check that we get a getheaders.
559575
with mininode_lock:
560576
test_node.last_message.pop("getheaders", None)
561-
test_node.send_header_for_blocks([blocks[i%len(blocks)]])
577+
test_node.send_header_for_blocks([blocks[i % len(blocks)]])
562578
test_node.wait_for_getheaders()
563579

564580
# Eventually this stops working.

0 commit comments

Comments
 (0)