From 3f751205b1fe279d958af39719293fb40d7a1221 Mon Sep 17 00:00:00 2001 From: mdr0id Date: Thu, 5 Dec 2019 15:55:06 -0800 Subject: [PATCH] update io module, fix py3 division, and string encoding --- qa/rpc-tests/bipdersig-p2p.py | 4 +- qa/rpc-tests/p2p_txexpiringsoon.py | 4 +- qa/rpc-tests/test_framework/blockstore.py | 2 +- qa/rpc-tests/test_framework/blocktools.py | 4 +- qa/rpc-tests/test_framework/equihash.py | 45 ++++++++++++----------- qa/rpc-tests/test_framework/script.py | 10 ++--- qa/rpc-tests/tx_expiry_helper.py | 2 +- 7 files changed, 35 insertions(+), 36 deletions(-) diff --git a/qa/rpc-tests/bipdersig-p2p.py b/qa/rpc-tests/bipdersig-p2p.py index 05a25c1d7..130768fff 100755 --- a/qa/rpc-tests/bipdersig-p2p.py +++ b/qa/rpc-tests/bipdersig-p2p.py @@ -47,7 +47,7 @@ class BIP66Test(ComparisonTestFramework): rawtx = node.createrawtransaction(inputs, outputs) signresult = node.signrawtransaction(rawtx) tx = CTransaction() - f = io.StringIO(unhexlify(signresult['hex'])) + f = io.BytesIO(unhexlify(signresult['hex'])) tx.deserialize(f) return tx @@ -73,7 +73,7 @@ class BIP66Test(ComparisonTestFramework): self.nodes[0].generate(100) hashTip = self.nodes[0].getbestblockhash() hashFinalSaplingRoot = int("0x" + self.nodes[0].getblock(hashTip)['finalsaplingroot'], 0) - self.tip = int (hashTip , 0) + self.tip = int ("0x"+hashTip , 0) self.nodeaddress = self.nodes[0].getnewaddress() '''Check that the rules are enforced.''' diff --git a/qa/rpc-tests/p2p_txexpiringsoon.py b/qa/rpc-tests/p2p_txexpiringsoon.py index cb39977f8..3d5d10a78 100755 --- a/qa/rpc-tests/p2p_txexpiringsoon.py +++ b/qa/rpc-tests/p2p_txexpiringsoon.py @@ -11,8 +11,6 @@ from test_framework.util import assert_equal, connect_nodes_bi, fail, \ initialize_chain_clean, p2p_port, start_nodes, sync_blocks, sync_mempools from tx_expiry_helper import TestNode, create_transaction -from binascii import hexlify - class TxExpiringSoonTest(BitcoinTestFramework): @@ -163,7 +161,7 @@ class TxExpiringSoonTest(BitcoinTestFramework): # Confirm tx2 cannot be submitted to a mempool because it is expiring soon. try: - rawtx2 = hexlify(tx2.serialize()) + rawtx2 = tx2.serialize().hex() self.nodes[2].sendrawtransaction(rawtx2) fail("Sending transaction should have failed") except JSONRPCException as e: diff --git a/qa/rpc-tests/test_framework/blockstore.py b/qa/rpc-tests/test_framework/blockstore.py index a04d01692..4998ce04a 100644 --- a/qa/rpc-tests/test_framework/blockstore.py +++ b/qa/rpc-tests/test_framework/blockstore.py @@ -23,7 +23,7 @@ class BlockStore(): serialized_block = self.blockDB[repr(blockhash)] except KeyError: return None - f = io.StringIO(serialized_block) + f = io.BytesIO(serialized_block) ret = CBlock() 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 11ff2274c..e821d39e8 100644 --- a/qa/rpc-tests/test_framework/blocktools.py +++ b/qa/rpc-tests/test_framework/blocktools.py @@ -54,11 +54,11 @@ def create_coinbase(heightAdjust = 0): coinbaseoutput.nValue = int(12.5*100000000) halvings = int((counter+heightAdjust)/150) # regtest coinbaseoutput.nValue >>= halvings - coinbaseoutput.scriptPubKey = "" + coinbaseoutput.scriptPubKey = b"" coinbase.vout = [ coinbaseoutput ] if halvings == 0: # regtest froutput = CTxOut() - froutput.nValue = coinbaseoutput.nValue / 5 + froutput.nValue = coinbaseoutput.nValue // 5 # regtest fraddr = bytearray([0x67, 0x08, 0xe6, 0x67, 0x0d, 0xb0, 0xb9, 0x50, 0xda, 0xc6, 0x80, 0x31, 0x02, 0x5c, 0xc5, 0xb6, diff --git a/qa/rpc-tests/test_framework/equihash.py b/qa/rpc-tests/test_framework/equihash.py index f025095cd..d2d7cbea9 100755 --- a/qa/rpc-tests/test_framework/equihash.py +++ b/qa/rpc-tests/test_framework/equihash.py @@ -1,5 +1,6 @@ from operator import itemgetter import struct +from functools import reduce DEBUG = False VERBOSE = False @@ -12,8 +13,8 @@ def expand_array(inp, out_len, bit_len, byte_pad=0): assert bit_len >= 8 and word_size >= 7+bit_len bit_len_mask = (1<= 8 and word_size >= 7+bit_len - in_width = (bit_len+7)/8 + byte_pad - assert out_len == bit_len*len(inp)/(8*in_width) + in_width = (bit_len+7)//8 + byte_pad + assert out_len == bit_len*len(inp)//(8*in_width) out = bytearray(out_len) bit_len_mask = (1 << bit_len) - 1 @@ -80,19 +81,19 @@ def compress_array(inp, out_len, bit_len, byte_pad=0): def get_indices_from_minimal(minimal, bit_len): eh_index_size = 4 - assert (bit_len+7)/8 <= eh_index_size - len_indices = 8*eh_index_size*len(minimal)/bit_len - byte_pad = eh_index_size - (bit_len+7)/8 + assert (bit_len+7)//8 <= eh_index_size + len_indices = 8*eh_index_size*len(minimal)//bit_len + byte_pad = eh_index_size - (bit_len+7)//8 expanded = expand_array(minimal, len_indices, bit_len, byte_pad) return [struct.unpack('>I', expanded[i:i+4])[0] for i in range(0, len_indices, eh_index_size)] def get_minimal_from_indices(indices, bit_len): eh_index_size = 4 - assert (bit_len+7)/8 <= eh_index_size + assert (bit_len+7)//8 <= eh_index_size len_indices = len(indices)*eh_index_size - min_len = bit_len*len_indices/(8*eh_index_size) - byte_pad = eh_index_size - (bit_len+7)/8 - byte_indices = bytearray(''.join([struct.pack('>I', i) for i in indices])) + min_len = bit_len*len_indices//(8*eh_index_size) + byte_pad = eh_index_size - (bit_len+7)//8 + byte_indices = bytearray(b''.join([struct.pack('>I', i) for i in indices])) return compress_array(byte_indices, min_len, bit_len, byte_pad) @@ -114,7 +115,7 @@ def count_zeroes(h): return (h+'1').index('1') def has_collision(ha, hb, i, l): - res = [ha[j] == hb[j] for j in range((i-1)*l/8, i*l/8)] + res = [ha[j] == hb[j] for j in range((i-1)*l//8, i*l//8)] return reduce(lambda x, y: x and y, res) def distinct_indices(a, b): @@ -130,23 +131,23 @@ def xor(ha, hb): def gbp_basic(digest, n, k): '''Implementation of Basic Wagner's algorithm for the GBP.''' validate_params(n, k) - collision_length = n/(k+1) + collision_length = n//(k+1) hash_length = (k+1)*((collision_length+7)//8) - indices_per_hash_output = 512/n + indices_per_hash_output = 512//n # 1) Generate first list if DEBUG: print('Generating first list') X = [] - tmp_hash = '' + tmp_hash = b'' for i in range(0, 2**(collision_length+1)): r = i % indices_per_hash_output if r == 0: # X_i = H(I||V||x_i) curr_digest = digest.copy() - hash_xi(curr_digest, i/indices_per_hash_output) + hash_xi(curr_digest, i//indices_per_hash_output) tmp_hash = curr_digest.digest() X.append(( - expand_array(bytearray(tmp_hash[r*n/8:(r+1)*n/8]), + expand_array(bytearray(tmp_hash[r*n//8:(r+1)*n//8]), hash_length, collision_length), (i,) )) @@ -229,9 +230,9 @@ def gbp_basic(digest, n, k): def gbp_validate(digest, minimal, n, k): validate_params(n, k) - collision_length = n/(k+1) + collision_length = n//(k+1) hash_length = (k+1)*((collision_length+7)//8) - indices_per_hash_output = 512/n + indices_per_hash_output = 512//n solution_width = (1 << k)*(collision_length+1)//8 if len(minimal) != solution_width: @@ -244,10 +245,10 @@ def gbp_validate(digest, minimal, n, k): r = i % indices_per_hash_output # X_i = H(I||V||x_i) curr_digest = digest.copy() - hash_xi(curr_digest, i/indices_per_hash_output) + hash_xi(curr_digest, i//indices_per_hash_output) tmp_hash = curr_digest.digest() X.append(( - expand_array(bytearray(tmp_hash[r*n/8:(r+1)*n/8]), + expand_array(bytearray(tmp_hash[r*n//8:(r+1)*n//8]), hash_length, collision_length), (i,) )) @@ -289,5 +290,5 @@ def print_hash(h): def validate_params(n, k): if (k >= n): raise ValueError('n must be larger than k') - if (((n/(k+1))+1) >= 32): + if (((n//(k+1))+1) >= 32): raise ValueError('Parameters must satisfy n/(k+1)+1 < 32') diff --git a/qa/rpc-tests/test_framework/script.py b/qa/rpc-tests/test_framework/script.py index ec87f6fed..a393bb0ff 100644 --- a/qa/rpc-tests/test_framework/script.py +++ b/qa/rpc-tests/test_framework/script.py @@ -41,9 +41,9 @@ class CScriptOp(int): def encode_op_pushdata(d): """Encode a PUSHDATA op, returning bytes""" if len(d) < 0x4c: - return b'' + bchr(len(d)) + d # OP_PUSHDATA + return b'' + struct.pack(b'