test: check testnet4 difficulty adjustment

This commit is contained in:
Sjors Provoost 2024-12-31 14:48:40 +01:00
parent a9ce7d1774
commit d3564be226
No known key found for this signature in database
GPG key ID: 57FF9BDBCC301009
4 changed files with 2097 additions and 0 deletions

File diff suppressed because one or more lines are too long

View file

@ -0,0 +1,73 @@
#!/usr/bin/env python3
# Copyright (c) 2024 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 mining on testnet
Test mining related RPCs that involve difficulty adjustment, which
regtest doesn't have.
It uses the first retarget period of testnet4, generated as follows:
for i in {1..2015}
do
hash=`bitcoin-cli -testnet4 getblockhash $i`
block=`bitcoin-cli -testnet4 getblock $hash 0`
echo $block >> data/testnet4.hex
done
"""
from test_framework.test_framework import BitcoinTestFramework
from test_framework.util import (
assert_equal,
)
from test_framework.blocktools import (
DIFF_1_N_BITS,
DIFF_1_TARGET,
DIFF_4_N_BITS,
DIFF_4_TARGET,
nbits_str,
target_str
)
import os
class MiningTestnetTest(BitcoinTestFramework):
def set_test_params(self):
self.num_nodes = 1
self.setup_clean_chain = True
self.chain = "testnet4"
def add_options(self, parser):
parser.add_argument(
'--datafile',
default='data/testnet4.hex',
help='Test data file (default: %(default)s)',
)
def run_test(self):
node = self.nodes[0]
self.log.info("Load testnet4 blocks")
self.headers_file_path = os.path.join(os.path.dirname(os.path.realpath(__file__)), self.options.datafile)
with open(self.headers_file_path, encoding='utf-8') as blocks_data:
for block in blocks_data:
node.submitblock(block.strip())
assert_equal(node.getblockcount(), 2015)
mining_info = node.getmininginfo()
assert_equal(mining_info['difficulty'], 1)
assert_equal(mining_info['bits'], nbits_str(DIFF_1_N_BITS))
assert_equal(mining_info['target'], target_str(DIFF_1_TARGET))
assert_equal(mining_info['next']['height'], 2016)
assert_equal(mining_info['next']['difficulty'], 4)
assert_equal(mining_info['next']['bits'], nbits_str(DIFF_4_N_BITS))
assert_equal(mining_info['next']['target'], target_str(DIFF_4_TARGET))
assert_equal(node.getdifficulty(next=True), 4)
assert_equal(node.gettarget(next=True), target_str(DIFF_4_TARGET))
if __name__ == '__main__':
MiningTestnetTest(__file__).main()

View file

@ -70,6 +70,14 @@ REGTEST_N_BITS = 0x207fffff # difficulty retargeting is disabled in REGTEST cha
REGTEST_TARGET = 0x7fffff0000000000000000000000000000000000000000000000000000000000
assert_equal(uint256_from_compact(REGTEST_N_BITS), REGTEST_TARGET)
DIFF_1_N_BITS = 0x1d00ffff
DIFF_1_TARGET = 0x00000000ffff0000000000000000000000000000000000000000000000000000
assert_equal(uint256_from_compact(DIFF_1_N_BITS), DIFF_1_TARGET)
DIFF_4_N_BITS = 0x1c3fffc0
DIFF_4_TARGET = int(DIFF_1_TARGET / 4)
assert_equal(uint256_from_compact(DIFF_4_N_BITS), DIFF_4_TARGET)
def nbits_str(nbits):
return f"{nbits:08x}"

View file

@ -316,6 +316,7 @@ BASE_SCRIPTS = [
'wallet_upgradewallet.py --legacy-wallet',
'wallet_crosschain.py',
'mining_basic.py',
'mining_testnet.py',
'feature_signet.py',
'p2p_mutated_blocks.py',
'wallet_implicitsegwit.py --legacy-wallet',