Skip to content

Commit 03c9004

Browse files
fanquakevijaydasmp
authored andcommitted
Merge bitcoin#28727: test: replace random_bytes with random.randbytes
fe3ac37 test: replace random_bytes with randbytes bitcoin#28720 (ns-xvrn) Pull request description: With Python upgraded to 3.9 replaced the `random_bytes` function in util of functional tests and replaced it's usage with `random.randbytes`. Closes bitcoin#28720. ACKs for top commit: maflcko: lgtm ACK fe3ac37 BrandonOdiwuor: ACK fe3ac37 stickies-v: ACK fe3ac37, thanks for picking this up kristapsk: utACK fe3ac37 Tree-SHA512: f65a75e73ebd840c2936eb133d42bccd552f25b717c8ca25c18d06e0593e12f292389cfcc0a0b0759004b67a46ea0c8ac237973ef90f246139778230be1e64e1 Adding random back to utils.py as it is used in other places Replacing random_bytes with random.randbytes
1 parent b641073 commit 03c9004

6 files changed

Lines changed: 17 additions & 24 deletions

File tree

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):
@@ -49,11 +48,11 @@ def run_test(self):
4948
self.wallet.rescan_utxos()
5049

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

5857
self.log.info("Testing null data transaction with default -datacarrier and -datacarriersize values.")
5958
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
@@ -6,7 +6,7 @@
66
import threading
77
from test_framework.messages import MAX_PROTOCOL_MESSAGE_LENGTH
88
from test_framework.test_framework import BitcoinTestFramework
9-
from test_framework.util import random_bytes
9+
from random import randbytes
1010

1111
class NetDeadlockTest(BitcoinTestFramework):
1212
def set_test_params(self):
@@ -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(MAX_PROTOCOL_MESSAGE_LENGTH).hex()
21+
rand_msg = randbytes(MAX_PROTOCOL_MESSAGE_LENGTH).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/p2p_v2_misbehaving.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
from enum import Enum
88

99
from test_framework.messages import MAGIC_BYTES
10-
from test_framework.util import random_bytes
1110
from test_framework.p2p import P2PInterface
1211
from test_framework.test_framework import BitcoinTestFramework
1312
from test_framework.util import random_bitflip
@@ -91,7 +90,7 @@ def complete_handshake(self, response):
9190
class NonEmptyVersionPacketState(EncryptedP2PState):
9291
""""Add option for sending non-empty transport version packet."""
9392
def complete_handshake(self, response):
94-
self.transport_version = random_bytes(5)
93+
self.transport_version = random.randbytes(5)
9594
return super().complete_handshake(response)
9695

9796

test/functional/rpc_psbt.py

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

88
from decimal import Decimal
99
from itertools import product
10+
from random import randbytes
1011

1112
from test_framework.descriptors import descsum_create
1213
from test_framework.key import ECKey
@@ -32,7 +33,6 @@
3233
assert_greater_than,
3334
assert_raises_rpc_error,
3435
find_output,
35-
random_bytes,
3636
)
3737
from test_framework.wallet_util import bytes_to_wif
3838

@@ -598,10 +598,10 @@ def test_psbt_input_keys(psbt_input, keys):
598598

599599
self.log.info("Test decoding PSBT with per-input preimage types")
600600
# note that the decodepsbt RPC doesn't check whether preimages and hashes match
601-
hash_ripemd160, preimage_ripemd160 = random_bytes(20), random_bytes(50)
602-
hash_sha256, preimage_sha256 = random_bytes(32), random_bytes(50)
603-
hash_hash160, preimage_hash160 = random_bytes(20), random_bytes(50)
604-
hash_hash256, preimage_hash256 = random_bytes(32), random_bytes(50)
601+
hash_ripemd160, preimage_ripemd160 = randbytes(20), randbytes(50)
602+
hash_sha256, preimage_sha256 = randbytes(32), randbytes(50)
603+
hash_hash160, preimage_hash160 = randbytes(20), randbytes(50)
604+
hash_hash256, preimage_hash256 = randbytes(32), randbytes(50)
605605

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

test/functional/test_framework/util.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -311,10 +311,6 @@ def sha256sum_file(filename):
311311
d = f.read(4096)
312312
return h.digest()
313313

314-
# TODO: Remove and use random.randbytes(n) directly
315-
def random_bytes(n):
316-
"""Return a random bytes object of length n."""
317-
return random.randbytes(n)
318314

319315
# RPC/P2P connection constants and functions
320316
############################################

test/functional/test_framework/v2_p2p.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
from .crypto.hkdf import hkdf_sha256
1313
from .key import TaggedHash
1414
from .messages import MAGIC_BYTES
15-
from .util import random_bytes
1615

1716

1817
CHACHA20POLY1305_EXPANSION = 16
@@ -156,7 +155,7 @@ def generate_keypair_and_garbage(self, garbage_len=None):
156155
self.privkey_ours, self.ellswift_ours = ellswift_create()
157156
if garbage_len is None:
158157
garbage_len = random.randrange(MAX_GARBAGE_LEN + 1)
159-
self.sent_garbage = random_bytes(garbage_len)
158+
self.sent_garbage = random.randbytes(garbage_len)
160159
return self.ellswift_ours + self.sent_garbage
161160

162161
def initiate_v2_handshake(self):

0 commit comments

Comments
 (0)