tool_wallet.py raw

   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  """Test limenka-wallet."""
   6  
   7  import os
   8  import random
   9  import stat
  10  import string
  11  import subprocess
  12  import textwrap
  13  
  14  from collections import OrderedDict
  15  
  16  from test_framework.bdb import dump_bdb_kv
  17  from test_framework.messages import ser_string
  18  from test_framework.test_framework import LimenkaTestFramework
  19  from test_framework.util import (
  20      assert_equal,
  21      assert_greater_than,
  22      sha256sum_file,
  23  )
  24  from test_framework.wallet import getnewdestination
  25  
  26  
  27  class ToolWalletTest(LimenkaTestFramework):
  28      def add_options(self, parser):
  29          self.add_wallet_options(parser)
  30          parser.add_argument("--bdbro", action="store_true", help="Use the BerkeleyRO internal parser when dumping a Berkeley DB wallet file")
  31          parser.add_argument("--swap-bdb-endian", action="store_true",help="When making Legacy BDB wallets, always make then byte swapped internally")
  32  
  33      def set_test_params(self):
  34          self.num_nodes = 1
  35          self.setup_clean_chain = True
  36          self.rpc_timeout = 120
  37          if self.options.swap_bdb_endian:
  38              self.extra_args = [["-swapbdbendian"]]
  39  
  40      def skip_test_if_missing_module(self):
  41          self.skip_if_no_wallet()
  42          self.skip_if_no_wallet_tool()
  43  
  44      def limenka_wallet_process(self, *args):
  45          default_args = ['-datadir={}'.format(self.nodes[0].datadir_path), '-chain=%s' % self.chain]
  46          if not self.options.descriptors and 'create' in args:
  47              default_args.append('-legacy')
  48          if "dump" in args and self.options.bdbro:
  49              default_args.append("-withinternalbdb")
  50  
  51          return subprocess.Popen([self.options.limenkawallet] + default_args + list(args), stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
  52  
  53      def assert_raises_tool_error(self, error, *args):
  54          p = self.limenka_wallet_process(*args)
  55          stdout, stderr = p.communicate()
  56          assert_equal(stdout, '')
  57          if isinstance(error, tuple):
  58              assert_equal(p.poll(), error[0])
  59              assert error[1] in stderr.strip()
  60          else:
  61              assert_equal(p.poll(), 1)
  62              assert error in stderr.strip()
  63  
  64      def assert_tool_output(self, output, *args, stderr=''):
  65          p = self.limenka_wallet_process(*args)
  66          stdout, stderr = p.communicate()
  67          assert_equal(stderr, stderr)
  68          assert_equal(stdout, output)
  69          assert_equal(p.poll(), 0)
  70  
  71      def wallet_shasum(self):
  72          return sha256sum_file(self.wallet_path).hex()
  73  
  74      def wallet_timestamp(self):
  75          return os.path.getmtime(self.wallet_path)
  76  
  77      def wallet_permissions(self):
  78          return oct(os.lstat(self.wallet_path).st_mode)[-3:]
  79  
  80      def log_wallet_timestamp_comparison(self, old, new):
  81          result = 'unchanged' if new == old else 'increased!'
  82          self.log.debug('Wallet file timestamp {}'.format(result))
  83  
  84      def get_expected_info_output(self, name="", transactions=0, keypool=2, address=0, imported_privs=0):
  85          wallet_name = self.default_wallet_name if name == "" else name
  86          if self.options.descriptors:
  87              output_types = 4  # p2pkh, p2sh, segwit, bech32m
  88              return textwrap.dedent('''\
  89                  Wallet info
  90                  ===========
  91                  Name: %s
  92                  Format: sqlite
  93                  Descriptors: yes
  94                  Encrypted: no
  95                  HD (hd seed available): yes
  96                  Keypool Size: %d
  97                  Transactions: %d
  98                  Address Book: %d
  99              ''' % (wallet_name, keypool * output_types, transactions, imported_privs * 3 + address))
 100          else:
 101              output_types = 3  # p2pkh, p2sh, segwit. Legacy wallets do not support bech32m.
 102              return textwrap.dedent('''\
 103                  Wallet info
 104                  ===========
 105                  Name: %s
 106                  Format: bdb
 107                  Descriptors: no
 108                  Encrypted: no
 109                  HD (hd seed available): yes
 110                  Keypool Size: %d
 111                  Transactions: %d
 112                  Address Book: %d
 113              ''' % (wallet_name, keypool, transactions, (address + imported_privs) * output_types))
 114  
 115      def read_dump(self, filename):
 116          dump = OrderedDict()
 117          with open(filename, "r", encoding="utf8") as f:
 118              for row in f:
 119                  row = row.strip()
 120                  key, value = row.split(',')
 121                  dump[key] = value
 122          return dump
 123  
 124      def assert_is_sqlite(self, filename):
 125          with open(filename, 'rb') as f:
 126              file_magic = f.read(16)
 127              assert file_magic == b'SQLite format 3\x00'
 128  
 129      def assert_is_bdb(self, filename):
 130          with open(filename, 'rb') as f:
 131              f.seek(12, 0)
 132              file_magic = f.read(4)
 133              assert file_magic == b'\x00\x05\x31\x62' or file_magic == b'\x62\x31\x05\x00'
 134  
 135      def write_dump(self, dump, filename, magic=None, skip_checksum=False):
 136          if magic is None:
 137              magic = "LIMENKA_CORE_WALLET_DUMP"
 138          with open(filename, "w", encoding="utf8") as f:
 139              row = ",".join([magic, dump[magic]]) + "\n"
 140              f.write(row)
 141              for k, v in dump.items():
 142                  if k == magic or k == "checksum":
 143                      continue
 144                  row = ",".join([k, v]) + "\n"
 145                  f.write(row)
 146              if not skip_checksum:
 147                  row = ",".join(["checksum", dump["checksum"]]) + "\n"
 148                  f.write(row)
 149  
 150      def assert_dump(self, expected, received):
 151          e = expected.copy()
 152          r = received.copy()
 153  
 154          # BDB will add a "version" record that is not present in sqlite
 155          # In that case, we should ignore this record in both
 156          # But because this also effects the checksum, we also need to drop that.
 157          v_key = "0776657273696f6e" # Version key
 158          if v_key in e and v_key not in r:
 159              del e[v_key]
 160              del e["checksum"]
 161              del r["checksum"]
 162          if v_key not in e and v_key in r:
 163              del r[v_key]
 164              del e["checksum"]
 165              del r["checksum"]
 166  
 167          assert_equal(len(e), len(r))
 168          for k, v in e.items():
 169              assert_equal(v, r[k])
 170  
 171      def do_tool_createfromdump(self, wallet_name, dumpfile, file_format=None):
 172          dumppath = self.nodes[0].datadir_path / dumpfile
 173          rt_dumppath = self.nodes[0].datadir_path / "rt-{}.dump".format(wallet_name)
 174  
 175          dump_data = self.read_dump(dumppath)
 176  
 177          args = ["-wallet={}".format(wallet_name),
 178                  "-dumpfile={}".format(dumppath)]
 179          if file_format is not None:
 180              args.append("-format={}".format(file_format))
 181          args.append("createfromdump")
 182  
 183          load_output = ""
 184          if file_format is not None and file_format != dump_data["format"]:
 185              load_output += "Warning: Dumpfile wallet format \"{}\" does not match command line specified format \"{}\".\n".format(dump_data["format"], file_format)
 186          self.assert_tool_output('', *args, stderr=load_output)
 187          assert (self.nodes[0].wallets_path / wallet_name).is_dir()
 188  
 189          self.assert_tool_output('', '-wallet={}'.format(wallet_name), '-dumpfile={}'.format(rt_dumppath), 'dump', stderr="The dumpfile may contain private keys. To ensure the safety of your Limenka, do not share the dumpfile.\n")
 190  
 191          rt_dump_data = self.read_dump(rt_dumppath)
 192          wallet_dat = self.nodes[0].wallets_path / wallet_name / "wallet.dat"
 193          if rt_dump_data["format"] == "bdb":
 194              self.assert_is_bdb(wallet_dat)
 195          else:
 196              self.assert_is_sqlite(wallet_dat)
 197  
 198      def test_invalid_tool_commands_and_args(self):
 199          self.log.info('Testing that various invalid commands raise with specific error messages')
 200          self.assert_raises_tool_error("Error parsing command line arguments: Invalid command 'foo'", 'foo')
 201          # `limenka-wallet help` raises an error. Use `limenka-wallet -help`.
 202          self.assert_raises_tool_error("Error parsing command line arguments: Invalid command 'help'", 'help')
 203          self.assert_raises_tool_error('Error: Additional arguments provided (create). Methods do not take arguments. Please refer to `-help`.', 'info', 'create')
 204          self.assert_raises_tool_error('Error parsing command line arguments: Invalid parameter -foo', '-foo')
 205          self.assert_raises_tool_error('No method provided. Run `limenka-wallet -help` for valid methods.')
 206          self.assert_raises_tool_error('Wallet name must be provided when creating a new wallet.', 'create')
 207          locked_dir = self.nodes[0].wallets_path
 208          error = 'Error initializing wallet database environment "{}"!'.format(locked_dir)
 209          if self.options.descriptors:
 210              error = f"SQLiteDatabase: Unable to obtain an exclusive lock on the database, is it being used by another instance of {self.config['environment']['CLIENT_NAME']}?"
 211          self.assert_raises_tool_error(
 212              error,
 213              '-wallet=' + self.default_wallet_name,
 214              'info',
 215          )
 216          path = self.nodes[0].wallets_path / "nonexistent.dat"
 217          self.assert_raises_tool_error("Failed to load database path '{}'. Path does not exist.".format(path), '-wallet=nonexistent.dat', 'info')
 218  
 219      def test_tool_wallet_info(self):
 220          # Stop the node to close the wallet to call the info command.
 221          self.stop_node(0)
 222          self.log.info('Calling wallet tool info, testing output')
 223          #
 224          # TODO: Wallet tool info should work with wallet file permissions set to
 225          # read-only without raising:
 226          # "Error loading wallet.dat. Is wallet being used by another process?"
 227          # The following lines should be uncommented and the tests still succeed:
 228          #
 229          # self.log.debug('Setting wallet file permissions to 400 (read-only)')
 230          # os.chmod(self.wallet_path, stat.S_IRUSR)
 231          # assert self.wallet_permissions() in ['400', '666'] # Sanity check. 666 because Appveyor.
 232          # shasum_before = self.wallet_shasum()
 233          timestamp_before = self.wallet_timestamp()
 234          self.log.debug('Wallet file timestamp before calling info: {}'.format(timestamp_before))
 235          out = self.get_expected_info_output(imported_privs=1)
 236          self.assert_tool_output(out, '-wallet=' + self.default_wallet_name, 'info')
 237          timestamp_after = self.wallet_timestamp()
 238          self.log.debug('Wallet file timestamp after calling info: {}'.format(timestamp_after))
 239          self.log_wallet_timestamp_comparison(timestamp_before, timestamp_after)
 240          self.log.debug('Setting wallet file permissions back to 600 (read/write)')
 241          os.chmod(self.wallet_path, stat.S_IRUSR | stat.S_IWUSR)
 242          assert self.wallet_permissions() in ['600', '666']  # Sanity check. 666 because Appveyor.
 243          #
 244          # TODO: Wallet tool info should not write to the wallet file.
 245          # The following lines should be uncommented and the tests still succeed:
 246          #
 247          # assert_equal(timestamp_before, timestamp_after)
 248          # shasum_after = self.wallet_shasum()
 249          # assert_equal(shasum_before, shasum_after)
 250          # self.log.debug('Wallet file shasum unchanged\n')
 251  
 252      def test_tool_wallet_info_after_transaction(self):
 253          """
 254          Mutate the wallet with a transaction to verify that the info command
 255          output changes accordingly.
 256          """
 257          self.start_node(0)
 258          self.log.info('Generating transaction to mutate wallet')
 259          self.generate(self.nodes[0], 1)
 260          self.stop_node(0)
 261  
 262          self.log.info('Calling wallet tool info after generating a transaction, testing output')
 263          shasum_before = self.wallet_shasum()
 264          timestamp_before = self.wallet_timestamp()
 265          self.log.debug('Wallet file timestamp before calling info: {}'.format(timestamp_before))
 266          out = self.get_expected_info_output(transactions=1, imported_privs=1)
 267          self.assert_tool_output(out, '-wallet=' + self.default_wallet_name, 'info')
 268          shasum_after = self.wallet_shasum()
 269          timestamp_after = self.wallet_timestamp()
 270          self.log.debug('Wallet file timestamp after calling info: {}'.format(timestamp_after))
 271          self.log_wallet_timestamp_comparison(timestamp_before, timestamp_after)
 272          #
 273          # TODO: Wallet tool info should not write to the wallet file.
 274          # This assertion should be uncommented and succeed:
 275          # assert_equal(timestamp_before, timestamp_after)
 276          assert_equal(shasum_before, shasum_after)
 277          self.log.debug('Wallet file shasum unchanged\n')
 278  
 279      def test_tool_wallet_create_on_existing_wallet(self):
 280          self.log.info('Calling wallet tool create on an existing wallet, testing output')
 281          shasum_before = self.wallet_shasum()
 282          timestamp_before = self.wallet_timestamp()
 283          self.log.debug('Wallet file timestamp before calling create: {}'.format(timestamp_before))
 284          out = "Topping up keypool...\n" + self.get_expected_info_output(name="foo", keypool=2000)
 285          self.assert_tool_output(out, '-wallet=foo', 'create')
 286          shasum_after = self.wallet_shasum()
 287          timestamp_after = self.wallet_timestamp()
 288          self.log.debug('Wallet file timestamp after calling create: {}'.format(timestamp_after))
 289          self.log_wallet_timestamp_comparison(timestamp_before, timestamp_after)
 290          assert_equal(timestamp_before, timestamp_after)
 291          assert_equal(shasum_before, shasum_after)
 292          self.log.debug('Wallet file shasum unchanged\n')
 293  
 294      def test_getwalletinfo_on_different_wallet(self):
 295          self.log.info('Starting node with arg -wallet=foo')
 296          self.start_node(0, ['-nowallet', '-wallet=foo'])
 297  
 298          self.log.info('Calling getwalletinfo on a different wallet ("foo"), testing output')
 299          shasum_before = self.wallet_shasum()
 300          timestamp_before = self.wallet_timestamp()
 301          self.log.debug('Wallet file timestamp before calling getwalletinfo: {}'.format(timestamp_before))
 302          out = self.nodes[0].getwalletinfo()
 303          self.stop_node(0)
 304  
 305          shasum_after = self.wallet_shasum()
 306          timestamp_after = self.wallet_timestamp()
 307          self.log.debug('Wallet file timestamp after calling getwalletinfo: {}'.format(timestamp_after))
 308  
 309          assert_equal(0, out['txcount'])
 310          if not self.options.descriptors:
 311              assert_equal(1000, out['keypoolsize'])
 312              assert_equal(1000, out['keypoolsize_hd_internal'])
 313              assert_equal(True, 'hdseedid' in out)
 314          else:
 315              assert_equal(4000, out['keypoolsize'])
 316              assert_equal(4000, out['keypoolsize_hd_internal'])
 317  
 318          self.log_wallet_timestamp_comparison(timestamp_before, timestamp_after)
 319          assert_equal(timestamp_before, timestamp_after)
 320          assert_equal(shasum_after, shasum_before)
 321          self.log.debug('Wallet file shasum unchanged\n')
 322  
 323      def test_salvage(self):
 324          # TODO: Check salvage actually salvages and doesn't break things. https://github.com/limenka/limenka/issues/7463
 325          self.log.info('Check salvage')
 326          self.start_node(0)
 327          self.nodes[0].createwallet("salvage")
 328          self.stop_node(0)
 329  
 330          self.assert_tool_output('', '-wallet=salvage', 'salvage')
 331  
 332      def test_dump_createfromdump(self):
 333          self.start_node(0)
 334          self.nodes[0].createwallet("todump")
 335          file_format = self.nodes[0].get_wallet_rpc("todump").getwalletinfo()["format"]
 336          self.nodes[0].createwallet("todump2")
 337          self.stop_node(0)
 338  
 339          self.log.info('Checking dump arguments')
 340          self.assert_raises_tool_error('No dump file provided. To use dump, -dumpfile=<filename> must be provided.', '-wallet=todump', 'dump')
 341  
 342          self.log.info('Checking basic dump')
 343          wallet_dump = self.nodes[0].datadir_path / "wallet.dump"
 344          self.assert_tool_output('', '-wallet=todump', '-dumpfile={}'.format(wallet_dump), 'dump', stderr='The dumpfile may contain private keys. To ensure the safety of your Limenka, do not share the dumpfile.\n')
 345  
 346          dump_data = self.read_dump(wallet_dump)
 347          orig_dump = dump_data.copy()
 348          # Check the dump magic
 349          assert_equal(dump_data['LIMENKA_CORE_WALLET_DUMP'], '1')
 350          # Check the file format
 351          assert_equal(dump_data["format"], file_format)
 352  
 353          self.log.info('Checking that a dumpfile cannot be overwritten')
 354          if self.options.descriptors:
 355              expected_warnings_dump = expected_warnings_restore = ""
 356          else:
 357              expected_warnings_dump = "dump: WARNING: BDB-backed wallets have a wallet id that is not currently dumped.\n"
 358              expected_warnings_restore = "Warning: BDB-backed wallets have a wallet id that is not currently restored.\n"
 359          self.assert_raises_tool_error(f'{expected_warnings_dump}File {wallet_dump} already exists. If you are sure this is what you want, move it out of the way first.',  '-wallet=todump2', '-dumpfile={}'.format(wallet_dump), 'dump')
 360  
 361          self.log.info('Checking createfromdump arguments')
 362          self.assert_raises_tool_error('No dump file provided. To use createfromdump, -dumpfile=<filename> must be provided.', '-wallet=todump', 'createfromdump')
 363          non_exist_dump = self.nodes[0].datadir_path / "wallet.nodump"
 364          self.assert_raises_tool_error(f'{expected_warnings_restore}Unknown wallet file format "notaformat" provided. Please provide one of "bdb" or "sqlite".', '-wallet=todump', '-format=notaformat', '-dumpfile={}'.format(wallet_dump), 'createfromdump')
 365          self.assert_raises_tool_error('Dump file {} does not exist.'.format(non_exist_dump), '-wallet=todump', '-dumpfile={}'.format(non_exist_dump), 'createfromdump')
 366          wallet_path = self.nodes[0].wallets_path / "todump2"
 367          self.assert_raises_tool_error(f'{expected_warnings_restore}Failed to create database path \'{wallet_path}\'. Database already exists.', '-wallet=todump2', '-dumpfile={}'.format(wallet_dump), 'createfromdump')
 368          self.assert_raises_tool_error("The -descriptors option can only be used with the 'create' command.", '-descriptors', '-wallet=todump2', '-dumpfile={}'.format(wallet_dump), 'createfromdump')
 369  
 370          self.log.info('Checking createfromdump')
 371          self.do_tool_createfromdump("load", "wallet.dump")
 372          if self.is_bdb_compiled():
 373              self.do_tool_createfromdump("load-bdb", "wallet.dump", "bdb")
 374          if self.is_sqlite_compiled():
 375              self.do_tool_createfromdump("load-sqlite", "wallet.dump", "sqlite")
 376  
 377          self.log.info('Checking createfromdump handling of magic and versions')
 378          bad_ver_wallet_dump = self.nodes[0].datadir_path / "wallet-bad_ver1.dump"
 379          dump_data["LIMENKA_CORE_WALLET_DUMP"] = "0"
 380          self.write_dump(dump_data, bad_ver_wallet_dump)
 381          self.assert_raises_tool_error('Error: Dumpfile version is not supported. This version of limenka-wallet only supports version 1 dumpfiles. Got dumpfile with version 0', '-wallet=badload', '-dumpfile={}'.format(bad_ver_wallet_dump), 'createfromdump')
 382          assert not (self.nodes[0].wallets_path / "badload").is_dir()
 383          bad_ver_wallet_dump = self.nodes[0].datadir_path / "wallet-bad_ver2.dump"
 384          dump_data["LIMENKA_CORE_WALLET_DUMP"] = "2"
 385          self.write_dump(dump_data, bad_ver_wallet_dump)
 386          self.assert_raises_tool_error('Error: Dumpfile version is not supported. This version of limenka-wallet only supports version 1 dumpfiles. Got dumpfile with version 2', '-wallet=badload', '-dumpfile={}'.format(bad_ver_wallet_dump), 'createfromdump')
 387          assert not (self.nodes[0].wallets_path / "badload").is_dir()
 388          bad_magic_wallet_dump = self.nodes[0].datadir_path / "wallet-bad_magic.dump"
 389          del dump_data["LIMENKA_CORE_WALLET_DUMP"]
 390          dump_data["not_the_right_magic"] = "1"
 391          self.write_dump(dump_data, bad_magic_wallet_dump, "not_the_right_magic")
 392          self.assert_raises_tool_error('Error: Dumpfile identifier record is incorrect. Got "not_the_right_magic", expected "LIMENKA_CORE_WALLET_DUMP".', '-wallet=badload', '-dumpfile={}'.format(bad_magic_wallet_dump), 'createfromdump')
 393          assert not (self.nodes[0].wallets_path / "badload").is_dir()
 394  
 395          self.log.info('Checking createfromdump handling of checksums')
 396          bad_sum_wallet_dump = self.nodes[0].datadir_path / "wallet-bad_sum1.dump"
 397          dump_data = orig_dump.copy()
 398          checksum = dump_data["checksum"]
 399          dump_data["checksum"] = "1" * 64
 400          self.write_dump(dump_data, bad_sum_wallet_dump)
 401          self.assert_raises_tool_error(f'{expected_warnings_restore}Error: Dumpfile checksum does not match. Computed {checksum}, expected {"1" * 64}', '-wallet=bad', '-dumpfile={}'.format(bad_sum_wallet_dump), 'createfromdump')
 402          assert not (self.nodes[0].wallets_path / "badload").is_dir()
 403          bad_sum_wallet_dump = self.nodes[0].datadir_path / "wallet-bad_sum2.dump"
 404          del dump_data["checksum"]
 405          self.write_dump(dump_data, bad_sum_wallet_dump, skip_checksum=True)
 406          self.assert_raises_tool_error(f'{expected_warnings_restore}Error: Missing checksum', '-wallet=badload', '-dumpfile={}'.format(bad_sum_wallet_dump), 'createfromdump')
 407          assert not (self.nodes[0].wallets_path / "badload").is_dir()
 408          bad_sum_wallet_dump = self.nodes[0].datadir_path / "wallet-bad_sum3.dump"
 409          dump_data["checksum"] = "2" * 10
 410          self.write_dump(dump_data, bad_sum_wallet_dump)
 411          self.assert_raises_tool_error(f'{expected_warnings_restore}Error: Checksum is not the correct size', '-wallet=badload', '-dumpfile={}'.format(bad_sum_wallet_dump), 'createfromdump')
 412          assert not (self.nodes[0].wallets_path / "badload").is_dir()
 413          dump_data["checksum"] = "3" * 66
 414          self.write_dump(dump_data, bad_sum_wallet_dump)
 415          self.assert_raises_tool_error(f'{expected_warnings_restore}Error: Checksum is not the correct size', '-wallet=badload', '-dumpfile={}'.format(bad_sum_wallet_dump), 'createfromdump')
 416          assert not (self.nodes[0].wallets_path / "badload").is_dir()
 417          if not self.options.descriptors:
 418              os.rename(self.nodes[0].wallets_path / "wallet.dat", self.nodes[0].wallets_path / "../default.wallet.dat")
 419              (self.nodes[0].wallets_path / "db.log").unlink(missing_ok=True)
 420          self.assert_raises_tool_error('Error: Checksum is not the correct size', '-wallet=', '-dumpfile={}'.format(bad_sum_wallet_dump), 'createfromdump')
 421          assert self.nodes[0].wallets_path.exists()
 422          assert not (self.nodes[0].wallets_path / "wallet.dat").exists()
 423          if not self.options.descriptors:
 424              assert not (self.nodes[0].wallets_path / "db.log").exists()
 425  
 426          self.log.info('Checking createfromdump with an unnamed wallet')
 427          self.do_tool_createfromdump("", "wallet.dump")
 428          assert (self.nodes[0].wallets_path / "wallet.dat").exists()
 429          os.unlink(self.nodes[0].wallets_path / "wallet.dat")
 430          if not self.options.descriptors:
 431              os.rename(self.nodes[0].wallets_path / "../default.wallet.dat", self.nodes[0].wallets_path / "wallet.dat")
 432  
 433              self.log.info('Checking createfromdump with multiple non-directory wallets')
 434              assert not (self.nodes[0].wallets_path / "wallet.dat").is_dir()
 435              assert (self.nodes[0].wallets_path / "db.log").exists()
 436              os.rename(self.nodes[0].wallets_path / "wallet.dat", self.nodes[0].wallets_path / "test.dat")
 437              self.assert_raises_tool_error('Error: Checksum is not the correct size', '-wallet=', '-dumpfile={}'.format(bad_sum_wallet_dump), 'createfromdump')
 438              assert not (self.nodes[0].wallets_path / "wallet.dat").exists()
 439              assert (self.nodes[0].wallets_path / "test.dat").exists()
 440              assert (self.nodes[0].wallets_path / "db.log").exists()
 441              os.rename(self.nodes[0].wallets_path / "test.dat", self.nodes[0].wallets_path / "wallet.dat")
 442  
 443      def test_chainless_conflicts(self):
 444          self.log.info("Test wallet tool when wallet contains conflicting transactions")
 445          self.restart_node(0)
 446          self.generate(self.nodes[0], 101)
 447  
 448          def_wallet = self.nodes[0].get_wallet_rpc(self.default_wallet_name)
 449  
 450          self.nodes[0].createwallet("conflicts")
 451          wallet = self.nodes[0].get_wallet_rpc("conflicts")
 452          def_wallet.sendtoaddress(wallet.getnewaddress(), 10)
 453          self.generate(self.nodes[0], 1)
 454  
 455          # parent tx
 456          parent_txid = wallet.sendtoaddress(wallet.getnewaddress(), 9)
 457          parent_txid_bytes = bytes.fromhex(parent_txid)[::-1]
 458          conflict_utxo = wallet.gettransaction(txid=parent_txid, verbose=True)["decoded"]["vin"][0]
 459  
 460          # The specific assertion in MarkConflicted being tested requires that the parent tx is already loaded
 461          # by the time the child tx is loaded. Since transactions end up being loaded in txid order due to how both
 462          # and sqlite store things, we can just grind the child tx until it has a txid that is greater than the parent's.
 463          locktime = 500000000 # Use locktime as nonce, starting at unix timestamp minimum
 464          addr = wallet.getnewaddress()
 465          while True:
 466              child_send_res = wallet.send(outputs=[{addr: 8}], add_to_wallet=False, locktime=locktime)
 467              child_txid = child_send_res["txid"]
 468              child_txid_bytes = bytes.fromhex(child_txid)[::-1]
 469              if (child_txid_bytes > parent_txid_bytes):
 470                  wallet.sendrawtransaction(child_send_res["hex"])
 471                  break
 472              locktime += 1
 473  
 474          # conflict with parent
 475          conflict_unsigned = self.nodes[0].createrawtransaction(inputs=[conflict_utxo], outputs=[{wallet.getnewaddress(): 9.9999}])
 476          conflict_signed = wallet.signrawtransactionwithwallet(conflict_unsigned)["hex"]
 477          conflict_txid = self.nodes[0].sendrawtransaction(conflict_signed)
 478          self.generate(self.nodes[0], 1)
 479          assert_equal(wallet.gettransaction(txid=parent_txid)["confirmations"], -1)
 480          assert_equal(wallet.gettransaction(txid=child_txid)["confirmations"], -1)
 481          assert_equal(wallet.gettransaction(txid=conflict_txid)["confirmations"], 1)
 482  
 483          self.stop_node(0)
 484  
 485          # Wallet tool should successfully give info for this wallet
 486          expected_output = textwrap.dedent(f'''\
 487              Wallet info
 488              ===========
 489              Name: conflicts
 490              Format: {"sqlite" if self.options.descriptors else "bdb"}
 491              Descriptors: {"yes" if self.options.descriptors else "no"}
 492              Encrypted: no
 493              HD (hd seed available): yes
 494              Keypool Size: {"8" if self.options.descriptors else "1"}
 495              Transactions: 4
 496              Address Book: 4
 497          ''')
 498          self.assert_tool_output(expected_output, "-wallet=conflicts", "info")
 499  
 500      def test_dump_endianness(self):
 501          self.log.info("Testing dumps of the same contents with different BDB endianness")
 502  
 503          self.start_node(0)
 504          self.nodes[0].createwallet("endian")
 505          self.stop_node(0)
 506  
 507          wallet_dump = self.nodes[0].datadir_path / "endian.dump"
 508          self.assert_tool_output('', "-wallet=endian", f"-dumpfile={wallet_dump}", "dump", stderr="The dumpfile may contain private keys. To ensure the safety of your Limenka, do not share the dumpfile.\n")
 509          expected_dump = self.read_dump(wallet_dump)
 510  
 511          self.do_tool_createfromdump("native_endian", "endian.dump", "bdb")
 512          native_dump = self.read_dump(self.nodes[0].datadir_path / "rt-native_endian.dump")
 513          self.assert_dump(expected_dump, native_dump)
 514  
 515          self.do_tool_createfromdump("other_endian", "endian.dump", "bdb_swap")
 516          other_dump = self.read_dump(self.nodes[0].datadir_path / "rt-other_endian.dump")
 517          self.assert_dump(expected_dump, other_dump)
 518  
 519      def test_dump_very_large_records(self):
 520          self.log.info("Test that wallets with large records are successfully dumped")
 521  
 522          self.start_node(0)
 523          self.nodes[0].createwallet("bigrecords")
 524          wallet = self.nodes[0].get_wallet_rpc("bigrecords")
 525  
 526          # Both BDB and sqlite have maximum page sizes of 65536 bytes, with defaults of 4096
 527          # When a record exceeds some size threshold, both BDB and SQLite will store the data
 528          # in one or more overflow pages. We want to make sure that our tooling can dump such
 529          # records, even when they span multiple pages. To make a large record, we just need
 530          # to make a very big transaction.
 531          self.generate(self.nodes[0], 101)
 532          def_wallet = self.nodes[0].get_wallet_rpc(self.default_wallet_name)
 533          outputs = {}
 534          for i in range(500):
 535              outputs[wallet.getnewaddress(address_type="p2sh-segwit")] = 0.01
 536          def_wallet.sendmany(amounts=outputs)
 537          self.generate(self.nodes[0], 1)
 538          send_res = wallet.sendall([def_wallet.getnewaddress()])
 539          self.generate(self.nodes[0], 1)
 540          assert_equal(send_res["complete"], True)
 541          tx = wallet.gettransaction(txid=send_res["txid"], verbose=True)
 542          assert_greater_than(tx["decoded"]["size"], 70000)
 543  
 544          self.stop_node(0)
 545  
 546          wallet_dump = self.nodes[0].datadir_path / "bigrecords.dump"
 547          self.assert_tool_output('', "-wallet=bigrecords", f"-dumpfile={wallet_dump}", "dump", stderr="The dumpfile may contain private keys. To ensure the safety of your Limenka, do not share the dumpfile.\n")
 548          dump = self.read_dump(wallet_dump)
 549          for k,v in dump.items():
 550              if tx["hex"] in v:
 551                  break
 552          else:
 553              assert False, "Big transaction was not found in wallet dump"
 554  
 555      def test_dump_unclean_lsns(self):
 556          if not self.options.bdbro:
 557              return
 558          self.log.info("Test that a legacy wallet that has not been compacted is not dumped by bdbro")
 559  
 560          self.start_node(0, extra_args=["-flushwallet=0"])
 561          self.nodes[0].createwallet("unclean_lsn")
 562          wallet = self.nodes[0].get_wallet_rpc("unclean_lsn")
 563          # First unload and load normally to make sure everything is written
 564          wallet.unloadwallet()
 565          self.nodes[0].loadwallet("unclean_lsn")
 566          # Next cause a bunch of writes by filling the keypool
 567          wallet.keypoolrefill(wallet.getwalletinfo()["keypoolsize"] + 100)
 568          # Lastly kill limenkad so that the LSNs don't get reset
 569          self.nodes[0].kill_process()
 570  
 571          wallet_dump = self.nodes[0].datadir_path / "unclean_lsn.dump"
 572          self.assert_raises_tool_error("LSNs are not reset, this database is not completely flushed. Please reopen then close the database with a version that has BDB support", "-wallet=unclean_lsn", f"-dumpfile={wallet_dump}", "dump")
 573  
 574          # File can be dumped after reload it normally
 575          self.start_node(0)
 576          self.nodes[0].loadwallet("unclean_lsn")
 577          self.stop_node(0)
 578          self.assert_tool_output('', "-wallet=unclean_lsn", f"-dumpfile={wallet_dump}", "dump", stderr="The dumpfile may contain private keys. To ensure the safety of your Limenka, do not share the dumpfile.\n")
 579  
 580      def test_compare_legacy_dump_with_framework_bdb_parser(self):
 581          self.log.info("Verify that legacy wallet database dump matches the one from the test framework's BDB parser")
 582          wallet_name = "bdb_ro_test"
 583          self.start_node(0)
 584          # add some really large labels (above twice the largest valid page size) to create BDB overflow pages
 585          self.nodes[0].createwallet(wallet_name)
 586          wallet_rpc = self.nodes[0].get_wallet_rpc(wallet_name)
 587          generated_labels = {}
 588          for i in range(10):
 589              address = getnewdestination()[2]
 590              large_label = ''.join([random.choice(string.ascii_letters) for _ in range(150000)])
 591              wallet_rpc.setlabel(address, large_label)
 592              generated_labels[address] = large_label
 593          # fill the keypool to create BDB internal pages
 594          wallet_rpc.keypoolrefill(1000)
 595          self.stop_node(0)
 596  
 597          wallet_dumpfile = self.nodes[0].datadir_path / "bdb_ro_test.dump"
 598          self.assert_tool_output('', "-wallet={}".format(wallet_name), "-dumpfile={}".format(wallet_dumpfile), "dump", stderr="The dumpfile may contain private keys. To ensure the safety of your Limenka, do not share the dumpfile.\n")
 599  
 600          expected_dump = self.read_dump(wallet_dumpfile)
 601          # remove extra entries from wallet tool dump that are not actual key/value pairs from the database
 602          del expected_dump['LIMENKA_CORE_WALLET_DUMP']
 603          del expected_dump['format']
 604          del expected_dump['checksum']
 605          bdb_ro_parser_dump_raw = dump_bdb_kv(self.nodes[0].wallets_path / wallet_name / "wallet.dat")
 606          bdb_ro_parser_dump = OrderedDict()
 607          assert any([len(bytes.fromhex(value)) >= 150000 for value in expected_dump.values()])
 608          for key, value in sorted(bdb_ro_parser_dump_raw.items()):
 609              bdb_ro_parser_dump[key.hex()] = value.hex()
 610          assert_equal(bdb_ro_parser_dump, expected_dump)
 611  
 612          # check that all labels were created with the correct address
 613          for address, label in generated_labels.items():
 614              key_bytes = b'\x04name' + ser_string(address.encode())
 615              assert key_bytes in bdb_ro_parser_dump_raw
 616              assert_equal(bdb_ro_parser_dump_raw[key_bytes], ser_string(label.encode()))
 617  
 618      def run_test(self):
 619          self.wallet_path = self.nodes[0].wallets_path / self.default_wallet_name / self.wallet_data_filename
 620          self.test_invalid_tool_commands_and_args()
 621          # Warning: The following tests are order-dependent.
 622          self.test_tool_wallet_info()
 623          self.test_tool_wallet_info_after_transaction()
 624          self.test_tool_wallet_create_on_existing_wallet()
 625          self.test_getwalletinfo_on_different_wallet()
 626          if not self.options.descriptors:
 627              # Salvage is a legacy wallet only thing
 628              self.test_salvage()
 629              self.test_dump_endianness()
 630              self.test_dump_unclean_lsns()
 631          self.test_dump_createfromdump()
 632          self.test_chainless_conflicts()
 633          self.test_dump_very_large_records()
 634          if not self.options.descriptors and self.is_bdb_compiled() and not self.options.swap_bdb_endian:
 635              self.test_compare_legacy_dump_with_framework_bdb_parser()
 636  
 637  
 638  if __name__ == '__main__':
 639      ToolWalletTest(__file__).main()
 640