Skip to content

Commit fa6bf21

Browse files
author
MarcoFalke
committed
scripted-diff: test: Use py3.5 bytes::hex() method
-BEGIN VERIFY SCRIPT- sed -i -e "s/def bytes_to_hex_str/def b_2_x/g" $(git grep -l bytes_to_hex_str) export RE_B_0="[^()]*" # match no bracket export RE_B_1="${RE_B_0}\(${RE_B_0}\)${RE_B_0}" # match exactly one () export RE_B_2="${RE_B_0}\(${RE_B_1}\)${RE_B_0}" # match wrapped (()) export RE_M="(b2x|bytes_to_hex_str)\(((${RE_B_0}|${RE_B_1}|${RE_B_2})*)\)" sed -i --regexp-extended -e "s/${RE_M}/\2.hex()/g" $(git grep -l -E '(b2x|bytes_to_hex_str)') sed -i --regexp-extended -e "/ +bytes_to_hex_str( as b2x)?,/d" $(git grep -l bytes_to_hex_str) sed -i --regexp-extended -e "s/ +bytes_to_hex_str( as b2x)?,//g" $(git grep -l bytes_to_hex_str) sed -i --regexp-extended -e "s/, bytes_to_hex_str( as b2x)?//g" $(git grep -l bytes_to_hex_str) export RE_M="(binascii\.)?hexlify\(((${RE_B_0}|${RE_B_1}|${RE_B_2})*)\).decode\(${RE_B_0}\)" sed -i --regexp-extended -e "s/${RE_M}/\2.hex()/g" $(git grep -l hexlify -- ':(exclude)share') sed -i --regexp-extended -e "/from binascii import hexlify$/d" $(git grep -l hexlify -- ':(exclude)share') sed -i --regexp-extended -e "s/(from binascii import) .*hexlify/\1 unhexlify/g" $(git grep -l hexlify -- ':(exclude)share') sed -i -e 's/ignore-names "/ignore-names "b_2_x,/g' ./test/lint/lint-python-dead-code.sh -END VERIFY SCRIPT-
1 parent 9e3122d commit fa6bf21

26 files changed

+134
-146
lines changed

contrib/linearize/linearize-data.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
import datetime
1717
import time
1818
from collections import namedtuple
19-
from binascii import hexlify, unhexlify
19+
from binascii import unhexlify
2020

2121
settings = {}
2222

@@ -61,7 +61,7 @@ def calc_hash_str(blk_hdr):
6161
hash = calc_hdr_hash(blk_hdr)
6262
hash = bufreverse(hash)
6363
hash = wordreverse(hash)
64-
hash_str = hexlify(hash).decode('utf-8')
64+
hash_str = hash.hex()
6565
return hash_str
6666

6767
def get_blk_dt(blk_hdr):
@@ -213,7 +213,7 @@ def run(self):
213213

214214
inMagic = inhdr[:4]
215215
if (inMagic != self.settings['netmagic']):
216-
print("Invalid magic: " + hexlify(inMagic).decode('utf-8'))
216+
print("Invalid magic: " + inMagic.hex())
217217
return
218218
inLenLE = inhdr[4:]
219219
su = struct.unpack("<I", inLenLE)

test/functional/feature_bip68_sequence.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from test_framework.messages import COIN, COutPoint, CTransaction, CTxIn, CTxOut, FromHex, ToHex
1111
from test_framework.script import CScript
1212
from test_framework.test_framework import BitcoinTestFramework
13-
from test_framework.util import assert_equal, assert_greater_than, assert_raises_rpc_error, bytes_to_hex_str, get_bip9_status, satoshi_round, sync_blocks
13+
from test_framework.util import assert_equal, assert_greater_than, assert_raises_rpc_error, get_bip9_status, satoshi_round, sync_blocks
1414

1515
SEQUENCE_LOCKTIME_DISABLE_FLAG = (1<<31)
1616
SEQUENCE_LOCKTIME_TYPE_FLAG = (1<<22) # this means use time (0 means height)
@@ -372,7 +372,7 @@ def test_bip68_not_consensus(self):
372372
add_witness_commitment(block)
373373
block.solve()
374374

375-
self.nodes[0].submitblock(bytes_to_hex_str(block.serialize(True)))
375+
self.nodes[0].submitblock(block.serialize(True).hex())
376376
assert_equal(self.nodes[0].getbestblockhash(), block.hash)
377377

