Skip to content

Commit f27444f

Browse files
committed
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.
1 parent aa68ed2 commit f27444f

File tree

2 files changed

+13
-15
lines changed

2 files changed

+13
-15
lines changed

test/functional/test_framework/blocktools.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
ser_uint256,
2929
tx_from_hex,
3030
uint256_from_compact,
31-
uint256_from_str,
3231
WITNESS_SCALE_FACTOR,
3332
)
3433
from .script import (
@@ -111,8 +110,8 @@ def create_block(hashprev=None, coinbase=None, ntime=None, *, version=None, tmpl
111110
return block
112111

113112
def get_witness_script(witness_root, witness_nonce):
114-
witness_commitment = uint256_from_str(hash256(ser_uint256(witness_root) + ser_uint256(witness_nonce)))
115-
output_data = WITNESS_COMMITMENT_HEADER + ser_uint256(witness_commitment)
113+
witness_commitment = hash256(ser_uint256(witness_root) + ser_uint256(witness_nonce))
114+
output_data = WITNESS_COMMITMENT_HEADER + witness_commitment
116115
return CScript([OP_RETURN, output_data])
117116

118117
def add_witness_commitment(block, nonce=0):

test/functional/test_framework/script.py

+11-12
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,7 @@
1717
CTxOut,
1818
hash256,
1919
ser_string,
20-
ser_uint256,
2120
sha256,
22-
uint256_from_str,
2321
)
2422

2523
from .crypto.ripemd160 import ripemd160
@@ -711,41 +709,42 @@ def sign_input_segwitv0(tx, input_index, input_scriptpubkey, input_amount, privk
711709
# Note that this corresponds to sigversion == 1 in EvalScript, which is used
712710
# for version 0 witnesses.
713711
def SegwitV0SignatureMsg(script, txTo, inIdx, hashtype, amount):
712+
ZERO_HASH = bytes([0]*32)
714713

715-
hashPrevouts = 0
716-
hashSequence = 0
717-
hashOutputs = 0
714+
hashPrevouts = ZERO_HASH
715+
hashSequence = ZERO_HASH
716+
hashOutputs = ZERO_HASH
718717

719718
if not (hashtype & SIGHASH_ANYONECANPAY):
720719
serialize_prevouts = bytes()
721720
for i in txTo.vin:
722721
serialize_prevouts += i.prevout.serialize()
723-
hashPrevouts = uint256_from_str(hash256(serialize_prevouts))
722+
hashPrevouts = hash256(serialize_prevouts)
724723

725724
if (not (hashtype & SIGHASH_ANYONECANPAY) and (hashtype & 0x1f) != SIGHASH_SINGLE and (hashtype & 0x1f) != SIGHASH_NONE):
726725
serialize_sequence = bytes()
727726
for i in txTo.vin:
728727
serialize_sequence += i.nSequence.to_bytes(4, "little")
729-
hashSequence = uint256_from_str(hash256(serialize_sequence))
728+
hashSequence = hash256(serialize_sequence)
730729

731730
if ((hashtype & 0x1f) != SIGHASH_SINGLE and (hashtype & 0x1f) != SIGHASH_NONE):
732731
serialize_outputs = bytes()
733732
for o in txTo.vout:
734733
serialize_outputs += o.serialize()
735-
hashOutputs = uint256_from_str(hash256(serialize_outputs))
734+
hashOutputs = hash256(serialize_outputs)
736735
elif ((hashtype & 0x1f) == SIGHASH_SINGLE and inIdx < len(txTo.vout)):
737736
serialize_outputs = txTo.vout[inIdx].serialize()
738-
hashOutputs = uint256_from_str(hash256(serialize_outputs))
737+
hashOutputs = hash256(serialize_outputs)
739738

740739
ss = bytes()
741740
ss += txTo.version.to_bytes(4, "little")
742-
ss += ser_uint256(hashPrevouts)
743-
ss += ser_uint256(hashSequence)
741+
ss += hashPrevout
742+
ss += hashSequence
744743
ss += txTo.vin[inIdx].prevout.serialize()
745744
ss += ser_string(script)
746745
ss += amount.to_bytes(8, "little", signed=True)
747746
ss += txTo.vin[inIdx].nSequence.to_bytes(4, "little")
748-
ss += ser_uint256(hashOutputs)
747+
ss += hashOutputs
749748
ss += txTo.nLockTime.to_bytes(4, "little")
750749
ss += hashtype.to_bytes(4, "little")
751750
return ss

0 commit comments

Comments
 (0)