diff options
author | Wladimir J. van der Laan <laanwj@gmail.com> | 2016-04-03 15:35:59 +0200 |
---|---|---|
committer | Wladimir J. van der Laan <laanwj@gmail.com> | 2016-04-03 15:54:35 +0200 |
commit | ff5874bcf7e391acf3b454c11c21636547400818 (patch) | |
tree | 5bf4080f939dcb44923edb98f3f13fdf9ae897f3 /qa/rpc-tests/test_framework | |
parent | e0b3e19816b62fdf6faeda44459f30945241fbd8 (diff) | |
parent | 444480649f08e6037f8ac178224b30a82e9ad72e (diff) |
Merge #7778: [qa] Bug fixes and refactor
4444806 [qa] mininode: Combine struct.pack format strings (MarcoFalke)
faaa3c9 [qa] mininode: Catch exceptions in got_data (MarcoFalke)
fa2cea1 [qa] rpc-tests: Properly use integers, floats (MarcoFalke)
fa524d9 [qa] Use python2/3 syntax (MarcoFalke)
Diffstat (limited to 'qa/rpc-tests/test_framework')
-rw-r--r-- | qa/rpc-tests/test_framework/blockstore.py | 7 | ||||
-rw-r--r-- | qa/rpc-tests/test_framework/blocktools.py | 6 | ||||
-rwxr-xr-x | qa/rpc-tests/test_framework/comptool.py | 6 | ||||
-rwxr-xr-x | qa/rpc-tests/test_framework/mininode.py | 128 | ||||
-rw-r--r-- | qa/rpc-tests/test_framework/netutil.py | 2 | ||||
-rw-r--r-- | qa/rpc-tests/test_framework/socks5.py | 2 | ||||
-rwxr-xr-x | qa/rpc-tests/test_framework/test_framework.py | 4 |
7 files changed, 80 insertions, 75 deletions
diff --git a/qa/rpc-tests/test_framework/blockstore.py b/qa/rpc-tests/test_framework/blockstore.py index b9775b477c..73d9ffbb2f 100644 --- a/qa/rpc-tests/test_framework/blockstore.py +++ b/qa/rpc-tests/test_framework/blockstore.py @@ -3,8 +3,9 @@ # and for constructing a getheaders message # -from mininode import * +from .mininode import * import dbm +from io import BytesIO class BlockStore(object): def __init__(self, datadir): @@ -21,7 +22,7 @@ class BlockStore(object): serialized_block = self.blockDB[repr(blockhash)] except KeyError: return None - f = cStringIO.StringIO(serialized_block) + f = BytesIO(serialized_block) ret = CBlock() ret.deserialize(f) ret.calc_sha256() @@ -115,7 +116,7 @@ class TxStore(object): serialized_tx = self.txDB[repr(txhash)] except KeyError: return None - f = cStringIO.StringIO(serialized_tx) + f = BytesIO(serialized_tx) ret = CTransaction() ret.deserialize(f) ret.calc_sha256() diff --git a/qa/rpc-tests/test_framework/blocktools.py b/qa/rpc-tests/test_framework/blocktools.py index b075f69c47..afa0f5f9ba 100644 --- a/qa/rpc-tests/test_framework/blocktools.py +++ b/qa/rpc-tests/test_framework/blocktools.py @@ -4,8 +4,8 @@ # file COPYING or http://www.opensource.org/licenses/mit-license.php. # -from mininode import * -from script import CScript, OP_TRUE, OP_CHECKSIG +from .mininode import * +from .script import CScript, OP_TRUE, OP_CHECKSIG # Create a block (with regtest difficulty) def create_block(hashprev, coinbase, nTime=None): @@ -29,7 +29,7 @@ def serialize_script_num(value): neg = value < 0 absvalue = -value if neg else value while (absvalue): - r.append(chr(absvalue & 0xff)) + r.append(int(absvalue & 0xff)) absvalue >>= 8 if r[-1] & 0x80: r.append(0x80 if neg else 0) diff --git a/qa/rpc-tests/test_framework/comptool.py b/qa/rpc-tests/test_framework/comptool.py index f19edbf069..6279070fbc 100755 --- a/qa/rpc-tests/test_framework/comptool.py +++ b/qa/rpc-tests/test_framework/comptool.py @@ -4,9 +4,9 @@ # file COPYING or http://www.opensource.org/licenses/mit-license.php. # -from mininode import * -from blockstore import BlockStore, TxStore -from util import p2p_port +from .mininode import * +from .blockstore import BlockStore, TxStore +from .util import p2p_port ''' This is a tool for comparing two or more bitcoinds to each other diff --git a/qa/rpc-tests/test_framework/mininode.py b/qa/rpc-tests/test_framework/mininode.py index 20386c642c..53f5e8805a 100755 --- a/qa/rpc-tests/test_framework/mininode.py +++ b/qa/rpc-tests/test_framework/mininode.py @@ -24,7 +24,8 @@ import binascii import time import sys import random -import cStringIO +from io import BytesIO +from codecs import encode import hashlib from threading import RLock from threading import Thread @@ -75,12 +76,12 @@ def deser_string(f): def ser_string(s): if len(s) < 253: - return chr(len(s)) + s + return struct.pack("B", len(s)) + s elif len(s) < 0x10000: - return chr(253) + struct.pack("<H", len(s)) + s + return struct.pack("<BH", 253, len(s)) + s elif len(s) < 0x100000000L: - return chr(254) + struct.pack("<I", len(s)) + s - return chr(255) + struct.pack("<Q", len(s)) + s + return struct.pack("<BI", 254, len(s)) + s + return struct.pack("<BQ", 255, len(s)) + s def deser_uint256(f): @@ -132,13 +133,13 @@ def deser_vector(f, c): def ser_vector(l): r = "" if len(l) < 253: - r = chr(len(l)) + r = struct.pack("B", len(l)) elif len(l) < 0x10000: - r = chr(253) + struct.pack("<H", len(l)) + r = struct.pack("<BH", 253, len(l)) elif len(l) < 0x100000000L: - r = chr(254) + struct.pack("<I", len(l)) + r = struct.pack("<BI", 254, len(l)) else: - r = chr(255) + struct.pack("<Q", len(l)) + r = struct.pack("<BQ", 255, len(l)) for i in l: r += i.serialize() return r @@ -162,13 +163,13 @@ def deser_uint256_vector(f): def ser_uint256_vector(l): r = "" if len(l) < 253: - r = chr(len(l)) + r = struct.pack("B", len(l)) elif len(l) < 0x10000: - r = chr(253) + struct.pack("<H", len(l)) + r = struct.pack("<BH", 253, len(l)) elif len(l) < 0x100000000L: - r = chr(254) + struct.pack("<I", len(l)) + r = struct.pack("<BI", 254, len(l)) else: - r = chr(255) + struct.pack("<Q", len(l)) + r = struct.pack("<BQ", 255, len(l)) for i in l: r += ser_uint256(i) return r @@ -192,13 +193,13 @@ def deser_string_vector(f): def ser_string_vector(l): r = "" if len(l) < 253: - r = chr(len(l)) + r = struct.pack("B", len(l)) elif len(l) < 0x10000: - r = chr(253) + struct.pack("<H", len(l)) + r = struct.pack("<BH", 253, len(l)) elif len(l) < 0x100000000L: - r = chr(254) + struct.pack("<I", len(l)) + r = struct.pack("<BI", 254, len(l)) else: - r = chr(255) + struct.pack("<Q", len(l)) + r = struct.pack("<BQ", 255, len(l)) for sv in l: r += ser_string(sv) return r @@ -222,20 +223,20 @@ def deser_int_vector(f): def ser_int_vector(l): r = "" if len(l) < 253: - r = chr(len(l)) + r = struct.pack("B", len(l)) elif len(l) < 0x10000: - r = chr(253) + struct.pack("<H", len(l)) + r = struct.pack("<BH", 253, len(l)) elif len(l) < 0x100000000L: - r = chr(254) + struct.pack("<I", len(l)) + r = struct.pack("<BI", 254, len(l)) else: - r = chr(255) + struct.pack("<Q", len(l)) + r = struct.pack("<BQ", 255, len(l)) for i in l: r += struct.pack("<i", i) return r # Deserialize from a hex string representation (eg from RPC) def FromHex(obj, hex_string): - obj.deserialize(cStringIO.StringIO(binascii.unhexlify(hex_string))) + obj.deserialize(BytesIO(binascii.unhexlify(hex_string))) return obj # Convert a binary-serializable object to hex (eg for submission via RPC) @@ -423,7 +424,7 @@ class CTransaction(object): def calc_sha256(self): if self.sha256 is None: self.sha256 = uint256_from_str(hash256(self.serialize())) - self.hash = hash256(self.serialize())[::-1].encode('hex_codec') + self.hash = encode(hash256(self.serialize())[::-1], 'hex') def is_valid(self): self.calc_sha256() @@ -492,7 +493,7 @@ class CBlockHeader(object): r += struct.pack("<I", self.nBits) r += struct.pack("<I", self.nNonce) self.sha256 = uint256_from_str(hash256(r)) - self.hash = hash256(r)[::-1].encode('hex_codec') + self.hash = encode(hash256(r)[::-1], 'hex') def rehash(self): self.sha256 = None @@ -640,7 +641,7 @@ class msg_version(object): def __init__(self): self.nVersion = MY_VERSION self.nServices = 1 - self.nTime = time.time() + self.nTime = int(time.time()) self.addrTo = CAddress() self.addrFrom = CAddress() self.nNonce = random.getrandbits(64) @@ -985,7 +986,7 @@ class msg_reject(object): def __init__(self): self.message = "" - self.code = "" + self.code = 0 self.reason = "" self.data = 0L @@ -1251,43 +1252,46 @@ class NodeConn(asyncore.dispatcher): self.sendbuf = self.sendbuf[sent:] def got_data(self): - while True: - if len(self.recvbuf) < 4: - return - if self.recvbuf[:4] != self.MAGIC_BYTES[self.network]: - raise ValueError("got garbage %s" % repr(self.recvbuf)) - if self.ver_recv < 209: - if len(self.recvbuf) < 4 + 12 + 4: - return - command = self.recvbuf[4:4+12].split("\x00", 1)[0] - msglen = struct.unpack("<i", self.recvbuf[4+12:4+12+4])[0] - checksum = None - if len(self.recvbuf) < 4 + 12 + 4 + msglen: - return - msg = self.recvbuf[4+12+4:4+12+4+msglen] - self.recvbuf = self.recvbuf[4+12+4+msglen:] - else: - if len(self.recvbuf) < 4 + 12 + 4 + 4: - return - command = self.recvbuf[4:4+12].split("\x00", 1)[0] - msglen = struct.unpack("<i", self.recvbuf[4+12:4+12+4])[0] - checksum = self.recvbuf[4+12+4:4+12+4+4] - if len(self.recvbuf) < 4 + 12 + 4 + 4 + msglen: + try: + while True: + if len(self.recvbuf) < 4: return - msg = self.recvbuf[4+12+4+4:4+12+4+4+msglen] - th = sha256(msg) - h = sha256(th) - if checksum != h[:4]: - raise ValueError("got bad checksum " + repr(self.recvbuf)) - self.recvbuf = self.recvbuf[4+12+4+4+msglen:] - if command in self.messagemap: - f = cStringIO.StringIO(msg) - t = self.messagemap[command]() - t.deserialize(f) - self.got_message(t) - else: - self.show_debug_msg("Unknown command: '" + command + "' " + - repr(msg)) + if self.recvbuf[:4] != self.MAGIC_BYTES[self.network]: + raise ValueError("got garbage %s" % repr(self.recvbuf)) + if self.ver_recv < 209: + if len(self.recvbuf) < 4 + 12 + 4: + return + command = self.recvbuf[4:4+12].split("\x00", 1)[0] + msglen = struct.unpack("<i", self.recvbuf[4+12:4+12+4])[0] + checksum = None + if len(self.recvbuf) < 4 + 12 + 4 + msglen: + return + msg = self.recvbuf[4+12+4:4+12+4+msglen] + self.recvbuf = self.recvbuf[4+12+4+msglen:] + else: + if len(self.recvbuf) < 4 + 12 + 4 + 4: + return + command = self.recvbuf[4:4+12].split("\x00", 1)[0] + msglen = struct.unpack("<i", self.recvbuf[4+12:4+12+4])[0] + checksum = self.recvbuf[4+12+4:4+12+4+4] + if len(self.recvbuf) < 4 + 12 + 4 + 4 + msglen: + return + msg = self.recvbuf[4+12+4+4:4+12+4+4+msglen] + th = sha256(msg) + h = sha256(th) + if checksum != h[:4]: + raise ValueError("got bad checksum " + repr(self.recvbuf)) + self.recvbuf = self.recvbuf[4+12+4+4+msglen:] + if command in self.messagemap: + f = BytesIO(msg) + t = self.messagemap[command]() + t.deserialize(f) + self.got_message(t) + else: + self.show_debug_msg("Unknown command: '" + command + "' " + + repr(msg)) + except Exception as e: + print 'got_data:', repr(e) def send_message(self, message, pushbuf=False): if self.state != "connected" and not pushbuf: diff --git a/qa/rpc-tests/test_framework/netutil.py b/qa/rpc-tests/test_framework/netutil.py index bfdef76ad1..bbc58a14ec 100644 --- a/qa/rpc-tests/test_framework/netutil.py +++ b/qa/rpc-tests/test_framework/netutil.py @@ -45,7 +45,7 @@ def _convert_ip_port(array): # convert host from mangled-per-four-bytes form as used by kernel host = binascii.unhexlify(host) host_out = '' - for x in range(0, len(host)/4): + for x in range(0, len(host) // 4): (val,) = struct.unpack('=I', host[x*4:(x+1)*4]) host_out += '%08x' % val diff --git a/qa/rpc-tests/test_framework/socks5.py b/qa/rpc-tests/test_framework/socks5.py index 1dbfb98d5d..12327a6c5f 100644 --- a/qa/rpc-tests/test_framework/socks5.py +++ b/qa/rpc-tests/test_framework/socks5.py @@ -117,7 +117,7 @@ class Socks5Connection(object): self.serv.queue.put(cmdin) print('Proxy: ', cmdin) # Fall through to disconnect - except Exception,e: + except Exception as e: traceback.print_exc(file=sys.stderr) self.serv.queue.put(e) finally: diff --git a/qa/rpc-tests/test_framework/test_framework.py b/qa/rpc-tests/test_framework/test_framework.py index 584f318d0b..19ee472609 100755 --- a/qa/rpc-tests/test_framework/test_framework.py +++ b/qa/rpc-tests/test_framework/test_framework.py @@ -26,7 +26,7 @@ from .util import ( check_json_precision, initialize_chain_clean, ) -from authproxy import AuthServiceProxy, JSONRPCException +from .authproxy import AuthServiceProxy, JSONRPCException class BitcoinTestFramework(object): @@ -140,7 +140,7 @@ class BitcoinTestFramework(object): print("JSONRPC error: "+e.error['message']) traceback.print_tb(sys.exc_info()[2]) except AssertionError as e: - print("Assertion failed: "+e.message) + print("Assertion failed: "+ str(e)) traceback.print_tb(sys.exc_info()[2]) except Exception as e: print("Unexpected exception caught during testing: "+str(e)) |