378378
def activateCSV(self):

test/functional/feature_cltv.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
from test_framework.test_framework import BitcoinTestFramework
1616
from test_framework.util import (
1717
assert_equal,
18-
bytes_to_hex_str,
1918
hex_str_to_bytes,
2019
)
2120

@@ -114,7 +113,7 @@ def run_test(self):
114113
# rejected from the mempool for exactly that reason.
115114
assert_equal(
116115
[{'txid': spendtx.hash, 'allowed': False, 'reject-reason': '64: non-mandatory-script-verify-flag (Negative locktime)'}],
117-
self.nodes[0].testmempoolaccept(rawtxs=[bytes_to_hex_str(spendtx.serialize())], allowhighfees=True)
116+
self.nodes[0].testmempoolaccept(rawtxs=[spendtx.serialize().hex()], allowhighfees=True)
118117
)
119118

120119
# Now we verify that a block with this transaction is also invalid.

test/functional/feature_dersig.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
from test_framework.test_framework import BitcoinTestFramework
1515
from test_framework.util import (
1616
assert_equal,
17-
bytes_to_hex_str,
1817
wait_until,
1918
)
2019

@@ -103,7 +102,7 @@ def run_test(self):
103102
# rejected from the mempool for exactly that reason.
104103
assert_equal(
105104
[{'txid': spendtx.hash, 'allowed': False, 'reject-reason': '64: non-mandatory-script-verify-flag (Non-canonical DER signature)'}],
106-
self.nodes[0].testmempoolaccept(rawtxs=[bytes_to_hex_str(spendtx.serialize())], allowhighfees=True)
105+
self.nodes[0].testmempoolaccept(rawtxs=[spendtx.serialize().hex()], allowhighfees=True)
107106
)
108107

109108
# Now we verify that a block with this transaction is also invalid.

test/functional/feature_nulldummy.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
from test_framework.messages import CTransaction
1919
from test_framework.script import CScript
2020
from test_framework.test_framework import BitcoinTestFramework
21-
from test_framework.util import assert_equal, assert_raises_rpc_error, bytes_to_hex_str
21+
from test_framework.util import assert_equal, assert_raises_rpc_error
2222

2323
NULLDUMMY_ERROR = "non-mandatory-script-verify-flag (Dummy CHECKMULTISIG argument must be zero) (code 64)"
2424

@@ -64,17 +64,17 @@ def run_test(self):
6464

6565
self.log.info("Test 1: NULLDUMMY compliant base transactions should be accepted to mempool and mined before activation [430]")
6666
test1txs = [create_transaction(self.nodes[0], coinbase_txid[0], self.ms_address, amount=49)]
67-
txid1 = self.nodes[0].sendrawtransaction(bytes_to_hex_str(test1txs[0].serialize_with_witness()), True)
67+
txid1 = self.nodes[0].sendrawtransaction(test1txs[0].serialize_with_witness().hex(), True)
6868
test1txs.append(create_transaction(self.nodes[0], txid1, self.ms_address, amount=48))
69-
txid2 = self.nodes[0].sendrawtransaction(bytes_to_hex_str(test1txs[1].serialize_with_witness()), True)
69+
txid2 = self.nodes[0].sendrawtransaction(test1txs[1].serialize_with_witness().hex(), True)
7070
test1txs.append(create_transaction(self.nodes[0], coinbase_txid[1], self.wit_ms_address, amount=49))
71-
txid3 = self.nodes[0].sendrawtransaction(bytes_to_hex_str(test1txs[2].serialize_with_witness()), True)
71+
txid3 = self.nodes[0].sendrawtransaction(test1txs[2].serialize_with_witness().hex(), True)
7272
self.block_submit(self.nodes[0], test1txs, False, True)
7373

7474
self.log.info("Test 2: Non-NULLDUMMY base multisig transaction should not be accepted to mempool before activation")
7575
test2tx = create_transaction(self.nodes[0], txid2, self.ms_address, amount=47)
7676
trueDummy(test2tx)
77-
assert_raises_rpc_error(-26, NULLDUMMY_ERROR, self.nodes[0].sendrawtransaction, bytes_to_hex_str(test2tx.serialize_with_witness()), True)
77+
assert_raises_rpc_error(-26, NULLDUMMY_ERROR, self.nodes[0].sendrawtransaction, test2tx.serialize_with_witness().hex(), True)
7878

