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.

p2p-mempool.py 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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 p2p mempool message.
  6. Test that nodes are disconnected if they send mempool messages when bloom
  7. filters are not enabled.
  8. """
  9. from test_framework.mininode import *
  10. from test_framework.test_framework import BitcoinTestFramework
  11. from test_framework.util import *
  12. class TestNode(NodeConnCB):
  13. def __init__(self):
  14. NodeConnCB.__init__(self)
  15. self.connection = None
  16. self.ping_counter = 1
  17. self.last_pong = msg_pong()
  18. self.block_receive_map = {}
  19. def add_connection(self, conn):
  20. self.connection = conn
  21. self.peer_disconnected = False
  22. def on_inv(self, conn, message):
  23. pass
  24. # Track the last getdata message we receive (used in the test)
  25. def on_getdata(self, conn, message):
  26. self.last_getdata = message
  27. def on_block(self, conn, message):
  28. message.block.calc_sha256()
  29. try:
  30. self.block_receive_map[message.block.sha256] += 1
  31. except KeyError as e:
  32. self.block_receive_map[message.block.sha256] = 1
  33. # Spin until verack message is received from the node.
  34. # We use this to signal that our test can begin. This
  35. # is called from the testing thread, so it needs to acquire
  36. # the global lock.
  37. def wait_for_verack(self):
  38. def veracked():
  39. return self.verack_received
  40. return wait_until(veracked, timeout=10)
  41. def wait_for_disconnect(self):
  42. def disconnected():
  43. return self.peer_disconnected
  44. return wait_until(disconnected, timeout=10)
  45. # Wrapper for the NodeConn's send_message function
  46. def send_message(self, message):
  47. self.connection.send_message(message)
  48. def on_pong(self, conn, message):
  49. self.last_pong = message
  50. def on_close(self, conn):
  51. self.peer_disconnected = True
  52. # Sync up with the node after delivery of a block
  53. def sync_with_ping(self, timeout=30):
  54. def received_pong():
  55. return (self.last_pong.nonce == self.ping_counter)
  56. self.connection.send_message(msg_ping(nonce=self.ping_counter))
  57. success = wait_until(received_pong, timeout=timeout)
  58. self.ping_counter += 1
  59. return success
  60. def send_mempool(self):
  61. self.lastInv = []
  62. self.send_message(msg_mempool())
  63. class P2PMempoolTests(BitcoinTestFramework):
  64. def __init__(self):
  65. super().__init__()
  66. self.setup_clean_chain = True
  67. self.num_nodes = 2
  68. def setup_network(self):
  69. # Start a node with maxuploadtarget of 200 MB (/24h)
  70. self.nodes = []
  71. self.nodes.append(start_node(0, self.options.tmpdir, ["-peerbloomfilters=0"]))
  72. def run_test(self):
  73. #connect a mininode
  74. aTestNode = TestNode()
  75. node = NodeConn('127.0.0.1', p2p_port(0), self.nodes[0], aTestNode)
  76. aTestNode.add_connection(node)
  77. NetworkThread().start()
  78. aTestNode.wait_for_verack()
  79. #request mempool
  80. aTestNode.send_mempool()
  81. aTestNode.wait_for_disconnect()
  82. #mininode must be disconnected at this point
  83. assert_equal(len(self.nodes[0].getpeerinfo()), 0)
  84. if __name__ == '__main__':
  85. P2PMempoolTests().main()