Skip to content

Commit fe3ac37

Browse files
author
ns-xvrn
committed
test: replace random_bytes with randbytes bitcoin#28720
1 parent 64879f4 commit fe3ac37

File tree

5 files changed

+35
-44
lines changed

5 files changed

+35
-44
lines changed

test/functional/feature_taproot.py

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,6 @@
9595
from test_framework.util import (
9696
assert_raises_rpc_error,
9797
assert_equal,
98-
random_bytes,
9998
)
10099
from test_framework.wallet_util import generate_keypair
101100
from test_framework.key import (
@@ -694,7 +693,7 @@ def spenders_taproot_active():
694693

695694
# Generate an invalid public key
696695
while True:
697-
invalid_pub = random_bytes(32)
696+
invalid_pub = random.randbytes(32)
698697
if not secp256k1.GE.is_valid_x(int.from_bytes(invalid_pub, 'big')):
699698
break
700699

@@ -710,7 +709,7 @@ def spenders_taproot_active():
710709
# == Tests for signature hashing ==
711710

712711
# Run all tests once with no annex, and once with a valid random annex.
713-
for annex in [None, lambda _: bytes([ANNEX_TAG]) + random_bytes(random.randrange(0, 250))]:
712+
for annex in [None, lambda _: bytes([ANNEX_TAG]) + random.randbytes(random.randrange(0, 250))]:
714713
# Non-empty annex is non-standard
715714
no_annex = annex is None
716715

@@ -739,7 +738,7 @@ def spenders_taproot_active():
739738
scripts = [
740739
("pk_codesep", CScript(random_checksig_style(pubs[1]) + bytes([OP_CODESEPARATOR]))), # codesep after checksig
741740
("codesep_pk", CScript(bytes([OP_CODESEPARATOR]) + random_checksig_style(pubs[1]))), # codesep before checksig
742-
("branched_codesep", CScript([random_bytes(random.randrange(2, 511)), OP_DROP, OP_IF, OP_CODESEPARATOR, pubs[0], OP_ELSE, OP_CODESEPARATOR, pubs[1], OP_ENDIF, OP_CHECKSIG])), # branch dependent codesep
741+
("branched_codesep", CScript([random.randbytes(random.randrange(2, 511)), OP_DROP, OP_IF, OP_CODESEPARATOR, pubs[0], OP_ELSE, OP_CODESEPARATOR, pubs[1], OP_ENDIF, OP_CHECKSIG])), # branch dependent codesep
743742
# Note that the first data push in the "branched_codesep" script has the purpose of
744743
# randomizing the sighash, both by varying script size and content. In order to
745744
# avoid MINIMALDATA script verification errors caused by not-minimal-encoded data
@@ -792,8 +791,8 @@ def spenders_taproot_active():
792791
add_spender(spenders, "siglen/empty_csv", tap=tap, key=secs[2], leaf="csv", hashtype=hashtype, **SINGLE_SIG, failure={"sign": b""}, **ERR_CHECKSIGVERIFY)
793792
add_spender(spenders, "siglen/empty_cs", tap=tap, key=secs[2], leaf="cs_pos", hashtype=hashtype, **SINGLE_SIG, failure={"sign": b""}, **ERR_NO_SUCCESS)
794793
add_spender(spenders, "siglen/empty_csa", tap=tap, key=secs[2], leaf="csa_pos", hashtype=hashtype, **SINGLE_SIG, failure={"sign": b""}, **ERR_NO_SUCCESS)
795-
add_spender(spenders, "siglen/empty_cs_neg", tap=tap, key=secs[2], leaf="cs_neg", hashtype=hashtype, **SINGLE_SIG, sign=b"", failure={"sign": lambda _: random_bytes(random.randrange(1, 63))}, **ERR_SIG_SIZE)
796-
add_spender(spenders, "siglen/empty_csa_neg", tap=tap, key=secs[2], leaf="csa_neg", hashtype=hashtype, **SINGLE_SIG, sign=b"", failure={"sign": lambda _: random_bytes(random.randrange(66, 100))}, **ERR_SIG_SIZE)
794+
add_spender(spenders, "siglen/empty_cs_neg", tap=tap, key=secs[2], leaf="cs_neg", hashtype=hashtype, **SINGLE_SIG, sign=b"", failure={"sign": lambda _: random.randbytes(random.randrange(1, 63))}, **ERR_SIG_SIZE)
795+
add_spender(spenders, "siglen/empty_csa_neg", tap=tap, key=secs[2], leaf="csa_neg", hashtype=hashtype, **SINGLE_SIG, sign=b"", failure={"sign": lambda _: random.randbytes(random.randrange(66, 100))}, **ERR_SIG_SIZE)
797796
# Appending a zero byte to signatures invalidates them
798797
add_spender(spenders, "siglen/padzero_keypath", tap=tap, key=secs[3], hashtype=hashtype, **SIG_ADD_ZERO, **(ERR_SIG_HASHTYPE if hashtype == SIGHASH_DEFAULT else ERR_SIG_SIZE))
799798
add_spender(spenders, "siglen/padzero_csv", tap=tap, key=secs[2], leaf="csv", hashtype=hashtype, **SINGLE_SIG, **SIG_ADD_ZERO, **(ERR_SIG_HASHTYPE if hashtype == SIGHASH_DEFAULT else ERR_SIG_SIZE))
@@ -874,14 +873,14 @@ def mutate(spk):
874873
# Test that empty witnesses are invalid.
875874
add_spender(spenders, "spendpath/emptywit", tap=tap, leaf="128deep", **SINGLE_SIG, key=secs[0], failure={"witness": []}, **ERR_EMPTY_WITNESS)
876875
# Test that adding garbage to the control block invalidates it.
877-
add_spender(spenders, "spendpath/padlongcontrol", tap=tap, leaf="128deep", **SINGLE_SIG, key=secs[0], failure={"controlblock": lambda ctx: default_controlblock(ctx) + random_bytes(random.randrange(1, 32))}, **ERR_CONTROLBLOCK_SIZE)
876+
add_spender(spenders, "spendpath/padlongcontrol", tap=tap, leaf="128deep", **SINGLE_SIG, key=secs[0], failure={"controlblock": lambda ctx: default_controlblock(ctx) + random.randbytes(random.randrange(1, 32))}, **ERR_CONTROLBLOCK_SIZE)
878877
# Test that truncating the control block invalidates it.
879878
add_spender(spenders, "spendpath/trunclongcontrol", tap=tap, leaf="128deep", **SINGLE_SIG, key=secs[0], failure={"controlblock": lambda ctx: default_merklebranch(ctx)[0:random.randrange(1, 32)]}, **ERR_CONTROLBLOCK_SIZE)
880879

881880
scripts = [("s", CScript([pubs[0], OP_CHECKSIG]))]
882881
tap = taproot_construct(pubs[1], scripts)
883882
# Test that adding garbage to the control block invalidates it.
884-
add_spender(spenders, "spendpath/padshortcontrol", tap=tap, leaf="s", **SINGLE_SIG, key=secs[0], failure={"controlblock": lambda ctx: default_controlblock(ctx) + random_bytes(random.randrange(1, 32))}, **ERR_CONTROLBLOCK_SIZE)
883+
add_spender(spenders, "spendpath/padshortcontrol", tap=tap, leaf="s", **SINGLE_SIG, key=secs[0], failure={"controlblock": lambda ctx: default_controlblock(ctx) + random.randbytes(random.randrange(1, 32))}, **ERR_CONTROLBLOCK_SIZE)
885884
# Test that truncating the control block invalidates it.
886885
add_spender(spenders, "spendpath/truncshortcontrol", tap=tap, leaf="s", **SINGLE_SIG, key=secs[0], failure={"controlblock": lambda ctx: default_merklebranch(ctx)[0:random.randrange(1, 32)]}, **ERR_CONTROLBLOCK_SIZE)
887886
# Test that truncating the control block to 1 byte ("-1 Merkle length") invalidates it
@@ -970,9 +969,9 @@ def big_spend_inputs(ctx):
970969
# 24) Script that expects an input stack of 1001 elements
971970
("t24", CScript([OP_DROP] * 1000 + [pubs[1], OP_CHECKSIG])),
972971
# 25) Script that pushes a MAX_SCRIPT_ELEMENT_SIZE-bytes element
973-
("t25", CScript([random_bytes(MAX_SCRIPT_ELEMENT_SIZE), OP_DROP, pubs[1], OP_CHECKSIG])),
972+
("t25", CScript([random.randbytes(MAX_SCRIPT_ELEMENT_SIZE), OP_DROP, pubs[1], OP_CHECKSIG])),
974973
# 26) Script that pushes a (MAX_SCRIPT_ELEMENT_SIZE+1)-bytes element
975-
("t26", CScript([random_bytes(MAX_SCRIPT_ELEMENT_SIZE+1), OP_DROP, pubs[1], OP_CHECKSIG])),
974+
("t26", CScript([random.randbytes(MAX_SCRIPT_ELEMENT_SIZE+1), OP_DROP, pubs[1], OP_CHECKSIG])),
976975
# 27) CHECKSIGADD that must fail because numeric argument number is >4 bytes
977976
("t27", CScript([CScriptNum(OVERSIZE_NUMBER), pubs[1], OP_CHECKSIGADD])),
978977
# 28) Pushes random CScriptNum value, checks OP_CHECKSIGADD result
@@ -1005,9 +1004,9 @@ def big_spend_inputs(ctx):
10051004
"tap": tap,
10061005
}
10071006
# Test that MAX_SCRIPT_ELEMENT_SIZE byte stack element inputs are valid, but not one more (and 80 bytes is standard but 81 is not).
1008-
add_spender(spenders, "tapscript/inputmaxlimit", leaf="t0", **common, standard=False, inputs=[getter("sign"), random_bytes(MAX_SCRIPT_ELEMENT_SIZE)], failure={"inputs": [getter("sign"), random_bytes(MAX_SCRIPT_ELEMENT_SIZE+1)]}, **ERR_PUSH_LIMIT)
1009-
add_spender(spenders, "tapscript/input80limit", leaf="t0", **common, inputs=[getter("sign"), random_bytes(80)])
1010-
add_spender(spenders, "tapscript/input81limit", leaf="t0", **common, standard=False, inputs=[getter("sign"), random_bytes(81)])
1007+
add_spender(spenders, "tapscript/inputmaxlimit", leaf="t0", **common, standard=False, inputs=[getter("sign"), random.randbytes(MAX_SCRIPT_ELEMENT_SIZE)], failure={"inputs": [getter("sign"), random.randbytes(MAX_SCRIPT_ELEMENT_SIZE+1)]}, **ERR_PUSH_LIMIT)
1008+
add_spender(spenders, "tapscript/input80limit", leaf="t0", **common, inputs=[getter("sign"), random.randbytes(80)])
1009+
add_spender(spenders, "tapscript/input81limit", leaf="t0", **common, standard=False, inputs=[getter("sign"), random.randbytes(81)])
10111010
# Test that OP_CHECKMULTISIG and OP_CHECKMULTISIGVERIFY cause failure, but OP_CHECKSIG and OP_CHECKSIGVERIFY work.
10121011
add_spender(spenders, "tapscript/disabled_checkmultisig", leaf="t1", **common, **SINGLE_SIG, failure={"leaf": "t3"}, **ERR_TAPSCRIPT_CHECKMULTISIG)
10131012
add_spender(spenders, "tapscript/disabled_checkmultisigverify", leaf="t2", **common, **SINGLE_SIG, failure={"leaf": "t4"}, **ERR_TAPSCRIPT_CHECKMULTISIG)
@@ -1062,17 +1061,17 @@ def big_spend_inputs(ctx):
10621061
# n OP_CHECKSIGVERIFYs and 1 OP_CHECKSIGADD, but also one unexecuted OP_CHECKSIGVERIFY.
10631062
lambda n, pk: (CScript([OP_DROP, pk, OP_0, OP_IF, OP_2DUP, OP_CHECKSIGVERIFY, OP_ENDIF] + [OP_2DUP, OP_CHECKSIGVERIFY] * n + [OP_2, OP_SWAP, OP_CHECKSIGADD, OP_3, OP_EQUAL]), n + 1),
10641063
# n OP_CHECKSIGVERIFYs and 1 OP_CHECKSIGADD, but also one unexecuted OP_CHECKSIG.
1065-
lambda n, pk: (CScript([random_bytes(220), OP_2DROP, pk, OP_1, OP_NOTIF, OP_2DUP, OP_CHECKSIG, OP_VERIFY, OP_ENDIF] + [OP_2DUP, OP_CHECKSIGVERIFY] * n + [OP_4, OP_SWAP, OP_CHECKSIGADD, OP_5, OP_EQUAL]), n + 1),
1064+
lambda n, pk: (CScript([random.randbytes(220), OP_2DROP, pk, OP_1, OP_NOTIF, OP_2DUP, OP_CHECKSIG, OP_VERIFY, OP_ENDIF] + [OP_2DUP, OP_CHECKSIGVERIFY] * n + [OP_4, OP_SWAP, OP_CHECKSIGADD, OP_5, OP_EQUAL]), n + 1),
10661065
# n OP_CHECKSIGVERIFYs and 1 OP_CHECKSIGADD, but also one unexecuted OP_CHECKSIGADD.
10671066
lambda n, pk: (CScript([OP_DROP, pk, OP_1, OP_IF, OP_ELSE, OP_2DUP, OP_6, OP_SWAP, OP_CHECKSIGADD, OP_7, OP_EQUALVERIFY, OP_ENDIF] + [OP_2DUP, OP_CHECKSIGVERIFY] * n + [OP_8, OP_SWAP, OP_CHECKSIGADD, OP_9, OP_EQUAL]), n + 1),
10681067
# n+1 OP_CHECKSIGs, but also one OP_CHECKSIG with an empty signature.
10691068
lambda n, pk: (CScript([OP_DROP, OP_0, pk, OP_CHECKSIG, OP_NOT, OP_VERIFY, pk] + [OP_2DUP, OP_CHECKSIG, OP_VERIFY] * n + [OP_CHECKSIG]), n + 1),
10701069
# n OP_CHECKSIGADDs and 1 OP_CHECKSIG, but also an OP_CHECKSIGADD with an empty signature.
10711070
lambda n, pk: (CScript([OP_DROP, OP_0, OP_10, pk, OP_CHECKSIGADD, OP_10, OP_EQUALVERIFY, pk] + [OP_2DUP, OP_16, OP_SWAP, OP_CHECKSIGADD, b'\x11', OP_EQUALVERIFY] * n + [OP_CHECKSIG]), n + 1),
10721071
]
1073-
for annex in [None, bytes([ANNEX_TAG]) + random_bytes(random.randrange(1000))]:
1072+
for annex in [None, bytes([ANNEX_TAG]) + random.randbytes(random.randrange(1000))]:
10741073
for hashtype in [SIGHASH_DEFAULT, SIGHASH_ALL]:
1075-
for pubkey in [pubs[1], random_bytes(random.choice([x for x in range(2, 81) if x != 32]))]:
1074+
for pubkey in [pubs[1], random.randbytes(random.choice([x for x in range(2, 81) if x != 32]))]:
10761075
for fn_num, fn in enumerate(SIGOPS_RATIO_SCRIPTS):
10771076
merkledepth = random.randrange(129)
10781077

