update io module, fix py3 division, and string encoding

This commit is contained in:
mdr0id 2019-12-05 15:55:06 -08:00
parent bc14f56398
commit 3f751205b1
7 changed files with 35 additions and 36 deletions

View File

@ -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.'''

View File

@ -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:

View File

@ -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()

View File

@ -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,

View File

@ -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<<bit_len)-1
out_width = (bit_len+7)/8 + byte_pad
assert out_len == 8*out_width*len(inp)/bit_len
out_width = (bit_len+7)//8 + byte_pad
assert out_len == 8*out_width*len(inp)//bit_len
out = bytearray(out_len)
bit_len_mask = (1 << bit_len) - 1
@ -47,8 +48,8 @@ def expand_array(inp, out_len, bit_len, byte_pad=0):
def compress_array(inp, out_len, bit_len, byte_pad=0):
assert bit_len >= 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')

View File

@ -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'<B', len(d)) + d # OP_PUSHDATA
elif len(d) <= 0xff:
return b'\x4c' + bchr(len(d)) + d # OP_PUSHDATA1
return b'\x4c' + struct.pack(b'<B', len(d)) + d # OP_PUSHDATA1
elif len(d) <= 0xffff:
return b'\x4d' + struct.pack(b'<H', len(d)) + d # OP_PUSHDATA2
elif len(d) <= 0xffffffff:
@ -632,7 +632,7 @@ class CScriptNum(object):
r.append(0x80 if neg else 0)
elif neg:
r[-1] |= 0x80
return bytes(bchr(len(r)) + r)
return struct.pack("<B",len(r)) + r
class CScript(bytes):
@ -649,7 +649,7 @@ class CScript(bytes):
def __coerce_instance(cls, other):
# Coerce other into bytes
if isinstance(other, CScriptOp):
other = bytes(other)
other = bytes([other])
elif isinstance(other, CScriptNum):
if (other.value == 0):
other = bytes([CScriptOp(OP_0)])
@ -774,7 +774,7 @@ class CScript(bytes):
# need to change
def _repr(o):
if isinstance(o, bytes):
return "x('%s')" % binascii.hexlify(o).decode('utf8')
return "x('%s')" % o.hex().decode('utf8')
else:
return repr(o)

View File

@ -90,7 +90,7 @@ def create_transaction(node, coinbase, to_address, amount, expiry_height):
f = io.BytesIO(unhexlify(rawtx))
tx.deserialize(f)
tx.nExpiryHeight = expiry_height
rawtx = hexlify(tx.serialize())
rawtx = tx.serialize().hex()
signresult = node.signrawtransaction(rawtx)
f = io.BytesIO(unhexlify(signresult['hex']))