2018-10-07 18:24:20 +02:00
#!/usr/bin/env python3
2022-12-25 00:49:50 +01:00
# Copyright (c) 2018-2022 The Bitcoin Core developers
2018-10-07 18:24:20 +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. """
2020-10-29 20:55:37 +01:00
import random
import string
2018-10-07 18:24:20 +02:00
from test_framework . test_framework import BitcoinTestFramework
from test_framework . test_node import ErrorMatch
class FilelockTest ( BitcoinTestFramework ) :
2022-11-09 12:53:13 +01:00
def add_options ( self , parser ) :
self . add_wallet_options ( parser )
2018-10-07 18:24:20 +02:00
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 )
2020-09-29 02:24:06 +02:00
self . nodes [ 0 ] . start ( )
2018-10-07 18:24:20 +02:00
self . nodes [ 0 ] . wait_for_rpc_connection ( )
def run_test ( self ) :
2023-06-14 13:00:11 +02:00
datadir = self . nodes [ 0 ] . chain_path
2021-06-11 08:13:45 +02:00
self . log . info ( f " Using datadir { datadir } " )
2018-10-07 18:24:20 +02:00
self . log . info ( " Check that we can ' t start a second bitcoind instance using the same datadir " )
2021-06-11 08:13:45 +02:00
expected_msg = f " Error: Cannot obtain a lock on data directory { datadir } . { self . config [ ' environment ' ] [ ' PACKAGE_NAME ' ] } is probably already running. "
2023-09-02 07:09:43 +02:00
self . nodes [ 1 ] . assert_start_raises_init_error ( extra_args = [ f ' -datadir= { self . nodes [ 0 ] . datadir_path } ' , ' -noserver ' ] , expected_msg = expected_msg )
2018-10-07 18:24:20 +02:00
2023-11-27 11:29:02 +01:00
self . log . info ( " Check that cookie and PID file are not deleted when attempting to start a second bitcoind using the same datadir " )
2023-11-03 15:34:42 +01:00
cookie_file = datadir / " .cookie "
assert cookie_file . exists ( ) # should not be deleted during the second bitcoind instance shutdown
2023-11-27 11:29:02 +01:00
pid_file = datadir / " bitcoind.pid "
assert pid_file . exists ( )
2023-11-03 15:34:42 +01:00
2018-10-07 18:24:20 +02:00
if self . is_wallet_compiled ( ) :
2020-10-29 20:55:37 +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 )
2023-06-14 13:00:11 +02:00
wallet_dir = self . nodes [ 0 ] . wallets_path
2020-10-29 20:55:37 +01:00
self . log . info ( " Check that we can ' t start a second bitcoind instance using the same wallet " )
if descriptors :
2021-10-19 22:53:23 +02:00
expected_msg = f " Error: SQLiteDatabase: Unable to obtain an exclusive lock on the database, is it being used by another instance of { self . config [ ' environment ' ] [ ' PACKAGE_NAME ' ] } ? "
2020-10-29 20:55:37 +01:00
else :
expected_msg = " Error: Error initializing wallet database environment "
2021-06-11 08:13:45 +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 )
2020-10-29 20:55:37 +01:00
if self . is_bdb_compiled ( ) :
check_wallet_filelock ( False )
if self . is_sqlite_compiled ( ) :
check_wallet_filelock ( True )
2018-10-07 18:24:20 +02:00
if __name__ == ' __main__ ' :
FilelockTest ( ) . main ( )