@@ -1109,7 +1108,7 @@ def predict_sigops_ratio(n, dummy_size):
11091108
scripts = [scripts, random.choice(PARTNER_MERKLE_FN)]
11101109
tap = taproot_construct(pubs[0], scripts)
11111110
standard = annex is None and dummylen <= 80 and len(pubkey) == 32
1112-
add_spender(spenders, "tapscript/sigopsratio_%i" % fn_num, tap=tap, leaf="s", annex=annex, hashtype=hashtype, key=secs[1], inputs=[getter("sign"), random_bytes(dummylen)], standard=standard, failure={"inputs": [getter("sign"), random_bytes(dummylen - 1)]}, **ERR_SIGOPS_RATIO)
1111+
add_spender(spenders, "tapscript/sigopsratio_%i" % fn_num, tap=tap, leaf="s", annex=annex, hashtype=hashtype, key=secs[1], inputs=[getter("sign"), random.randbytes(dummylen)], standard=standard, failure={"inputs": [getter("sign"), random.randbytes(dummylen - 1)]}, **ERR_SIGOPS_RATIO)
11131112

11141113
# Future leaf versions
11151114
for leafver in range(0, 0x100, 2):
@@ -1123,8 +1122,8 @@ def predict_sigops_ratio(n, dummy_size):
11231122
("return_unkver", CScript([OP_RETURN]), leafver),
11241123
("undecodable_c0", CScript([OP_PUSHDATA1])),
11251124
("undecodable_unkver", CScript([OP_PUSHDATA1]), leafver),
1126-
("bigpush_c0", CScript([random_bytes(MAX_SCRIPT_ELEMENT_SIZE+1), OP_DROP])),
1127-
("bigpush_unkver", CScript([random_bytes(MAX_SCRIPT_ELEMENT_SIZE+1), OP_DROP]), leafver),
1125+
("bigpush_c0", CScript([random.randbytes(MAX_SCRIPT_ELEMENT_SIZE+1), OP_DROP])),
1126+
("bigpush_unkver", CScript([random.randbytes(MAX_SCRIPT_ELEMENT_SIZE+1), OP_DROP]), leafver),
11281127
("1001push_c0", CScript([OP_0] * 1001)),
11291128
("1001push_unkver", CScript([OP_0] * 1001), leafver),
11301129
]
@@ -1153,8 +1152,8 @@ def predict_sigops_ratio(n, dummy_size):
11531152
("undecodable_success", CScript([opcode, OP_PUSHDATA1])),
11541153
("undecodable_nop", CScript([OP_NOP, OP_PUSHDATA1])),
11551154
("undecodable_bypassed_success", CScript([OP_PUSHDATA1, OP_2, opcode])),
1156-
("bigpush_success", CScript([random_bytes(MAX_SCRIPT_ELEMENT_SIZE+1), OP_DROP, opcode])),
1157-
("bigpush_nop", CScript([random_bytes(MAX_SCRIPT_ELEMENT_SIZE+1), OP_DROP, OP_NOP])),
1155+
("bigpush_success", CScript([random.randbytes(MAX_SCRIPT_ELEMENT_SIZE+1), OP_DROP, opcode])),
1156+
("bigpush_nop", CScript([random.randbytes(MAX_SCRIPT_ELEMENT_SIZE+1), OP_DROP, OP_NOP])),
11581157
("1001push_success", CScript([OP_0] * 1001 + [opcode])),
11591158
("1001push_nop", CScript([OP_0] * 1001 + [OP_NOP])),
11601159
]

