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.
127 lines
4.7 KiB
127 lines
4.7 KiB
9 years ago
|
#!/usr/bin/env python3
|
||
7 years ago
|
# Copyright (c) 2015-2017 The Bitcoin Core developers
|
||
10 years ago
|
# Distributed under the MIT software license, see the accompanying
|
||
|
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
||
7 years ago
|
"""Test the ZMQ notification interface."""
|
||
8 years ago
|
import configparser
|
||
|
import os
|
||
|
import struct
|
||
10 years ago
|
|
||
8 years ago
|
from test_framework.test_framework import BitcoinTestFramework, SkipTest
|
||
7 years ago
|
from test_framework.mininode import CTransaction
|
||
8 years ago
|
from test_framework.util import (assert_equal,
|
||
|
bytes_to_hex_str,
|
||
8 years ago
|
hash256,
|
||
|
)
|
||
7 years ago
|
from io import BytesIO
|
||
10 years ago
|
|
||
7 years ago
|
class ZMQSubscriber:
|
||
|
def __init__(self, socket, topic):
|
||
|
self.sequence = 0
|
||
|
self.socket = socket
|
||
|
self.topic = topic
|
||
|
|
||
|
import zmq
|
||
|
self.socket.setsockopt(zmq.SUBSCRIBE, self.topic)
|
||
|
|
||
|
def receive(self):
|
||
|
topic, body, seq = self.socket.recv_multipart()
|
||
|
# Topic should match the subscriber topic.
|
||
|
assert_equal(topic, self.topic)
|
||
|
# Sequence should be incremental.
|
||
|
assert_equal(struct.unpack('<I', seq)[-1], self.sequence)
|
||
|
self.sequence += 1
|
||
|
return body
|
||
|
|
||
|
|
||
10 years ago
|
class ZMQTest (BitcoinTestFramework):
|
||
8 years ago
|
def set_test_params(self):
|
||
8 years ago
|
self.num_nodes = 2
|
||
10 years ago
|
|
||
|
def setup_nodes(self):
|
||
8 years ago
|
# Try to import python3-zmq. Skip this test if the import fails.
|
||
|
try:
|
||
|
import zmq
|
||
|
except ImportError:
|
||
8 years ago
|
raise SkipTest("python3-zmq module not available.")
|
||
8 years ago
|
|
||
7 years ago
|
# Check that bitcoin has been built with ZMQ enabled.
|
||
8 years ago
|
config = configparser.ConfigParser()
|
||
8 years ago
|
if not self.options.configfile:
|
||
7 years ago
|
self.options.configfile = os.path.abspath(os.path.join(os.path.dirname(__file__), "../config.ini"))
|
||
8 years ago
|
config.read_file(open(self.options.configfile))
|
||
8 years ago
|
|
||
|
if not config["components"].getboolean("ENABLE_ZMQ"):
|
||
8 years ago
|
raise SkipTest("bitcoind has not been built with zmq enabled.")
|
||
8 years ago
|
|
||
7 years ago
|
# Initialize ZMQ context and socket.
|
||
|
# All messages are received in the same socket which means
|
||
|
# that this test fails if the publishing order changes.
|
||
|
# Note that the publishing order is not defined in the documentation and
|
||
|
# is subject to change.
|
||
|
address = "tcp://127.0.0.1:28332"
|
||
|
self.zmq_context = zmq.Context()
|
||
|
socket = self.zmq_context.socket(zmq.SUB)
|
||
|
socket.set(zmq.RCVTIMEO, 60000)
|
||
|
socket.connect(address)
|
||
|
|
||
|
# Subscribe to all available topics.
|
||
|
self.hashblock = ZMQSubscriber(socket, b"hashblock")
|
||
|
self.hashtx = ZMQSubscriber(socket, b"hashtx")
|
||
|
self.rawblock = ZMQSubscriber(socket, b"rawblock")
|
||
|
self.rawtx = ZMQSubscriber(socket, b"rawtx")
|
||
|
|
||
|
self.extra_args = [["-zmqpub%s=%s" % (sub.topic.decode(), address) for sub in [self.hashblock, self.hashtx, self.rawblock, self.rawtx]], []]
|
||
7 years ago
|
self.add_nodes(self.num_nodes, self.extra_args)
|
||
8 years ago
|
self.start_nodes()
|
||
10 years ago
|
|
||
|
def run_test(self):
|
||
8 years ago
|
try:
|
||
|
self._zmq_test()
|
||
|
finally:
|
||
7 years ago
|
# Destroy the ZMQ context.
|
||
|
self.log.debug("Destroying ZMQ context")
|
||
|
self.zmq_context.destroy(linger=None)
|
||
8 years ago
|
|
||
|
def _zmq_test(self):
|
||
7 years ago
|
num_blocks = 5
|
||
|
self.log.info("Generate %(n)d blocks (and %(n)d coinbase txes)" % {"n": num_blocks})
|
||
|
genhashes = self.nodes[0].generate(num_blocks)
|
||
10 years ago
|
self.sync_all()
|
||
|
|
||
7 years ago
|
for x in range(num_blocks):
|
||
|
# Should receive the coinbase txid.
|
||
|
txid = self.hashtx.receive()
|
||
|
|
||
|
# Should receive the coinbase raw transaction.
|
||
|
hex = self.rawtx.receive()
|
||
7 years ago
|
tx = CTransaction()
|
||
|
tx.deserialize(BytesIO(hex))
|
||
|
tx.calc_sha256()
|
||
|
assert_equal(tx.hash, bytes_to_hex_str(txid))
|
||
10 years ago
|
|
||
7 years ago
|
# Should receive the generated block hash.
|
||
|
hash = bytes_to_hex_str(self.hashblock.receive())
|
||
|
assert_equal(genhashes[x], hash)
|
||
|
# The block should only have the coinbase txid.
|
||
|
assert_equal([bytes_to_hex_str(txid)], self.nodes[1].getblock(hash)["tx"])
|
||
|
|
||
|
# Should receive the generated raw block.
|
||
|
block = self.rawblock.receive()
|
||
|
assert_equal(genhashes[x], bytes_to_hex_str(hash256(block[:80])))
|
||
10 years ago
|
|
||
8 years ago
|
self.log.info("Wait for tx from second node")
|
||
7 years ago
|
payment_txid = self.nodes[1].sendtoaddress(self.nodes[0].getnewaddress(), 1.0)
|
||
10 years ago
|
self.sync_all()
|
||
|
|
||
7 years ago
|
# Should receive the broadcasted txid.
|
||
|
txid = self.hashtx.receive()
|
||
|
assert_equal(payment_txid, bytes_to_hex_str(txid))
|
||
|
|
||
|
# Should receive the broadcasted raw transaction.
|
||
|
hex = self.rawtx.receive()
|
||
|
assert_equal(payment_txid, bytes_to_hex_str(hash256(hex)))
|
||
10 years ago
|
|
||
|
if __name__ == '__main__':
|
||
8 years ago
|
ZMQTest().main()
|