mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-12-02 16:59:44 +01:00
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-
This commit is contained in:
@@ -27,7 +27,6 @@ from test_framework.test_framework import BitcoinTestFramework
|
||||
from test_framework.util import (
|
||||
assert_equal,
|
||||
assert_raises_rpc_error,
|
||||
bytes_to_hex_str as b2x,
|
||||
connect_nodes_bi,
|
||||
)
|
||||
from test_framework.script import CScriptNum
|
||||
@@ -36,7 +35,7 @@ from test_framework.script import CScriptNum
|
||||
def assert_template(node, block, expect, rehash=True):
|
||||
if rehash:
|
||||
block.hashMerkleRoot = block.calc_merkle_root()
|
||||
rsp = node.getblocktemplate(template_request={'data': b2x(block.serialize()), 'mode': 'proposal', 'rules': ['segwit']})
|
||||
rsp = node.getblocktemplate(template_request={'data': block.serialize().hex(), 'mode': 'proposal', 'rules': ['segwit']})
|
||||
assert_equal(rsp, expect)
|
||||
|
||||
|
||||
@@ -64,8 +63,8 @@ class MiningTest(BitcoinTestFramework):
|
||||
def assert_submitblock(block, result_str_1, result_str_2=None):
|
||||
block.solve()
|
||||
result_str_2 = result_str_2 or 'duplicate-invalid'
|
||||
assert_equal(result_str_1, node.submitblock(hexdata=b2x(block.serialize())))
|
||||
assert_equal(result_str_2, node.submitblock(hexdata=b2x(block.serialize())))
|
||||
assert_equal(result_str_1, node.submitblock(hexdata=block.serialize().hex()))
|
||||
assert_equal(result_str_2, node.submitblock(hexdata=block.serialize().hex()))
|
||||
|
||||
self.log.info('getmininginfo')
|
||||
mining_info = node.getmininginfo()
|
||||
@@ -112,7 +111,7 @@ class MiningTest(BitcoinTestFramework):
|
||||
assert_template(node, block, None)
|
||||
|
||||
self.log.info("submitblock: Test block decode failure")
|
||||
assert_raises_rpc_error(-22, "Block decode failed", node.submitblock, b2x(block.serialize()[:-15]))
|
||||
assert_raises_rpc_error(-22, "Block decode failed", node.submitblock, block.serialize()[:-15].hex())
|
||||
|
||||
self.log.info("getblocktemplate: Test bad input hash for coinbase transaction")
|
||||
bad_block = copy.deepcopy(block)
|
||||
@@ -121,10 +120,10 @@ class MiningTest(BitcoinTestFramework):
|
||||
assert_template(node, bad_block, 'bad-cb-missing')
|
||||
|
||||
self.log.info("submitblock: Test invalid coinbase transaction")
|
||||
assert_raises_rpc_error(-22, "Block does not start with a coinbase", node.submitblock, b2x(bad_block.serialize()))
|
||||
assert_raises_rpc_error(-22, "Block does not start with a coinbase", node.submitblock, bad_block.serialize().hex())
|
||||
|
||||
self.log.info("getblocktemplate: Test truncated final transaction")
|
||||
assert_raises_rpc_error(-22, "Block decode failed", node.getblocktemplate, {'data': b2x(block.serialize()[:-1]), 'mode': 'proposal', 'rules': ['segwit']})
|
||||
assert_raises_rpc_error(-22, "Block decode failed", node.getblocktemplate, {'data': block.serialize()[:-1].hex(), 'mode': 'proposal', 'rules': ['segwit']})
|
||||
|
||||
self.log.info("getblocktemplate: Test duplicate transaction")
|
||||
bad_block = copy.deepcopy(block)
|
||||
@@ -153,7 +152,7 @@ class MiningTest(BitcoinTestFramework):
|
||||
bad_block_sn = bytearray(block.serialize())
|
||||
assert_equal(bad_block_sn[BLOCK_HEADER_SIZE], 1)
|
||||
bad_block_sn[BLOCK_HEADER_SIZE] += 1
|
||||
assert_raises_rpc_error(-22, "Block decode failed", node.getblocktemplate, {'data': b2x(bad_block_sn), 'mode': 'proposal', 'rules': ['segwit']})
|
||||
assert_raises_rpc_error(-22, "Block decode failed", node.getblocktemplate, {'data': bad_block_sn.hex(), 'mode': 'proposal', 'rules': ['segwit']})
|
||||
|
||||
self.log.info("getblocktemplate: Test bad bits")
|
||||
bad_block = copy.deepcopy(block)
|
||||
@@ -184,7 +183,7 @@ class MiningTest(BitcoinTestFramework):
|
||||
self.log.info('submitheader tests')
|
||||
assert_raises_rpc_error(-22, 'Block header decode failed', lambda: node.submitheader(hexdata='xx' * BLOCK_HEADER_SIZE))
|
||||
assert_raises_rpc_error(-22, 'Block header decode failed', lambda: node.submitheader(hexdata='ff' * (BLOCK_HEADER_SIZE-2)))
|
||||
assert_raises_rpc_error(-25, 'Must submit previous header', lambda: node.submitheader(hexdata=b2x(super(CBlock, bad_block).serialize())))
|
||||
assert_raises_rpc_error(-25, 'Must submit previous header', lambda: node.submitheader(hexdata=super(CBlock, bad_block).serialize().hex()))
|
||||
|
||||
block.nTime += 1
|
||||
block.solve()
|
||||
@@ -193,23 +192,23 @@ class MiningTest(BitcoinTestFramework):
|
||||
return {'hash': b_hash, 'height': 202, 'branchlen': branchlen, 'status': status}
|
||||
|
||||
assert chain_tip(block.hash) not in node.getchaintips()
|
||||
node.submitheader(hexdata=b2x(block.serialize()))
|
||||
node.submitheader(hexdata=block.serialize().hex())
|
||||
assert chain_tip(block.hash) in node.getchaintips()
|
||||
node.submitheader(hexdata=b2x(CBlockHeader(block).serialize())) # Noop
|
||||
node.submitheader(hexdata=CBlockHeader(block).serialize().hex()) # Noop
|
||||
assert chain_tip(block.hash) in node.getchaintips()
|
||||
|
||||
bad_block_root = copy.deepcopy(block)
|
||||
bad_block_root.hashMerkleRoot += 2
|
||||
bad_block_root.solve()
|
||||
assert chain_tip(bad_block_root.hash) not in node.getchaintips()
|
||||
node.submitheader(hexdata=b2x(CBlockHeader(bad_block_root).serialize()))
|
||||
node.submitheader(hexdata=CBlockHeader(bad_block_root).serialize().hex())
|
||||
assert chain_tip(bad_block_root.hash) in node.getchaintips()
|
||||
# Should still reject invalid blocks, even if we have the header:
|
||||
assert_equal(node.submitblock(hexdata=b2x(bad_block_root.serialize())), 'bad-txnmrklroot')
|
||||
assert_equal(node.submitblock(hexdata=b2x(bad_block_root.serialize())), 'bad-txnmrklroot')
|
||||
assert_equal(node.submitblock(hexdata=bad_block_root.serialize().hex()), 'bad-txnmrklroot')
|
||||
assert_equal(node.submitblock(hexdata=bad_block_root.serialize().hex()), 'bad-txnmrklroot')
|
||||
assert chain_tip(bad_block_root.hash) in node.getchaintips()
|
||||
# We know the header for this invalid block, so should just return early without error:
|
||||
node.submitheader(hexdata=b2x(CBlockHeader(bad_block_root).serialize()))
|
||||
node.submitheader(hexdata=CBlockHeader(bad_block_root).serialize().hex())
|
||||
assert chain_tip(bad_block_root.hash) in node.getchaintips()
|
||||
|
||||
bad_block_lock = copy.deepcopy(block)
|
||||
@@ -217,19 +216,19 @@ class MiningTest(BitcoinTestFramework):
|
||||
bad_block_lock.vtx[0].rehash()
|
||||
bad_block_lock.hashMerkleRoot = bad_block_lock.calc_merkle_root()
|
||||
bad_block_lock.solve()
|
||||
assert_equal(node.submitblock(hexdata=b2x(bad_block_lock.serialize())), 'bad-txns-nonfinal')
|
||||
assert_equal(node.submitblock(hexdata=b2x(bad_block_lock.serialize())), 'duplicate-invalid')
|
||||
assert_equal(node.submitblock(hexdata=bad_block_lock.serialize().hex()), 'bad-txns-nonfinal')
|
||||
assert_equal(node.submitblock(hexdata=bad_block_lock.serialize().hex()), 'duplicate-invalid')
|
||||
# Build a "good" block on top of the submitted bad block
|
||||
bad_block2 = copy.deepcopy(block)
|
||||
bad_block2.hashPrevBlock = bad_block_lock.sha256
|
||||
bad_block2.solve()
|
||||
assert_raises_rpc_error(-25, 'bad-prevblk', lambda: node.submitheader(hexdata=b2x(CBlockHeader(bad_block2).serialize())))
|
||||
assert_raises_rpc_error(-25, 'bad-prevblk', lambda: node.submitheader(hexdata=CBlockHeader(bad_block2).serialize().hex()))
|
||||
|
||||
# Should reject invalid header right away
|
||||
bad_block_time = copy.deepcopy(block)
|
||||
bad_block_time.nTime = 1
|
||||
bad_block_time.solve()
|
||||
assert_raises_rpc_error(-25, 'time-too-old', lambda: node.submitheader(hexdata=b2x(CBlockHeader(bad_block_time).serialize())))
|
||||
assert_raises_rpc_error(-25, 'time-too-old', lambda: node.submitheader(hexdata=CBlockHeader(bad_block_time).serialize().hex()))
|
||||
|
||||
# Should ask for the block from a p2p node, if they announce the header as well:
|
||||
node.add_p2p_connection(P2PDataStore())
|
||||
@@ -240,11 +239,11 @@ class MiningTest(BitcoinTestFramework):
|
||||
|
||||
# Building a few blocks should give the same results
|
||||
node.generatetoaddress(10, node.get_deterministic_priv_key().address)
|
||||
assert_raises_rpc_error(-25, 'time-too-old', lambda: node.submitheader(hexdata=b2x(CBlockHeader(bad_block_time).serialize())))
|
||||
assert_raises_rpc_error(-25, 'bad-prevblk', lambda: node.submitheader(hexdata=b2x(CBlockHeader(bad_block2).serialize())))
|
||||
node.submitheader(hexdata=b2x(CBlockHeader(block).serialize()))
|
||||
node.submitheader(hexdata=b2x(CBlockHeader(bad_block_root).serialize()))
|
||||
assert_equal(node.submitblock(hexdata=b2x(block.serialize())), 'duplicate') # valid
|
||||
assert_raises_rpc_error(-25, 'time-too-old', lambda: node.submitheader(hexdata=CBlockHeader(bad_block_time).serialize().hex()))
|
||||
assert_raises_rpc_error(-25, 'bad-prevblk', lambda: node.submitheader(hexdata=CBlockHeader(bad_block2).serialize().hex()))
|
||||
node.submitheader(hexdata=CBlockHeader(block).serialize().hex())
|
||||
node.submitheader(hexdata=CBlockHeader(bad_block_root).serialize().hex())
|
||||
assert_equal(node.submitblock(hexdata=block.serialize().hex()), 'duplicate') # valid
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
||||
Reference in New Issue
Block a user