2018-10-20 03:59:28 +02:00
|
|
|
#!/usr/bin/env python3
|
2023-08-16 19:27:31 +02:00
|
|
|
# Copyright (c) 2018-2020 The Bitcoin Core developers
|
2018-10-20 03:59:28 +02:00
|
|
|
# Distributed under the MIT software license, see the accompanying
|
|
|
|
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
"""Check that it's not possible to start a second bitcoind instance using the same datadir or wallet."""
|
|
|
|
import os
|
2023-02-14 09:50:58 +01:00
|
|
|
import random
|
|
|
|
import string
|
2018-10-20 03:59:28 +02:00
|
|
|
|
|
|
|
from test_framework.test_framework import BitcoinTestFramework
|
|
|
|
from test_framework.test_node import ErrorMatch
|
|
|
|
|
|
|
|
class FilelockTest(BitcoinTestFramework):
|
|
|
|
def set_test_params(self):
|
|
|
|
self.setup_clean_chain = True
|
|
|
|
self.num_nodes = 2
|
|
|
|
|
|
|
|
def setup_network(self):
|
|
|
|
self.add_nodes(self.num_nodes, extra_args=None)
|
2022-11-30 20:23:48 +01:00
|
|
|
self.nodes[0].start()
|
2018-10-20 03:59:28 +02:00
|
|
|
self.nodes[0].wait_for_rpc_connection()
|
|
|
|
|
|
|
|
def run_test(self):
|
2020-02-04 21:55:18 +01:00
|
|
|
datadir = os.path.join(self.nodes[0].datadir, self.chain)
|
2021-08-18 21:11:23 +02:00
|
|
|
self.log.info(f"Using datadir {datadir}")
|
2018-10-20 03:59:28 +02:00
|
|
|
|
|
|
|
self.log.info("Check that we can't start a second dashd instance using the same datadir")
|
2021-08-18 21:11:23 +02:00
|
|
|
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)
|
2018-10-20 03:59:28 +02:00
|
|
|
|
|
|
|
if self.is_wallet_compiled():
|
2021-02-05 14:17:04 +01:00
|
|
|
def check_wallet_filelock(descriptors):
|
|
|
|
wallet_name = ''.join([random.choice(string.ascii_lowercase) for _ in range(6)])
|
|
|
|
self.nodes[0].createwallet(wallet_name=wallet_name, descriptors=descriptors)
|
|
|
|
wallet_dir = os.path.join(datadir, 'wallets')
|
|
|
|
self.log.info("Check that we can't start a second dashd instance using the same wallet")
|
|
|
|
if descriptors:
|
|
|
|
expected_msg = "Error: SQLiteDatabase: Unable to obtain an exclusive lock on the database, is it being used by another dashd?"
|
|
|
|
else:
|
|
|
|
expected_msg = "Error: Error initializing wallet database environment"
|
2021-08-18 21:11:23 +02:00
|
|
|
self.nodes[1].assert_start_raises_init_error(extra_args=[f'-walletdir={wallet_dir}', f'-wallet={wallet_name}', '-noserver'], expected_msg=expected_msg, match=ErrorMatch.PARTIAL_REGEX)
|
2021-02-05 14:17:04 +01:00
|
|
|
|
2023-02-14 09:50:58 +01:00
|
|
|
if self.is_bdb_compiled():
|
2021-02-05 14:17:04 +01:00
|
|
|
check_wallet_filelock(False)
|
|
|
|
if self.is_sqlite_compiled():
|
|
|
|
check_wallet_filelock(True)
|
2018-10-20 03:59:28 +02:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
FilelockTest().main()
|