Dash Core  0.12.2.1
P2P Digital Currency
zmq_test.py
Go to the documentation of this file.
1 #!/usr/bin/env python2
2 # Copyright (c) 2015 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 
6 #
7 # Test ZMQ interface
8 #
9 
10 from test_framework.test_framework import BitcoinTestFramework
11 from test_framework.util import *
12 import zmq
13 import binascii
14 
15 try:
16  import http.client as httplib
17 except ImportError:
18  import httplib
19 try:
20  import urllib.parse as urlparse
21 except ImportError:
22  import urlparse
23 
25 
26  port = 28332
27 
28  def setup_nodes(self):
29  self.zmqContext = zmq.Context()
30  self.zmqSubSocket = self.zmqContext.socket(zmq.SUB)
31  self.zmqSubSocket.setsockopt(zmq.SUBSCRIBE, b"hashblock")
32  self.zmqSubSocket.setsockopt(zmq.SUBSCRIBE, b"hashtx")
33  self.zmqSubSocket.connect("tcp://127.0.0.1:%i" % self.port)
34  return start_nodes(4, self.options.tmpdir, extra_args=[
35  ['-zmqpubhashtx=tcp://127.0.0.1:'+str(self.port), '-zmqpubhashblock=tcp://127.0.0.1:'+str(self.port)],
36  [],
37  [],
38  []
39  ])
40 
41  def run_test(self):
42  self.sync_all()
43 
44  genhashes = self.nodes[0].generate(1)
45  self.sync_all()
46 
47  print "listen..."
48  msg = self.zmqSubSocket.recv_multipart()
49  topic = msg[0]
50  body = msg[1]
51 
52  msg = self.zmqSubSocket.recv_multipart()
53  topic = msg[0]
54  body = msg[1]
55  blkhash = bytes_to_hex_str(body)
56 
57  assert_equal(genhashes[0], blkhash) #blockhash from generate must be equal to the hash received over zmq
58 
59  n = 10
60  genhashes = self.nodes[1].generate(n)
61  self.sync_all()
62 
63  zmqHashes = []
64  for x in range(0,n*2):
65  msg = self.zmqSubSocket.recv_multipart()
66  topic = msg[0]
67  body = msg[1]
68  if topic == b"hashblock":
69  zmqHashes.append(bytes_to_hex_str(body))
70 
71  for x in range(0,n):
72  assert_equal(genhashes[x], zmqHashes[x]) #blockhash from generate must be equal to the hash received over zmq
73 
74  #test tx from a second node
75  hashRPC = self.nodes[1].sendtoaddress(self.nodes[0].getnewaddress(), 1.0)
76  self.sync_all()
77 
78  # now we should receive a zmq msg because the tx was broadcast
79  msg = self.zmqSubSocket.recv_multipart()
80  topic = msg[0]
81  body = msg[1]
82  hashZMQ = ""
83  if topic == b"hashtx":
84  hashZMQ = bytes_to_hex_str(body)
85 
86  assert_equal(hashRPC, hashZMQ) #blockhash from generate must be equal to the hash received over zmq
87 
88 
89 if __name__ == '__main__':
90  ZMQTest ().main ()
def run_test(self)
Definition: zmq_test.py:41
def start_nodes(num_nodes, dirname, extra_args=None, rpchost=None, binary=None)
Definition: util.py:305
UniValue sendtoaddress(const UniValue &params, bool fHelp)
Definition: rpcwallet.cpp:409
UniValue getnewaddress(const UniValue &params, bool fHelp)
Definition: rpcwallet.cpp:113
def setup_nodes(self)
Definition: zmq_test.py:28
UniValue generate(const UniValue &params, bool fHelp)
Definition: mining.cpp:122
def assert_equal(thing1, thing2)
Definition: util.py:461
def bytes_to_hex_str(byte_str)
Definition: util.py:108