Changeset View
Changeset View
Standalone View
Standalone View
test/functional/interface_zmq.py
#!/usr/bin/env python3 | #!/usr/bin/env python3 | ||||
# Copyright (c) 2015-2019 The Bitcoin Core developers | # Copyright (c) 2015-2019 The Bitcoin Core developers | ||||
# Distributed under the MIT software license, see the accompanying | # Distributed under the MIT software license, see the accompanying | ||||
# file COPYING or http://www.opensource.org/licenses/mit-license.php. | # file COPYING or http://www.opensource.org/licenses/mit-license.php. | ||||
"""Test the ZMQ notification interface.""" | """Test the ZMQ notification interface.""" | ||||
import struct | import struct | ||||
from io import BytesIO | from io import BytesIO | ||||
from test_framework.address import ADDRESS_BCHREG_UNSPENDABLE | |||||
from test_framework.test_framework import BitcoinTestFramework | from test_framework.test_framework import BitcoinTestFramework | ||||
from test_framework.messages import CTransaction | from test_framework.messages import CTransaction | ||||
from test_framework.util import ( | from test_framework.util import ( | ||||
assert_equal, | assert_equal, | ||||
hash256, | hash256, | ||||
) | ) | ||||
ADDRESS = "tcp://127.0.0.1:28332" | ADDRESS = "tcp://127.0.0.1:28332" | ||||
Show All 20 Lines | |||||
class ZMQTest (BitcoinTestFramework): | class ZMQTest (BitcoinTestFramework): | ||||
def set_test_params(self): | def set_test_params(self): | ||||
self.num_nodes = 2 | self.num_nodes = 2 | ||||
def skip_test_if_missing_module(self): | def skip_test_if_missing_module(self): | ||||
self.skip_if_no_py3_zmq() | self.skip_if_no_py3_zmq() | ||||
self.skip_if_no_bitcoind_zmq() | self.skip_if_no_bitcoind_zmq() | ||||
self.skip_if_no_wallet() | |||||
def setup_nodes(self): | def setup_nodes(self): | ||||
import zmq | import zmq | ||||
# Initialize ZMQ context and socket. | # Initialize ZMQ context and socket. | ||||
# All messages are received in the same socket which means that this | # All messages are received in the same socket which means that this | ||||
# test fails if the publishing order changes. | # test fails if the publishing order changes. | ||||
# Note that the publishing order is not defined in the documentation and | # Note that the publishing order is not defined in the documentation and | ||||
Show All 25 Lines | def run_test(self): | ||||
# Destroy the ZMQ context. | # Destroy the ZMQ context. | ||||
self.log.debug("Destroying ZMQ context") | self.log.debug("Destroying ZMQ context") | ||||
self.zmq_context.destroy(linger=None) | self.zmq_context.destroy(linger=None) | ||||
def _zmq_test(self): | def _zmq_test(self): | ||||
num_blocks = 5 | num_blocks = 5 | ||||
self.log.info( | self.log.info( | ||||
"Generate {0} blocks (and {0} coinbase txes)".format(num_blocks)) | "Generate {0} blocks (and {0} coinbase txes)".format(num_blocks)) | ||||
genhashes = self.nodes[0].generate(num_blocks) | genhashes = self.nodes[0].generatetoaddress( | ||||
num_blocks, ADDRESS_BCHREG_UNSPENDABLE) | |||||
self.sync_all() | self.sync_all() | ||||
for x in range(num_blocks): | for x in range(num_blocks): | ||||
# Should receive the coinbase txid. | # Should receive the coinbase txid. | ||||
txid = self.hashtx.receive() | txid = self.hashtx.receive() | ||||
# Should receive the coinbase raw transaction. | # Should receive the coinbase raw transaction. | ||||
hex = self.rawtx.receive() | hex = self.rawtx.receive() | ||||
tx = CTransaction() | tx = CTransaction() | ||||
tx.deserialize(BytesIO(hex)) | tx.deserialize(BytesIO(hex)) | ||||
tx.calc_sha256() | tx.calc_sha256() | ||||
assert_equal(tx.hash, txid.hex()) | assert_equal(tx.hash, txid.hex()) | ||||
# Should receive the generated block hash. | # Should receive the generated block hash. | ||||
hash = self.hashblock.receive().hex() | hash = self.hashblock.receive().hex() | ||||
assert_equal(genhashes[x], hash) | assert_equal(genhashes[x], hash) | ||||
# The block should only have the coinbase txid. | # The block should only have the coinbase txid. | ||||
assert_equal([txid.hex()], self.nodes[1].getblock(hash)["tx"]) | assert_equal([txid.hex()], self.nodes[1].getblock(hash)["tx"]) | ||||
# Should receive the generated raw block. | # Should receive the generated raw block. | ||||
block = self.rawblock.receive() | block = self.rawblock.receive() | ||||
assert_equal(genhashes[x], hash256(block[:80]).hex()) | assert_equal(genhashes[x], hash256(block[:80]).hex()) | ||||
if self.is_wallet_compiled(): | |||||
self.log.info("Wait for tx from second node") | self.log.info("Wait for tx from second node") | ||||
payment_txid = self.nodes[1].sendtoaddress( | payment_txid = self.nodes[1].sendtoaddress( | ||||
self.nodes[0].getnewaddress(), 1.0) | self.nodes[0].getnewaddress(), 1.0) | ||||
self.sync_all() | self.sync_all() | ||||
# Should receive the broadcasted txid. | # Should receive the broadcasted txid. | ||||
txid = self.hashtx.receive() | txid = self.hashtx.receive() | ||||
assert_equal(payment_txid, txid.hex()) | assert_equal(payment_txid, txid.hex()) | ||||
# Should receive the broadcasted raw transaction. | # Should receive the broadcasted raw transaction. | ||||
hex = self.rawtx.receive() | hex = self.rawtx.receive() | ||||
assert_equal(payment_txid, hash256(hex).hex()) | assert_equal(payment_txid, hash256(hex).hex()) | ||||
self.log.info("Test the getzmqnotifications RPC") | self.log.info("Test the getzmqnotifications RPC") | ||||
assert_equal(self.nodes[0].getzmqnotifications(), [ | assert_equal(self.nodes[0].getzmqnotifications(), [ | ||||
{"type": "pubhashblock", "address": ADDRESS}, | {"type": "pubhashblock", "address": ADDRESS}, | ||||
{"type": "pubhashtx", "address": ADDRESS}, | {"type": "pubhashtx", "address": ADDRESS}, | ||||
{"type": "pubrawblock", "address": ADDRESS}, | {"type": "pubrawblock", "address": ADDRESS}, | ||||
{"type": "pubrawtx", "address": ADDRESS}, | {"type": "pubrawtx", "address": ADDRESS}, | ||||
]) | ]) | ||||
assert_equal(self.nodes[1].getzmqnotifications(), []) | assert_equal(self.nodes[1].getzmqnotifications(), []) | ||||
if __name__ == '__main__': | if __name__ == '__main__': | ||||
ZMQTest().main() | ZMQTest().main() |