test: Use pathlib over os.path #28362

revert netutil chgs py3.8 compliant

fixes based on PR review
This commit is contained in:
ns-xvrn 2023-09-02 01:09:43 -04:00
parent 04265ba937
commit bfa0bd632a
22 changed files with 138 additions and 168 deletions

View file

@ -5,8 +5,8 @@
"""Test the blocksdir option. """Test the blocksdir option.
""" """
import os
import shutil import shutil
from pathlib import Path
from test_framework.test_framework import BitcoinTestFramework, initialize_datadir from test_framework.test_framework import BitcoinTestFramework, initialize_datadir
@ -18,20 +18,20 @@ class BlocksdirTest(BitcoinTestFramework):
def run_test(self): def run_test(self):
self.stop_node(0) self.stop_node(0)
assert os.path.isdir(os.path.join(self.nodes[0].blocks_path)) assert self.nodes[0].blocks_path.is_dir()
assert not os.path.isdir(os.path.join(self.nodes[0].datadir, "blocks")) assert not (self.nodes[0].datadir_path / "blocks").is_dir()
shutil.rmtree(self.nodes[0].datadir) shutil.rmtree(self.nodes[0].datadir_path)
initialize_datadir(self.options.tmpdir, 0, self.chain) initialize_datadir(self.options.tmpdir, 0, self.chain)
self.log.info("Starting with nonexistent blocksdir ...") self.log.info("Starting with nonexistent blocksdir ...")
blocksdir_path = os.path.join(self.options.tmpdir, 'blocksdir') blocksdir_path = Path(self.options.tmpdir) / 'blocksdir'
self.nodes[0].assert_start_raises_init_error([f"-blocksdir={blocksdir_path}"], f'Error: Specified blocks directory "{blocksdir_path}" does not exist.') self.nodes[0].assert_start_raises_init_error([f"-blocksdir={blocksdir_path}"], f'Error: Specified blocks directory "{blocksdir_path}" does not exist.')
os.mkdir(blocksdir_path) blocksdir_path.mkdir()
self.log.info("Starting with existing blocksdir ...") self.log.info("Starting with existing blocksdir ...")
self.start_node(0, [f"-blocksdir={blocksdir_path}"]) self.start_node(0, [f"-blocksdir={blocksdir_path}"])
self.log.info("mining blocks..") self.log.info("mining blocks..")
self.generatetoaddress(self.nodes[0], 10, self.nodes[0].get_deterministic_priv_key().address) self.generatetoaddress(self.nodes[0], 10, self.nodes[0].get_deterministic_priv_key().address)
assert os.path.isfile(os.path.join(blocksdir_path, self.chain, "blocks", "blk00000.dat")) assert (blocksdir_path / self.chain / "blocks" / "blk00000.dat").is_file()
assert os.path.isdir(os.path.join(self.nodes[0].blocks_path, "index")) assert (self.nodes[0].blocks_path / "index").is_dir()
if __name__ == '__main__': if __name__ == '__main__':

View file

