[qa] py2: Unfiddle strings into bytes explicitly in ZMQ RPC test
Extracted from bitcoin/bitcoin#7853 commit faa41ee204124da19dcf1e5b8a3aef1e216bf5e6
This commit is contained in:
@@ -1,6 +1,8 @@
|
|||||||
# Copyright (c) 2014 The Bitcoin Core developers
|
# Copyright (c) 2014 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.
|
||||||
|
|
||||||
|
|
||||||
#
|
#
|
||||||
# Helpful routines for regression testing
|
# Helpful routines for regression testing
|
||||||
#
|
#
|
||||||
@@ -9,6 +11,8 @@
|
|||||||
import os
|
import os
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
|
from binascii import hexlify, unhexlify
|
||||||
|
from base64 import b64encode
|
||||||
from decimal import Decimal, ROUND_DOWN
|
from decimal import Decimal, ROUND_DOWN
|
||||||
import json
|
import json
|
||||||
import random
|
import random
|
||||||
@@ -32,6 +36,15 @@ def check_json_precision():
|
|||||||
if satoshis != 2000000000000003:
|
if satoshis != 2000000000000003:
|
||||||
raise RuntimeError("JSON encode/decode loses precision")
|
raise RuntimeError("JSON encode/decode loses precision")
|
||||||
|
|
||||||
|
def bytes_to_hex_str(byte_str):
|
||||||
|
return hexlify(byte_str).decode('ascii')
|
||||||
|
|
||||||
|
def hex_str_to_bytes(hex_str):
|
||||||
|
return unhexlify(hex_str.encode('ascii'))
|
||||||
|
|
||||||
|
def str_to_b64str(string):
|
||||||
|
return b64encode(string.encode('utf-8')).decode('ascii')
|
||||||
|
|
||||||
def sync_blocks(rpc_connections, wait=1):
|
def sync_blocks(rpc_connections, wait=1):
|
||||||
"""
|
"""
|
||||||
Wait until everybody has the same block count
|
Wait until everybody has the same block count
|
||||||
|
|||||||
@@ -28,8 +28,8 @@ class ZMQTest (BitcoinTestFramework):
|
|||||||
def setup_nodes(self):
|
def setup_nodes(self):
|
||||||
self.zmqContext = zmq.Context()
|
self.zmqContext = zmq.Context()
|
||||||
self.zmqSubSocket = self.zmqContext.socket(zmq.SUB)
|
self.zmqSubSocket = self.zmqContext.socket(zmq.SUB)
|
||||||
self.zmqSubSocket.setsockopt(zmq.SUBSCRIBE, "hashblock")
|
self.zmqSubSocket.setsockopt(zmq.SUBSCRIBE, b"hashblock")
|
||||||
self.zmqSubSocket.setsockopt(zmq.SUBSCRIBE, "hashtx")
|
self.zmqSubSocket.setsockopt(zmq.SUBSCRIBE, b"hashtx")
|
||||||
self.zmqSubSocket.connect("tcp://127.0.0.1:%i" % self.port)
|
self.zmqSubSocket.connect("tcp://127.0.0.1:%i" % self.port)
|
||||||
return start_nodes(4, self.options.tmpdir, extra_args=[
|
return start_nodes(4, self.options.tmpdir, extra_args=[
|
||||||
['-zmqpubhashtx=tcp://127.0.0.1:'+str(self.port), '-zmqpubhashblock=tcp://127.0.0.1:'+str(self.port)],
|
['-zmqpubhashtx=tcp://127.0.0.1:'+str(self.port), '-zmqpubhashblock=tcp://127.0.0.1:'+str(self.port)],
|
||||||
@@ -46,13 +46,13 @@ class ZMQTest (BitcoinTestFramework):
|
|||||||
|
|
||||||
print "listen..."
|
print "listen..."
|
||||||
msg = self.zmqSubSocket.recv_multipart()
|
msg = self.zmqSubSocket.recv_multipart()
|
||||||
topic = str(msg[0])
|
topic = msg[0]
|
||||||
body = msg[1]
|
body = msg[1]
|
||||||
|
|
||||||
msg = self.zmqSubSocket.recv_multipart()
|
msg = self.zmqSubSocket.recv_multipart()
|
||||||
topic = str(msg[0])
|
topic = msg[0]
|
||||||
body = msg[1]
|
body = msg[1]
|
||||||
blkhash = binascii.hexlify(body)
|
blkhash = bytes_to_hex_str(body)
|
||||||
|
|
||||||
assert_equal(genhashes[0], blkhash) #blockhash from generate must be equal to the hash received over zmq
|
assert_equal(genhashes[0], blkhash) #blockhash from generate must be equal to the hash received over zmq
|
||||||
|
|
||||||
@@ -63,10 +63,10 @@ class ZMQTest (BitcoinTestFramework):
|
|||||||
zmqHashes = []
|
zmqHashes = []
|
||||||
for x in range(0,n*2):
|
for x in range(0,n*2):
|
||||||
msg = self.zmqSubSocket.recv_multipart()
|
msg = self.zmqSubSocket.recv_multipart()
|
||||||
topic = str(msg[0])
|
topic = msg[0]
|
||||||
body = msg[1]
|
body = msg[1]
|
||||||
if topic == "hashblock":
|
if topic == b"hashblock":
|
||||||
zmqHashes.append(binascii.hexlify(body))
|
zmqHashes.append(bytes_to_hex_str(body))
|
||||||
|
|
||||||
for x in range(0,n):
|
for x in range(0,n):
|
||||||
assert_equal(genhashes[x], zmqHashes[x]) #blockhash from generate must be equal to the hash received over zmq
|
assert_equal(genhashes[x], zmqHashes[x]) #blockhash from generate must be equal to the hash received over zmq
|
||||||
@@ -77,11 +77,11 @@ class ZMQTest (BitcoinTestFramework):
|
|||||||
|
|
||||||
# now we should receive a zmq msg because the tx was broadcast
|
# now we should receive a zmq msg because the tx was broadcast
|
||||||
msg = self.zmqSubSocket.recv_multipart()
|
msg = self.zmqSubSocket.recv_multipart()
|
||||||
topic = str(msg[0])
|
topic = msg[0]
|
||||||
body = msg[1]
|
body = msg[1]
|
||||||
hashZMQ = ""
|
hashZMQ = ""
|
||||||
if topic == "hashtx":
|
if topic == b"hashtx":
|
||||||
hashZMQ = binascii.hexlify(body)
|
hashZMQ = bytes_to_hex_str(body)
|
||||||
|
|
||||||
assert_equal(hashRPC, hashZMQ) #blockhash from generate must be equal to the hash received over zmq
|
assert_equal(hashRPC, hashZMQ) #blockhash from generate must be equal to the hash received over zmq
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user