LCOV - code coverage report
Current view: top level - src/wallet - dump.cpp (source / functions) Coverage Total Hit
Test: fuzz_coverage.info Lines: 24.5 % 200 49
Test Date: 2024-07-04 04:02:30 Functions: 33.3 % 3 1
Branches: 13.4 % 387 52

             Branch data     Line data    Source code
       1                 :             : // Copyright (c) 2020-2022 The Bitcoin Core developers
       2                 :             : // Distributed under the MIT software license, see the accompanying
       3                 :             : // file COPYING or http://www.opensource.org/licenses/mit-license.php.
       4                 :             : 
       5                 :             : #include <wallet/dump.h>
       6                 :             : 
       7                 :             : #include <common/args.h>
       8                 :             : #include <util/fs.h>
       9                 :             : #include <util/translation.h>
      10                 :             : #include <wallet/wallet.h>
      11                 :             : #include <wallet/walletdb.h>
      12                 :             : 
      13                 :             : #include <algorithm>
      14                 :             : #include <fstream>
      15                 :             : #include <memory>
      16                 :             : #include <string>
      17                 :             : #include <utility>
      18                 :             : #include <vector>
      19                 :             : 
      20                 :             : namespace wallet {
      21         [ +  - ]:           1 : static const std::string DUMP_MAGIC = "BITCOIN_CORE_WALLET_DUMP";
      22                 :             : uint32_t DUMP_VERSION = 1;
      23                 :             : 
      24                 :           3 : bool DumpWallet(const ArgsManager& args, WalletDatabase& db, bilingual_str& error)
      25                 :             : {
      26                 :             :     // Get the dumpfile
      27   [ +  -  +  -  :           3 :     std::string dump_filename = args.GetArg("-dumpfile", "");
                   +  - ]
      28         [ -  + ]:           3 :     if (dump_filename.empty()) {
      29         [ #  # ]:           0 :         error = _("No dump file provided. To use dump, -dumpfile=<filename> must be provided.");
      30                 :           0 :         return false;
      31                 :             :     }
      32                 :             : 
      33         [ +  - ]:           3 :     fs::path path = fs::PathFromString(dump_filename);
      34         [ +  - ]:           3 :     path = fs::absolute(path);
      35   [ +  -  -  + ]:           3 :     if (fs::exists(path)) {
      36   [ #  #  #  #  :           0 :         error = strprintf(_("File %s already exists. If you are sure this is what you want, move it out of the way first."), fs::PathToString(path));
                   #  # ]
      37                 :           0 :         return false;
      38                 :             :     }
      39         [ +  - ]:           3 :     std::ofstream dump_file;
      40         [ +  - ]:           3 :     dump_file.open(path);
      41   [ +  -  -  + ]:           3 :     if (dump_file.fail()) {
      42   [ #  #  #  #  :           0 :         error = strprintf(_("Unable to open %s for writing"), fs::PathToString(path));
                   #  # ]
      43                 :           0 :         return false;
      44                 :             :     }
      45                 :             : 
      46         [ +  - ]:           3 :     HashWriter hasher{};
      47                 :             : 
      48         [ +  - ]:           3 :     std::unique_ptr<DatabaseBatch> batch = db.MakeBatch();
      49                 :             : 
      50                 :           3 :     bool ret = true;
      51         [ +  - ]:           3 :     std::unique_ptr<DatabaseCursor> cursor = batch->GetNewCursor();
      52         [ +  - ]:           3 :     if (!cursor) {
      53         [ #  # ]:           0 :         error = _("Error: Couldn't create cursor into database");
      54                 :           0 :         ret = false;
      55                 :           0 :     }
      56                 :             : 
      57                 :             :     // Write out a magic string with version
      58         [ +  - ]:           3 :     std::string line = strprintf("%s,%u\n", DUMP_MAGIC, DUMP_VERSION);
      59         [ +  - ]:           3 :     dump_file.write(line.data(), line.size());
      60   [ +  -  +  - ]:           3 :     hasher << Span{line};
      61                 :             : 
      62                 :             :     // Write out the file format
      63         [ +  - ]:           3 :     std::string format = db.Format();
      64                 :             :     // BDB files that are opened using BerkeleyRODatabase have it's format as "bdb_ro"
      65                 :             :     // We want to override that format back to "bdb"
      66   [ +  -  +  - ]:           3 :     if (format == "bdb_ro") {
      67         [ +  - ]:           3 :         format = "bdb";
      68                 :           3 :     }
      69         [ +  - ]:           3 :     line = strprintf("%s,%s\n", "format", format);
      70         [ +  - ]:           3 :     dump_file.write(line.data(), line.size());
      71   [ +  -  +  - ]:           3 :     hasher << Span{line};
      72                 :             : 
      73         [ -  + ]:           3 :     if (ret) {
      74                 :             : 
      75                 :             :         // Read the records
      76                 :           4 :         while (true) {
      77         [ -  + ]:           4 :             DataStream ss_key{};
      78         [ -  + ]:           4 :             DataStream ss_value{};
      79         [ -  + ]:           4 :             DatabaseCursor::Status status = cursor->Next(ss_key, ss_value);
      80         [ +  + ]:           4 :             if (status == DatabaseCursor::Status::DONE) {
      81                 :           3 :                 ret = true;
      82                 :           3 :                 break;
      83         [ +  - ]:           1 :             } else if (status == DatabaseCursor::Status::FAIL) {
      84         [ #  # ]:           0 :                 error = _("Error reading next record from wallet database");
      85                 :           0 :                 ret = false;
      86                 :           0 :                 break;
      87                 :             :             }
      88   [ +  -  +  - ]:           1 :             std::string key_str = HexStr(ss_key);
      89   [ +  -  +  - ]:           1 :             std::string value_str = HexStr(ss_value);
      90         [ +  - ]:           1 :             line = strprintf("%s,%s\n", key_str, value_str);
      91         [ +  - ]:           1 :             dump_file.write(line.data(), line.size());
      92   [ +  -  +  - ]:           1 :             hasher << Span{line};
      93      [ +  -  + ]:           4 :         }
      94                 :           3 :     }
      95                 :             : 
      96                 :           3 :     cursor.reset();
      97                 :           3 :     batch.reset();
      98                 :             : 
      99         [ -  + ]:           3 :     if (ret) {
     100                 :             :         // Write the hash
     101   [ +  -  +  -  :           3 :         tfm::format(dump_file, "checksum,%s\n", HexStr(hasher.GetHash()));
             +  -  +  - ]
     102         [ +  - ]:           3 :         dump_file.close();
     103                 :           3 :     } else {
     104                 :             :         // Remove the dumpfile on failure
     105         [ #  # ]:           0 :         dump_file.close();
     106         [ #  # ]:           0 :         fs::remove(path);
     107                 :             :     }
     108                 :             : 
     109                 :           3 :     return ret;
     110                 :           3 : }
     111                 :             : 
     112                 :             : // The standard wallet deleter function blocks on the validation interface
     113                 :             : // queue, which doesn't exist for the bitcoin-wallet. Define our own
     114                 :             : // deleter here.
     115                 :           0 : static void WalletToolReleaseWallet(CWallet* wallet)
     116                 :             : {
     117                 :           0 :     wallet->WalletLogPrintf("Releasing wallet\n");
     118                 :           0 :     wallet->Close();
     119         [ #  # ]:           0 :     delete wallet;
     120                 :           0 : }
     121                 :             : 
     122                 :           0 : bool CreateFromDump(const ArgsManager& args, const std::string& name, const fs::path& wallet_path, bilingual_str& error, std::vector<bilingual_str>& warnings)
     123                 :             : {
     124                 :             :     // Get the dumpfile
     125   [ #  #  #  #  :           0 :     std::string dump_filename = args.GetArg("-dumpfile", "");
                   #  # ]
     126         [ #  # ]:           0 :     if (dump_filename.empty()) {
     127         [ #  # ]:           0 :         error = _("No dump file provided. To use createfromdump, -dumpfile=<filename> must be provided.");
     128                 :           0 :         return false;
     129                 :             :     }
     130                 :             : 
     131         [ #  # ]:           0 :     fs::path dump_path = fs::PathFromString(dump_filename);
     132         [ #  # ]:           0 :     dump_path = fs::absolute(dump_path);
     133   [ #  #  #  # ]:           0 :     if (!fs::exists(dump_path)) {
     134   [ #  #  #  #  :           0 :         error = strprintf(_("Dump file %s does not exist."), fs::PathToString(dump_path));
                   #  # ]
     135                 :           0 :         return false;
     136                 :             :     }
     137         [ #  # ]:           0 :     std::ifstream dump_file{dump_path};
     138                 :             : 
     139                 :             :     // Compute the checksum
     140         [ #  # ]:           0 :     HashWriter hasher{};
     141         [ #  # ]:           0 :     uint256 checksum;
     142                 :             : 
     143                 :             :     // Check the magic and version
     144                 :           0 :     std::string magic_key;
     145         [ #  # ]:           0 :     std::getline(dump_file, magic_key, ',');
     146                 :           0 :     std::string version_value;
     147         [ #  # ]:           0 :     std::getline(dump_file, version_value, '\n');
     148         [ #  # ]:           0 :     if (magic_key != DUMP_MAGIC) {
     149   [ #  #  #  # ]:           0 :         error = strprintf(_("Error: Dumpfile identifier record is incorrect. Got \"%s\", expected \"%s\"."), magic_key, DUMP_MAGIC);
     150         [ #  # ]:           0 :         dump_file.close();
     151                 :           0 :         return false;
     152                 :             :     }
     153                 :             :     // Check the version number (value of first record)
     154                 :           0 :     uint32_t ver;
     155   [ #  #  #  # ]:           0 :     if (!ParseUInt32(version_value, &ver)) {
     156   [ #  #  #  # ]:           0 :         error =strprintf(_("Error: Unable to parse version %u as a uint32_t"), version_value);
     157         [ #  # ]:           0 :         dump_file.close();
     158                 :           0 :         return false;
     159                 :             :     }
     160         [ #  # ]:           0 :     if (ver != DUMP_VERSION) {
     161   [ #  #  #  # ]:           0 :         error = strprintf(_("Error: Dumpfile version is not supported. This version of bitcoin-wallet only supports version 1 dumpfiles. Got dumpfile with version %s"), version_value);
     162         [ #  # ]:           0 :         dump_file.close();
     163                 :           0 :         return false;
     164                 :             :     }
     165         [ #  # ]:           0 :     std::string magic_hasher_line = strprintf("%s,%s\n", magic_key, version_value);
     166   [ #  #  #  # ]:           0 :     hasher << Span{magic_hasher_line};
     167                 :             : 
     168                 :             :     // Get the stored file format
     169                 :           0 :     std::string format_key;
     170         [ #  # ]:           0 :     std::getline(dump_file, format_key, ',');
     171                 :           0 :     std::string format_value;
     172         [ #  # ]:           0 :     std::getline(dump_file, format_value, '\n');
     173   [ #  #  #  # ]:           0 :     if (format_key != "format") {
     174   [ #  #  #  # ]:           0 :         error = strprintf(_("Error: Dumpfile format record is incorrect. Got \"%s\", expected \"format\"."), format_key);
     175         [ #  # ]:           0 :         dump_file.close();
     176                 :           0 :         return false;
     177                 :             :     }
     178                 :             :     // Get the data file format with format_value as the default
     179   [ #  #  #  # ]:           0 :     std::string file_format = args.GetArg("-format", format_value);
     180         [ #  # ]:           0 :     if (file_format.empty()) {
     181         [ #  # ]:           0 :         error = _("No wallet file format provided. To use createfromdump, -format=<format> must be provided.");
     182                 :           0 :         return false;
     183                 :             :     }
     184                 :           0 :     DatabaseFormat data_format;
     185   [ #  #  #  # ]:           0 :     if (file_format == "bdb") {
     186                 :           0 :         data_format = DatabaseFormat::BERKELEY;
     187   [ #  #  #  # ]:           0 :     } else if (file_format == "sqlite") {
     188                 :           0 :         data_format = DatabaseFormat::SQLITE;
     189   [ #  #  #  # ]:           0 :     } else if (file_format == "bdb_swap") {
     190                 :           0 :         data_format = DatabaseFormat::BERKELEY_SWAP;
     191                 :           0 :     } else {
     192   [ #  #  #  # ]:           0 :         error = strprintf(_("Unknown wallet file format \"%s\" provided. Please provide one of \"bdb\" or \"sqlite\"."), file_format);
     193                 :           0 :         return false;
     194                 :             :     }
     195         [ #  # ]:           0 :     if (file_format != format_value) {
     196   [ #  #  #  #  :           0 :         warnings.push_back(strprintf(_("Warning: Dumpfile wallet format \"%s\" does not match command line specified format \"%s\"."), format_value, file_format));
                   #  # ]
     197                 :           0 :     }
     198         [ #  # ]:           0 :     std::string format_hasher_line = strprintf("%s,%s\n", format_key, format_value);
     199   [ #  #  #  # ]:           0 :     hasher << Span{format_hasher_line};
     200                 :             : 
     201                 :           0 :     DatabaseOptions options;
     202                 :           0 :     DatabaseStatus status;
     203         [ #  # ]:           0 :     ReadDatabaseArgs(args, options);
     204                 :           0 :     options.require_create = true;
     205                 :           0 :     options.require_format = data_format;
     206         [ #  # ]:           0 :     std::unique_ptr<WalletDatabase> database = MakeDatabase(wallet_path, options, status, error);
     207         [ #  # ]:           0 :     if (!database) return false;
     208                 :             : 
     209                 :             :     // dummy chain interface
     210                 :           0 :     bool ret = true;
     211   [ #  #  #  #  :           0 :     std::shared_ptr<CWallet> wallet(new CWallet(/*chain=*/nullptr, name, std::move(database)), WalletToolReleaseWallet);
             #  #  #  # ]
     212                 :             :     {
     213   [ #  #  #  # ]:           0 :         LOCK(wallet->cs_wallet);
     214         [ #  # ]:           0 :         DBErrors load_wallet_ret = wallet->LoadWallet();
     215         [ #  # ]:           0 :         if (load_wallet_ret != DBErrors::LOAD_OK) {
     216   [ #  #  #  # ]:           0 :             error = strprintf(_("Error creating %s"), name);
     217                 :           0 :             return false;
     218                 :             :         }
     219                 :             : 
     220                 :             :         // Get the database handle
     221         [ #  # ]:           0 :         WalletDatabase& db = wallet->GetDatabase();
     222         [ #  # ]:           0 :         std::unique_ptr<DatabaseBatch> batch = db.MakeBatch();
     223         [ #  # ]:           0 :         batch->TxnBegin();
     224                 :             : 
     225                 :             :         // Read the records from the dump file and write them to the database
     226   [ #  #  #  # ]:           0 :         while (dump_file.good()) {
     227                 :           0 :             std::string key;
     228         [ #  # ]:           0 :             std::getline(dump_file, key, ',');
     229                 :           0 :             std::string value;
     230         [ #  # ]:           0 :             std::getline(dump_file, value, '\n');
     231                 :             : 
     232   [ #  #  #  # ]:           0 :             if (key == "checksum") {
     233         [ #  # ]:           0 :                 std::vector<unsigned char> parsed_checksum = ParseHex(value);
     234   [ #  #  #  # ]:           0 :                 if (parsed_checksum.size() != checksum.size()) {
     235   [ #  #  #  # ]:           0 :                     error = Untranslated("Error: Checksum is not the correct size");
     236                 :           0 :                     ret = false;
     237                 :           0 :                     break;
     238                 :             :                 }
     239   [ #  #  #  # ]:           0 :                 std::copy(parsed_checksum.begin(), parsed_checksum.end(), checksum.begin());
     240                 :           0 :                 break;
     241                 :           0 :             }
     242                 :             : 
     243         [ #  # ]:           0 :             std::string line = strprintf("%s,%s\n", key, value);
     244   [ #  #  #  # ]:           0 :             hasher << Span{line};
     245                 :             : 
     246   [ #  #  #  # ]:           0 :             if (key.empty() || value.empty()) {
     247                 :           0 :                 continue;
     248                 :             :             }
     249                 :             : 
     250   [ #  #  #  # ]:           0 :             if (!IsHex(key)) {
     251   [ #  #  #  # ]:           0 :                 error = strprintf(_("Error: Got key that was not hex: %s"), key);
     252                 :           0 :                 ret = false;
     253                 :           0 :                 break;
     254                 :             :             }
     255   [ #  #  #  # ]:           0 :             if (!IsHex(value)) {
     256   [ #  #  #  # ]:           0 :                 error = strprintf(_("Error: Got value that was not hex: %s"), value);
     257                 :           0 :                 ret = false;
     258                 :           0 :                 break;
     259                 :             :             }
     260                 :             : 
     261         [ #  # ]:           0 :             std::vector<unsigned char> k = ParseHex(key);
     262         [ #  # ]:           0 :             std::vector<unsigned char> v = ParseHex(value);
     263   [ #  #  #  #  :           0 :             if (!batch->Write(Span{k}, Span{v})) {
             #  #  #  # ]
     264   [ #  #  #  # ]:           0 :                 error = strprintf(_("Error: Unable to write record to new wallet"));
     265                 :           0 :                 ret = false;
     266                 :           0 :                 break;
     267                 :             :             }
     268   [ #  #  #  # ]:           0 :         }
     269                 :             : 
     270         [ #  # ]:           0 :         if (ret) {
     271         [ #  # ]:           0 :             uint256 comp_checksum = hasher.GetHash();
     272   [ #  #  #  # ]:           0 :             if (checksum.IsNull()) {
     273         [ #  # ]:           0 :                 error = _("Error: Missing checksum");
     274                 :           0 :                 ret = false;
     275   [ #  #  #  # ]:           0 :             } else if (checksum != comp_checksum) {
     276   [ #  #  #  #  :           0 :                 error = strprintf(_("Error: Dumpfile checksum does not match. Computed %s, expected %s"), HexStr(comp_checksum), HexStr(checksum));
          #  #  #  #  #  
                #  #  # ]
     277                 :           0 :                 ret = false;
     278                 :           0 :             }
     279                 :           0 :         }
     280                 :             : 
     281         [ #  # ]:           0 :         if (ret) {
     282         [ #  # ]:           0 :             batch->TxnCommit();
     283                 :           0 :         } else {
     284         [ #  # ]:           0 :             batch->TxnAbort();
     285                 :             :         }
     286                 :             : 
     287                 :           0 :         batch.reset();
     288                 :             : 
     289         [ #  # ]:           0 :         dump_file.close();
     290         [ #  # ]:           0 :     }
     291                 :           0 :     wallet.reset(); // The pointer deleter will close the wallet for us.
     292                 :             : 
     293                 :             :     // Remove the wallet dir if we have a failure
     294         [ #  # ]:           0 :     if (!ret) {
     295         [ #  # ]:           0 :         fs::remove_all(wallet_path);
     296                 :           0 :     }
     297                 :             : 
     298                 :           0 :     return ret;
     299                 :           0 : }
     300                 :             : } // namespace wallet
        

Generated by: LCOV version 2.0-1