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.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, hash256 | ||||
from test_framework.util import ( | from test_framework.util import ( | ||||
assert_equal, | assert_equal, | ||||
connect_nodes, | connect_nodes, | ||||
hash256, | |||||
) | ) | ||||
from time import sleep | from time import sleep | ||||
def hash256_reversed(byte_str): | |||||
return hash256(byte_str)[::-1] | |||||
class ZMQSubscriber: | class ZMQSubscriber: | ||||
def __init__(self, socket, topic): | def __init__(self, socket, topic): | ||||
self.sequence = 0 | self.sequence = 0 | ||||
self.socket = socket | self.socket = socket | ||||
self.topic = topic | self.topic = topic | ||||
import zmq | import zmq | ||||
self.socket.setsockopt(zmq.SUBSCRIBE, self.topic) | self.socket.setsockopt(zmq.SUBSCRIBE, self.topic) | ||||
▲ Show 20 Lines • Show All 72 Lines • ▼ Show 20 Lines | def test_basic(self): | ||||
# Should receive the generated block hash. | # Should receive the generated block hash. | ||||
hash = hashblock.receive().hex() | hash = 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 = rawblock.receive() | block = rawblock.receive() | ||||
assert_equal(genhashes[x], hash256(block[:80]).hex()) | assert_equal(genhashes[x], hash256_reversed(block[:80]).hex()) | ||||
if self.is_wallet_compiled(): | 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 = hashtx.receive() | txid = 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 = rawtx.receive() | hex = rawtx.receive() | ||||
assert_equal(payment_txid, hash256(hex).hex()) | assert_equal(payment_txid, hash256_reversed(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, "hwm": 1000}, | {"type": "pubhashblock", "address": address, "hwm": 1000}, | ||||
{"type": "pubhashtx", "address": address, "hwm": 1000}, | {"type": "pubhashtx", "address": address, "hwm": 1000}, | ||||
{"type": "pubrawblock", "address": address, "hwm": 1000}, | {"type": "pubrawblock", "address": address, "hwm": 1000}, | ||||
{"type": "pubrawtx", "address": address, "hwm": 1000}, | {"type": "pubrawtx", "address": address, "hwm": 1000}, | ||||
]) | ]) | ||||
Show All 37 Lines |