mirror of https://github.com/zcash/zcash.git
[qa] py2: Unfiddle strings into bytes explicitly
(cherry picked from commit bitcoin/bitcoin@faa41ee204) Zcash: Excluding RPC tests or framework code we don't have.
This commit is contained in:
parent
d9cb6b89b0
commit
b67c86abdb
|
@ -4,12 +4,11 @@
|
|||
# file COPYING or https://www.opensource.org/licenses/mit-license.php .
|
||||
|
||||
from test_framework.test_framework import ComparisonTestFramework
|
||||
from test_framework.util import start_nodes
|
||||
from test_framework.util import hex_str_to_bytes, start_nodes
|
||||
from test_framework.mininode import CTransaction, NetworkThread
|
||||
from test_framework.blocktools import create_coinbase, create_block
|
||||
from test_framework.comptool import TestInstance, TestManager
|
||||
from test_framework.script import CScript, OP_1NEGATE, OP_NOP2, OP_DROP
|
||||
from binascii import unhexlify
|
||||
from io import BytesIO
|
||||
|
||||
'''
|
||||
|
@ -47,7 +46,7 @@ class BIP65Test(ComparisonTestFramework):
|
|||
rawtx = node.createrawtransaction(inputs, outputs)
|
||||
signresult = node.signrawtransaction(rawtx)
|
||||
tx = CTransaction()
|
||||
f = BytesIO(unhexlify(signresult['hex']))
|
||||
f = BytesIO(hex_str_to_bytes(signresult['hex']))
|
||||
tx.deserialize(f)
|
||||
return tx
|
||||
|
||||
|
|
|
@ -4,12 +4,11 @@
|
|||
# file COPYING or https://www.opensource.org/licenses/mit-license.php .
|
||||
|
||||
from test_framework.test_framework import ComparisonTestFramework
|
||||
from test_framework.util import start_nodes
|
||||
from test_framework.util import hex_str_to_bytes, start_nodes
|
||||
from test_framework.mininode import CTransaction, NetworkThread
|
||||
from test_framework.blocktools import create_coinbase, create_block
|
||||
from test_framework.comptool import TestInstance, TestManager
|
||||
from test_framework.script import CScript
|
||||
from binascii import unhexlify
|
||||
from io import BytesIO
|
||||
|
||||
|
||||
|
@ -47,7 +46,7 @@ class BIP66Test(ComparisonTestFramework):
|
|||
rawtx = node.createrawtransaction(inputs, outputs)
|
||||
signresult = node.signrawtransaction(rawtx)
|
||||
tx = CTransaction()
|
||||
f = BytesIO(unhexlify(signresult['hex']))
|
||||
f = BytesIO(hex_str_to_bytes(signresult['hex']))
|
||||
tx.deserialize(f)
|
||||
return tx
|
||||
|
||||
|
|
|
@ -150,7 +150,6 @@ class RawTransactionsTest(BitcoinTestFramework):
|
|||
assert_equal(fee + totalOut, utx['amount']) #compare vin total and totalout+fee
|
||||
|
||||
|
||||
|
||||
#####################################################################
|
||||
# test a fundrawtransaction with which will not get a change output #
|
||||
#####################################################################
|
||||
|
@ -180,7 +179,6 @@ class RawTransactionsTest(BitcoinTestFramework):
|
|||
assert_equal(fee + totalOut, utx['amount']) #compare vin total and totalout+fee
|
||||
|
||||
|
||||
|
||||
#########################################################################
|
||||
# test a fundrawtransaction with a VIN smaller than the required amount #
|
||||
#########################################################################
|
||||
|
@ -486,7 +484,6 @@ class RawTransactionsTest(BitcoinTestFramework):
|
|||
assert_equal(oldBalance+Decimal('11.10000000'), self.nodes[0].getbalance())
|
||||
|
||||
|
||||
|
||||
###############################################
|
||||
# multiple (~19) inputs tx test | Compare fee #
|
||||
###############################################
|
||||
|
|
|
@ -145,7 +145,7 @@ class GetBlockTemplateProposalTest(BitcoinTestFramework):
|
|||
|
||||
# Test 5: Add an invalid tx to the end (non-duplicate)
|
||||
txlist.append(bytearray(txlist[0]))
|
||||
txlist[-1][4+1] = b'\xff'
|
||||
txlist[-1][4+1] = 0xff
|
||||
assert_template(node, tmpl, txlist, 'bad-txns-inputs-missingorspent')
|
||||
txlist.pop()
|
||||
|
||||
|
|
|
@ -79,9 +79,9 @@ class InvalidBlockRequestTest(ComparisonTestFramework):
|
|||
block2 = create_block(self.tip, create_coinbase(height), self.block_time)
|
||||
self.block_time += 1
|
||||
|
||||
# chr(81) is OP_TRUE
|
||||
tx1 = create_transaction(self.block1.vtx[0], 0, chr(81), 10*100000000)
|
||||
tx2 = create_transaction(tx1, 0, chr(81), 10*100000000)
|
||||
# b'0x51' is OP_TRUE
|
||||
tx1 = create_transaction(self.block1.vtx[0], 0, b'\x51', 10*100000000)
|
||||
tx2 = create_transaction(tx1, 0, b'\x51', 10*100000000)
|
||||
|
||||
block2.vtx.extend([tx1, tx2])
|
||||
block2.hashMerkleRoot = block2.calc_merkle_root()
|
||||
|
@ -97,7 +97,7 @@ class InvalidBlockRequestTest(ComparisonTestFramework):
|
|||
assert(block2_orig.vtx != block2.vtx)
|
||||
|
||||
self.tip = block2.sha256
|
||||
yield TestInstance([[block2, RejectResult(16,'bad-txns-duplicate')], [block2_orig, True]])
|
||||
yield TestInstance([[block2, RejectResult(16, b'bad-txns-duplicate')], [block2_orig, True]])
|
||||
height += 1
|
||||
|
||||
'''
|
||||
|
@ -112,7 +112,7 @@ class InvalidBlockRequestTest(ComparisonTestFramework):
|
|||
block3.rehash()
|
||||
block3.solve()
|
||||
|
||||
yield TestInstance([[block3, RejectResult(16,'bad-cb-amount')]])
|
||||
yield TestInstance([[block3, RejectResult(16, b'bad-cb-amount')]])
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
|
|
@ -65,10 +65,10 @@ class InvalidTxRequestTest(ComparisonTestFramework):
|
|||
height += 1
|
||||
yield test
|
||||
|
||||
# chr(100) is OP_NOTIF
|
||||
# b'\x64' is OP_NOTIF
|
||||
# Transaction will be rejected with code 16 (REJECT_INVALID)
|
||||
tx1 = create_transaction(self.block1.vtx[0], 0, chr(100), 10*100000000)
|
||||
yield TestInstance([[tx1, RejectResult(16, 'mandatory-script-verify-flag-failed')]])
|
||||
tx1 = create_transaction(self.block1.vtx[0], 0, b'\x64', 10*100000000)
|
||||
yield TestInstance([[tx1, RejectResult(16, b'mandatory-script-verify-flag-failed')]])
|
||||
|
||||
# TODO: test further transactions...
|
||||
|
||||
|
|
|
@ -116,6 +116,5 @@ class HTTPBasicsTest (BitcoinTestFramework):
|
|||
conn.close()
|
||||
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
HTTPBasicsTest ().main ()
|
||||
|
|
|
@ -79,7 +79,7 @@ class FullBlockTest(ComparisonTestFramework):
|
|||
block = create_block(base_block_hash, coinbase, self.block_time)
|
||||
if (spend != None):
|
||||
tx = CTransaction()
|
||||
tx.vin.append(CTxIn(COutPoint(spend.tx.sha256, spend.n), "", 0xffffffff)) # no signature yet
|
||||
tx.vin.append(CTxIn(COutPoint(spend.tx.sha256, spend.n), b"", 0xffffffff)) # no signature yet
|
||||
# This copies the java comparison tool testing behavior: the first
|
||||
# txout has a garbage scriptPubKey, "to make sure we're not
|
||||
# pre-verifying too much" (?)
|
||||
|
@ -89,7 +89,7 @@ class FullBlockTest(ComparisonTestFramework):
|
|||
else:
|
||||
tx.vout.append(CTxOut(1, script))
|
||||
# Now sign it if necessary
|
||||
scriptSig = ""
|
||||
scriptSig = b""
|
||||
scriptPubKey = bytearray(spend.tx.vout[spend.n].scriptPubKey)
|
||||
if (scriptPubKey[0] == OP_TRUE): # looks like an anyone-can-spend
|
||||
scriptSig = CScript([OP_TRUE])
|
||||
|
|
|
@ -333,7 +333,7 @@ class PruneTest(BitcoinTestFramework):
|
|||
# \ \
|
||||
# ++...++(1044) ..
|
||||
#
|
||||
# N0 ********************(1032) @@...@@@(1552)
|
||||
# N0 ********************(1032) @@...@@@(1552)
|
||||
# \
|
||||
# *...**(1320)
|
||||
|
||||
|
|
|
@ -120,8 +120,6 @@ class RawTransactionsTest(BitcoinTestFramework):
|
|||
assert_equal(self.nodes[2].getbalance(), bal+Decimal('1.20000000')) #node2 has both keys of the 2of2 ms addr., tx should affect the balance
|
||||
|
||||
|
||||
|
||||
|
||||
# 2of3 test from different nodes
|
||||
bal = self.nodes[2].getbalance()
|
||||
addr1 = self.nodes[1].getnewaddress()
|
||||
|
|
|
@ -9,7 +9,7 @@
|
|||
|
||||
from test_framework.test_framework import BitcoinTestFramework
|
||||
from test_framework.util import assert_equal, assert_greater_than, \
|
||||
start_nodes, connect_nodes_bi
|
||||
hex_str_to_bytes, start_nodes, connect_nodes_bi
|
||||
|
||||
import struct
|
||||
import binascii
|
||||
|
@ -36,7 +36,7 @@ def http_get_call(host, port, path, response_object = 0):
|
|||
if response_object:
|
||||
return conn.getresponse()
|
||||
|
||||
return conn.getresponse().read().decode("utf-8")
|
||||
return conn.getresponse().read().decode('utf-8')
|
||||
|
||||
# allows simple http post calls with a request body
|
||||
def http_post_call(host, port, path, requestdata = '', response_object = 0):
|
||||
|
@ -139,9 +139,9 @@ class RESTTest (BitcoinTestFramework):
|
|||
bb_hash = self.nodes[0].getbestblockhash()
|
||||
|
||||
binaryRequest = b'\x01\x02'
|
||||
binaryRequest += binascii.unhexlify(txid)
|
||||
binaryRequest += hex_str_to_bytes(txid)
|
||||
binaryRequest += struct.pack("i", n);
|
||||
binaryRequest += binascii.unhexlify(vintx);
|
||||
binaryRequest += hex_str_to_bytes(vintx);
|
||||
binaryRequest += struct.pack("i", 0);
|
||||
|
||||
bin_response = http_post_call(url.hostname, url.port, '/rest/getutxos'+self.FORMAT_SEPARATOR+'bin', binaryRequest)
|
||||
|
@ -249,7 +249,7 @@ class RESTTest (BitcoinTestFramework):
|
|||
assert_greater_than(int(response_header_hex.getheader('content-length')), 354)
|
||||
response_header_hex_str = response_header_hex.read()
|
||||
assert_equal(response_hex_str[0:354], response_header_hex_str[0:354])
|
||||
assert_equal(encode(response_header_str, "hex-codec")[0:354], response_header_hex_str[0:354])
|
||||
assert_equal(encode(response_header_str, "hex_codec")[0:354], response_header_hex_str[0:354])
|
||||
|
||||
# check json format
|
||||
block_json_string = http_get_call(url.hostname, url.port, '/rest/block/'+bb_hash+self.FORMAT_SEPARATOR+'json')
|
||||
|
@ -299,7 +299,6 @@ class RESTTest (BitcoinTestFramework):
|
|||
assert_greater_than(int(response.getheader('content-length')), 10)
|
||||
|
||||
|
||||
|
||||
# check block tx details
|
||||
# let's make 3 tx and mine them on node 1
|
||||
txs = []
|
||||
|
|
|
@ -58,7 +58,7 @@ class RejectResult(object):
|
|||
'''
|
||||
Outcome that expects rejection of a transaction or block.
|
||||
'''
|
||||
def __init__(self, code, reason=''):
|
||||
def __init__(self, code, reason=b''):
|
||||
self.code = code
|
||||
self.reason = reason
|
||||
def match(self, other):
|
||||
|
@ -125,9 +125,9 @@ class TestNode(NodeConnCB):
|
|||
raise AssertionError("Got pong for unknown ping [%s]" % repr(message))
|
||||
|
||||
def on_reject(self, conn, message):
|
||||
if message.message == 'tx':
|
||||
if message.message == b'tx':
|
||||
self.tx_reject_map[message.data] = RejectResult(message.code, message.reason)
|
||||
if message.message == 'block':
|
||||
if message.message == b'block':
|
||||
self.block_reject_map[message.data] = RejectResult(message.code, message.reason)
|
||||
|
||||
def send_inv(self, obj):
|
||||
|
|
|
@ -28,8 +28,9 @@ import asyncore
|
|||
import time
|
||||
import sys
|
||||
import random
|
||||
from binascii import hexlify
|
||||
from binascii import hexlify, unhexlify
|
||||
from io import BytesIO
|
||||
from codecs import encode
|
||||
import hashlib
|
||||
from threading import RLock
|
||||
from threading import Thread
|
||||
|
@ -627,7 +628,7 @@ class CTxIn(object):
|
|||
|
||||
def __repr__(self):
|
||||
return "CTxIn(prevout=%s scriptSig=%s nSequence=%i)" \
|
||||
% (self.prevout, hexlify(self.scriptSig),
|
||||
% (repr(self.prevout), hexlify(self.scriptSig),
|
||||
self.nSequence)
|
||||
|
||||
|
||||
|
@ -764,7 +765,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].hex()
|
||||
self.hash = encode(hash256(self.serialize())[::-1], 'hex_codec').decode('ascii')
|
||||
|
||||
def is_valid(self):
|
||||
self.calc_sha256()
|
||||
|
@ -856,7 +857,7 @@ class CBlockHeader(object):
|
|||
r += ser_uint256(self.nNonce)
|
||||
r += ser_char_vector(self.nSolution)
|
||||
self.sha256 = uint256_from_str(hash256(r))
|
||||
self.hash = hash256(r)[::-1].hex()
|
||||
self.hash = encode(hash256(r)[::-1], 'hex_codec').decode('ascii')
|
||||
|
||||
def rehash(self):
|
||||
self.sha256 = None
|
||||
|
|
|
@ -4,6 +4,7 @@
|
|||
# file COPYING or https://www.opensource.org/licenses/mit-license.php .
|
||||
|
||||
# Linux network utilities
|
||||
|
||||
import sys
|
||||
import socket
|
||||
import struct
|
||||
|
|
|
@ -23,6 +23,8 @@ if sys.version > '3':
|
|||
bord = lambda x: x
|
||||
|
||||
from pyblake2 import blake2b
|
||||
|
||||
from binascii import hexlify
|
||||
import struct
|
||||
|
||||
from test_framework.bignum import bn2vch
|
||||
|
@ -627,7 +629,7 @@ class CScriptNum(object):
|
|||
neg = obj.value < 0
|
||||
absvalue = -obj.value if neg else obj.value
|
||||
while (absvalue):
|
||||
r.append(chr(absvalue & 0xff))
|
||||
r.append(absvalue & 0xff)
|
||||
absvalue >>= 8
|
||||
if r[-1] & 0x80:
|
||||
r.append(0x80 if neg else 0)
|
||||
|
@ -775,7 +777,7 @@ class CScript(bytes):
|
|||
# need to change
|
||||
def _repr(o):
|
||||
if isinstance(o, bytes):
|
||||
return "x('%s')" % o.hex().decode('ascii')
|
||||
return b"x('%s')" % hexlify(o).decode('ascii')
|
||||
else:
|
||||
return repr(o)
|
||||
|
||||
|
|
Loading…
Reference in New Issue