test/functional/mempool_datacarrier.py

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,11 @@
1313
)
1414
from test_framework.test_framework import BitcoinTestFramework
1515
from test_framework.test_node import TestNode
16-
from test_framework.util import (
17-
assert_raises_rpc_error,
18-
random_bytes,
19-
)
16+
from test_framework.util import assert_raises_rpc_error
2017
from test_framework.wallet import MiniWallet
2118

19+
from random import randbytes
20+
2221

2322
class DataCarrierTest(BitcoinTestFramework):
2423
def set_test_params(self):
@@ -48,11 +47,11 @@ def run_test(self):
4847
self.wallet = MiniWallet(self.nodes[0])
4948

5049
# By default, only 80 bytes are used for data (+1 for OP_RETURN, +2 for the pushdata opcodes).
51-
default_size_data = random_bytes(MAX_OP_RETURN_RELAY - 3)
52-
too_long_data = random_bytes(MAX_OP_RETURN_RELAY - 2)
53-
small_data = random_bytes(MAX_OP_RETURN_RELAY - 4)
54-
one_byte = random_bytes(1)
55-
zero_bytes = random_bytes(0)
50+
default_size_data = randbytes(MAX_OP_RETURN_RELAY - 3)
51+
too_long_data = randbytes(MAX_OP_RETURN_RELAY - 2)
52+
small_data = randbytes(MAX_OP_RETURN_RELAY - 4)
53+
one_byte = randbytes(1)
54+
zero_bytes = randbytes(0)
5655

