Skip to content

Commit cf0a8b9

Browse files
committed
Merge bitcoin/bitcoin#24559: test: add test for signet miner script
038d2a6 test: add test for signet miner script (Sebastian Falbesoner) 449b96e test: add `is_bitcoin_util_compiled` helper (Sebastian Falbesoner) dde33ec test: determine path to `bitcoin-util` in test framework (Sebastian Falbesoner) Pull request description: This PR adds a very basic test for the signet miner script (contrib/signet/miner). ~~It was based on #24553 (merged by now) which fixes a bug (and was also the motivation to write this test).~~ The test roughly follows the steps from https://en.bitcoin.it/wiki/Signet#Custom_Signet, except that the challenge key-pair is created solely with the test framework. Calibration is also skipped, the difficulty is simply set to the first mainnet target `0x1d00ffff` (see also https://bitcoin.stackexchange.com/a/57186). ACKs for top commit: laanwj: re-ACK 038d2a6 Tree-SHA512: 150698a3c0cda3679661b47688e3b932c9761e777fdd284776b867b485db6a8895960177bd02a53f838a4c9b9bbe6a9beea8d7a5b14825b38e4e43b3177821b3
2 parents d0f7493 + 038d2a6 commit cf0a8b9

File tree

5 files changed

+82
-1
lines changed

5 files changed

+82
-1
lines changed

Makefile.am

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@ DIST_CONTRIB = \
4747
$(top_srcdir)/test/sanitizer_suppressions/tsan \
4848
$(top_srcdir)/test/sanitizer_suppressions/ubsan \
4949
$(top_srcdir)/contrib/linearize/linearize-data.py \
50-
$(top_srcdir)/contrib/linearize/linearize-hashes.py
50+
$(top_srcdir)/contrib/linearize/linearize-hashes.py \
51+
$(top_srcdir)/contrib/signet/miner
5152

5253
DIST_SHARE = \
5354
$(top_srcdir)/share/genbuild.sh \

test/config.ini.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ RPCAUTH=@abs_top_srcdir@/share/rpcauth/rpcauth.py
1919
@USE_SQLITE_TRUE@USE_SQLITE=true
2020
@USE_BDB_TRUE@USE_BDB=true
2121
@BUILD_BITCOIN_CLI_TRUE@ENABLE_CLI=true
22+
@BUILD_BITCOIN_UTIL_TRUE@ENABLE_BITCOIN_UTIL=true
2223
@BUILD_BITCOIN_WALLET_TRUE@ENABLE_WALLET_TOOL=true
2324
@BUILD_BITCOIND_TRUE@ENABLE_BITCOIND=true
2425
@ENABLE_FUZZ_TRUE@ENABLE_FUZZ=true

test/functional/test_framework/test_framework.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,8 +244,14 @@ def setup(self):
244244
"src",
245245
"bitcoin-cli" + config["environment"]["EXEEXT"],
246246
)
247+
fname_bitcoinutil = os.path.join(
248+
config["environment"]["BUILDDIR"],
249+
"src",
250+
"bitcoin-util" + config["environment"]["EXEEXT"],
251+
)
247252
self.options.bitcoind = os.getenv("BITCOIND", default=fname_bitcoind)
248253
self.options.bitcoincli = os.getenv("BITCOINCLI", default=fname_bitcoincli)
254+
self.options.bitcoinutil = os.getenv("BITCOINUTIL", default=fname_bitcoinutil)
249255

250256
os.environ['PATH'] = os.pathsep.join([
251257
os.path.join(config['environment']['BUILDDIR'], 'src'),
@@ -880,6 +886,11 @@ def skip_if_no_wallet_tool(self):
880886
if not self.is_wallet_tool_compiled():
881887
raise SkipTest("bitcoin-wallet has not been compiled")
882888

889+
def skip_if_no_bitcoin_util(self):
890+
"""Skip the running test if bitcoin-util has not been compiled."""
891+
if not self.is_bitcoin_util_compiled():
892+
raise SkipTest("bitcoin-util has not been compiled")
893+
883894
def skip_if_no_cli(self):
884895
"""Skip the running test if bitcoin-cli has not been compiled."""
885896
if not self.is_cli_compiled():
@@ -927,6 +938,10 @@ def is_wallet_tool_compiled(self):
927938
"""Checks whether bitcoin-wallet was compiled."""
928939
return self.config["components"].getboolean("ENABLE_WALLET_TOOL")
929940

941+
def is_bitcoin_util_compiled(self):
942+
"""Checks whether bitcoin-util was compiled."""
943+
return self.config["components"].getboolean("ENABLE_BITCOIN_UTIL")
944+
930945
def is_zmq_compiled(self):
931946
"""Checks whether the zmq module was compiled."""
932947
return self.config["components"].getboolean("ENABLE_ZMQ")

test/functional/test_runner.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,8 @@
145145
'wallet_txn_doublespend.py --mineblock',
146146
'tool_wallet.py --legacy-wallet',
147147
'tool_wallet.py --descriptors',
148+
'tool_signet_miner.py --legacy-wallet',
149+
'tool_signet_miner.py --descriptors',
148150
'wallet_txn_clone.py',
149151
'wallet_txn_clone.py --segwit',
150152
'rpc_getchaintips.py',

test/functional/tool_signet_miner.py

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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

Comments
 (0)