@ -5,7 +5,7 @@
"""Test various command line arguments and configuration file parameters.""" """Test various command line arguments and configuration file parameters."""
import os import os
import pathlib from pathlib import Path
import re import re
import sys import sys
import tempfile import tempfile
@ -39,8 +39,8 @@ class ConfArgsTest(BitcoinTestFramework):
extra_args=[f'-conf={bad_conf_file_path}'], extra_args=[f'-conf={bad_conf_file_path}'],
expected_msg=conf_in_config_file_err, expected_msg=conf_in_config_file_err,
) )
inc_conf_file_path = os.path.join(self.nodes[0].datadir, 'include.conf') inc_conf_file_path = self.nodes[0].datadir_path / 'include.conf'
with open(os.path.join(self.nodes[0].datadir, 'bitcoin.conf'), 'a', encoding='utf-8') as conf: with open(self.nodes[0].datadir_path / 'bitcoin.conf', 'a', encoding='utf-8') as conf:
conf.write(f'includeconf={inc_conf_file_path}\n') conf.write(f'includeconf={inc_conf_file_path}\n')
with open(inc_conf_file_path, 'w', encoding='utf-8') as conf: with open(inc_conf_file_path, 'w', encoding='utf-8') as conf:
conf.write('conf=some.conf\n') conf.write('conf=some.conf\n')
@ -97,8 +97,8 @@ class ConfArgsTest(BitcoinTestFramework):
conf.write('server=1\nrpcuser=someuser\n[main]\nrpcpassword=some#pass') conf.write('server=1\nrpcuser=someuser\n[main]\nrpcpassword=some#pass')
self.nodes[0].assert_start_raises_init_error(expected_msg='Error: Error reading configuration file: parse error on line 4, using # in rpcpassword can be ambiguous and should be avoided') self.nodes[0].assert_start_raises_init_error(expected_msg='Error: Error reading configuration file: parse error on line 4, using # in rpcpassword can be ambiguous and should be avoided')
inc_conf_file2_path = os.path.join(self.nodes[0].datadir, 'include2.conf') inc_conf_file2_path = self.nodes[0].datadir_path / 'include2.conf'
with open(os.path.join(self.nodes[0].datadir, 'bitcoin.conf'), 'a', encoding='utf-8') as conf: with open(self.nodes[0].datadir_path / 'bitcoin.conf', 'a', encoding='utf-8') as conf:
conf.write(f'includeconf={inc_conf_file2_path}\n') conf.write(f'includeconf={inc_conf_file2_path}\n')
with open(inc_conf_file_path, 'w', encoding='utf-8') as conf: with open(inc_conf_file_path, 'w', encoding='utf-8') as conf:
@ -123,15 +123,15 @@ class ConfArgsTest(BitcoinTestFramework):
# Create a temporary directory that will be treated as the default data # Create a temporary directory that will be treated as the default data
# directory by bitcoind. # directory by bitcoind.
env, default_datadir = util.get_temp_default_datadir(pathlib.Path(self.options.tmpdir, "test_config_file_log")) env, default_datadir = util.get_temp_default_datadir(Path(self.options.tmpdir, "test_config_file_log"))
default_datadir.mkdir(parents=True) default_datadir.mkdir(parents=True)
# Write a bitcoin.conf file in the default data directory containing a # Write a bitcoin.conf file in the default data directory containing a
# datadir= line pointing at the node datadir. # datadir= line pointing at the node datadir.
node = self.nodes[0] node = self.nodes[0]
conf_text = pathlib.Path(node.bitcoinconf).read_text() conf_text = node.bitcoinconf.read_text()
conf_path = default_datadir / "bitcoin.conf" conf_path = default_datadir / "bitcoin.conf"
conf_path.write_text(f"datadir={node.datadir}\n{conf_text}") conf_path.write_text(f"datadir={node.datadir_path}\n{conf_text}")
# Drop the node -datadir= argument during this test, because if it is # Drop the node -datadir= argument during this test, because if it is
# specified it would take precedence over the datadir setting in the # specified it would take precedence over the datadir setting in the
@ -218,7 +218,8 @@ class ConfArgsTest(BitcoinTestFramework):
def test_seed_peers(self): def test_seed_peers(self):
self.log.info('Test seed peers') self.log.info('Test seed peers')
default_data_dir = self.nodes[0].datadir default_data_dir = self.nodes[0].datadir_path
peer_dat = default_data_dir / 'peers.dat'
# Only regtest has no fixed seeds. To avoid connections to random # Only regtest has no fixed seeds. To avoid connections to random
# nodes, regtest is the only network where it is safe to enable # nodes, regtest is the only network where it is safe to enable
# -fixedseeds in tests # -fixedseeds in tests
@ -229,7 +230,7 @@ class ConfArgsTest(BitcoinTestFramework):
# We expect the node will use DNS Seeds, but Regtest mode does not have # We expect the node will use DNS Seeds, but Regtest mode does not have
# any valid DNS seeds. So after 60 seconds, the node should fallback to # any valid DNS seeds. So after 60 seconds, the node should fallback to
# fixed seeds # fixed seeds
assert not os.path.exists(os.path.join(default_data_dir, "peers.dat")) assert not peer_dat.exists()
start = int(time.time()) start = int(time.time())
with self.nodes[0].assert_debug_log( with self.nodes[0].assert_debug_log(
expected_msgs=[ expected_msgs=[
@ -248,7 +249,7 @@ class ConfArgsTest(BitcoinTestFramework):
# No peers.dat exists and -dnsseed=0 # No peers.dat exists and -dnsseed=0
# We expect the node will fallback immediately to fixed seeds # We expect the node will fallback immediately to fixed seeds
assert not os.path.exists(os.path.join(default_data_dir, "peers.dat")) assert not peer_dat.exists()
with self.nodes[0].assert_debug_log(expected_msgs=[ with self.nodes[0].assert_debug_log(expected_msgs=[
"Loaded 0 addresses from peers.dat", "Loaded 0 addresses from peers.dat",
"DNS seeding disabled", "DNS seeding disabled",
@ -260,7 +261,7 @@ class ConfArgsTest(BitcoinTestFramework):
# No peers.dat exists and dns seeds are disabled. # No peers.dat exists and dns seeds are disabled.
# We expect the node will not add fixed seeds when explicitly disabled. # We expect the node will not add fixed seeds when explicitly disabled.
assert not os.path.exists(os.path.join(default_data_dir, "peers.dat")) assert not peer_dat.exists()
with self.nodes[0].assert_debug_log(expected_msgs=[ with self.nodes[0].assert_debug_log(expected_msgs=[
"Loaded 0 addresses from peers.dat", "Loaded 0 addresses from peers.dat",
"DNS seeding disabled", "DNS seeding disabled",
@ -271,7 +272,7 @@ class ConfArgsTest(BitcoinTestFramework):
# No peers.dat exists and -dnsseed=0, but a -addnode is provided # No peers.dat exists and -dnsseed=0, but a -addnode is provided
# We expect the node will allow 60 seconds prior to using fixed seeds # We expect the node will allow 60 seconds prior to using fixed seeds
assert not os.path.exists(os.path.join(default_data_dir, "peers.dat")) assert not peer_dat.exists()
start = int(time.time()) start = int(time.time())
with self.nodes[0].assert_debug_log( with self.nodes[0].assert_debug_log(
expected_msgs=[ expected_msgs=[
@ -323,16 +324,16 @@ class ConfArgsTest(BitcoinTestFramework):
'because a conflicting -conf file argument is passed.') 'because a conflicting -conf file argument is passed.')
node = self.nodes[0] node = self.nodes[0]
with tempfile.NamedTemporaryFile(dir=self.options.tmpdir, mode="wt", delete=False) as temp_conf: with tempfile.NamedTemporaryFile(dir=self.options.tmpdir, mode="wt", delete=False) as temp_conf:
temp_conf.write(f"datadir={node.datadir}\n") temp_conf.write(f"datadir={node.datadir_path}\n")
node.assert_start_raises_init_error([f"-conf={temp_conf.name}"], re.escape( node.assert_start_raises_init_error([f"-conf={temp_conf.name}"], re.escape(
f'Error: Data directory "{node.datadir}" contains a "bitcoin.conf" file which is ignored, because a ' f'Error: Data directory "{node.datadir_path}" contains a "bitcoin.conf" file which is ignored, because a '
f'different configuration file "{temp_conf.name}" from command line argument "-conf={temp_conf.name}" ' f'different configuration file "{temp_conf.name}" from command line argument "-conf={temp_conf.name}" '
f'is being used instead.') + r"[\s\S]*", match=ErrorMatch.FULL_REGEX) f'is being used instead.') + r"[\s\S]*", match=ErrorMatch.FULL_REGEX)
# Test that passing a redundant -conf command line argument pointing to # Test that passing a redundant -conf command line argument pointing to
# the same bitcoin.conf that would be loaded anyway does not trigger an # the same bitcoin.conf that would be loaded anyway does not trigger an
# error. # error.
self.start_node(0, [f'-conf={node.datadir}/bitcoin.conf']) self.start_node(0, [f'-conf={node.datadir_path}/bitcoin.conf'])
self.stop_node(0) self.stop_node(0)
def test_ignored_default_conf(self): def test_ignored_default_conf(self):
@ -346,7 +347,7 @@ class ConfArgsTest(BitcoinTestFramework):
# Create a temporary directory that will be treated as the default data # Create a temporary directory that will be treated as the default data
# directory by bitcoind. # directory by bitcoind.
env, default_datadir = util.get_temp_default_datadir(pathlib.Path(self.options.tmpdir, "home")) env, default_datadir = util.get_temp_default_datadir(Path(self.options.tmpdir, "home"))
default_datadir.mkdir(parents=True) default_datadir.mkdir(parents=True)
# Write a bitcoin.conf file in the default data directory containing a # Write a bitcoin.conf file in the default data directory containing a
@ -354,7 +355,7 @@ class ConfArgsTest(BitcoinTestFramework):
# startup error because the node datadir contains a different # startup error because the node datadir contains a different
# bitcoin.conf that would be ignored. # bitcoin.conf that would be ignored.
node = self.nodes[0] node = self.nodes[0]
(default_datadir / "bitcoin.conf").write_text(f"datadir={node.datadir}\n") (default_datadir / "bitcoin.conf").write_text(f"datadir={node.datadir_path}\n")
# Drop the node -datadir= argument during this test, because if it is # Drop the node -datadir= argument during this test, because if it is
# specified it would take precedence over the datadir setting in the # specified it would take precedence over the datadir setting in the
@ -362,7 +363,7 @@ class ConfArgsTest(BitcoinTestFramework):
node_args = node.args node_args = node.args
node.args = [arg for arg in node.args if not arg.startswith("-datadir=")] node.args = [arg for arg in node.args if not arg.startswith("-datadir=")]
node.assert_start_raises_init_error([], re.escape( node.assert_start_raises_init_error([], re.escape(
f'Error: Data directory "{node.datadir}" contains a "bitcoin.conf" file which is ignored, because a ' f'Error: Data directory "{node.datadir_path}" contains a "bitcoin.conf" file which is ignored, because a '
f'different configuration file "{default_datadir}/bitcoin.conf" from data directory "{default_datadir}" ' f'different configuration file "{default_datadir}/bitcoin.conf" from data directory "{default_datadir}" '
f'is being used instead.') + r"[\s\S]*", env=env, match=ErrorMatch.FULL_REGEX) f'is being used instead.') + r"[\s\S]*", env=env, match=ErrorMatch.FULL_REGEX)
node.args = node_args node.args = node_args
@ -392,16 +393,16 @@ class ConfArgsTest(BitcoinTestFramework):
# Remove the -datadir argument so it doesn't override the config file # Remove the -datadir argument so it doesn't override the config file
self.nodes[0].args = [arg for arg in self.nodes[0].args if not arg.startswith("-datadir")] self.nodes[0].args = [arg for arg in self.nodes[0].args if not arg.startswith("-datadir")]
default_data_dir = self.nodes[0].datadir default_data_dir = self.nodes[0].datadir_path
new_data_dir = os.path.join(default_data_dir, 'newdatadir') new_data_dir = default_data_dir / 'newdatadir'
new_data_dir_2 = os.path.join(default_data_dir, 'newdatadir2') new_data_dir_2 = default_data_dir / 'newdatadir2'
# Check that using -datadir argument on non-existent directory fails # Check that using -datadir argument on non-existent directory fails
self.nodes[0].datadir = new_data_dir self.nodes[0].datadir_path = new_data_dir
self.nodes[0].assert_start_raises_init_error([f'-datadir={new_data_dir}'], f'Error: Specified data directory "{new_data_dir}" does not exist.') self.nodes[0].assert_start_raises_init_error([f'-datadir={new_data_dir}'], f'Error: Specified data directory "{new_data_dir}" does not exist.')
# Check that using non-existent datadir in conf file fails # Check that using non-existent datadir in conf file fails
conf_file = os.path.join(default_data_dir, "bitcoin.conf") conf_file = default_data_dir / "bitcoin.conf"
# datadir needs to be set before [chain] section # datadir needs to be set before [chain] section
with open(conf_file, encoding='utf8') as f: with open(conf_file, encoding='utf8') as f:
@ -413,20 +414,20 @@ class ConfArgsTest(BitcoinTestFramework):
self.nodes[0].assert_start_raises_init_error([f'-conf={conf_file}'], f'Error: Error reading configuration file: specified data directory "{new_data_dir}" does not exist.') self.nodes[0].assert_start_raises_init_error([f'-conf={conf_file}'], f'Error: Error reading configuration file: specified data directory "{new_data_dir}" does not exist.')
# Check that an explicitly specified config file that cannot be opened fails # Check that an explicitly specified config file that cannot be opened fails
none_existent_conf_file = os.path.join(default_data_dir, "none_existent_bitcoin.conf") none_existent_conf_file = default_data_dir / "none_existent_bitcoin.conf"
self.nodes[0].assert_start_raises_init_error(['-conf=' + none_existent_conf_file], 'Error: Error reading configuration file: specified config file "' + none_existent_conf_file + '" could not be opened.') self.nodes[0].assert_start_raises_init_error(['-conf=' + f'{none_existent_conf_file}'], 'Error: Error reading configuration file: specified config file "' + f'{none_existent_conf_file}' + '" could not be opened.')
# Create the directory and ensure the config file now works # Create the directory and ensure the config file now works
os.mkdir(new_data_dir) new_data_dir.mkdir()
self.start_node(0, [f'-conf={conf_file}']) self.start_node(0, [f'-conf={conf_file}'])
self.stop_node(0) self.stop_node(0)
assert os.path.exists(os.path.join(new_data_dir, self.chain, 'blocks')) assert (new_data_dir / self.chain / 'blocks').exists()
# Ensure command line argument overrides datadir in conf # Ensure command line argument overrides datadir in conf
os.mkdir(new_data_dir_2) new_data_dir_2.mkdir()
self.nodes[0].datadir = new_data_dir_2 self.nodes[0].datadir_path = new_data_dir_2
self.start_node(0, [f'-datadir={new_data_dir_2}', f'-conf={conf_file}']) self.start_node(0, [f'-datadir={new_data_dir_2}', f'-conf={conf_file}'])
assert os.path.exists(os.path.join(new_data_dir_2, self.chain, 'blocks')) assert (new_data_dir_2 / self.chain / 'blocks').exists()
if __name__ == '__main__': if __name__ == '__main__':

View file

@ -28,7 +28,7 @@ class FilelockTest(BitcoinTestFramework):
self.log.info("Check that we can't start a second bitcoind instance using the same datadir") self.log.info("Check that we can't start a second bitcoind instance using the same datadir")
expected_msg = f"Error: Cannot obtain a lock on data directory {datadir}. {self.config['environment']['PACKAGE_NAME']} is probably already running." expected_msg = f"Error: Cannot obtain a lock on data directory {datadir}. {self.config['environment']['PACKAGE_NAME']} is probably already running."
self.nodes[1].assert_start_raises_init_error(extra_args=[f'-datadir={self.nodes[0].datadir}', '-noserver'], expected_msg=expected_msg) self.nodes[1].assert_start_raises_init_error(extra_args=[f'-datadir={self.nodes[0].datadir_path}', '-noserver'], expected_msg=expected_msg)
if self.is_wallet_compiled(): if self.is_wallet_compiled():
def check_wallet_filelock(descriptors): def check_wallet_filelock(descriptors):

View file

@ -10,7 +10,7 @@ To generate that file this test uses the helper scripts available
in contrib/linearize. in contrib/linearize.
""" """
import os from pathlib import Path
import subprocess import subprocess
import sys import sys
import tempfile import tempfile
@ -32,10 +32,10 @@ class LoadblockTest(BitcoinTestFramework):
self.generate(self.nodes[0], COINBASE_MATURITY, sync_fun=self.no_op) self.generate(self.nodes[0], COINBASE_MATURITY, sync_fun=self.no_op)
# Parsing the url of our node to get settings for config file # Parsing the url of our node to get settings for config file
data_dir = self.nodes[0].datadir data_dir = self.nodes[0].datadir_path
node_url = urllib.parse.urlparse(self.nodes[0].url) node_url = urllib.parse.urlparse(self.nodes[0].url)
cfg_file = os.path.join(data_dir, "linearize.cfg") cfg_file = data_dir / "linearize.cfg"
bootstrap_file = os.path.join(self.options.tmpdir, "bootstrap.dat") bootstrap_file = Path(self.options.tmpdir) / "bootstrap.dat"
genesis_block = self.nodes[0].getblockhash(0) genesis_block = self.nodes[0].getblockhash(0)
blocks_dir = self.nodes[0].blocks_path blocks_dir = self.nodes[0].blocks_path
hash_list = tempfile.NamedTemporaryFile(dir=data_dir, hash_list = tempfile.NamedTemporaryFile(dir=data_dir,
@ -58,16 +58,16 @@ class LoadblockTest(BitcoinTestFramework):
cfg.write(f"hashlist={hash_list.name}\n") cfg.write(f"hashlist={hash_list.name}\n")
base_dir = self.config["environment"]["SRCDIR"] base_dir = self.config["environment"]["SRCDIR"]
linearize_dir = os.path.join(base_dir, "contrib", "linearize") linearize_dir = Path(base_dir) / "contrib" / "linearize"
self.log.info("Run linearization of block hashes") self.log.info("Run linearization of block hashes")
linearize_hashes_file = os.path.join(linearize_dir, "linearize-hashes.py") linearize_hashes_file = linearize_dir / "linearize-hashes.py"
subprocess.run([sys.executable, linearize_hashes_file, cfg_file], subprocess.run([sys.executable, linearize_hashes_file, cfg_file],
stdout=hash_list, stdout=hash_list,
check=True) check=True)
self.log.info("Run linearization of block data") self.log.info("Run linearization of block data")
linearize_data_file = os.path.join(linearize_dir, "linearize-data.py") linearize_data_file = linearize_dir / "linearize-data.py"
subprocess.run([sys.executable, linearize_data_file, cfg_file], subprocess.run([sys.executable, linearize_data_file, cfg_file],
check=True) check=True)

View file

@ -6,7 +6,6 @@
import json import json
from pathlib import Path
from test_framework.test_framework import BitcoinTestFramework from test_framework.test_framework import BitcoinTestFramework
from test_framework.test_node import ErrorMatch from test_framework.test_node import ErrorMatch
@ -21,8 +20,8 @@ class SettingsTest(BitcoinTestFramework):
def run_test(self): def run_test(self):
node, = self.nodes node, = self.nodes
settings = Path(node.chain_path, "settings.json") settings = node.chain_path / "settings.json"
conf = Path(node.datadir, "bitcoin.conf") conf = node.datadir_path / "bitcoin.conf"
# Assert empty settings file was created # Assert empty settings file was created
self.stop_node(0) self.stop_node(0)
@ -79,7 +78,7 @@ class SettingsTest(BitcoinTestFramework):
self.stop_node(0) self.stop_node(0)
# Test alternate settings path # Test alternate settings path
altsettings = Path(node.datadir, "altsettings.json") altsettings = node.datadir_path / "altsettings.json"
with altsettings.open("w") as fp: with altsettings.open("w") as fp:
fp.write('{"key": "value"}') fp.write('{"key": "value"}')
with node.assert_debug_log(expected_msgs=['Setting file arg: key = "value"']): with node.assert_debug_log(expected_msgs=['Setting file arg: key = "value"']):

View file

@ -94,7 +94,7 @@ class TestBitcoinCli(BitcoinTestFramework):
assert_equal(self.nodes[0].cli("-named", "echo", "arg0=0", "arg1=1", "arg2=2", "arg1=3").send_cli(), ['0', '3', '2']) assert_equal(self.nodes[0].cli("-named", "echo", "arg0=0", "arg1=1", "arg2=2", "arg1=3").send_cli(), ['0', '3', '2'])
assert_raises_rpc_error(-8, "Parameter args specified multiple times", self.nodes[0].cli("-named", "echo", "args=[0,1,2,3]", "4", "5", "6", ).send_cli) assert_raises_rpc_error(-8, "Parameter args specified multiple times", self.nodes[0].cli("-named", "echo", "args=[0,1,2,3]", "4", "5", "6", ).send_cli)
user, password = get_auth_cookie(self.nodes[0].datadir, self.chain) user, password = get_auth_cookie(self.nodes[0].datadir_path, self.chain)
self.log.info("Test -stdinrpcpass option") self.log.info("Test -stdinrpcpass option")
assert_equal(BLOCKS, self.nodes[0].cli(f'-rpcuser={user}', '-stdinrpcpass', input=password).getblockcount()) assert_equal(BLOCKS, self.nodes[0].cli(f'-rpcuser={user}', '-stdinrpcpass', input=password).getblockcount())

View file

@ -56,7 +56,7 @@ class RPCBindTest(BitcoinTestFramework):
self.nodes[0].rpchost = None self.nodes[0].rpchost = None
self.start_nodes([node_args]) self.start_nodes([node_args])
# connect to node through non-loopback interface # connect to node through non-loopback interface
node = get_rpc_proxy(rpc_url(self.nodes[0].datadir, 0, self.chain, "%s:%d" % (rpchost, rpcport)), 0, coveragedir=self.options.coveragedir) node = get_rpc_proxy(rpc_url(self.nodes[0].datadir_path, 0, self.chain, "%s:%d" % (rpchost, rpcport)), 0, coveragedir=self.options.coveragedir)
node.getnetworkinfo() node.getnetworkinfo()
self.stop_nodes() self.stop_nodes()

View file

@ -4,7 +4,6 @@
# file COPYING or http://www.opensource.org/licenses/mit-license.php. # file COPYING or http://www.opensource.org/licenses/mit-license.php.
"""Test the generation of UTXO snapshots using `dumptxoutset`. """Test the generation of UTXO snapshots using `dumptxoutset`.
""" """
from pathlib import Path
from test_framework.blocktools import COINBASE_MATURITY from test_framework.blocktools import COINBASE_MATURITY
from test_framework.test_framework import BitcoinTestFramework from test_framework.test_framework import BitcoinTestFramework
@ -29,7 +28,7 @@ class DumptxoutsetTest(BitcoinTestFramework):
FILENAME = 'txoutset.dat' FILENAME = 'txoutset.dat'
out = node.dumptxoutset(FILENAME) out = node.dumptxoutset(FILENAME)
expected_path = Path(node.datadir) / self.chain / FILENAME expected_path = node.datadir_path / self.chain / FILENAME
assert expected_path.is_file() assert expected_path.is_file()

View file

@ -1006,5 +1006,5 @@ class BitcoinTestFramework(metaclass=BitcoinTestMetaClass):
return self.config["components"].getboolean("USE_BDB") return self.config["components"].getboolean("USE_BDB")
def has_blockfile(self, node, filenum: str): def has_blockfile(self, node, filenum: str):
blocksdir = os.path.join(node.datadir, self.chain, 'blocks', '') blocksdir = node.datadir_path / self.chain / 'blocks'
return os.path.isfile(os.path.join(blocksdir, f"blk{filenum}.dat")) return (blocksdir / f"blk{filenum}.dat").is_file()

View file

@ -67,7 +67,7 @@ class TestNode():
To make things easier for the test writer, any unrecognised messages will To make things easier for the test writer, any unrecognised messages will
be dispatched to the RPC connection.""" be dispatched to the RPC connection."""
def __init__(self, i, datadir, *, chain, rpchost, timewait, timeout_factor, bitcoind, bitcoin_cli, coverage_dir, cwd, extra_conf=None, extra_args=None, use_cli=False, start_perf=False, use_valgrind=False, version=None, descriptors=False): def __init__(self, i, datadir_path, *, chain, rpchost, timewait, timeout_factor, bitcoind, bitcoin_cli, coverage_dir, cwd, extra_conf=None, extra_args=None, use_cli=False, start_perf=False, use_valgrind=False, version=None, descriptors=False):
""" """
Kwargs: Kwargs:
start_perf (bool): If True, begin profiling the node with `perf` as soon as start_perf (bool): If True, begin profiling the node with `perf` as soon as
@ -76,10 +76,10 @@ class TestNode():
self.index = i self.index = i
self.p2p_conn_index = 1 self.p2p_conn_index = 1
self.datadir = datadir self.datadir_path = datadir_path
self.bitcoinconf = os.path.join(self.datadir, "bitcoin.conf") self.bitcoinconf = self.datadir_path / "bitcoin.conf"
self.stdout_dir = os.path.join(self.datadir, "stdout") self.stdout_dir = self.datadir_path / "stdout"
self.stderr_dir = os.path.join(self.datadir, "stderr") self.stderr_dir = self.datadir_path / "stderr"
self.chain = chain self.chain = chain
self.rpchost = rpchost self.rpchost = rpchost
self.rpc_timeout = timewait self.rpc_timeout = timewait
@ -88,7 +88,7 @@ class TestNode():
self.cwd = cwd self.cwd = cwd
self.descriptors = descriptors self.descriptors = descriptors
if extra_conf is not None: if extra_conf is not None:
append_config(datadir, extra_conf) append_config(self.datadir_path, extra_conf)
# Most callers will just need to add extra args to the standard list below. # Most callers will just need to add extra args to the standard list below.
# For those callers that need more flexibility, they can just set the args property directly. # For those callers that need more flexibility, they can just set the args property directly.
# Note that common args are set in the config file (see initialize_datadir) # Note that common args are set in the config file (see initialize_datadir)
@ -99,7 +99,7 @@ class TestNode():
# spam debug.log. # spam debug.log.
self.args = [ self.args = [
self.binary, self.binary,
"-datadir=" + self.datadir, f"-datadir={self.datadir_path}",
"-logtimemicros", "-logtimemicros",
"-debug", "-debug",
"-debugexclude=libevent", "-debugexclude=libevent",
@ -111,9 +111,7 @@ class TestNode():
self.args.append("-disablewallet") self.args.append("-disablewallet")
if use_valgrind: if use_valgrind:
default_suppressions_file = os.path.join( default_suppressions_file = Path(__file__).parents[3] / "contrib" / "valgrind.supp"
os.path.dirname(os.path.realpath(__file__)),
"..", "..", "..", "contrib", "valgrind.supp")
suppressions_file = os.getenv("VALGRIND_SUPPRESSIONS_FILE", suppressions_file = os.getenv("VALGRIND_SUPPRESSIONS_FILE",
default_suppressions_file) default_suppressions_file)
self.args = ["valgrind", "--suppressions={}".format(suppressions_file), self.args = ["valgrind", "--suppressions={}".format(suppressions_file),
@ -127,7 +125,7 @@ class TestNode():
if self.version_is_at_least(239000): if self.version_is_at_least(239000):
self.args.append("-loglevel=trace") self.args.append("-loglevel=trace")
self.cli = TestNodeCLI(bitcoin_cli, self.datadir) self.cli = TestNodeCLI(bitcoin_cli, self.datadir_path)
self.use_cli = use_cli self.use_cli = use_cli
self.start_perf = start_perf self.start_perf = start_perf
@ -213,7 +211,7 @@ class TestNode():
# Delete any existing cookie file -- if such a file exists (eg due to # Delete any existing cookie file -- if such a file exists (eg due to
# unclean shutdown), it will get overwritten anyway by bitcoind, and # unclean shutdown), it will get overwritten anyway by bitcoind, and
# potentially interfere with our attempt to authenticate # potentially interfere with our attempt to authenticate
delete_cookie_file(self.datadir, self.chain) delete_cookie_file(self.datadir_path, self.chain)
# add environment variable LIBC_FATAL_STDERR_=1 so that libc errors are written to stderr and not the terminal # add environment variable LIBC_FATAL_STDERR_=1 so that libc errors are written to stderr and not the terminal
subp_env = dict(os.environ, LIBC_FATAL_STDERR_="1") subp_env = dict(os.environ, LIBC_FATAL_STDERR_="1")
@ -243,7 +241,7 @@ class TestNode():
f'bitcoind exited with status {self.process.returncode} during initialization. {str_error}')) f'bitcoind exited with status {self.process.returncode} during initialization. {str_error}'))
try: try:
rpc = get_rpc_proxy( rpc = get_rpc_proxy(
rpc_url(self.datadir, self.index, self.chain, self.rpchost), rpc_url(self.datadir_path, self.index, self.chain, self.rpchost),
self.index, self.index,
timeout=self.rpc_timeout // 2, # Shorter timeout to allow for one retry in case of ETIMEDOUT timeout=self.rpc_timeout // 2, # Shorter timeout to allow for one retry in case of ETIMEDOUT
coveragedir=self.coverage_dir, coveragedir=self.coverage_dir,
@ -307,7 +305,7 @@ class TestNode():
poll_per_s = 4 poll_per_s = 4
for _ in range(poll_per_s * self.rpc_timeout): for _ in range(poll_per_s * self.rpc_timeout):
try: try:
get_auth_cookie(self.datadir, self.chain) get_auth_cookie(self.datadir_path, self.chain)
self.log.debug("Cookie credentials successfully retrieved") self.log.debug("Cookie credentials successfully retrieved")
return return
except ValueError: # cookie file not found and no rpcuser or rpcpassword; bitcoind is still starting except ValueError: # cookie file not found and no rpcuser or rpcpassword; bitcoind is still starting
@ -424,10 +422,6 @@ class TestNode():
with open(self.bitcoinconf, 'w', encoding='utf8') as conf: with open(self.bitcoinconf, 'w', encoding='utf8') as conf:
conf.write(conf_data) conf.write(conf_data)
@property
def datadir_path(self) -> Path:
return Path(self.datadir)
@property @property
def chain_path(self) -> Path: def chain_path(self) -> Path:
return self.datadir_path / self.chain return self.datadir_path / self.chain
@ -556,7 +550,7 @@ class TestNode():
"perf output won't be very useful without debug symbols compiled into bitcoind") "perf output won't be very useful without debug symbols compiled into bitcoind")
output_path = tempfile.NamedTemporaryFile( output_path = tempfile.NamedTemporaryFile(
dir=self.datadir, dir=self.datadir_path,
prefix="{}.perf.data.".format(profile_name or 'test'), prefix="{}.perf.data.".format(profile_name or 'test'),
delete=False, delete=False,
).name ).name
@ -783,7 +777,7 @@ class TestNodeCLI():
"""Run bitcoin-cli command. Deserializes returned string as python object.""" """Run bitcoin-cli command. Deserializes returned string as python object."""
pos_args = [arg_to_cli(arg) for arg in args] pos_args = [arg_to_cli(arg) for arg in args]
named_args = [str(key) + "=" + arg_to_cli(value) for (key, value) in kwargs.items()] named_args = [str(key) + "=" + arg_to_cli(value) for (key, value) in kwargs.items()]
p_args = [self.binary, "-datadir=" + self.datadir] + self.options p_args = [self.binary, f"-datadir={self.datadir}"] + self.options
if named_args: if named_args:
p_args += ["-named"] p_args += ["-named"]
if command is not None: if command is not None:

View file

@ -413,7 +413,7 @@ def write_config(config_path, *, n, chain, extra_config="", disable_autoconnect=
def get_datadir_path(dirname, n): def get_datadir_path(dirname, n):
return os.path.join(dirname, "node" + str(n)) return pathlib.Path(dirname) / f"node{n}"
def get_temp_default_datadir(temp_dir: pathlib.Path) -> Tuple[dict, pathlib.Path]: def get_temp_default_datadir(temp_dir: pathlib.Path) -> Tuple[dict, pathlib.Path]:

View file

@ -32,7 +32,7 @@ class ToolWalletTest(BitcoinTestFramework):
self.skip_if_no_wallet_tool() self.skip_if_no_wallet_tool()
def bitcoin_wallet_process(self, *args): def bitcoin_wallet_process(self, *args):
default_args = ['-datadir={}'.format(self.nodes[0].datadir), '-chain=%s' % self.chain] default_args = ['-datadir={}'.format(self.nodes[0].datadir_path), '-chain=%s' % self.chain]
if not self.options.descriptors and 'create' in args: if not self.options.descriptors and 'create' in args:
default_args.append('-legacy') default_args.append('-legacy')
@ -153,8 +153,8 @@ class ToolWalletTest(BitcoinTestFramework):
assert_equal(v, r[k]) assert_equal(v, r[k])
def do_tool_createfromdump(self, wallet_name, dumpfile, file_format=None): def do_tool_createfromdump(self, wallet_name, dumpfile, file_format=None):
dumppath = os.path.join(self.nodes[0].datadir, dumpfile) dumppath = self.nodes[0].datadir_path / dumpfile
rt_dumppath = os.path.join(self.nodes[0].datadir, "rt-{}.dump".format(wallet_name)) rt_dumppath = self.nodes[0].datadir_path / "rt-{}.dump".format(wallet_name)
dump_data = self.read_dump(dumppath) dump_data = self.read_dump(dumppath)
@ -324,7 +324,7 @@ class ToolWalletTest(BitcoinTestFramework):
self.assert_raises_tool_error('No dump file provided. To use dump, -dumpfile=<filename> must be provided.', '-wallet=todump', 'dump') self.assert_raises_tool_error('No dump file provided. To use dump, -dumpfile=<filename> must be provided.', '-wallet=todump', 'dump')
self.log.info('Checking basic dump') self.log.info('Checking basic dump')
wallet_dump = os.path.join(self.nodes[0].datadir, "wallet.dump") wallet_dump = self.nodes[0].datadir_path / "wallet.dump"
self.assert_tool_output('The dumpfile may contain private keys. To ensure the safety of your Bitcoin, do not share the dumpfile.\n', '-wallet=todump', '-dumpfile={}'.format(wallet_dump), 'dump') self.assert_tool_output('The dumpfile may contain private keys. To ensure the safety of your Bitcoin, do not share the dumpfile.\n', '-wallet=todump', '-dumpfile={}'.format(wallet_dump), 'dump')
dump_data = self.read_dump(wallet_dump) dump_data = self.read_dump(wallet_dump)
@ -339,7 +339,7 @@ class ToolWalletTest(BitcoinTestFramework):
self.log.info('Checking createfromdump arguments') self.log.info('Checking createfromdump arguments')
self.assert_raises_tool_error('No dump file provided. To use createfromdump, -dumpfile=<filename> must be provided.', '-wallet=todump', 'createfromdump') self.assert_raises_tool_error('No dump file provided. To use createfromdump, -dumpfile=<filename> must be provided.', '-wallet=todump', 'createfromdump')
non_exist_dump = os.path.join(self.nodes[0].datadir, "wallet.nodump") non_exist_dump = self.nodes[0].datadir_path / "wallet.nodump"
self.assert_raises_tool_error('Unknown wallet file format "notaformat" provided. Please provide one of "bdb" or "sqlite".', '-wallet=todump', '-format=notaformat', '-dumpfile={}'.format(wallet_dump), 'createfromdump') self.assert_raises_tool_error('Unknown wallet file format "notaformat" provided. Please provide one of "bdb" or "sqlite".', '-wallet=todump', '-format=notaformat', '-dumpfile={}'.format(wallet_dump), 'createfromdump')
self.assert_raises_tool_error('Dump file {} does not exist.'.format(non_exist_dump), '-wallet=todump', '-dumpfile={}'.format(non_exist_dump), 'createfromdump') self.assert_raises_tool_error('Dump file {} does not exist.'.format(non_exist_dump), '-wallet=todump', '-dumpfile={}'.format(non_exist_dump), 'createfromdump')
wallet_path = self.nodes[0].wallets_path / "todump2" wallet_path = self.nodes[0].wallets_path / "todump2"
@ -354,17 +354,17 @@ class ToolWalletTest(BitcoinTestFramework):
self.do_tool_createfromdump("load-sqlite", "wallet.dump", "sqlite") self.do_tool_createfromdump("load-sqlite", "wallet.dump", "sqlite")
self.log.info('Checking createfromdump handling of magic and versions') self.log.info('Checking createfromdump handling of magic and versions')
bad_ver_wallet_dump = os.path.join(self.nodes[0].datadir, "wallet-bad_ver1.dump") bad_ver_wallet_dump = self.nodes[0].datadir_path / "wallet-bad_ver1.dump"
dump_data["BITCOIN_CORE_WALLET_DUMP"] = "0" dump_data["BITCOIN_CORE_WALLET_DUMP"] = "0"
self.write_dump(dump_data, bad_ver_wallet_dump) self.write_dump(dump_data, bad_ver_wallet_dump)
self.assert_raises_tool_error('Error: Dumpfile version is not supported. This version of bitcoin-wallet only supports version 1 dumpfiles. Got dumpfile with version 0', '-wallet=badload', '-dumpfile={}'.format(bad_ver_wallet_dump), 'createfromdump') self.assert_raises_tool_error('Error: Dumpfile version is not supported. This version of bitcoin-wallet only supports version 1 dumpfiles. Got dumpfile with version 0', '-wallet=badload', '-dumpfile={}'.format(bad_ver_wallet_dump), 'createfromdump')
assert not (self.nodes[0].wallets_path / "badload").is_dir() assert not (self.nodes[0].wallets_path / "badload").is_dir()
bad_ver_wallet_dump = os.path.join(self.nodes[0].datadir, "wallet-bad_ver2.dump") bad_ver_wallet_dump = self.nodes[0].datadir_path / "wallet-bad_ver2.dump"
dump_data["BITCOIN_CORE_WALLET_DUMP"] = "2" dump_data["BITCOIN_CORE_WALLET_DUMP"] = "2"
self.write_dump(dump_data, bad_ver_wallet_dump) self.write_dump(dump_data, bad_ver_wallet_dump)
self.assert_raises_tool_error('Error: Dumpfile version is not supported. This version of bitcoin-wallet only supports version 1 dumpfiles. Got dumpfile with version 2', '-wallet=badload', '-dumpfile={}'.format(bad_ver_wallet_dump), 'createfromdump') self.assert_raises_tool_error('Error: Dumpfile version is not supported. This version of bitcoin-wallet only supports version 1 dumpfiles. Got dumpfile with version 2', '-wallet=badload', '-dumpfile={}'.format(bad_ver_wallet_dump), 'createfromdump')
assert not (self.nodes[0].wallets_path / "badload").is_dir() assert not (self.nodes[0].wallets_path / "badload").is_dir()
bad_magic_wallet_dump = os.path.join(self.nodes[0].datadir, "wallet-bad_magic.dump") bad_magic_wallet_dump = self.nodes[0].datadir_path / "wallet-bad_magic.dump"
del dump_data["BITCOIN_CORE_WALLET_DUMP"] del dump_data["BITCOIN_CORE_WALLET_DUMP"]
dump_data["not_the_right_magic"] = "1" dump_data["not_the_right_magic"] = "1"
self.write_dump(dump_data, bad_magic_wallet_dump, "not_the_right_magic") self.write_dump(dump_data, bad_magic_wallet_dump, "not_the_right_magic")
@ -372,19 +372,19 @@ class ToolWalletTest(BitcoinTestFramework):
assert not (self.nodes[0].wallets_path / "badload").is_dir() assert not (self.nodes[0].wallets_path / "badload").is_dir()
self.log.info('Checking createfromdump handling of checksums') self.log.info('Checking createfromdump handling of checksums')
bad_sum_wallet_dump = os.path.join(self.nodes[0].datadir, "wallet-bad_sum1.dump") bad_sum_wallet_dump = self.nodes[0].datadir_path / "wallet-bad_sum1.dump"
dump_data = orig_dump.copy() dump_data = orig_dump.copy()
checksum = dump_data["checksum"] checksum = dump_data["checksum"]
dump_data["checksum"] = "1" * 64 dump_data["checksum"] = "1" * 64
self.write_dump(dump_data, bad_sum_wallet_dump) self.write_dump(dump_data, bad_sum_wallet_dump)
self.assert_raises_tool_error('Error: Dumpfile checksum does not match. Computed {}, expected {}'.format(checksum, "1" * 64), '-wallet=bad', '-dumpfile={}'.format(bad_sum_wallet_dump), 'createfromdump') self.assert_raises_tool_error('Error: Dumpfile checksum does not match. Computed {}, expected {}'.format(checksum, "1" * 64), '-wallet=bad', '-dumpfile={}'.format(bad_sum_wallet_dump), 'createfromdump')
assert not (self.nodes[0].wallets_path / "badload").is_dir() assert not (self.nodes[0].wallets_path / "badload").is_dir()
bad_sum_wallet_dump = os.path.join(self.nodes[0].datadir, "wallet-bad_sum2.dump") bad_sum_wallet_dump = self.nodes[0].datadir_path / "wallet-bad_sum2.dump"
del dump_data["checksum"] del dump_data["checksum"]
self.write_dump(dump_data, bad_sum_wallet_dump, skip_checksum=True) self.write_dump(dump_data, bad_sum_wallet_dump, skip_checksum=True)
self.assert_raises_tool_error('Error: Missing checksum', '-wallet=badload', '-dumpfile={}'.format(bad_sum_wallet_dump), 'createfromdump') self.assert_raises_tool_error('Error: Missing checksum', '-wallet=badload', '-dumpfile={}'.format(bad_sum_wallet_dump), 'createfromdump')
assert not (self.nodes[0].wallets_path / "badload").is_dir() assert not (self.nodes[0].wallets_path / "badload").is_dir()
bad_sum_wallet_dump = os.path.join(self.nodes[0].datadir, "wallet-bad_sum3.dump") bad_sum_wallet_dump = self.nodes[0].datadir_path / "wallet-bad_sum3.dump"
dump_data["checksum"] = "2" * 10 dump_data["checksum"] = "2" * 10
self.write_dump(dump_data, bad_sum_wallet_dump) self.write_dump(dump_data, bad_sum_wallet_dump)
self.assert_raises_tool_error('Error: Checksum is not the correct size', '-wallet=badload', '-dumpfile={}'.format(bad_sum_wallet_dump), 'createfromdump') self.assert_raises_tool_error('Error: Checksum is not the correct size', '-wallet=badload', '-dumpfile={}'.format(bad_sum_wallet_dump), 'createfromdump')
@ -452,7 +452,7 @@ class ToolWalletTest(BitcoinTestFramework):
self.assert_tool_output(expected_output, "-wallet=conflicts", "info") self.assert_tool_output(expected_output, "-wallet=conflicts", "info")
def run_test(self): def run_test(self):
self.wallet_path = os.path.join(self.nodes[0].wallets_path, self.default_wallet_name, self.wallet_data_filename) self.wallet_path = self.nodes[0].wallets_path / self.default_wallet_name / self.wallet_data_filename
self.test_invalid_tool_commands_and_args() self.test_invalid_tool_commands_and_args()
# Warning: The following tests are order-dependent. # Warning: The following tests are order-dependent.
self.test_tool_wallet_info() self.test_tool_wallet_info()

View file

@ -109,36 +109,35 @@ class WalletBackupTest(BitcoinTestFramework):
self.stop_node(2) self.stop_node(2)
def erase_three(self): def erase_three(self):
os.remove(os.path.join(self.nodes[0].wallets_path, self.default_wallet_name, self.wallet_data_filename)) for node_num in range(3):
os.remove(os.path.join(self.nodes[1].wallets_path, self.default_wallet_name, self.wallet_data_filename)) (self.nodes[node_num].wallets_path / self.default_wallet_name / self.wallet_data_filename).unlink()
os.remove(os.path.join(self.nodes[2].wallets_path, self.default_wallet_name, self.wallet_data_filename))
def restore_invalid_wallet(self): def restore_invalid_wallet(self):
node = self.nodes[3] node = self.nodes[3]
invalid_wallet_file = os.path.join(self.nodes[0].datadir, 'invalid_wallet_file.bak') invalid_wallet_file = self.nodes[0].datadir_path / 'invalid_wallet_file.bak'
open(invalid_wallet_file, 'a', encoding="utf8").write('invald wallet') open(invalid_wallet_file, 'a', encoding="utf8").write('invald wallet')
wallet_name = "res0" wallet_name = "res0"
not_created_wallet_file = os.path.join(node.wallets_path, wallet_name) not_created_wallet_file = node.wallets_path / wallet_name
error_message = "Wallet file verification failed. Failed to load database path '{}'. Data is not in recognized format.".format(not_created_wallet_file) error_message = "Wallet file verification failed. Failed to load database path '{}'. Data is not in recognized format.".format(not_created_wallet_file)
assert_raises_rpc_error(-18, error_message, node.restorewallet, wallet_name, invalid_wallet_file) assert_raises_rpc_error(-18, error_message, node.restorewallet, wallet_name, invalid_wallet_file)
assert not os.path.exists(not_created_wallet_file) assert not not_created_wallet_file.exists()
def restore_nonexistent_wallet(self): def restore_nonexistent_wallet(self):
node = self.nodes[3] node = self.nodes[3]
nonexistent_wallet_file = os.path.join(self.nodes[0].datadir, 'nonexistent_wallet.bak') nonexistent_wallet_file = self.nodes[0].datadir_path / 'nonexistent_wallet.bak'
wallet_name = "res0" wallet_name = "res0"
assert_raises_rpc_error(-8, "Backup file does not exist", node.restorewallet, wallet_name, nonexistent_wallet_file) assert_raises_rpc_error(-8, "Backup file does not exist", node.restorewallet, wallet_name, nonexistent_wallet_file)
not_created_wallet_file = os.path.join(node.wallets_path, wallet_name) not_created_wallet_file = node.wallets_path / wallet_name
assert not os.path.exists(not_created_wallet_file) assert not not_created_wallet_file.exists()
def restore_wallet_existent_name(self): def restore_wallet_existent_name(self):
node = self.nodes[3] node = self.nodes[3]
backup_file = os.path.join(self.nodes[0].datadir, 'wallet.bak') backup_file = self.nodes[0].datadir_path / 'wallet.bak'
wallet_name = "res0" wallet_name = "res0"
wallet_file = os.path.join(node.wallets_path, wallet_name) wallet_file = node.wallets_path / wallet_name
error_message = "Failed to create database path '{}'. Database already exists.".format(wallet_file) error_message = "Failed to create database path '{}'. Database already exists.".format(wallet_file)
assert_raises_rpc_error(-36, error_message, node.restorewallet, wallet_name, backup_file) assert_raises_rpc_error(-36, error_message, node.restorewallet, wallet_name, backup_file)
assert os.path.exists(wallet_file) assert wallet_file.exists()
def run_test(self): def run_test(self):
self.log.info("Generating initial blockchain") self.log.info("Generating initial blockchain")
@ -159,14 +158,12 @@ class WalletBackupTest(BitcoinTestFramework):
self.log.info("Backing up") self.log.info("Backing up")
self.nodes[0].backupwallet(os.path.join(self.nodes[0].datadir, 'wallet.bak')) for node_num in range(3):
self.nodes[1].backupwallet(os.path.join(self.nodes[1].datadir, 'wallet.bak')) self.nodes[node_num].backupwallet(self.nodes[node_num].datadir_path / 'wallet.bak')
self.nodes[2].backupwallet(os.path.join(self.nodes[2].datadir, 'wallet.bak'))
if not self.options.descriptors: if not self.options.descriptors:
self.nodes[0].dumpwallet(os.path.join(self.nodes[0].datadir, 'wallet.dump')) for node_num in range(3):
self.nodes[1].dumpwallet(os.path.join(self.nodes[1].datadir, 'wallet.dump')) self.nodes[node_num].dumpwallet(self.nodes[node_num].datadir_path / 'wallet.dump')
self.nodes[2].dumpwallet(os.path.join(self.nodes[2].datadir, 'wallet.dump'))
self.log.info("More transactions") self.log.info("More transactions")
for _ in range(5): for _ in range(5):
@ -193,17 +190,13 @@ class WalletBackupTest(BitcoinTestFramework):
self.restore_invalid_wallet() self.restore_invalid_wallet()
self.restore_nonexistent_wallet() self.restore_nonexistent_wallet()
backup_file_0 = os.path.join(self.nodes[0].datadir, 'wallet.bak') backup_files = []
backup_file_1 = os.path.join(self.nodes[1].datadir, 'wallet.bak') for node_num in range(3):
backup_file_2 = os.path.join(self.nodes[2].datadir, 'wallet.bak') backup_files.append(self.nodes[node_num].datadir_path / 'wallet.bak')
self.nodes[3].restorewallet("res0", backup_file_0) for idx, backup_file in enumerate(backup_files):
self.nodes[3].restorewallet("res1", backup_file_1) self.nodes[3].restorewallet(f'res{idx}', backup_file)
self.nodes[3].restorewallet("res2", backup_file_2) assert (self.nodes[3].wallets_path / f'res{idx}').exists()
assert os.path.exists(os.path.join(self.nodes[3].wallets_path, "res0"))
assert os.path.exists(os.path.join(self.nodes[3].wallets_path, "res1"))
assert os.path.exists(os.path.join(self.nodes[3].wallets_path, "res2"))
res0_rpc = self.nodes[3].get_wallet_rpc("res0") res0_rpc = self.nodes[3].get_wallet_rpc("res0")
res1_rpc = self.nodes[3].get_wallet_rpc("res1") res1_rpc = self.nodes[3].get_wallet_rpc("res1")
@ -221,22 +214,16 @@ class WalletBackupTest(BitcoinTestFramework):
self.erase_three() self.erase_three()
#start node2 with no chain #start node2 with no chain
shutil.rmtree(os.path.join(self.nodes[2].blocks_path)) shutil.rmtree(self.nodes[2].blocks_path)
shutil.rmtree(os.path.join(self.nodes[2].chain_path, 'chainstate')) shutil.rmtree(self.nodes[2].chain_path / 'chainstate')
self.start_three(["-nowallet"]) self.start_three(["-nowallet"])
# Create new wallets for the three nodes. # Create new wallets for the three nodes.
# We will use this empty wallets to test the 'importwallet()' RPC command below. # We will use this empty wallets to test the 'importwallet()' RPC command below.
for node_num in range(3): for node_num in range(3):
self.nodes[node_num].createwallet(wallet_name=self.default_wallet_name, descriptors=self.options.descriptors, load_on_startup=True) self.nodes[node_num].createwallet(wallet_name=self.default_wallet_name, descriptors=self.options.descriptors, load_on_startup=True)
assert_equal(self.nodes[node_num].getbalance(), 0)
assert_equal(self.nodes[0].getbalance(), 0) self.nodes[node_num].importwallet(self.nodes[node_num].datadir_path / 'wallet.dump')
assert_equal(self.nodes[1].getbalance(), 0)
assert_equal(self.nodes[2].getbalance(), 0)
self.nodes[0].importwallet(os.path.join(self.nodes[0].datadir, 'wallet.dump'))
self.nodes[1].importwallet(os.path.join(self.nodes[1].datadir, 'wallet.dump'))
self.nodes[2].importwallet(os.path.join(self.nodes[2].datadir, 'wallet.dump'))
self.sync_blocks() self.sync_blocks()

View file

@ -3,7 +3,6 @@
# Distributed under the MIT software license, see the accompanying # Distributed under the MIT software license, see the accompanying
# file COPYING or https://www.opensource.org/licenses/mit-license.php. # file COPYING or https://www.opensource.org/licenses/mit-license.php.
import os
from test_framework.test_framework import BitcoinTestFramework from test_framework.test_framework import BitcoinTestFramework
from test_framework.address import ( from test_framework.address import (
@ -110,7 +109,7 @@ class WalletBlankTest(BitcoinTestFramework):
assert_equal(info["descriptors"], False) assert_equal(info["descriptors"], False)
assert_equal(info["blank"], True) assert_equal(info["blank"], True)
wallet_dump_path = os.path.join(self.nodes[0].datadir, "wallet.dump") wallet_dump_path = self.nodes[0].datadir_path / "wallet.dump"
def_wallet.dumpwallet(wallet_dump_path) def_wallet.dumpwallet(wallet_dump_path)
wallet.importwallet(wallet_dump_path) wallet.importwallet(wallet_dump_path)

View file

@ -3,8 +3,6 @@
# Distributed under the MIT software license, see the accompanying # Distributed under the MIT software license, see the accompanying
# file COPYING or http://www.opensource.org/licenses/mit-license.php. # file COPYING or http://www.opensource.org/licenses/mit-license.php.
import os
from test_framework.test_framework import BitcoinTestFramework from test_framework.test_framework import BitcoinTestFramework
from test_framework.util import assert_raises_rpc_error from test_framework.util import assert_raises_rpc_error
@ -31,13 +29,13 @@ class WalletCrossChain(BitcoinTestFramework):
def run_test(self): def run_test(self):
self.log.info("Creating wallets") self.log.info("Creating wallets")
node0_wallet = os.path.join(self.nodes[0].datadir, 'node0_wallet') node0_wallet = self.nodes[0].datadir_path / 'node0_wallet'
node0_wallet_backup = os.path.join(self.nodes[0].datadir, 'node0_wallet.bak') node0_wallet_backup = self.nodes[0].datadir_path / 'node0_wallet.bak'
self.nodes[0].createwallet(node0_wallet) self.nodes[0].createwallet(node0_wallet)
self.nodes[0].backupwallet(node0_wallet_backup) self.nodes[0].backupwallet(node0_wallet_backup)
self.nodes[0].unloadwallet(node0_wallet) self.nodes[0].unloadwallet(node0_wallet)
node1_wallet = os.path.join(self.nodes[1].datadir, 'node1_wallet') node1_wallet = self.nodes[1].datadir_path / 'node1_wallet'
node1_wallet_backup = os.path.join(self.nodes[0].datadir, 'node1_wallet.bak') node1_wallet_backup = self.nodes[0].datadir_path / 'node1_wallet.bak'
self.nodes[1].createwallet(node1_wallet) self.nodes[1].createwallet(node1_wallet)
self.nodes[1].backupwallet(node1_wallet_backup) self.nodes[1].backupwallet(node1_wallet_backup)
self.nodes[1].unloadwallet(node1_wallet) self.nodes[1].unloadwallet(node1_wallet)

View file

@ -3,7 +3,6 @@
# Distributed under the MIT software license, see the accompanying # Distributed under the MIT software license, see the accompanying
# file COPYING or http://www.opensource.org/licenses/mit-license.php. # file COPYING or http://www.opensource.org/licenses/mit-license.php.
"""Test descriptor wallet function.""" """Test descriptor wallet function."""
import os
try: try:
import sqlite3 import sqlite3
@ -234,7 +233,7 @@ class WalletDescriptorTest(BitcoinTestFramework):
self.log.info("Test that loading descriptor wallet containing legacy key types throws error") self.log.info("Test that loading descriptor wallet containing legacy key types throws error")
self.nodes[0].createwallet(wallet_name="crashme", descriptors=True) self.nodes[0].createwallet(wallet_name="crashme", descriptors=True)
self.nodes[0].unloadwallet("crashme") self.nodes[0].unloadwallet("crashme")
wallet_db = os.path.join(self.nodes[0].wallets_path, "crashme", self.wallet_data_filename) wallet_db = self.nodes[0].wallets_path / "crashme" / self.wallet_data_filename
conn = sqlite3.connect(wallet_db) conn = sqlite3.connect(wallet_db)
with conn: with conn:
# add "cscript" entry: key type is uint160 (20 bytes), value type is CScript (zero-length here) # add "cscript" entry: key type is uint160 (20 bytes), value type is CScript (zero-length here)

View file

@ -4,7 +4,6 @@
# file COPYING or http://www.opensource.org/licenses/mit-license.php. # file COPYING or http://www.opensource.org/licenses/mit-license.php.
"""Test the dumpwallet RPC.""" """Test the dumpwallet RPC."""
import datetime import datetime
import os
import time import time
from test_framework.test_framework import BitcoinTestFramework from test_framework.test_framework import BitcoinTestFramework
@ -111,8 +110,8 @@ class WalletDumpTest(BitcoinTestFramework):
def run_test(self): def run_test(self):
self.nodes[0].createwallet("dump") self.nodes[0].createwallet("dump")
wallet_unenc_dump = os.path.join(self.nodes[0].datadir, "wallet.unencrypted.dump") wallet_unenc_dump = self.nodes[0].datadir_path / "wallet.unencrypted.dump"
wallet_enc_dump = os.path.join(self.nodes[0].datadir, "wallet.encrypted.dump") wallet_enc_dump = self.nodes[0].datadir_path / "wallet.encrypted.dump"
# generate 30 addresses to compare against the dump # generate 30 addresses to compare against the dump
# - 10 legacy P2PKH # - 10 legacy P2PKH
@ -156,7 +155,7 @@ class WalletDumpTest(BitcoinTestFramework):
self.log.info('Dump unencrypted wallet') self.log.info('Dump unencrypted wallet')
result = self.nodes[0].dumpwallet(wallet_unenc_dump) result = self.nodes[0].dumpwallet(wallet_unenc_dump)
assert_equal(result['filename'], wallet_unenc_dump) assert_equal(result['filename'], str(wallet_unenc_dump))
found_comments, found_legacy_addr, found_p2sh_segwit_addr, found_bech32_addr, found_script_addr, found_addr_chg, found_addr_rsv, hd_master_addr_unenc = \ found_comments, found_legacy_addr, found_p2sh_segwit_addr, found_bech32_addr, found_script_addr, found_addr_chg, found_addr_rsv, hd_master_addr_unenc = \
read_dump(wallet_unenc_dump, addrs, [multisig_addr], None) read_dump(wallet_unenc_dump, addrs, [multisig_addr], None)
@ -220,7 +219,7 @@ class WalletDumpTest(BitcoinTestFramework):
w3.sendtoaddress(w3.getnewaddress(), 10) w3.sendtoaddress(w3.getnewaddress(), 10)
w3.unloadwallet() w3.unloadwallet()
self.nodes[0].loadwallet("w3") self.nodes[0].loadwallet("w3")
w3.dumpwallet(os.path.join(self.nodes[0].datadir, "w3.dump")) w3.dumpwallet(self.nodes[0].datadir_path / "w3.dump")
if __name__ == '__main__': if __name__ == '__main__':
WalletDumpTest().main() WalletDumpTest().main()

View file

@ -4,7 +4,6 @@
# file COPYING or http://www.opensource.org/licenses/mit-license.php. # file COPYING or http://www.opensource.org/licenses/mit-license.php.
"""Test that fast rescan using block filters for descriptor wallets detects """Test that fast rescan using block filters for descriptor wallets detects
top-ups correctly and finds the same transactions than the slow variant.""" top-ups correctly and finds the same transactions than the slow variant."""
import os
from typing import List from typing import List
from test_framework.address import address_to_scriptpubkey from test_framework.address import address_to_scriptpubkey
@ -43,7 +42,7 @@ class WalletFastRescanTest(BitcoinTestFramework):
wallet = MiniWallet(node) wallet = MiniWallet(node)
self.log.info("Create descriptor wallet with backup") self.log.info("Create descriptor wallet with backup")
WALLET_BACKUP_FILENAME = os.path.join(node.datadir, 'wallet.bak') WALLET_BACKUP_FILENAME = node.datadir_path / 'wallet.bak'
node.createwallet(wallet_name='topup_test', descriptors=True) node.createwallet(wallet_name='topup_test', descriptors=True)
w = node.get_wallet_rpc('topup_test') w = node.get_wallet_rpc('topup_test')
fixed_key = get_generate_key() fixed_key = get_generate_key()

View file

@ -4,7 +4,6 @@
# file COPYING or http://www.opensource.org/licenses/mit-license.php. # file COPYING or http://www.opensource.org/licenses/mit-license.php.
"""Test Hierarchical Deterministic wallet function.""" """Test Hierarchical Deterministic wallet function."""
import os
import shutil import shutil
from test_framework.blocktools import COINBASE_MATURITY from test_framework.blocktools import COINBASE_MATURITY
@ -51,8 +50,8 @@ class WalletHDTest(BitcoinTestFramework):
self.nodes[1].importprivkey(non_hd_key) self.nodes[1].importprivkey(non_hd_key)
# This should be enough to keep the master key and the non-HD key # This should be enough to keep the master key and the non-HD key
self.nodes[1].backupwallet(os.path.join(self.nodes[1].datadir, "hd.bak")) self.nodes[1].backupwallet(self.nodes[1].datadir_path / "hd.bak")
#self.nodes[1].dumpwallet(os.path.join(self.nodes[1].datadir, "hd.dump")) #self.nodes[1].dumpwallet(self.nodes[1].datadir_path / "hd.dump")
# Derive some HD addresses and remember the last # Derive some HD addresses and remember the last
# Also send funds to each add # Also send funds to each add
@ -87,11 +86,11 @@ class WalletHDTest(BitcoinTestFramework):
self.stop_node(1) self.stop_node(1)
# we need to delete the complete chain directory # we need to delete the complete chain directory
# otherwise node1 would auto-recover all funds in flag the keypool keys as used # otherwise node1 would auto-recover all funds in flag the keypool keys as used
shutil.rmtree(os.path.join(self.nodes[1].blocks_path)) shutil.rmtree(self.nodes[1].blocks_path)
shutil.rmtree(os.path.join(self.nodes[1].chain_path, "chainstate")) shutil.rmtree(self.nodes[1].chain_path / "chainstate")
shutil.copyfile( shutil.copyfile(
os.path.join(self.nodes[1].datadir, "hd.bak"), self.nodes[1].datadir_path / "hd.bak",
os.path.join(self.nodes[1].wallets_path, self.default_wallet_name, self.wallet_data_filename), self.nodes[1].wallets_path / self.default_wallet_name / self.wallet_data_filename
) )
self.start_node(1) self.start_node(1)
@ -115,11 +114,11 @@ class WalletHDTest(BitcoinTestFramework):
# Try a RPC based rescan # Try a RPC based rescan
self.stop_node(1) self.stop_node(1)
shutil.rmtree(os.path.join(self.nodes[1].blocks_path)) shutil.rmtree(self.nodes[1].blocks_path)
shutil.rmtree(os.path.join(self.nodes[1].chain_path, "chainstate")) shutil.rmtree(self.nodes[1].chain_path / "chainstate")
shutil.copyfile( shutil.copyfile(
os.path.join(self.nodes[1].datadir, "hd.bak"), self.nodes[1].datadir_path / "hd.bak",
os.path.join(self.nodes[1].wallets_path, self.default_wallet_name, self.wallet_data_filename), self.nodes[1].wallets_path / self.default_wallet_name / self.wallet_data_filename
) )
self.start_node(1, extra_args=self.extra_args[1]) self.start_node(1, extra_args=self.extra_args[1])
self.connect_nodes(0, 1) self.connect_nodes(0, 1)

View file

@ -10,7 +10,6 @@ Two nodes. Node1 is under test. Node0 is providing transactions and generating b
- Generate 110 keys (enough to drain the keypool). Store key 90 (in the initial keypool) and key 110 (beyond the initial keypool). Send funds to key 90 and key 110. - Generate 110 keys (enough to drain the keypool). Store key 90 (in the initial keypool) and key 110 (beyond the initial keypool). Send funds to key 90 and key 110.
- Stop node1, clear the datadir, move wallet file back into the datadir and restart node1. - Stop node1, clear the datadir, move wallet file back into the datadir and restart node1.
- connect node1 to node0. Verify that they sync and node1 receives its funds.""" - connect node1 to node0. Verify that they sync and node1 receives its funds."""
import os
import shutil import shutil
from test_framework.blocktools import COINBASE_MATURITY from test_framework.blocktools import COINBASE_MATURITY
@ -33,8 +32,8 @@ class KeypoolRestoreTest(BitcoinTestFramework):
self.skip_if_no_wallet() self.skip_if_no_wallet()
def run_test(self): def run_test(self):
wallet_path = os.path.join(self.nodes[1].wallets_path, self.default_wallet_name, self.wallet_data_filename) wallet_path = self.nodes[1].wallets_path / self.default_wallet_name / self.wallet_data_filename
wallet_backup_path = os.path.join(self.nodes[1].datadir, "wallet.bak") wallet_backup_path = self.nodes[1].datadir_path / "wallet.bak"
self.generate(self.nodes[0], COINBASE_MATURITY + 1) self.generate(self.nodes[0], COINBASE_MATURITY + 1)
self.log.info("Make backup of wallet") self.log.info("Make backup of wallet")

View file

@ -4,7 +4,6 @@
# file COPYING or http://www.opensource.org/licenses/mit-license.php. # file COPYING or http://www.opensource.org/licenses/mit-license.php.
"""Test wallet import on pruned node.""" """Test wallet import on pruned node."""
import os
from test_framework.util import assert_equal, assert_raises_rpc_error from test_framework.util import assert_equal, assert_raises_rpc_error
from test_framework.blocktools import ( from test_framework.blocktools import (
@ -74,7 +73,7 @@ class WalletPruningTest(BitcoinTestFramework):
# Import wallet into pruned node # Import wallet into pruned node
self.nodes[1].createwallet(wallet_name="wallet_pruned", descriptors=False, load_on_startup=True) self.nodes[1].createwallet(wallet_name="wallet_pruned", descriptors=False, load_on_startup=True)
self.nodes[1].importwallet(os.path.join(self.nodes[0].datadir, wallet_file)) self.nodes[1].importwallet(self.nodes[0].datadir_path / wallet_file)
# Make sure that prune node's wallet correctly accounts for balances # Make sure that prune node's wallet correctly accounts for balances
assert_equal(self.nodes[1].getbalance(), self.nodes[0].getbalance()) assert_equal(self.nodes[1].getbalance(), self.nodes[0].getbalance())
@ -93,12 +92,12 @@ class WalletPruningTest(BitcoinTestFramework):
# Make sure wallet cannot be imported because of missing blocks # Make sure wallet cannot be imported because of missing blocks
# This will try to rescan blocks `TIMESTAMP_WINDOW` (2h) before the wallet birthheight. # This will try to rescan blocks `TIMESTAMP_WINDOW` (2h) before the wallet birthheight.
# There are 6 blocks an hour, so 11 blocks (excluding birthheight). # There are 6 blocks an hour, so 11 blocks (excluding birthheight).
assert_raises_rpc_error(-4, f"Pruned blocks from height {wallet_birthheight - 11} required to import keys. Use RPC call getblockchaininfo to determine your pruned height.", self.nodes[1].importwallet, os.path.join(self.nodes[0].datadir, wallet_file)) assert_raises_rpc_error(-4, f"Pruned blocks from height {wallet_birthheight - 11} required to import keys. Use RPC call getblockchaininfo to determine your pruned height.", self.nodes[1].importwallet, self.nodes[0].datadir_path / wallet_file)
self.log.info("- Done") self.log.info("- Done")
def get_birthheight(self, wallet_file): def get_birthheight(self, wallet_file):
"""Gets birthheight of a wallet on node0""" """Gets birthheight of a wallet on node0"""
with open(os.path.join(self.nodes[0].datadir, wallet_file), 'r', encoding="utf8") as f: with open(self.nodes[0].datadir_path / wallet_file, 'r', encoding="utf8") as f:
for line in f: for line in f:
if line.startswith('# * Best block at time of backup'): if line.startswith('# * Best block at time of backup'):
wallet_birthheight = int(line.split(' ')[9]) wallet_birthheight = int(line.split(' ')[9])
@ -106,12 +105,12 @@ class WalletPruningTest(BitcoinTestFramework):
def has_block(self, block_index): def has_block(self, block_index):
"""Checks if the pruned node has the specific blk0000*.dat file""" """Checks if the pruned node has the specific blk0000*.dat file"""
return os.path.isfile(os.path.join(self.nodes[1].blocks_path, f"blk{block_index:05}.dat")) return (self.nodes[1].blocks_path / f"blk{block_index:05}.dat").is_file()
def create_wallet(self, wallet_name, *, unload=False): def create_wallet(self, wallet_name, *, unload=False):
"""Creates and dumps a wallet on the non-pruned node0 to be later import by the pruned node""" """Creates and dumps a wallet on the non-pruned node0 to be later import by the pruned node"""
self.nodes[0].createwallet(wallet_name=wallet_name, descriptors=False, load_on_startup=True) self.nodes[0].createwallet(wallet_name=wallet_name, descriptors=False, load_on_startup=True)
self.nodes[0].dumpwallet(os.path.join(self.nodes[0].datadir, wallet_name + ".dat")) self.nodes[0].dumpwallet(self.nodes[0].datadir_path / f"{wallet_name}.dat")
if (unload): if (unload):
self.nodes[0].unloadwallet(wallet_name) self.nodes[0].unloadwallet(wallet_name)

View file

@ -14,7 +14,6 @@ disconnected.
""" """
from decimal import Decimal from decimal import Decimal
import os
import shutil import shutil
from test_framework.test_framework import BitcoinTestFramework from test_framework.test_framework import BitcoinTestFramework
@ -88,8 +87,8 @@ class ReorgsRestoreTest(BitcoinTestFramework):
# Node0 wallet file is loaded on longest sync'ed node1 # Node0 wallet file is loaded on longest sync'ed node1
self.stop_node(1) self.stop_node(1)
self.nodes[0].backupwallet(os.path.join(self.nodes[0].datadir, 'wallet.bak')) self.nodes[0].backupwallet(self.nodes[0].datadir_path / 'wallet.bak')
shutil.copyfile(os.path.join(self.nodes[0].datadir, 'wallet.bak'), os.path.join(self.nodes[1].chain_path, self.default_wallet_name, self.wallet_data_filename)) shutil.copyfile(self.nodes[0].datadir_path / 'wallet.bak', self.nodes[1].chain_path / self.default_wallet_name / self.wallet_data_filename)
self.start_node(1) self.start_node(1)
tx_after_reorg = self.nodes[1].gettransaction(txid) tx_after_reorg = self.nodes[1].gettransaction(txid)
# Check that normal confirmed tx is confirmed again but with different blockhash # Check that normal confirmed tx is confirmed again but with different blockhash