Skip to content

Commit faa41ee

Browse files
author
MarcoFalke
committed
[qa] py2: Unfiddle strings into bytes explicitly
1 parent 0afac87 commit faa41ee

28 files changed

+205
-213
lines changed

qa/rpc-tests/bip65-cltv-p2p.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
from test_framework.blocktools import create_coinbase, create_block
1111
from test_framework.comptool import TestInstance, TestManager
1212
from test_framework.script import CScript, OP_1NEGATE, OP_CHECKLOCKTIMEVERIFY, OP_DROP
13-
from binascii import unhexlify
1413
from io import BytesIO
1514
import time
1615

@@ -60,7 +59,7 @@ def create_transaction(self, node, coinbase, to_address, amount):
6059
rawtx = node.createrawtransaction(inputs, outputs)
6160
signresult = node.signrawtransaction(rawtx)
6261
tx = CTransaction()
63-
f = BytesIO(unhexlify(signresult['hex']))
62+
f = BytesIO(hex_str_to_bytes(signresult['hex']))
6463
tx.deserialize(f)
6564
return tx
6665

qa/rpc-tests/bip68-112-113-p2p.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
from test_framework.blocktools import create_coinbase, create_block
1111
from test_framework.comptool import TestInstance, TestManager
1212
from test_framework.script import *
13-
from binascii import unhexlify
1413
from io import BytesIO
1514
import time
1615

@@ -119,15 +118,15 @@ def create_transaction(self, node, txid, to_address, amount):
119118
outputs = { to_address : amount }
120119
rawtx = node.createrawtransaction(inputs, outputs)
121120
tx = CTransaction()
122-
f = BytesIO(unhexlify(rawtx))
121+
f = BytesIO(hex_str_to_bytes(rawtx))
123122
tx.deserialize(f)
124123
return tx
125124

126125
def sign_transaction(self, node, unsignedtx):
127126
rawtx = ToHex(unsignedtx)
128127
signresult = node.signrawtransaction(rawtx)
129128
tx = CTransaction()
130-
f = BytesIO(unhexlify(signresult['hex']))
129+
f = BytesIO(hex_str_to_bytes(signresult['hex']))
131130
tx.deserialize(f)
132131
return tx
133132

