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.
94 lines
3.1 KiB
94 lines
3.1 KiB
![]()
9 years ago
|
#!/usr/bin/env python3
|
||
|
# Copyright (c) 2015-2016 The Bitcoin Core developers
|
||
![]()
9 years ago
|
# Distributed under the MIT software license, see the accompanying
|
||
|
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
||
![]()
8 years ago
|
"""Test the ZMQ API."""
|
||
![]()
9 years ago
|
|
||
|
from test_framework.test_framework import BitcoinTestFramework
|
||
|
from test_framework.util import *
|
||
|
import zmq
|
||
![]()
9 years ago
|
import struct
|
||
![]()
9 years ago
|
|
||
|
class ZMQTest (BitcoinTestFramework):
|
||
|
|
||
![]()
8 years ago
|
def __init__(self):
|
||
|
super().__init__()
|
||
|
self.num_nodes = 4
|
||
|
|
||
![]()
9 years ago
|
port = 28332
|
||
|
|
||
|
def setup_nodes(self):
|
||
|
self.zmqContext = zmq.Context()
|
||
|
self.zmqSubSocket = self.zmqContext.socket(zmq.SUB)
|
||
![]()
9 years ago
|
self.zmqSubSocket.setsockopt(zmq.SUBSCRIBE, b"hashblock")
|
||
|
self.zmqSubSocket.setsockopt(zmq.SUBSCRIBE, b"hashtx")
|
||
![]()
9 years ago
|
self.zmqSubSocket.connect("tcp://127.0.0.1:%i" % self.port)
|
||
![]()
8 years ago
|
return start_nodes(self.num_nodes, self.options.tmpdir, extra_args=[
|
||
![]()
9 years ago
|
['-zmqpubhashtx=tcp://127.0.0.1:'+str(self.port), '-zmqpubhashblock=tcp://127.0.0.1:'+str(self.port)],
|
||
|
[],
|
||
|
[],
|
||
|
[]
|
||
|
])
|
||
|
|
||
|
def run_test(self):
|
||
|
self.sync_all()
|
||
|
|
||
![]()
9 years ago
|
genhashes = self.nodes[0].generate(1)
|
||
![]()
9 years ago
|
self.sync_all()
|
||
|
|
||
![]()
8 years ago
|
self.log.info("listen...")
|
||
![]()
9 years ago
|
msg = self.zmqSubSocket.recv_multipart()
|
||
![]()
9 years ago
|
topic = msg[0]
|
||
![]()
9 years ago
|
assert_equal(topic, b"hashtx")
|
||
![]()
9 years ago
|
body = msg[1]
|
||
![]()
9 years ago
|
msgSequence = struct.unpack('<I', msg[-1])[-1]
|
||
|
assert_equal(msgSequence, 0) #must be sequence 0 on hashtx
|
||
![]()
9 years ago
|
|
||
|
msg = self.zmqSubSocket.recv_multipart()
|
||
![]()
9 years ago
|
topic = msg[0]
|
||
![]()
9 years ago
|
body = msg[1]
|
||
![]()
9 years ago
|
msgSequence = struct.unpack('<I', msg[-1])[-1]
|
||
|
assert_equal(msgSequence, 0) #must be sequence 0 on hashblock
|
||
![]()
9 years ago
|
blkhash = bytes_to_hex_str(body)
|
||
![]()
9 years ago
|
|
||
|
assert_equal(genhashes[0], blkhash) #blockhash from generate must be equal to the hash received over zmq
|
||
|
|
||
|
n = 10
|
||
![]()
9 years ago
|
genhashes = self.nodes[1].generate(n)
|
||
![]()
9 years ago
|
self.sync_all()
|
||
|
|
||
|
zmqHashes = []
|
||
![]()
9 years ago
|
blockcount = 0
|
||
![]()
9 years ago
|
for x in range(0,n*2):
|
||
|
msg = self.zmqSubSocket.recv_multipart()
|
||
![]()
9 years ago
|
topic = msg[0]
|
||
![]()
9 years ago
|
body = msg[1]
|
||
![]()
9 years ago
|
if topic == b"hashblock":
|
||
|
zmqHashes.append(bytes_to_hex_str(body))
|
||
![]()
9 years ago
|
msgSequence = struct.unpack('<I', msg[-1])[-1]
|
||
|
assert_equal(msgSequence, blockcount+1)
|
||
|
blockcount += 1
|
||
![]()
9 years ago
|
|
||
|
for x in range(0,n):
|
||
|
assert_equal(genhashes[x], zmqHashes[x]) #blockhash from generate must be equal to the hash received over zmq
|
||
|
|
||
|
#test tx from a second node
|
||
|
hashRPC = self.nodes[1].sendtoaddress(self.nodes[0].getnewaddress(), 1.0)
|
||
|
self.sync_all()
|
||
|
|
||
![]()
9 years ago
|
# now we should receive a zmq msg because the tx was broadcast
|
||
![]()
9 years ago
|
msg = self.zmqSubSocket.recv_multipart()
|
||
![]()
9 years ago
|
topic = msg[0]
|
||
![]()
9 years ago
|
body = msg[1]
|
||
|
hashZMQ = ""
|
||
![]()
9 years ago
|
if topic == b"hashtx":
|
||
|
hashZMQ = bytes_to_hex_str(body)
|
||
![]()
9 years ago
|
msgSequence = struct.unpack('<I', msg[-1])[-1]
|
||
|
assert_equal(msgSequence, blockcount+1)
|
||
![]()
9 years ago
|
|
||
|
assert_equal(hashRPC, hashZMQ) #blockhash from generate must be equal to the hash received over zmq
|
||
|
|
||
|
|
||
|
if __name__ == '__main__':
|
||
|
ZMQTest ().main ()
|