diff options
author | ns-xvrn <ns@xvrn.tech> | 2023-10-25 00:45:29 -0400 |
---|---|---|
committer | ns-xvrn <ns@xvrn.tech> | 2023-10-25 08:56:41 -0400 |
commit | fe3ac3700d31a6329056fee983d91dd8e4c987c1 (patch) | |
tree | 2b9846a12afcd3a381bbaae7b0c5a5716f942d18 /test/functional | |
parent | 64879f4c03b9d933898c0bb83d9b7e9c20729e51 (diff) | |
download | bitcoin-fe3ac3700d31a6329056fee983d91dd8e4c987c1.tar.xz |
test: replace random_bytes with randbytes #28720
Diffstat (limited to 'test/functional')
-rwxr-xr-x | test/functional/feature_taproot.py | 41 | ||||
-rwxr-xr-x | test/functional/mempool_datacarrier.py | 17 | ||||
-rwxr-xr-x | test/functional/p2p_net_deadlock.py | 4 | ||||
-rwxr-xr-x | test/functional/rpc_psbt.py | 10 | ||||
-rw-r--r-- | test/functional/test_framework/util.py | 7 |
5 files changed, 35 insertions, 44 deletions
diff --git a/test/functional/feature_taproot.py b/test/functional/feature_taproot.py index e32319961e..1d8c87feb5 100755 --- a/test/functional/feature_taproot.py +++ b/test/functional/feature_taproot.py @@ -95,7 +95,6 @@ from test_framework.test_framework import BitcoinTestFramework from test_framework.util import ( assert_raises_rpc_error, assert_equal, - random_bytes, ) from test_framework.wallet_util import generate_keypair from test_framework.key import ( @@ -694,7 +693,7 @@ def spenders_taproot_active(): # Generate an invalid public key while True: - invalid_pub = random_bytes(32) + invalid_pub = random.randbytes(32) if not secp256k1.GE.is_valid_x(int.from_bytes(invalid_pub, 'big')): break @@ -710,7 +709,7 @@ def spenders_taproot_active(): # == Tests for signature hashing == # Run all tests once with no annex, and once with a valid random annex. - for annex in [None, lambda _: bytes([ANNEX_TAG]) + random_bytes(random.randrange(0, 250))]: + for annex in [None, lambda _: bytes([ANNEX_TAG]) + random.randbytes(random.randrange(0, 250))]: # Non-empty annex is non-standard no_annex = annex is None @@ -739,7 +738,7 @@ def spenders_taproot_active(): scripts = [ ("pk_codesep", CScript(random_checksig_style(pubs[1]) + bytes([OP_CODESEPARATOR]))), # codesep after checksig ("codesep_pk", CScript(bytes([OP_CODESEPARATOR]) + random_checksig_style(pubs[1]))), # codesep before checksig - ("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 + ("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 # Note that the first data push in the "branched_codesep" script has the purpose of # randomizing the sighash, both by varying script size and content. In order to # avoid MINIMALDATA script verification errors caused by not-minimal-encoded data @@ -792,8 +791,8 @@ def spenders_taproot_active(): add_spender(spenders, "siglen/empty_csv", tap=tap, key=secs[2], leaf="csv", hashtype=hashtype, **SINGLE_SIG, failure={"sign": b""}, **ERR_CHECKSIGVERIFY) add_spender(spenders, "siglen/empty_cs", tap=tap, key=secs[2], leaf="cs_pos", hashtype=hashtype, **SINGLE_SIG, failure={"sign": b""}, **ERR_NO_SUCCESS) add_spender(spenders, "siglen/empty_csa", tap=tap, key=secs[2], leaf="csa_pos", hashtype=hashtype, **SINGLE_SIG, failure={"sign": b""}, **ERR_NO_SUCCESS) - 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) - 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) + 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) + 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) # Appending a zero byte to signatures invalidates them 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)) 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 spenders_taproot_active(): # Test that empty witnesses are invalid. add_spender(spenders, "spendpath/emptywit", tap=tap, leaf="128deep", **SINGLE_SIG, key=secs[0], failure={"witness": []}, **ERR_EMPTY_WITNESS) # Test that adding garbage to the control block invalidates it. - 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) + 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) # Test that truncating the control block invalidates it. 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) scripts = [("s", CScript([pubs[0], OP_CHECKSIG]))] tap = taproot_construct(pubs[1], scripts) # Test that adding garbage to the control block invalidates it. - 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) + 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) # Test that truncating the control block invalidates it. 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) # Test that truncating the control block to 1 byte ("-1 Merkle length") invalidates it @@ -970,9 +969,9 @@ def spenders_taproot_active(): # 24) Script that expects an input stack of 1001 elements ("t24", CScript([OP_DROP] * 1000 + [pubs[1], OP_CHECKSIG])), # 25) Script that pushes a MAX_SCRIPT_ELEMENT_SIZE-bytes element - ("t25", CScript([random_bytes(MAX_SCRIPT_ELEMENT_SIZE), OP_DROP, pubs[1], OP_CHECKSIG])), + ("t25", CScript([random.randbytes(MAX_SCRIPT_ELEMENT_SIZE), OP_DROP, pubs[1], OP_CHECKSIG])), # 26) Script that pushes a (MAX_SCRIPT_ELEMENT_SIZE+1)-bytes element - ("t26", CScript([random_bytes(MAX_SCRIPT_ELEMENT_SIZE+1), OP_DROP, pubs[1], OP_CHECKSIG])), + ("t26", CScript([random.randbytes(MAX_SCRIPT_ELEMENT_SIZE+1), OP_DROP, pubs[1], OP_CHECKSIG])), # 27) CHECKSIGADD that must fail because numeric argument number is >4 bytes ("t27", CScript([CScriptNum(OVERSIZE_NUMBER), pubs[1], OP_CHECKSIGADD])), # 28) Pushes random CScriptNum value, checks OP_CHECKSIGADD result @@ -1005,9 +1004,9 @@ def spenders_taproot_active(): "tap": tap, } # 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). - 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) - add_spender(spenders, "tapscript/input80limit", leaf="t0", **common, inputs=[getter("sign"), random_bytes(80)]) - add_spender(spenders, "tapscript/input81limit", leaf="t0", **common, standard=False, inputs=[getter("sign"), random_bytes(81)]) + 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) + add_spender(spenders, "tapscript/input80limit", leaf="t0", **common, inputs=[getter("sign"), random.randbytes(80)]) + add_spender(spenders, "tapscript/input81limit", leaf="t0", **common, standard=False, inputs=[getter("sign"), random.randbytes(81)]) # Test that OP_CHECKMULTISIG and OP_CHECKMULTISIGVERIFY cause failure, but OP_CHECKSIG and OP_CHECKSIGVERIFY work. add_spender(spenders, "tapscript/disabled_checkmultisig", leaf="t1", **common, **SINGLE_SIG, failure={"leaf": "t3"}, **ERR_TAPSCRIPT_CHECKMULTISIG) add_spender(spenders, "tapscript/disabled_checkmultisigverify", leaf="t2", **common, **SINGLE_SIG, failure={"leaf": "t4"}, **ERR_TAPSCRIPT_CHECKMULTISIG) @@ -1062,7 +1061,7 @@ def spenders_taproot_active(): # n OP_CHECKSIGVERIFYs and 1 OP_CHECKSIGADD, but also one unexecuted OP_CHECKSIGVERIFY. 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), # n OP_CHECKSIGVERIFYs and 1 OP_CHECKSIGADD, but also one unexecuted OP_CHECKSIG. - 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), + 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), # n OP_CHECKSIGVERIFYs and 1 OP_CHECKSIGADD, but also one unexecuted OP_CHECKSIGADD. 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), # n+1 OP_CHECKSIGs, but also one OP_CHECKSIG with an empty signature. @@ -1070,9 +1069,9 @@ def spenders_taproot_active(): # n OP_CHECKSIGADDs and 1 OP_CHECKSIG, but also an OP_CHECKSIGADD with an empty signature. 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), ] - for annex in [None, bytes([ANNEX_TAG]) + random_bytes(random.randrange(1000))]: + for annex in [None, bytes([ANNEX_TAG]) + random.randbytes(random.randrange(1000))]: for hashtype in [SIGHASH_DEFAULT, SIGHASH_ALL]: - for pubkey in [pubs[1], random_bytes(random.choice([x for x in range(2, 81) if x != 32]))]: + for pubkey in [pubs[1], random.randbytes(random.choice([x for x in range(2, 81) if x != 32]))]: for fn_num, fn in enumerate(SIGOPS_RATIO_SCRIPTS): merkledepth = random.randrange(129) @@ -1109,7 +1108,7 @@ def spenders_taproot_active(): scripts = [scripts, random.choice(PARTNER_MERKLE_FN)] tap = taproot_construct(pubs[0], scripts) standard = annex is None and dummylen <= 80 and len(pubkey) == 32 - 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) + 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) # Future leaf versions for leafver in range(0, 0x100, 2): @@ -1123,8 +1122,8 @@ def spenders_taproot_active(): ("return_unkver", CScript([OP_RETURN]), leafver), ("undecodable_c0", CScript([OP_PUSHDATA1])), ("undecodable_unkver", CScript([OP_PUSHDATA1]), leafver), - ("bigpush_c0", CScript([random_bytes(MAX_SCRIPT_ELEMENT_SIZE+1), OP_DROP])), - ("bigpush_unkver", CScript([random_bytes(MAX_SCRIPT_ELEMENT_SIZE+1), OP_DROP]), leafver), + ("bigpush_c0", CScript([random.randbytes(MAX_SCRIPT_ELEMENT_SIZE+1), OP_DROP])), + ("bigpush_unkver", CScript([random.randbytes(MAX_SCRIPT_ELEMENT_SIZE+1), OP_DROP]), leafver), ("1001push_c0", CScript([OP_0] * 1001)), ("1001push_unkver", CScript([OP_0] * 1001), leafver), ] @@ -1153,8 +1152,8 @@ def spenders_taproot_active(): ("undecodable_success", CScript([opcode, OP_PUSHDATA1])), ("undecodable_nop", CScript([OP_NOP, OP_PUSHDATA1])), ("undecodable_bypassed_success", CScript([OP_PUSHDATA1, OP_2, opcode])), - ("bigpush_success", CScript([random_bytes(MAX_SCRIPT_ELEMENT_SIZE+1), OP_DROP, opcode])), - ("bigpush_nop", CScript([random_bytes(MAX_SCRIPT_ELEMENT_SIZE+1), OP_DROP, OP_NOP])), + ("bigpush_success", CScript([random.randbytes(MAX_SCRIPT_ELEMENT_SIZE+1), OP_DROP, opcode])), + ("bigpush_nop", CScript([random.randbytes(MAX_SCRIPT_ELEMENT_SIZE+1), OP_DROP, OP_NOP])), ("1001push_success", CScript([OP_0] * 1001 + [opcode])), ("1001push_nop", CScript([OP_0] * 1001 + [OP_NOP])), ] diff --git a/test/functional/mempool_datacarrier.py b/test/functional/mempool_datacarrier.py index 951bf37ae8..2e27aa988e 100755 --- a/test/functional/mempool_datacarrier.py +++ b/test/functional/mempool_datacarrier.py @@ -13,12 +13,11 @@ from test_framework.script import ( ) from test_framework.test_framework import BitcoinTestFramework from test_framework.test_node import TestNode -from test_framework.util import ( - assert_raises_rpc_error, - random_bytes, -) +from test_framework.util import assert_raises_rpc_error from test_framework.wallet import MiniWallet +from random import randbytes + class DataCarrierTest(BitcoinTestFramework): def set_test_params(self): @@ -48,11 +47,11 @@ class DataCarrierTest(BitcoinTestFramework): self.wallet = MiniWallet(self.nodes[0]) # By default, only 80 bytes are used for data (+1 for OP_RETURN, +2 for the pushdata opcodes). - default_size_data = random_bytes(MAX_OP_RETURN_RELAY - 3) - too_long_data = random_bytes(MAX_OP_RETURN_RELAY - 2) - small_data = random_bytes(MAX_OP_RETURN_RELAY - 4) - one_byte = random_bytes(1) - zero_bytes = random_bytes(0) + default_size_data = randbytes(MAX_OP_RETURN_RELAY - 3) + too_long_data = randbytes(MAX_OP_RETURN_RELAY - 2) + small_data = randbytes(MAX_OP_RETURN_RELAY - 4) + one_byte = randbytes(1) + zero_bytes = randbytes(0) self.log.info("Testing null data transaction with default -datacarrier and -datacarriersize values.") self.test_null_data_transaction(node=self.nodes[0], data=default_size_data, success=True) diff --git a/test/functional/p2p_net_deadlock.py b/test/functional/p2p_net_deadlock.py index f69fe52146..1a357b944b 100755 --- a/test/functional/p2p_net_deadlock.py +++ b/test/functional/p2p_net_deadlock.py @@ -5,7 +5,7 @@ import threading from test_framework.test_framework import BitcoinTestFramework -from test_framework.util import random_bytes +from random import randbytes class NetDeadlockTest(BitcoinTestFramework): @@ -18,7 +18,7 @@ class NetDeadlockTest(BitcoinTestFramework): node1 = self.nodes[1] self.log.info("Simultaneously send a large message on both sides") - rand_msg = random_bytes(4000000).hex() + rand_msg = randbytes(4000000).hex() thread0 = threading.Thread(target=node0.sendmsgtopeer, args=(0, "unknown", rand_msg)) thread1 = threading.Thread(target=node1.sendmsgtopeer, args=(0, "unknown", rand_msg)) diff --git a/test/functional/rpc_psbt.py b/test/functional/rpc_psbt.py index 60df48f025..9f1557f65a 100755 --- a/test/functional/rpc_psbt.py +++ b/test/functional/rpc_psbt.py @@ -6,6 +6,7 @@ """ from decimal import Decimal from itertools import product +from random import randbytes from test_framework.descriptors import descsum_create from test_framework.key import H_POINT @@ -40,7 +41,6 @@ from test_framework.util import ( assert_raises_rpc_error, find_output, find_vout_for_address, - random_bytes, ) from test_framework.wallet_util import ( generate_keypair, @@ -893,10 +893,10 @@ class PSBTTest(BitcoinTestFramework): self.log.info("Test decoding PSBT with per-input preimage types") # note that the decodepsbt RPC doesn't check whether preimages and hashes match - hash_ripemd160, preimage_ripemd160 = random_bytes(20), random_bytes(50) - hash_sha256, preimage_sha256 = random_bytes(32), random_bytes(50) - hash_hash160, preimage_hash160 = random_bytes(20), random_bytes(50) - hash_hash256, preimage_hash256 = random_bytes(32), random_bytes(50) + hash_ripemd160, preimage_ripemd160 = randbytes(20), randbytes(50) + hash_sha256, preimage_sha256 = randbytes(32), randbytes(50) + hash_hash160, preimage_hash160 = randbytes(20), randbytes(50) + hash_hash256, preimage_hash256 = randbytes(32), randbytes(50) tx = CTransaction() tx.vin = [CTxIn(outpoint=COutPoint(hash=int('aa' * 32, 16), n=0), scriptSig=b""), diff --git a/test/functional/test_framework/util.py b/test/functional/test_framework/util.py index 96d70b7380..efb1dbd554 100644 --- a/test/functional/test_framework/util.py +++ b/test/functional/test_framework/util.py @@ -13,7 +13,6 @@ import json import logging import os import pathlib -import random import re import sys import time @@ -287,12 +286,6 @@ def sha256sum_file(filename): return h.digest() -# TODO: Remove and use random.randbytes(n) directly -def random_bytes(n): - """Return a random bytes object of length n.""" - return random.randbytes(n) - - # RPC/P2P connection constants and functions ############################################ |