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.

zmq_test.py 3.2KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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 the ZMQ API."""
  6. from test_framework.test_framework import BitcoinTestFramework
  7. from test_framework.util import *
  8. import zmq
  9. import struct
  10. class ZMQTest (BitcoinTestFramework):
  11. def __init__(self):
  12. super().__init__()
  13. self.num_nodes = 4
  14. port = 28332
  15. def setup_nodes(self):
  16. self.zmqContext = zmq.Context()
  17. self.zmqSubSocket = self.zmqContext.socket(zmq.SUB)
  18. self.zmqSubSocket.setsockopt(zmq.SUBSCRIBE, b"hashblock")
  19. self.zmqSubSocket.setsockopt(zmq.SUBSCRIBE, b"hashtx")
  20. self.zmqSubSocket.connect("tcp://127.0.0.1:%i" % self.port)
  21. return start_nodes(self.num_nodes, self.options.tmpdir, extra_args=[
  22. ['-zmqpubhashtx=tcp://127.0.0.1:'+str(self.port), '-zmqpubhashblock=tcp://127.0.0.1:'+str(self.port)],
  23. [],
  24. [],
  25. []
  26. ])
  27. def run_test(self):
  28. self.sync_all()
  29. genhashes = self.nodes[0].generate(1)
  30. self.sync_all()
  31. self.log.info("listen...")
  32. msg = self.zmqSubSocket.recv_multipart()
  33. topic = msg[0]
  34. assert_equal(topic, b"hashtx")
  35. body = msg[1]
  36. nseq = msg[2]
  37. msgSequence = struct.unpack('<I', msg[-1])[-1]
  38. assert_equal(msgSequence, 0) #must be sequence 0 on hashtx
  39. msg = self.zmqSubSocket.recv_multipart()
  40. topic = msg[0]
  41. body = msg[1]
  42. msgSequence = struct.unpack('<I', msg[-1])[-1]
  43. assert_equal(msgSequence, 0) #must be sequence 0 on hashblock
  44. blkhash = bytes_to_hex_str(body)
  45. assert_equal(genhashes[0], blkhash) #blockhash from generate must be equal to the hash received over zmq
  46. n = 10
  47. genhashes = self.nodes[1].generate(n)
  48. self.sync_all()
  49. zmqHashes = []
  50. blockcount = 0
  51. for x in range(0,n*2):
  52. msg = self.zmqSubSocket.recv_multipart()
  53. topic = msg[0]
  54. body = msg[1]
  55. if topic == b"hashblock":
  56. zmqHashes.append(bytes_to_hex_str(body))
  57. msgSequence = struct.unpack('<I', msg[-1])[-1]
  58. assert_equal(msgSequence, blockcount+1)
  59. blockcount += 1
  60. for x in range(0,n):
  61. assert_equal(genhashes[x], zmqHashes[x]) #blockhash from generate must be equal to the hash received over zmq
  62. #test tx from a second node
  63. hashRPC = self.nodes[1].sendtoaddress(self.nodes[0].getnewaddress(), 1.0)
  64. self.sync_all()
  65. # now we should receive a zmq msg because the tx was broadcast
  66. msg = self.zmqSubSocket.recv_multipart()
  67. topic = msg[0]
  68. body = msg[1]
  69. hashZMQ = ""
  70. if topic == b"hashtx":
  71. hashZMQ = bytes_to_hex_str(body)
  72. msgSequence = struct.unpack('<I', msg[-1])[-1]
  73. assert_equal(msgSequence, blockcount+1)
  74. assert_equal(hashRPC, hashZMQ) #blockhash from generate must be equal to the hash received over zmq
  75. if __name__ == '__main__':
  76. ZMQTest ().main ()