From 346a099fc1e282c8b53d740d65999d8866d17953 Mon Sep 17 00:00:00 2001 From: Sebastian Falbesoner Date: Wed, 12 Mar 2025 20:16:04 +0100 Subject: [PATCH] test: avoid unneeded hash -> uint256 -> hash roundtrips In the functional test framework, we often treat hashes as uint256 integers, which seems to be confusing and for no good reason, as hashes are just sequences of bytes. This commit gets rid of obvious internal instances of that where individual functional tests are not affected. In the long-term, it might make sense to store other hashes (mostly txids) as actual bytes to avoid annoying conversions and improve code readability. --- test/functional/test_framework/blocktools.py | 5 ++--- test/functional/test_framework/script.py | 23 ++++++++++---------- 2 files changed, 13 insertions(+), 15 deletions(-) diff --git a/test/functional/test_framework/blocktools.py b/test/functional/test_framework/blocktools.py index 38600bc005a..8e616285572 100644 --- a/test/functional/test_framework/blocktools.py +++ b/test/functional/test_framework/blocktools.py @@ -28,7 +28,6 @@ from .messages import ( ser_uint256, tx_from_hex, uint256_from_compact, - uint256_from_str, WITNESS_SCALE_FACTOR, ) from .script import ( @@ -111,8 +110,8 @@ def create_block(hashprev=None, coinbase=None, ntime=None, *, version=None, tmpl return block def get_witness_script(witness_root, witness_nonce): - witness_commitment = uint256_from_str(hash256(ser_uint256(witness_root) + ser_uint256(witness_nonce))) - output_data = WITNESS_COMMITMENT_HEADER + ser_uint256(witness_commitment) + witness_commitment = hash256(ser_uint256(witness_root) + ser_uint256(witness_nonce)) + output_data = WITNESS_COMMITMENT_HEADER + witness_commitment return CScript([OP_RETURN, output_data]) def add_witness_commitment(block, nonce=0): diff --git a/test/functional/test_framework/script.py b/test/functional/test_framework/script.py index d510cf9b1ca..12bfee7c776 100644 --- a/test/functional/test_framework/script.py +++ b/test/functional/test_framework/script.py @@ -17,9 +17,7 @@ from .messages import ( CTxOut, hash256, ser_string, - ser_uint256, sha256, - uint256_from_str, ) from .crypto.ripemd160 import ripemd160 @@ -711,41 +709,42 @@ def sign_input_segwitv0(tx, input_index, input_scriptpubkey, input_amount, privk # Note that this corresponds to sigversion == 1 in EvalScript, which is used # for version 0 witnesses. def SegwitV0SignatureMsg(script, txTo, inIdx, hashtype, amount): + ZERO_HASH = bytes([0]*32) - hashPrevouts = 0 - hashSequence = 0 - hashOutputs = 0 + hashPrevouts = ZERO_HASH + hashSequence = ZERO_HASH + hashOutputs = ZERO_HASH if not (hashtype & SIGHASH_ANYONECANPAY): serialize_prevouts = bytes() for i in txTo.vin: serialize_prevouts += i.prevout.serialize() - hashPrevouts = uint256_from_str(hash256(serialize_prevouts)) + hashPrevouts = hash256(serialize_prevouts) if (not (hashtype & SIGHASH_ANYONECANPAY) and (hashtype & 0x1f) != SIGHASH_SINGLE and (hashtype & 0x1f) != SIGHASH_NONE): serialize_sequence = bytes() for i in txTo.vin: serialize_sequence += i.nSequence.to_bytes(4, "little") - hashSequence = uint256_from_str(hash256(serialize_sequence)) + hashSequence = hash256(serialize_sequence) if ((hashtype & 0x1f) != SIGHASH_SINGLE and (hashtype & 0x1f) != SIGHASH_NONE): serialize_outputs = bytes() for o in txTo.vout: serialize_outputs += o.serialize() - hashOutputs = uint256_from_str(hash256(serialize_outputs)) + hashOutputs = hash256(serialize_outputs) elif ((hashtype & 0x1f) == SIGHASH_SINGLE and inIdx < len(txTo.vout)): serialize_outputs = txTo.vout[inIdx].serialize() - hashOutputs = uint256_from_str(hash256(serialize_outputs)) + hashOutputs = hash256(serialize_outputs) ss = bytes() ss += txTo.version.to_bytes(4, "little") - ss += ser_uint256(hashPrevouts) - ss += ser_uint256(hashSequence) + ss += hashPrevouts + ss += hashSequence ss += txTo.vin[inIdx].prevout.serialize() ss += ser_string(script) ss += amount.to_bytes(8, "little", signed=True) ss += txTo.vin[inIdx].nSequence.to_bytes(4, "little") - ss += ser_uint256(hashOutputs) + ss += hashOutputs ss += txTo.nLockTime.to_bytes(4, "little") ss += hashtype.to_bytes(4, "little") return ss