|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# Copyright (c) 2022 The Bitcoin Core developers |
| 3 | +# Distributed under the MIT software license, see the accompanying |
| 4 | +# file COPYING or http://www.opensource.org/licenses/mit-license.php. |
| 5 | +"""Test signet miner tool""" |
| 6 | + |
| 7 | +import os.path |
| 8 | +import subprocess |
| 9 | +import sys |
| 10 | +import time |
| 11 | + |
| 12 | +from test_framework.key import ECKey |
| 13 | +from test_framework.script_util import key_to_p2wpkh_script |
| 14 | +from test_framework.test_framework import BitcoinTestFramework |
| 15 | +from test_framework.util import assert_equal |
| 16 | +from test_framework.wallet_util import bytes_to_wif |
| 17 | + |
| 18 | + |
| 19 | +CHALLENGE_PRIVATE_KEY = (42).to_bytes(32, 'big') |
| 20 | + |
| 21 | + |
| 22 | +class SignetMinerTest(BitcoinTestFramework): |
| 23 | + def set_test_params(self): |
| 24 | + self.chain = "signet" |
| 25 | + self.setup_clean_chain = True |
| 26 | + self.num_nodes = 1 |
| 27 | + |
| 28 | + # generate and specify signet challenge (simple p2wpkh script) |
| 29 | + privkey = ECKey() |
| 30 | + privkey.set(CHALLENGE_PRIVATE_KEY, True) |
| 31 | + pubkey = privkey.get_pubkey().get_bytes() |
| 32 | + challenge = key_to_p2wpkh_script(pubkey) |
| 33 | + self.extra_args = [[f'-signetchallenge={challenge.hex()}']] |
| 34 | + |
| 35 | + def skip_test_if_missing_module(self): |
| 36 | + self.skip_if_no_cli() |
| 37 | + self.skip_if_no_wallet() |
| 38 | + self.skip_if_no_bitcoin_util() |
| 39 | + |
| 40 | + def run_test(self): |
| 41 | + node = self.nodes[0] |
| 42 | + # import private key needed for signing block |
| 43 | + node.importprivkey(bytes_to_wif(CHALLENGE_PRIVATE_KEY)) |
| 44 | + |
| 45 | + # generate block with signet miner tool |
| 46 | + base_dir = self.config["environment"]["SRCDIR"] |
| 47 | + signet_miner_path = os.path.join(base_dir, "contrib", "signet", "miner") |
| 48 | + subprocess.run([ |
| 49 | + sys.executable, |
| 50 | + signet_miner_path, |
| 51 | + f'--cli={node.cli.binary} -datadir={node.cli.datadir}', |
| 52 | + 'generate', |
| 53 | + f'--address={node.getnewaddress()}', |
| 54 | + f'--grind-cmd={self.options.bitcoinutil} grind', |
| 55 | + '--nbits=1d00ffff', |
| 56 | + f'--set-block-time={int(time.time())}', |
| 57 | + ], check=True, stderr=subprocess.STDOUT) |
| 58 | + assert_equal(node.getblockcount(), 1) |
| 59 | + |
| 60 | + |
| 61 | +if __name__ == "__main__": |
| 62 | + SignetMinerTest().main() |
0 commit comments