5756
self.log.info("Testing null data transaction with default -datacarrier and -datacarriersize values.")
5857
self.test_null_data_transaction(node=self.nodes[0], data=default_size_data, success=True)

test/functional/p2p_net_deadlock.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
import threading
77
from test_framework.test_framework import BitcoinTestFramework
8-
from test_framework.util import random_bytes
8+
from random import randbytes
99

1010

1111
class NetDeadlockTest(BitcoinTestFramework):
@@ -18,7 +18,7 @@ def run_test(self):
1818
node1 = self.nodes[1]
1919

2020
self.log.info("Simultaneously send a large message on both sides")
21-
rand_msg = random_bytes(4000000).hex()
21+
rand_msg = randbytes(4000000).hex()
2222

2323
thread0 = threading.Thread(target=node0.sendmsgtopeer, args=(0, "unknown", rand_msg))
2424
thread1 = threading.Thread(target=node1.sendmsgtopeer, args=(0, "unknown", rand_msg))

test/functional/rpc_psbt.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"""
77
from decimal import Decimal
88
from itertools import product
9+
from random import randbytes
910

1011
from test_framework.descriptors import descsum_create
1112
from test_framework.key import H_POINT
@@ -40,7 +41,6 @@
4041
assert_raises_rpc_error,
4142
find_output,
4243
find_vout_for_address,
43-
random_bytes,
4444
)
4545
from test_framework.wallet_util import (
4646
generate_keypair,
@@ -893,10 +893,10 @@ def test_psbt_input_keys(psbt_input, keys):
893893

894894
self.log.info("Test decoding PSBT with per-input preimage types")
895895
# note that the decodepsbt RPC doesn't check whether preimages and hashes match
896-
hash_ripemd160, preimage_ripemd160 = random_bytes(20), random_bytes(50)
897-
hash_sha256, preimage_sha256 = random_bytes(32), random_bytes(50)
898-
hash_hash160, preimage_hash160 = random_bytes(20), random_bytes(50)
899-
hash_hash256, preimage_hash256 = random_bytes(32), random_bytes(50)
896+
hash_ripemd160, preimage_ripemd160 = randbytes(20), randbytes(50)
897+
hash_sha256, preimage_sha256 = randbytes(32), randbytes(50)
898+
hash_hash160, preimage_hash160 = randbytes(20), randbytes(50)
899+
hash_hash256, preimage_hash256 = randbytes(32), randbytes(50)
900900

901901
tx = CTransaction()
902902
tx.vin = [CTxIn(outpoint=COutPoint(hash=int('aa' * 32, 16), n=0), scriptSig=b""),

test/functional/test_framework/util.py

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
import logging
1414
import os
1515
import pathlib
16-
import random
1716
import re
1817
import sys
1918
import time
@@ -287,12 +286,6 @@ def sha256sum_file(filename):
287286
return h.digest()
288287

289288

290-
# TODO: Remove and use random.randbytes(n) directly
291-
def random_bytes(n):
292-
"""Return a random bytes object of length n."""
293-
return random.randbytes(n)
294-
295-
296289
# RPC/P2P connection constants and functions
297290
############################################
298291

0 commit comments

Comments
 (0)