You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

sendheaders.py 25KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605
  1. #!/usr/bin/env python3
  2. # Copyright (c) 2014-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 headers messages to announce blocks.
  6. Setup:
  7. - Two nodes, two p2p connections to node0. One p2p connection should only ever
  8. receive inv's (omitted from testing description below, this is our control).
  9. Second node is used for creating reorgs.
  10. Part 1: No headers announcements before "sendheaders"
  11. a. node mines a block [expect: inv]
  12. send getdata for the block [expect: block]
  13. b. node mines another block [expect: inv]
  14. send getheaders and getdata [expect: headers, then block]
  15. c. node mines another block [expect: inv]
  16. peer mines a block, announces with header [expect: getdata]
  17. d. node mines another block [expect: inv]
  18. Part 2: After "sendheaders", headers announcements should generally work.
  19. a. peer sends sendheaders [expect: no response]
  20. peer sends getheaders with current tip [expect: no response]
  21. b. node mines a block [expect: tip header]
  22. c. for N in 1, ..., 10:
  23. * for announce-type in {inv, header}
  24. - peer mines N blocks, announces with announce-type
  25. [ expect: getheaders/getdata or getdata, deliver block(s) ]
  26. - node mines a block [ expect: 1 header ]
  27. Part 3: Headers announcements stop after large reorg and resume after getheaders or inv from peer.
  28. - For response-type in {inv, getheaders}
  29. * node mines a 7 block reorg [ expect: headers announcement of 8 blocks ]
  30. * node mines an 8-block reorg [ expect: inv at tip ]
  31. * peer responds with getblocks/getdata [expect: inv, blocks ]
  32. * node mines another block [ expect: inv at tip, peer sends getdata, expect: block ]
  33. * node mines another block at tip [ expect: inv ]
  34. * peer responds with getheaders with an old hashstop more than 8 blocks back [expect: headers]
  35. * peer requests block [ expect: block ]
  36. * node mines another block at tip [ expect: inv, peer sends getdata, expect: block ]
  37. * peer sends response-type [expect headers if getheaders, getheaders/getdata if mining new block]
  38. * node mines 1 block [expect: 1 header, peer responds with getdata]
  39. Part 4: Test direct fetch behavior
  40. a. Announce 2 old block headers.
  41. Expect: no getdata requests.
  42. b. Announce 3 new blocks via 1 headers message.
  43. Expect: one getdata request for all 3 blocks.
  44. (Send blocks.)
  45. c. Announce 1 header that forks off the last two blocks.
  46. Expect: no response.
  47. d. Announce 1 more header that builds on that fork.
  48. Expect: one getdata request for two blocks.
  49. e. Announce 16 more headers that build on that fork.
  50. Expect: getdata request for 14 more blocks.
  51. f. Announce 1 more header that builds on that fork.
  52. Expect: no response.
  53. Part 5: Test handling of headers that don't connect.
  54. a. Repeat 10 times:
  55. 1. Announce a header that doesn't connect.
  56. Expect: getheaders message
  57. 2. Send headers chain.
  58. Expect: getdata for the missing blocks, tip update.
  59. b. Then send 9 more headers that don't connect.
  60. Expect: getheaders message each time.
  61. c. Announce a header that does connect.
  62. Expect: no response.
  63. d. Announce 49 headers that don't connect.
  64. Expect: getheaders message each time.
  65. e. Announce one more that doesn't connect.
  66. Expect: disconnect.
  67. """
  68. from test_framework.mininode import *
  69. from test_framework.test_framework import BitcoinTestFramework
  70. from test_framework.util import *
  71. from test_framework.blocktools import create_block, create_coinbase
  72. direct_fetch_response_time = 0.05
  73. class BaseNode(SingleNodeConnCB):
  74. def __init__(self):
  75. SingleNodeConnCB.__init__(self)
  76. self.last_inv = None
  77. self.last_headers = None
  78. self.last_block = None
  79. self.last_getdata = None
  80. self.block_announced = False
  81. self.last_getheaders = None
  82. self.disconnected = False
  83. self.last_blockhash_announced = None
  84. def clear_last_announcement(self):
  85. with mininode_lock:
  86. self.block_announced = False
  87. self.last_inv = None
  88. self.last_headers = None
  89. # Request data for a list of block hashes
  90. def get_data(self, block_hashes):
  91. msg = msg_getdata()
  92. for x in block_hashes:
  93. msg.inv.append(CInv(2, x))
  94. self.connection.send_message(msg)
  95. def get_headers(self, locator, hashstop):
  96. msg = msg_getheaders()
  97. msg.locator.vHave = locator
  98. msg.hashstop = hashstop
  99. self.connection.send_message(msg)
  100. def send_block_inv(self, blockhash):
  101. msg = msg_inv()
  102. msg.inv = [CInv(2, blockhash)]
  103. self.connection.send_message(msg)
  104. def on_inv(self, conn, message):
  105. self.last_inv = message
  106. self.block_announced = True
  107. self.last_blockhash_announced = message.inv[-1].hash
  108. def on_headers(self, conn, message):
  109. self.last_headers = message
  110. if len(message.headers):
  111. self.block_announced = True
  112. message.headers[-1].calc_sha256()
  113. self.last_blockhash_announced = message.headers[-1].sha256
  114. def on_block(self, conn, message):
  115. self.last_block = message.block
  116. self.last_block.calc_sha256()
  117. def on_getdata(self, conn, message):
  118. self.last_getdata = message
  119. def on_getheaders(self, conn, message):
  120. self.last_getheaders = message
  121. def on_close(self, conn):
  122. self.disconnected = True
  123. # Test whether the last announcement we received had the
  124. # right header or the right inv
  125. # inv and headers should be lists of block hashes
  126. def check_last_announcement(self, headers=None, inv=None):
  127. expect_headers = headers if headers != None else []
  128. expect_inv = inv if inv != None else []
  129. test_function = lambda: self.block_announced
  130. assert(wait_until(test_function, timeout=60))
  131. with mininode_lock:
  132. self.block_announced = False
  133. success = True
  134. compare_inv = []
  135. if self.last_inv != None:
  136. compare_inv = [x.hash for x in self.last_inv.inv]
  137. if compare_inv != expect_inv:
  138. success = False
  139. hash_headers = []
  140. if self.last_headers != None:
  141. # treat headers as a list of block hashes
  142. hash_headers = [ x.sha256 for x in self.last_headers.headers ]
  143. if hash_headers != expect_headers:
  144. success = False
  145. self.last_inv = None
  146. self.last_headers = None
  147. return success
  148. # Syncing helpers
  149. def wait_for_block(self, blockhash, timeout=60):
  150. test_function = lambda: self.last_block != None and self.last_block.sha256 == blockhash
  151. assert(wait_until(test_function, timeout=timeout))
  152. return
  153. def wait_for_getheaders(self, timeout=60):
  154. test_function = lambda: self.last_getheaders != None
  155. assert(wait_until(test_function, timeout=timeout))
  156. return
  157. def wait_for_getdata(self, hash_list, timeout=60):
  158. if hash_list == []:
  159. return
  160. test_function = lambda: self.last_getdata != None and [x.hash for x in self.last_getdata.inv] == hash_list
  161. assert(wait_until(test_function, timeout=timeout))
  162. return
  163. def wait_for_disconnect(self, timeout=60):
  164. test_function = lambda: self.disconnected
  165. assert(wait_until(test_function, timeout=timeout))
  166. return
  167. def wait_for_block_announcement(self, block_hash, timeout=60):
  168. test_function = lambda: self.last_blockhash_announced == block_hash
  169. assert(wait_until(test_function, timeout=timeout))
  170. return
  171. def send_header_for_blocks(self, new_blocks):
  172. headers_message = msg_headers()
  173. headers_message.headers = [ CBlockHeader(b) for b in new_blocks ]
  174. self.send_message(headers_message)
  175. def send_getblocks(self, locator):
  176. getblocks_message = msg_getblocks()
  177. getblocks_message.locator.vHave = locator
  178. self.send_message(getblocks_message)
  179. # InvNode: This peer should only ever receive inv's, because it doesn't ever send a
  180. # "sendheaders" message.
  181. class InvNode(BaseNode):
  182. def __init__(self):
  183. BaseNode.__init__(self)
  184. # TestNode: This peer is the one we use for most of the testing.
  185. class TestNode(BaseNode):
  186. def __init__(self):
  187. BaseNode.__init__(self)
  188. class SendHeadersTest(BitcoinTestFramework):
  189. def __init__(self):
  190. super().__init__()
  191. self.setup_clean_chain = True
  192. self.num_nodes = 2
  193. def setup_network(self):
  194. self.nodes = []
  195. self.nodes = start_nodes(self.num_nodes, self.options.tmpdir)
  196. connect_nodes(self.nodes[0], 1)
  197. # mine count blocks and return the new tip
  198. def mine_blocks(self, count):
  199. # Clear out last block announcement from each p2p listener
  200. [ x.clear_last_announcement() for x in self.p2p_connections ]
  201. self.nodes[0].generate(count)
  202. return int(self.nodes[0].getbestblockhash(), 16)
  203. # mine a reorg that invalidates length blocks (replacing them with
  204. # length+1 blocks).
  205. # Note: we clear the state of our p2p connections after the
  206. # to-be-reorged-out blocks are mined, so that we don't break later tests.
  207. # return the list of block hashes newly mined
  208. def mine_reorg(self, length):
  209. self.nodes[0].generate(length) # make sure all invalidated blocks are node0's
  210. sync_blocks(self.nodes, wait=0.1)
  211. for x in self.p2p_connections:
  212. x.wait_for_block_announcement(int(self.nodes[0].getbestblockhash(), 16))
  213. x.clear_last_announcement()
  214. tip_height = self.nodes[1].getblockcount()
  215. hash_to_invalidate = self.nodes[1].getblockhash(tip_height-(length-1))
  216. self.nodes[1].invalidateblock(hash_to_invalidate)
  217. all_hashes = self.nodes[1].generate(length+1) # Must be longer than the orig chain
  218. sync_blocks(self.nodes, wait=0.1)
  219. return [int(x, 16) for x in all_hashes]
  220. def run_test(self):
  221. # Setup the p2p connections and start up the network thread.
  222. inv_node = InvNode()
  223. test_node = TestNode()
  224. self.p2p_connections = [inv_node, test_node]
  225. connections = []
  226. connections.append(NodeConn('127.0.0.1', p2p_port(0), self.nodes[0], inv_node))
  227. # Set nServices to 0 for test_node, so no block download will occur outside of
  228. # direct fetching
  229. connections.append(NodeConn('127.0.0.1', p2p_port(0), self.nodes[0], test_node, services=0))
  230. inv_node.add_connection(connections[0])
  231. test_node.add_connection(connections[1])
  232. NetworkThread().start() # Start up network handling in another thread
  233. # Test logic begins here
  234. inv_node.wait_for_verack()
  235. test_node.wait_for_verack()
  236. tip = int(self.nodes[0].getbestblockhash(), 16)
  237. # PART 1
  238. # 1. Mine a block; expect inv announcements each time
  239. self.log.info("Part 1: headers don't start before sendheaders message...")
  240. for i in range(4):
  241. old_tip = tip
  242. tip = self.mine_blocks(1)
  243. assert_equal(inv_node.check_last_announcement(inv=[tip]), True)
  244. assert_equal(test_node.check_last_announcement(inv=[tip]), True)
  245. # Try a few different responses; none should affect next announcement
  246. if i == 0:
  247. # first request the block
  248. test_node.get_data([tip])
  249. test_node.wait_for_block(tip, timeout=5)
  250. elif i == 1:
  251. # next try requesting header and block
  252. test_node.get_headers(locator=[old_tip], hashstop=tip)
  253. test_node.get_data([tip])
  254. test_node.wait_for_block(tip)
  255. test_node.clear_last_announcement() # since we requested headers...
  256. elif i == 2:
  257. # this time announce own block via headers
  258. height = self.nodes[0].getblockcount()
  259. last_time = self.nodes[0].getblock(self.nodes[0].getbestblockhash())['time']
  260. block_time = last_time + 1
  261. new_block = create_block(tip, create_coinbase(height+1), block_time)
  262. new_block.solve()
  263. test_node.send_header_for_blocks([new_block])
  264. test_node.wait_for_getdata([new_block.sha256], timeout=5)
  265. test_node.send_message(msg_block(new_block))
  266. test_node.sync_with_ping() # make sure this block is processed
  267. inv_node.clear_last_announcement()
  268. test_node.clear_last_announcement()
  269. self.log.info("Part 1: success!")
  270. self.log.info("Part 2: announce blocks with headers after sendheaders message...")
  271. # PART 2
  272. # 2. Send a sendheaders message and test that headers announcements
  273. # commence and keep working.
  274. test_node.send_message(msg_sendheaders())
  275. prev_tip = int(self.nodes[0].getbestblockhash(), 16)
  276. test_node.get_headers(locator=[prev_tip], hashstop=0)
  277. test_node.sync_with_ping()
  278. # Now that we've synced headers, headers announcements should work
  279. tip = self.mine_blocks(1)
  280. assert_equal(inv_node.check_last_announcement(inv=[tip]), True)
  281. assert_equal(test_node.check_last_announcement(headers=[tip]), True)
  282. height = self.nodes[0].getblockcount()+1
  283. block_time += 10 # Advance far enough ahead
  284. for i in range(10):
  285. # Mine i blocks, and alternate announcing either via
  286. # inv (of tip) or via headers. After each, new blocks
  287. # mined by the node should successfully be announced
  288. # with block header, even though the blocks are never requested
  289. for j in range(2):
  290. blocks = []
  291. for b in range(i+1):
  292. blocks.append(create_block(tip, create_coinbase(height), block_time))
  293. blocks[-1].solve()
  294. tip = blocks[-1].sha256
  295. block_time += 1
  296. height += 1
  297. if j == 0:
  298. # Announce via inv
  299. test_node.send_block_inv(tip)
  300. test_node.wait_for_getheaders(timeout=5)
  301. # Should have received a getheaders now
  302. test_node.send_header_for_blocks(blocks)
  303. # Test that duplicate inv's won't result in duplicate
  304. # getdata requests, or duplicate headers announcements
  305. [ inv_node.send_block_inv(x.sha256) for x in blocks ]
  306. test_node.wait_for_getdata([x.sha256 for x in blocks], timeout=5)
  307. inv_node.sync_with_ping()
  308. else:
  309. # Announce via headers
  310. test_node.send_header_for_blocks(blocks)
  311. test_node.wait_for_getdata([x.sha256 for x in blocks], timeout=5)
  312. # Test that duplicate headers won't result in duplicate
  313. # getdata requests (the check is further down)
  314. inv_node.send_header_for_blocks(blocks)
  315. inv_node.sync_with_ping()
  316. [ test_node.send_message(msg_block(x)) for x in blocks ]
  317. test_node.sync_with_ping()
  318. inv_node.sync_with_ping()
  319. # This block should not be announced to the inv node (since it also
  320. # broadcast it)
  321. assert_equal(inv_node.last_inv, None)
  322. assert_equal(inv_node.last_headers, None)
  323. tip = self.mine_blocks(1)
  324. assert_equal(inv_node.check_last_announcement(inv=[tip]), True)
  325. assert_equal(test_node.check_last_announcement(headers=[tip]), True)
  326. height += 1
  327. block_time += 1
  328. self.log.info("Part 2: success!")
  329. self.log.info("Part 3: headers announcements can stop after large reorg, and resume after headers/inv from peer...")
  330. # PART 3. Headers announcements can stop after large reorg, and resume after
  331. # getheaders or inv from peer.
  332. for j in range(2):
  333. # First try mining a reorg that can propagate with header announcement
  334. new_block_hashes = self.mine_reorg(length=7)
  335. tip = new_block_hashes[-1]
  336. assert_equal(inv_node.check_last_announcement(inv=[tip]), True)
  337. assert_equal(test_node.check_last_announcement(headers=new_block_hashes), True)
  338. block_time += 8
  339. # Mine a too-large reorg, which should be announced with a single inv
  340. new_block_hashes = self.mine_reorg(length=8)
  341. tip = new_block_hashes[-1]
  342. assert_equal(inv_node.check_last_announcement(inv=[tip]), True)
  343. assert_equal(test_node.check_last_announcement(inv=[tip]), True)
  344. block_time += 9
  345. fork_point = self.nodes[0].getblock("%02x" % new_block_hashes[0])["previousblockhash"]
  346. fork_point = int(fork_point, 16)
  347. # Use getblocks/getdata
  348. test_node.send_getblocks(locator = [fork_point])
  349. assert_equal(test_node.check_last_announcement(inv=new_block_hashes), True)
  350. test_node.get_data(new_block_hashes)
  351. test_node.wait_for_block(new_block_hashes[-1])
  352. for i in range(3):
  353. # Mine another block, still should get only an inv
  354. tip = self.mine_blocks(1)
  355. assert_equal(inv_node.check_last_announcement(inv=[tip]), True)
  356. assert_equal(test_node.check_last_announcement(inv=[tip]), True)
  357. if i == 0:
  358. # Just get the data -- shouldn't cause headers announcements to resume
  359. test_node.get_data([tip])
  360. test_node.wait_for_block(tip)
  361. elif i == 1:
  362. # Send a getheaders message that shouldn't trigger headers announcements
  363. # to resume (best header sent will be too old)
  364. test_node.get_headers(locator=[fork_point], hashstop=new_block_hashes[1])
  365. test_node.get_data([tip])
  366. test_node.wait_for_block(tip)
  367. elif i == 2:
  368. test_node.get_data([tip])
  369. test_node.wait_for_block(tip)
  370. # This time, try sending either a getheaders to trigger resumption
  371. # of headers announcements, or mine a new block and inv it, also
  372. # triggering resumption of headers announcements.
  373. if j == 0:
  374. test_node.get_headers(locator=[tip], hashstop=0)
  375. test_node.sync_with_ping()
  376. else:
  377. test_node.send_block_inv(tip)
  378. test_node.sync_with_ping()
  379. # New blocks should now be announced with header
  380. tip = self.mine_blocks(1)
  381. assert_equal(inv_node.check_last_announcement(inv=[tip]), True)
  382. assert_equal(test_node.check_last_announcement(headers=[tip]), True)
  383. self.log.info("Part 3: success!")
  384. self.log.info("Part 4: Testing direct fetch behavior...")
  385. tip = self.mine_blocks(1)
  386. height = self.nodes[0].getblockcount() + 1
  387. last_time = self.nodes[0].getblock(self.nodes[0].getbestblockhash())['time']
  388. block_time = last_time + 1
  389. # Create 2 blocks. Send the blocks, then send the headers.
  390. blocks = []
  391. for b in range(2):
  392. blocks.append(create_block(tip, create_coinbase(height), block_time))
  393. blocks[-1].solve()
  394. tip = blocks[-1].sha256
  395. block_time += 1
  396. height += 1
  397. inv_node.send_message(msg_block(blocks[-1]))
  398. inv_node.sync_with_ping() # Make sure blocks are processed
  399. test_node.last_getdata = None
  400. test_node.send_header_for_blocks(blocks)
  401. test_node.sync_with_ping()
  402. # should not have received any getdata messages
  403. with mininode_lock:
  404. assert_equal(test_node.last_getdata, None)
  405. # This time, direct fetch should work
  406. blocks = []
  407. for b in range(3):
  408. blocks.append(create_block(tip, create_coinbase(height), block_time))
  409. blocks[-1].solve()
  410. tip = blocks[-1].sha256
  411. block_time += 1
  412. height += 1
  413. test_node.send_header_for_blocks(blocks)
  414. test_node.sync_with_ping()
  415. test_node.wait_for_getdata([x.sha256 for x in blocks], timeout=direct_fetch_response_time)
  416. [ test_node.send_message(msg_block(x)) for x in blocks ]
  417. test_node.sync_with_ping()
  418. # Now announce a header that forks the last two blocks
  419. tip = blocks[0].sha256
  420. height -= 1
  421. blocks = []
  422. # Create extra blocks for later
  423. for b in range(20):
  424. blocks.append(create_block(tip, create_coinbase(height), block_time))
  425. blocks[-1].solve()
  426. tip = blocks[-1].sha256
  427. block_time += 1
  428. height += 1
  429. # Announcing one block on fork should not trigger direct fetch
  430. # (less work than tip)
  431. test_node.last_getdata = None
  432. test_node.send_header_for_blocks(blocks[0:1])
  433. test_node.sync_with_ping()
  434. with mininode_lock:
  435. assert_equal(test_node.last_getdata, None)
  436. # Announcing one more block on fork should trigger direct fetch for
  437. # both blocks (same work as tip)
  438. test_node.send_header_for_blocks(blocks[1:2])
  439. test_node.sync_with_ping()
  440. test_node.wait_for_getdata([x.sha256 for x in blocks[0:2]], timeout=direct_fetch_response_time)
  441. # Announcing 16 more headers should trigger direct fetch for 14 more
  442. # blocks
  443. test_node.send_header_for_blocks(blocks[2:18])
  444. test_node.sync_with_ping()
  445. test_node.wait_for_getdata([x.sha256 for x in blocks[2:16]], timeout=direct_fetch_response_time)
  446. # Announcing 1 more header should not trigger any response
  447. test_node.last_getdata = None
  448. test_node.send_header_for_blocks(blocks[18:19])
  449. test_node.sync_with_ping()
  450. with mininode_lock:
  451. assert_equal(test_node.last_getdata, None)
  452. self.log.info("Part 4: success!")
  453. # Now deliver all those blocks we announced.
  454. [ test_node.send_message(msg_block(x)) for x in blocks ]
  455. self.log.info("Part 5: Testing handling of unconnecting headers")
  456. # First we test that receipt of an unconnecting header doesn't prevent
  457. # chain sync.
  458. for i in range(10):
  459. test_node.last_getdata = None
  460. blocks = []
  461. # Create two more blocks.
  462. for j in range(2):
  463. blocks.append(create_block(tip, create_coinbase(height), block_time))
  464. blocks[-1].solve()
  465. tip = blocks[-1].sha256
  466. block_time += 1
  467. height += 1
  468. # Send the header of the second block -> this won't connect.
  469. with mininode_lock:
  470. test_node.last_getheaders = None
  471. test_node.send_header_for_blocks([blocks[1]])
  472. test_node.wait_for_getheaders(timeout=1)
  473. test_node.send_header_for_blocks(blocks)
  474. test_node.wait_for_getdata([x.sha256 for x in blocks])
  475. [ test_node.send_message(msg_block(x)) for x in blocks ]
  476. test_node.sync_with_ping()
  477. assert_equal(int(self.nodes[0].getbestblockhash(), 16), blocks[1].sha256)
  478. blocks = []
  479. # Now we test that if we repeatedly don't send connecting headers, we
  480. # don't go into an infinite loop trying to get them to connect.
  481. MAX_UNCONNECTING_HEADERS = 10
  482. for j in range(MAX_UNCONNECTING_HEADERS+1):
  483. blocks.append(create_block(tip, create_coinbase(height), block_time))
  484. blocks[-1].solve()
  485. tip = blocks[-1].sha256
  486. block_time += 1
  487. height += 1
  488. for i in range(1, MAX_UNCONNECTING_HEADERS):
  489. # Send a header that doesn't connect, check that we get a getheaders.
  490. with mininode_lock:
  491. test_node.last_getheaders = None
  492. test_node.send_header_for_blocks([blocks[i]])
  493. test_node.wait_for_getheaders(timeout=1)
  494. # Next header will connect, should re-set our count:
  495. test_node.send_header_for_blocks([blocks[0]])
  496. # Remove the first two entries (blocks[1] would connect):
  497. blocks = blocks[2:]
  498. # Now try to see how many unconnecting headers we can send
  499. # before we get disconnected. Should be 5*MAX_UNCONNECTING_HEADERS
  500. for i in range(5*MAX_UNCONNECTING_HEADERS - 1):
  501. # Send a header that doesn't connect, check that we get a getheaders.
  502. with mininode_lock:
  503. test_node.last_getheaders = None
  504. test_node.send_header_for_blocks([blocks[i%len(blocks)]])
  505. test_node.wait_for_getheaders(timeout=1)
  506. # Eventually this stops working.
  507. with mininode_lock:
  508. self.last_getheaders = None
  509. test_node.send_header_for_blocks([blocks[-1]])
  510. # Should get disconnected
  511. test_node.wait_for_disconnect()
  512. with mininode_lock:
  513. self.last_getheaders = True
  514. self.log.info("Part 5: success!")
  515. # Finally, check that the inv node never received a getdata request,
  516. # throughout the test
  517. assert_equal(inv_node.last_getdata, None)
  518. if __name__ == '__main__':
  519. SendHeadersTest().main()