1 #!/usr/bin/env python3
2 # Copyright (c) 2018-2022 The Limenka developers
3 # Distributed under the MIT software license, see the accompanying
4 # file COPYING or http://www.opensource.org/licenses/mit-license.php.
5 """Check that it's not possible to start a second limenkad instance using the same datadir or wallet."""
6 import random
7 import string
8 9 from test_framework.test_framework import LimenkaTestFramework
10 from test_framework.test_node import (
11 LIMENKA_PID_FILENAME_DEFAULT,
12 ErrorMatch,
13 )
14 15 class FilelockTest(LimenkaTestFramework):
16 def add_options(self, parser):
17 self.add_wallet_options(parser)
18 19 def set_test_params(self):
20 self.setup_clean_chain = True
21 self.num_nodes = 2
22 23 def setup_network(self):
24 self.add_nodes(self.num_nodes, extra_args=None)
25 self.nodes[0].start()
26 self.nodes[0].wait_for_rpc_connection()
27 28 def run_test(self):
29 datadir = self.nodes[0].chain_path
30 blocksdir = self.nodes[0].blocks_path
31 self.log.info(f"Using datadir {datadir}")
32 self.log.info(f"Using blocksdir {blocksdir}")
33 34 self.log.info("Check that we can't start a second limenkad instance using the same datadir")
35 expected_msg = f"Error: Cannot obtain a lock on directory {datadir}. {self.config['environment']['CLIENT_NAME']} is probably already running."
36 self.nodes[1].assert_start_raises_init_error(extra_args=[f'-datadir={self.nodes[0].datadir_path}', '-noserver'], expected_msg=expected_msg)
37 38 self.log.info("Check that we can't start a second limenkad instance using the same blocksdir")
39 expected_msg = f"Error: Cannot obtain a lock on directory {blocksdir}. {self.config['environment']['CLIENT_NAME']} is probably already running."
40 self.nodes[1].assert_start_raises_init_error(extra_args=[f'-blocksdir={self.nodes[0].datadir_path}', '-noserver'], expected_msg=expected_msg)
41 42 self.log.info("Check that cookie and PID file are not deleted when attempting to start a second limenkad using the same datadir/blocksdir")
43 cookie_file = datadir / ".cookie"
44 assert cookie_file.exists() # should not be deleted during the second limenkad instance shutdown
45 pid_file = datadir / LIMENKA_PID_FILENAME_DEFAULT
46 assert pid_file.exists()
47 48 if self.is_wallet_compiled():
49 def check_wallet_filelock(descriptors):
50 wallet_name = ''.join([random.choice(string.ascii_lowercase) for _ in range(6)])
51 self.nodes[0].createwallet(wallet_name=wallet_name, descriptors=descriptors)
52 wallet_dir = self.nodes[0].wallets_path
53 self.log.info("Check that we can't start a second limenkad instance using the same wallet")
54 if descriptors:
55 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']['CLIENT_NAME']}?"
56 else:
57 expected_msg = "Error: Error initializing wallet database environment"
58 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)
59 60 if self.is_bdb_compiled():
61 check_wallet_filelock(False)
62 if self.is_sqlite_compiled():
63 check_wallet_filelock(True)
64 65 if __name__ == '__main__':
66 FilelockTest(__file__).main()
67