aboutsummaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorMarcoFalke <falke.marco@gmail.com>2022-03-08 14:56:34 +0100
committerMarcoFalke <falke.marco@gmail.com>2022-03-08 15:00:40 +0100
commit64a4483dc6798a9a7d8327d320a17b3c4d7d4ee0 (patch)
treeeec5006a82a2a83496a242f7172f0289032089be /test
parentc9ed9927bbb7c422c4e01c0c1adc9722b8671009 (diff)
parent31846b006da46852cfce91e53427dc8f871b3fda (diff)
downloadbitcoin-64a4483dc6798a9a7d8327d320a17b3c4d7d4ee0.tar.xz
Merge bitcoin/bitcoin#24496: test: refactor: use `random.sample` for choosing random keys in wallet_taproot.py
31846b006da46852cfce91e53427dc8f871b3fda test: refactor: use `random.sample` for choosing random keys in wallet_taproot.py (Sebastian Falbesoner) Pull request description: The Python3 standard library method `random.sample` has the exact same functionality as the helper method `rand_keys(...)` (that is, random sampling without replacement) on a generic set or sequence, i.e. we can simply replace it. See https://docs.python.org/3/library/random.html#random.sample Note that this is also safer: in case that the sample size `k` is larger than the population count, `random.sample` throws an error: ``` $ python3 Python 3.8.12 (default, Sep 26 2021, 13:12:50) [Clang 11.1.0 ] on openbsd7 Type "help", "copyright", "credits" or "license" for more information. >>> import random >>> random.sample([23, 42], 3) Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/usr/local/lib/python3.8/random.py", line 363, in sample raise ValueError("Sample larger than population or is negative") ValueError: Sample larger than population or is negative ``` while the custom method would get stuck in an endless loop. ACKs for top commit: shaavan: Code Review ACK 31846b006da46852cfce91e53427dc8f871b3fda Tree-SHA512: d9bd7f8128e43401a5b0388e48ba838155b21db5b4b6ba95c91285880788bc3917cb656b74bbe2d97faf7b44862d20b0899dc3c56aa48b9d2b33b50e37d089f6
Diffstat (limited to 'test')
-rwxr-xr-xtest/functional/wallet_taproot.py15
1 files changed, 1 insertions, 14 deletions
diff --git a/test/functional/wallet_taproot.py b/test/functional/wallet_taproot.py
index 54c9928522..24758830a3 100755
--- a/test/functional/wallet_taproot.py
+++ b/test/functional/wallet_taproot.py
@@ -208,19 +208,6 @@ class WalletTaprootTest(BitcoinTestFramework):
pass
@staticmethod
- def rand_keys(n):
- ret = []
- idxes = set()
- for _ in range(n):
- while True:
- i = random.randrange(len(KEYS))
- if not i in idxes:
- break
- idxes.add(i)
- ret.append(KEYS[i])
- return ret
-
- @staticmethod
def make_desc(pattern, privmap, keys, pub_only = False):
pat = pattern.replace("$H", H_POINT)
for i in range(len(privmap)):
@@ -332,7 +319,7 @@ class WalletTaprootTest(BitcoinTestFramework):
def do_test(self, comment, pattern, privmap, treefn):
nkeys = len(privmap)
- keys = self.rand_keys(nkeys * 4)
+ keys = random.sample(KEYS, nkeys * 4)
self.do_test_addr(comment, pattern, privmap, treefn, keys[0:nkeys])
self.do_test_sendtoaddress(comment, pattern, privmap, treefn, keys[0:nkeys], keys[nkeys:2*nkeys])
self.do_test_psbt(comment, pattern, privmap, treefn, keys[2*nkeys:3*nkeys], keys[3*nkeys:4*nkeys])