7979
self.log.info("Test 3: Non-NULLDUMMY base transactions should be accepted in a block before activation [431]")
8080
self.block_submit(self.nodes[0], [test2tx], False, True)
@@ -83,19 +83,19 @@ def run_test(self):
8383
test4tx = create_transaction(self.nodes[0], test2tx.hash, self.address, amount=46)
8484
test6txs = [CTransaction(test4tx)]
8585
trueDummy(test4tx)
86-
assert_raises_rpc_error(-26, NULLDUMMY_ERROR, self.nodes[0].sendrawtransaction, bytes_to_hex_str(test4tx.serialize_with_witness()), True)
86+
assert_raises_rpc_error(-26, NULLDUMMY_ERROR, self.nodes[0].sendrawtransaction, test4tx.serialize_with_witness().hex(), True)
8787
self.block_submit(self.nodes[0], [test4tx])
8888

8989
self.log.info("Test 5: Non-NULLDUMMY P2WSH multisig transaction invalid after activation")
9090
test5tx = create_transaction(self.nodes[0], txid3, self.wit_address, amount=48)
9191
test6txs.append(CTransaction(test5tx))
9292
test5tx.wit.vtxinwit[0].scriptWitness.stack[0] = b'\x01'
93-
assert_raises_rpc_error(-26, NULLDUMMY_ERROR, self.nodes[0].sendrawtransaction, bytes_to_hex_str(test5tx.serialize_with_witness()), True)
93+
assert_raises_rpc_error(-26, NULLDUMMY_ERROR, self.nodes[0].sendrawtransaction, test5tx.serialize_with_witness().hex(), True)
9494
self.block_submit(self.nodes[0], [test5tx], True)
9595

9696
self.log.info("Test 6: NULLDUMMY compliant base/witness transactions should be accepted to mempool and in block after activation [432]")
9797
for i in test6txs:
98-
self.nodes[0].sendrawtransaction(bytes_to_hex_str(i.serialize_with_witness()), True)
98+
self.nodes[0].sendrawtransaction(i.serialize_with_witness().hex(), True)
9999
self.block_submit(self.nodes[0], test6txs, True, True)
100100

101101
def block_submit(self, node, txs, witness=False, accept=False):
@@ -108,7 +108,7 @@ def block_submit(self, node, txs, witness=False, accept=False):
108108
witness and add_witness_commitment(block)
109109
block.rehash()
110110
block.solve()
111-
node.submitblock(bytes_to_hex_str(block.serialize(True)))
111+
node.submitblock(block.serialize(True).hex())
112112
if (accept):
113113
assert_equal(node.getbestblockhash(), block.hash)
114114
self.tip = block.sha256

test/functional/feature_rbf.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@
99
from test_framework.messages import COIN, COutPoint, CTransaction, CTxIn, CTxOut
1010
from test_framework.script import CScript, OP_DROP
1111
from test_framework.test_framework import BitcoinTestFramework
12-
from test_framework.util import assert_equal, assert_raises_rpc_error, bytes_to_hex_str, satoshi_round
12+
from test_framework.util import assert_equal, assert_raises_rpc_error, satoshi_round
1313

1414
MAX_REPLACEMENT_LIMIT = 100
1515

1616
def txToHex(tx):
17-
return bytes_to_hex_str(tx.serialize())
17+
return tx.serialize().hex()
1818

