Merge bitcoin/bitcoin#24559: test: add test for signet miner script

038d2a607f test: add test for signet miner script (Sebastian Falbesoner)
449b96ed97 test: add `is_bitcoin_util_compiled` helper (Sebastian Falbesoner)
dde33eca63 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 038d2a607f

Tree-SHA512: 150698a3c0cda3679661b47688e3b932c9761e777fdd284776b867b485db6a8895960177bd02a53f838a4c9b9bbe6a9beea8d7a5b14825b38e4e43b3177821b3
This commit is contained in:
laanwj 2022-04-14 19:25:42 +02:00
commit cf0a8b9c48
No known key found for this signature in database
GPG key ID: 1E4AED62986CD25D
5 changed files with 82 additions and 1 deletions

View file

@ -47,7 +47,8 @@ DIST_CONTRIB = \
$(top_srcdir)/test/sanitizer_suppressions/tsan \
$(top_srcdir)/test/sanitizer_suppressions/ubsan \
$(top_srcdir)/contrib/linearize/linearize-data.py \
$(top_srcdir)/contrib/linearize/linearize-hashes.py
$(top_srcdir)/contrib/linearize/linearize-hashes.py \
$(top_srcdir)/contrib/signet/miner
DIST_SHARE = \
$(top_srcdir)/share/genbuild.sh \

View file

@ -19,6 +19,7 @@ RPCAUTH=@abs_top_srcdir@/share/rpcauth/rpcauth.py
@USE_SQLITE_TRUE@USE_SQLITE=true
@USE_BDB_TRUE@USE_BDB=true
@BUILD_BITCOIN_CLI_TRUE@ENABLE_CLI=true
@BUILD_BITCOIN_UTIL_TRUE@ENABLE_BITCOIN_UTIL=true
@BUILD_BITCOIN_WALLET_TRUE@ENABLE_WALLET_TOOL=true
@BUILD_BITCOIND_TRUE@ENABLE_BITCOIND=true
@ENABLE_FUZZ_TRUE@ENABLE_FUZZ=true

View file

@ -244,8 +244,14 @@ class BitcoinTestFramework(metaclass=BitcoinTestMetaClass):
"src",
"bitcoin-cli" + config["environment"]["EXEEXT"],
)
fname_bitcoinutil = os.path.join(
config["environment"]["BUILDDIR"],
"src",
"bitcoin-util" + config["environment"]["EXEEXT"],
)
self.options.bitcoind = os.getenv("BITCOIND", default=fname_bitcoind)
self.options.bitcoincli = os.getenv("BITCOINCLI", default=fname_bitcoincli)
self.options.bitcoinutil = os.getenv("BITCOINUTIL", default=fname_bitcoinutil)
os.environ['PATH'] = os.pathsep.join([
os.path.join(config['environment']['BUILDDIR'], 'src'),
@ -880,6 +886,11 @@ class BitcoinTestFramework(metaclass=BitcoinTestMetaClass):
if not self.is_wallet_tool_compiled():
raise SkipTest("bitcoin-wallet has not been compiled")
def skip_if_no_bitcoin_util(self):
"""Skip the running test if bitcoin-util has not been compiled."""
if not self.is_bitcoin_util_compiled():
raise SkipTest("bitcoin-util has not been compiled")
def skip_if_no_cli(self):
"""Skip the running test if bitcoin-cli has not been compiled."""
if not self.is_cli_compiled():
@ -927,6 +938,10 @@ class BitcoinTestFramework(metaclass=BitcoinTestMetaClass):
"""Checks whether bitcoin-wallet was compiled."""
return self.config["components"].getboolean("ENABLE_WALLET_TOOL")
def is_bitcoin_util_compiled(self):
"""Checks whether bitcoin-util was compiled."""
return self.config["components"].getboolean("ENABLE_BITCOIN_UTIL")
def is_zmq_compiled(self):
"""Checks whether the zmq module was compiled."""
return self.config["components"].getboolean("ENABLE_ZMQ")

View file

@ -145,6 +145,8 @@ BASE_SCRIPTS = [
'wallet_txn_doublespend.py --mineblock',
'tool_wallet.py --legacy-wallet',
'tool_wallet.py --descriptors',
'tool_signet_miner.py --legacy-wallet',
'tool_signet_miner.py --descriptors',
'wallet_txn_clone.py',
'wallet_txn_clone.py --segwit',
'rpc_getchaintips.py',

View file

@ -0,0 +1,62 @@
#!/usr/bin/env python3
# Copyright (c) 2022 The Bitcoin Core developers
# Distributed under the MIT software license, see the accompanying
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
"""Test signet miner tool"""
import os.path
import subprocess
import sys
import time
from test_framework.key import ECKey
from test_framework.script_util import key_to_p2wpkh_script
from test_framework.test_framework import BitcoinTestFramework
from test_framework.util import assert_equal
from test_framework.wallet_util import bytes_to_wif
CHALLENGE_PRIVATE_KEY = (42).to_bytes(32, 'big')
class SignetMinerTest(BitcoinTestFramework):
def set_test_params(self):
self.chain = "signet"
self.setup_clean_chain = True
self.num_nodes = 1
# generate and specify signet challenge (simple p2wpkh script)
privkey = ECKey()
privkey.set(CHALLENGE_PRIVATE_KEY, True)
pubkey = privkey.get_pubkey().get_bytes()
challenge = key_to_p2wpkh_script(pubkey)
self.extra_args = [[f'-signetchallenge={challenge.hex()}']]
def skip_test_if_missing_module(self):
self.skip_if_no_cli()
self.skip_if_no_wallet()
self.skip_if_no_bitcoin_util()
def run_test(self):
node = self.nodes[0]
# import private key needed for signing block
node.importprivkey(bytes_to_wif(CHALLENGE_PRIVATE_KEY))
# generate block with signet miner tool
base_dir = self.config["environment"]["SRCDIR"]
signet_miner_path = os.path.join(base_dir, "contrib", "signet", "miner")
subprocess.run([
sys.executable,
signet_miner_path,
f'--cli={node.cli.binary} -datadir={node.cli.datadir}',
'generate',
f'--address={node.getnewaddress()}',
f'--grind-cmd={self.options.bitcoinutil} grind',
'--nbits=1d00ffff',
f'--set-block-time={int(time.time())}',
], check=True, stderr=subprocess.STDOUT)
assert_equal(node.getblockcount(), 1)
if __name__ == "__main__":
SignetMinerTest().main()