qa/rpc-tests/bip9-softforks.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
from test_framework.blocktools import create_coinbase, create_block
1111
from test_framework.comptool import TestInstance, TestManager
1212
from test_framework.script import CScript, OP_1NEGATE, OP_NOP3, OP_DROP
13-
from binascii import hexlify, unhexlify
1413
from io import BytesIO
1514
import time
1615
import itertools
@@ -30,7 +29,6 @@
3029
'''
3130

3231

33-
3432
class BIP9SoftForksTest(ComparisonTestFramework):
3533

3634
def __init__(self):
@@ -53,15 +51,15 @@ def create_transaction(self, node, coinbase, to_address, amount):
5351
outputs = { to_address : amount }
5452
rawtx = node.createrawtransaction(inputs, outputs)
5553
tx = CTransaction()
56-
f = BytesIO(unhexlify(rawtx))
54+
f = BytesIO(hex_str_to_bytes(rawtx))
5755
tx.deserialize(f)
5856
tx.nVersion = 2
5957
return tx
6058

6159
def sign_transaction(self, node, tx):
62-
signresult = node.signrawtransaction(hexlify(tx.serialize()))
60+
signresult = node.signrawtransaction(bytes_to_hex_str(tx.serialize()))
6361
tx = CTransaction()
64-
f = BytesIO(unhexlify(signresult['hex']))
62+
f = BytesIO(hex_str_to_bytes(signresult['hex']))
6563
tx.deserialize(f)
6664
return tx
6765

@@ -184,7 +182,6 @@ def test_BIP(self, bipName, activated_version, invalidate, invalidatePostSignatu
184182
NetworkThread().start() # Start up network handling in another thread
185183

186184

187-
188185
def get_tests(self):
189186
for test in itertools.chain(
190187
self.test_BIP('csv', 536870913, self.sequence_lock_invalidate, self.donothing),

qa/rpc-tests/bipdersig-p2p.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
from test_framework.blocktools import create_coinbase, create_block
1111
from test_framework.comptool import TestInstance, TestManager
1212
from test_framework.script import CScript
13-
from binascii import unhexlify
1413
from io import BytesIO
1514
import time
1615

@@ -25,7 +24,7 @@ def unDERify(tx):
2524
newscript = []
2625
for i in scriptSig:
2726
if (len(newscript) == 0):
28-
newscript.append(i[0:-1] + '\0' + i[-1])
27+
newscript.append(i[0:-1] + b'\0' + i[-1:])
2928
else:
3029
newscript.append(i)
3130
tx.vin[0].scriptSig = CScript(newscript)
@@ -68,7 +67,7 @@ def create_transaction(self, node, coinbase, to_address, amount):
6867
rawtx = node.createrawtransaction(inputs, outputs)
6968
signresult = node.signrawtransaction(rawtx)
7069
tx = CTransaction()
71-
f = BytesIO(unhexlify(signresult['hex']))
70+
f = BytesIO(hex_str_to_bytes(signresult['hex']))
7271
tx.deserialize(f)
7372
return tx
7473

qa/rpc-tests/decodescript.py

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
from test_framework.test_framework import BitcoinTestFramework
77
from test_framework.util import *
88
from test_framework.mininode import *
9-
from binascii import hexlify, unhexlify
109
from io import BytesIO
1110

1211
class DecodeScriptTest(BitcoinTestFramework):
@@ -131,7 +130,7 @@ def decoderawtransaction_asm_sighashtype(self):
131130
assert_equal('OP_DUP OP_HASH160 dc863734a218bfe83ef770ee9d41a27f824a6e56 OP_EQUALVERIFY OP_CHECKSIG', rpc_result['vout'][0]['scriptPubKey']['asm'])
132131
assert_equal('OP_HASH160 2a5edea39971049a540474c6a99edf0aa4074c58 OP_EQUAL', rpc_result['vout'][1]['scriptPubKey']['asm'])
133132
txSave = CTransaction()
134-
txSave.deserialize(BytesIO(unhexlify(tx)))
133+
txSave.deserialize(BytesIO(hex_str_to_bytes(tx)))
135134

136135
# make sure that a specifically crafted op_return value will not pass all the IsDERSignature checks and then get decoded as a sighash type
137136
tx = '01000000015ded05872fdbda629c7d3d02b194763ce3b9b1535ea884e3c8e765d42e316724020000006b48304502204c10d4064885c42638cbff3585915b322de33762598321145ba033fc796971e2022100bb153ad3baa8b757e30a2175bd32852d2e1cb9080f84d7e32fcdfd667934ef1b012103163c0ff73511ea1743fb5b98384a2ff09dd06949488028fd819f4d83f56264efffffffff0200000000000000000b6a0930060201000201000180380100000000001976a9141cabd296e753837c086da7a45a6c2fe0d49d7b7b88ac00000000'
@@ -147,7 +146,7 @@ def decoderawtransaction_asm_sighashtype(self):
147146
# some more full transaction tests of varying specific scriptSigs. used instead of
148147
# tests in decodescript_script_sig because the decodescript RPC is specifically
149148
# for working on scriptPubKeys (argh!).
150-
push_signature = hexlify(txSave.vin[0].scriptSig)[2:(0x48*2+4)]
149+
push_signature = bytes_to_hex_str(txSave.vin[0].scriptSig)[2:(0x48*2+4)]
151150
signature = push_signature[2:]
152151
der_signature = signature[:-2]
153152
signature_sighash_decoded = der_signature + '[ALL]'
@@ -156,25 +155,24 @@ def decoderawtransaction_asm_sighashtype(self):
156155
signature_2_sighash_decoded = der_signature + '[NONE|ANYONECANPAY]'
157156

158157
# 1) P2PK scriptSig
159-
txSave.vin[0].scriptSig = unhexlify(push_signature)
160-
rpc_result = self.nodes[0].decoderawtransaction(hexlify(txSave.serialize()))
158+
txSave.vin[0].scriptSig = hex_str_to_bytes(push_signature)
159+
rpc_result = self.nodes[0].decoderawtransaction(bytes_to_hex_str(txSave.serialize()))
161160
assert_equal(signature_sighash_decoded, rpc_result['vin'][0]['scriptSig']['asm'])
162161

163162
# make sure that the sighash decodes come out correctly for a more complex / lesser used case.
164-
txSave.vin[0].scriptSig = unhexlify(push_signature_2)
165-
rpc_result = self.nodes[0].decoderawtransaction(hexlify(txSave.serialize()))
163+
txSave.vin[0].scriptSig = hex_str_to_bytes(push_signature_2)
164+
rpc_result = self.nodes[0].decoderawtransaction(bytes_to_hex_str(txSave.serialize()))
166165
assert_equal(signature_2_sighash_decoded, rpc_result['vin'][0]['scriptSig']['asm'])
167166

168167
# 2) multisig scriptSig
169-
txSave.vin[0].scriptSig = unhexlify('00' + push_signature + push_signature_2)
170-
rpc_result = self.nodes[0].decoderawtransaction(hexlify(txSave.serialize()))
168+
txSave.vin[0].scriptSig = hex_str_to_bytes('00' + push_signature + push_signature_2)
169+
rpc_result = self.nodes[0].decoderawtransaction(bytes_to_hex_str(txSave.serialize()))
171170
assert_equal('0 ' + signature_sighash_decoded + ' ' + signature_2_sighash_decoded, rpc_result['vin'][0]['scriptSig']['asm'])
172171

173172
# 3) test a scriptSig that contains more than push operations.
174173
# in fact, it contains an OP_RETURN with data specially crafted to cause improper decode if the code does not catch it.
175-
txSave.vin[0].scriptSig = unhexlify('6a143011020701010101010101020601010101010101')
176-
rpc_result = self.nodes[0].decoderawtransaction(hexlify(txSave.serialize()))
177-
print(hexlify('636174'))
174+
txSave.vin[0].scriptSig = hex_str_to_bytes('6a143011020701010101010101020601010101010101')
175+
rpc_result = self.nodes[0].decoderawtransaction(bytes_to_hex_str(txSave.serialize()))
178176
assert_equal('OP_RETURN 3011020701010101010101020601010101010101', rpc_result['vin'][0]['scriptSig']['asm'])
179177

180178
def run_test(self):

qa/rpc-tests/fundrawtransaction.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,6 @@ def run_test(self):
148148
assert_equal(fee + totalOut, utx['amount']) #compare vin total and totalout+fee
149149

150150

151-
152151
#####################################################################
153152
# test a fundrawtransaction with which will not get a change output #
154153
#####################################################################
@@ -178,7 +177,6 @@ def run_test(self):
178177
assert_equal(fee + totalOut, utx['amount']) #compare vin total and totalout+fee
179178

180179

181-
182180
#########################################################################
183181
# test a fundrawtransaction with a VIN smaller than the required amount #
184182
#########################################################################
@@ -484,7 +482,6 @@ def run_test(self):
484482
assert_equal(oldBalance+Decimal('51.10000000'), self.nodes[0].getbalance())
485483

486484

487-
488485
###############################################
489486
# multiple (~19) inputs tx test | Compare fee #
490487
###############################################

qa/rpc-tests/getblocktemplate_proposals.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ def run_test(self):
130130

131131
# Test 5: Add an invalid tx to the end (non-duplicate)
132132
txlist.append(bytearray(txlist[0]))
133-
txlist[-1][4+1] = b'\xff'
133+
txlist[-1][4+1] = 0xff
134134
assert_template(node, tmpl, txlist, 'bad-txns-inputs-missingorspent')
135135
txlist.pop()
136136

qa/rpc-tests/httpbasics.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99

1010
from test_framework.test_framework import BitcoinTestFramework
1111
from test_framework.util import *
12-
import base64
1312

1413
try:
1514
import http.client as httplib
@@ -31,7 +30,7 @@ def run_test(self):
3130
#################################################
3231
url = urlparse.urlparse(self.nodes[0].url)
3332
authpair = url.username + ':' + url.password
34-
headers = {"Authorization": "Basic " + base64.b64encode(authpair)}
33+
headers = {"Authorization": "Basic " + str_to_b64str(authpair)}
3534

3635
conn = httplib.HTTPConnection(url.hostname, url.port)
3736
conn.connect()
@@ -48,7 +47,7 @@ def run_test(self):
4847
conn.close()
4948

5049
#same should be if we add keep-alive because this should be the std. behaviour
51-
headers = {"Authorization": "Basic " + base64.b64encode(authpair), "Connection": "keep-alive"}
50+
headers = {"Authorization": "Basic " + str_to_b64str(authpair), "Connection": "keep-alive"}
5251

5352
conn = httplib.HTTPConnection(url.hostname, url.port)
5453
conn.connect()
@@ -65,7 +64,7 @@ def run_test(self):
6564
conn.close()
6665

6766
#now do the same with "Connection: close"
68-
headers = {"Authorization": "Basic " + base64.b64encode(authpair), "Connection":"close"}
67+
headers = {"Authorization": "Basic " + str_to_b64str(authpair), "Connection":"close"}
6968

7069
conn = httplib.HTTPConnection(url.hostname, url.port)
7170
conn.connect()
@@ -77,7 +76,7 @@ def run_test(self):
7776
#node1 (2nd node) is running with disabled keep-alive option
7877
urlNode1 = urlparse.urlparse(self.nodes[1].url)
7978
authpair = urlNode1.username + ':' + urlNode1.password
80-
headers = {"Authorization": "Basic " + base64.b64encode(authpair)}
79+
headers = {"Authorization": "Basic " + str_to_b64str(authpair)}
8180

8281
conn = httplib.HTTPConnection(urlNode1.hostname, urlNode1.port)
8382
conn.connect()
@@ -88,7 +87,7 @@ def run_test(self):
8887
#node2 (third node) is running with standard keep-alive parameters which means keep-alive is on
8988
urlNode2 = urlparse.urlparse(self.nodes[2].url)
9089
authpair = urlNode2.username + ':' + urlNode2.password
91-
headers = {"Authorization": "Basic " + base64.b64encode(authpair)}
90+
headers = {"Authorization": "Basic " + str_to_b64str(authpair)}
9291

9392
conn = httplib.HTTPConnection(urlNode2.hostname, urlNode2.port)
9493
conn.connect()

qa/rpc-tests/invalidblockrequest.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,9 @@ def get_tests(self):
7777
block2 = create_block(self.tip, create_coinbase(height), self.block_time)
7878
self.block_time += 1
7979

80-
# chr(81) is OP_TRUE
81-
tx1 = create_transaction(self.block1.vtx[0], 0, chr(81), 50 * COIN)
82-
tx2 = create_transaction(tx1, 0, chr(81), 50 * COIN)
80+
# b'0x51' is OP_TRUE
81+
tx1 = create_transaction(self.block1.vtx[0], 0, b'\x51', 50 * COIN)
82+
tx2 = create_transaction(tx1, 0, b'\x51', 50 * COIN)
8383

8484
block2.vtx.extend([tx1, tx2])
8585
block2.hashMerkleRoot = block2.calc_merkle_root()
@@ -95,7 +95,7 @@ def get_tests(self):
9595
assert(block2_orig.vtx != block2.vtx)
9696

9797
self.tip = block2.sha256
98-
yield TestInstance([[block2, RejectResult(16,'bad-txns-duplicate')], [block2_orig, True]])
98+
yield TestInstance([[block2, RejectResult(16, b'bad-txns-duplicate')], [block2_orig, True]])
9999
height += 1
100100

101101
'''
@@ -110,7 +110,7 @@ def get_tests(self):
110110
block3.rehash()
111111
block3.solve()
112112

113-
yield TestInstance([[block3, RejectResult(16,'bad-cb-amount')]])
113+
yield TestInstance([[block3, RejectResult(16, b'bad-cb-amount')]])
114114

115115

116116
if __name__ == '__main__':

qa/rpc-tests/invalidtxrequest.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,10 @@ def get_tests(self):
6161
height += 1
6262
yield test
6363

64-
# chr(100) is OP_NOTIF
64+
# b'\x64' is OP_NOTIF
6565
# Transaction will be rejected with code 16 (REJECT_INVALID)
66-
tx1 = create_transaction(self.block1.vtx[0], 0, chr(100), 50 * COIN - 12000)
67-
yield TestInstance([[tx1, RejectResult(16, 'mandatory-script-verify-flag-failed')]])
66+
tx1 = create_transaction(self.block1.vtx[0], 0, b'\x64', 50 * COIN - 12000)
67+
yield TestInstance([[tx1, RejectResult(16, b'mandatory-script-verify-flag-failed')]])
6868

6969
# TODO: test further transactions...
7070

0 commit comments

Comments
 (0)