1919
def make_utxo(node, amount, confirmed=True, scriptPubKey=CScript([1])):
2020
"""Create a txout with a given amount and scriptPubKey

test/functional/feature_segwit.py

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
from test_framework.messages import COIN, COutPoint, CTransaction, CTxIn, CTxOut, FromHex, sha256, ToHex
1919
from test_framework.script import CScript, OP_HASH160, OP_CHECKSIG, OP_0, hash160, OP_EQUAL, OP_DUP, OP_EQUALVERIFY, OP_1, OP_2, OP_CHECKMULTISIG, OP_TRUE, OP_DROP
2020
from test_framework.test_framework import BitcoinTestFramework
21-
from test_framework.util import assert_equal, assert_raises_rpc_error, bytes_to_hex_str, connect_nodes, hex_str_to_bytes, sync_blocks, try_rpc
21+
from test_framework.util import assert_equal, assert_raises_rpc_error, connect_nodes, hex_str_to_bytes, sync_blocks, try_rpc
2222

2323
NODE_0 = 0
2424
NODE_2 = 2
@@ -181,7 +181,7 @@ def run_test(self):
181181
assert(self.nodes[1].getrawtransaction(tx_id, False, blockhash) == self.nodes[2].getrawtransaction(tx_id, False, blockhash))
182182
assert(self.nodes[0].getrawtransaction(tx_id, False, blockhash) != self.nodes[2].gettransaction(tx_id)["hex"])
183183
assert(self.nodes[1].getrawtransaction(tx_id, False, blockhash) == self.nodes[2].gettransaction(tx_id)["hex"])
184-
assert(self.nodes[0].getrawtransaction(tx_id, False, blockhash) == bytes_to_hex_str(tx.serialize_without_witness()))
184+
assert(self.nodes[0].getrawtransaction(tx_id, False, blockhash) == tx.serialize_without_witness().hex())
185185

186186
self.log.info("Verify witness txs without witness data are invalid after the fork")
187187
self.fail_accept(self.nodes[2], 'non-mandatory-script-verify-flag (Witness program hash mismatch) (code 64)', wit_ids[NODE_2][WIT_V0][2], sign=False)
@@ -392,22 +392,22 @@ def run_test(self):
392392
v = self.nodes[0].getaddressinfo(i)
393393
if (v['isscript']):
394394
bare = hex_str_to_bytes(v['hex'])
395-
importlist.append(bytes_to_hex_str(bare))
396-
importlist.append(bytes_to_hex_str(CScript([OP_0, sha256(bare)])))
395+
importlist.append(bare.hex())
396+
importlist.append(CScript([OP_0, sha256(bare)]).hex())
397397
else:
398398
pubkey = hex_str_to_bytes(v['pubkey'])
399399
p2pk = CScript([pubkey, OP_CHECKSIG])
400400
p2pkh = CScript([OP_DUP, OP_HASH160, hash160(pubkey), OP_EQUALVERIFY, OP_CHECKSIG])
401-
importlist.append(bytes_to_hex_str(p2pk))
402-
importlist.append(bytes_to_hex_str(p2pkh))
403-
importlist.append(bytes_to_hex_str(CScript([OP_0, hash160(pubkey)])))
404-
importlist.append(bytes_to_hex_str(CScript([OP_0, sha256(p2pk)])))
405-
importlist.append(bytes_to_hex_str(CScript([OP_0, sha256(p2pkh)])))
401+
importlist.append(p2pk.hex())
402+
importlist.append(p2pkh.hex())
403+
importlist.append(CScript([OP_0, hash160(pubkey)]).hex())
404+
importlist.append(CScript([OP_0, sha256(p2pk)]).hex())
405+
importlist.append(CScript([OP_0, sha256(p2pkh)]).hex())
406406

407-
importlist.append(bytes_to_hex_str(unsolvablep2pkh))
408-
importlist.append(bytes_to_hex_str(unsolvablep2wshp2pkh))
409-
importlist.append(bytes_to_hex_str(op1))
410-
importlist.append(bytes_to_hex_str(p2wshop1))
407+
importlist.append(unsolvablep2pkh.hex())
408+
importlist.append(unsolvablep2wshp2pkh.hex())
409+
importlist.append(op1.hex())
410+
importlist.append(p2wshop1.hex())
411411

412412
for i in importlist:
413413
# import all generated addresses. The wallet already has the private keys for some of these, so catch JSON RPC
@@ -535,7 +535,7 @@ def mine_and_test_listunspent(self, script_list, ismine):
535535
for i in script_list:
536536
tx.vout.append(CTxOut(10000000, i))
537537
tx.rehash()
538-
signresults = self.nodes[0].signrawtransactionwithwallet(bytes_to_hex_str(tx.serialize_without_witness()))['hex']
538+
signresults = self.nodes[0].signrawtransactionwithwallet(tx.serialize_without_witness().hex())['hex']
539539
txid = self.nodes[0].sendrawtransaction(signresults, True)
540540
txs_mined[txid] = self.nodes[0].generate(1)[0]
541541
sync_blocks(self.nodes)
@@ -587,7 +587,7 @@ def create_and_mine_tx_from_txids(self, txids, success=True):
587587
tx.vin.append(CTxIn(COutPoint(int('0x' + i, 0), j)))
588588
tx.vout.append(CTxOut(0, CScript()))
589589
tx.rehash()
590-
signresults = self.nodes[0].signrawtransactionwithwallet(bytes_to_hex_str(tx.serialize_without_witness()))['hex']
590+
signresults = self.nodes[0].signrawtransactionwithwallet(tx.serialize_without_witness().hex())['hex']
591591
self.nodes[0].sendrawtransaction(signresults, True)
592592
self.nodes[0].generate(1)
593593
sync_blocks(self.nodes)

test/functional/interface_rest.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ def run_test(self):
152152
bin_response = self.test_rest_request("/getutxos", http_method='POST', req_type=ReqType.BIN, body=bin_request, ret_type=RetType.BYTES)
153153
output = BytesIO(bin_response)
154154
chain_height, = unpack("i", output.read(4))
155-
response_hash = binascii.hexlify(output.read(32)[::-1]).decode('ascii')
155+
response_hash = output.read(32)[::-1].hex()
156156

157157
assert_equal(bb_hash, response_hash) # check if getutxo's chaintip during calculation was fine
158158
assert_equal(chain_height, 102) # chain height must be 102
@@ -252,7 +252,7 @@ def run_test(self):
252252
resp_hex = self.test_rest_request("/blockhashbyheight/{}".format(block_json_obj['height']), req_type=ReqType.HEX, ret_type=RetType.OBJ)
253253
assert_equal(resp_hex.read().decode('utf-8').rstrip(), bb_hash)
254254
resp_bytes = self.test_rest_request("/blockhashbyheight/{}".format(block_json_obj['height']), req_type=ReqType.BIN, ret_type=RetType.BYTES)
255-
blockhash = binascii.hexlify(resp_bytes[::-1]).decode('utf-8')
255+
blockhash = resp_bytes[::-1].hex()
256256
assert_equal(blockhash, bb_hash)
257257

258258
# Check invalid blockhashbyheight requests

test/functional/interface_zmq.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
from test_framework.messages import CTransaction
1111
from test_framework.util import (
1212
assert_equal,
13-
bytes_to_hex_str,
1413
hash256,
1514
)
1615
from io import BytesIO
@@ -94,17 +93,17 @@ def _zmq_test(self):
9493
tx = CTransaction()
9594
tx.deserialize(BytesIO(hex))
9695
tx.calc_sha256()
97-
assert_equal(tx.hash, bytes_to_hex_str(txid))
96+
assert_equal(tx.hash, txid.hex())
9897

9998
# Should receive the generated block hash.
100-
hash = bytes_to_hex_str(self.hashblock.receive())
99+
hash = self.hashblock.receive().hex()
101100
assert_equal(genhashes[x], hash)
102101
# The block should only have the coinbase txid.
103-
assert_equal([bytes_to_hex_str(txid)], self.nodes[1].getblock(hash)["tx"])
102+
assert_equal([txid.hex()], self.nodes[1].getblock(hash)["tx"])
104103

105104
# Should receive the generated raw block.
106105
block = self.rawblock.receive()
107-
assert_equal(genhashes[x], bytes_to_hex_str(hash256(block[:80])))
106+
assert_equal(genhashes[x], hash256(block[:80]).hex())
108107

109108
if self.is_wallet_compiled():
110109
self.log.info("Wait for tx from second node")
@@ -113,11 +112,11 @@ def _zmq_test(self):
113112

114113
# Should receive the broadcasted txid.
115114
txid = self.hashtx.receive()
116-
assert_equal(payment_txid, bytes_to_hex_str(txid))
115+
assert_equal(payment_txid, txid.hex())
117116

118117
# Should receive the broadcasted raw transaction.
119118
hex = self.rawtx.receive()
120-
assert_equal(payment_txid, bytes_to_hex_str(hash256(hex)))
119+
assert_equal(payment_txid, hash256(hex).hex())
121120

122121

123122
self.log.info("Test the getzmqnotifications RPC")

0 commit comments

Comments
 (0)