LCOV - code coverage report
Current view: top level - src/wallet - migrate.h (source / functions) Coverage Total Hit
Test: total_coverage.info Lines: 60.0 % 20 12
Test Date: 2025-05-10 04:59:59 Functions: 50.0 % 20 10
Branches: 50.0 % 8 4

             Branch data     Line data    Source code
       1                 :             : // Copyright (c) 2021-present 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                 :             : #ifndef BITCOIN_WALLET_MIGRATE_H
       6                 :             : #define BITCOIN_WALLET_MIGRATE_H
       7                 :             : 
       8                 :             : #include <wallet/db.h>
       9                 :             : 
      10                 :             : #include <optional>
      11                 :             : 
      12                 :             : namespace wallet {
      13                 :             : 
      14                 :             : using BerkeleyROData = std::map<SerializeData, SerializeData, std::less<>>;
      15                 :             : 
      16                 :             : /**
      17                 :             :  * A class representing a BerkeleyDB file from which we can only read records.
      18                 :             :  * This is used only for migration of legacy to descriptor wallets
      19                 :             :  */
      20                 :             : class BerkeleyRODatabase : public WalletDatabase
      21                 :             : {
      22                 :             : private:
      23                 :             :     const fs::path m_filepath;
      24                 :             : 
      25                 :             : public:
      26                 :             :     /** Create DB handle */
      27   [ +  -  +  - ]:          35 :     BerkeleyRODatabase(const fs::path& filepath, bool open = true) : WalletDatabase(), m_filepath(filepath)
      28                 :             :     {
      29   [ +  -  +  - ]:          35 :         if (open) Open();
      30                 :          35 :     }
      31                 :          70 :     ~BerkeleyRODatabase() = default;
      32                 :             : 
      33                 :             :     BerkeleyROData m_records;
      34                 :             : 
      35                 :             :     /** Open the database if it is not already opened. */
      36                 :             :     void Open() override;
      37                 :             : 
      38                 :             :     /** Rewrite the entire database on disk, with the exception of key pszSkip if non-zero
      39                 :             :      */
      40                 :           0 :     bool Rewrite(const char* pszSkip = nullptr) override { return false; }
      41                 :             : 
      42                 :             :     /** Back up the entire database to a file.
      43                 :             :      */
      44                 :             :     bool Backup(const std::string& strDest) const override;
      45                 :             : 
      46                 :             :     /** Flush to the database file and close the database.
      47                 :             :      *  Also close the environment if no other databases are open in it.
      48                 :             :      */
      49                 :          32 :     void Close() override {}
      50                 :             : 
      51                 :             :     /** Return path to main database file for logs and error messages. */
      52                 :         102 :     std::string Filename() override { return fs::PathToString(m_filepath); }
      53                 :             : 
      54                 :          99 :     std::string Format() override { return "bdb_ro"; }
      55                 :             : 
      56                 :             :     /** Make a DatabaseBatch connected to this database */
      57                 :             :     std::unique_ptr<DatabaseBatch> MakeBatch() override;
      58                 :             : };
      59                 :             : 
      60                 :             : class BerkeleyROCursor : public DatabaseCursor
      61                 :             : {
      62                 :             : private:
      63                 :             :     const BerkeleyRODatabase& m_database;
      64                 :             :     BerkeleyROData::const_iterator m_cursor;
      65                 :             :     BerkeleyROData::const_iterator m_cursor_end;
      66                 :             : 
      67                 :             : public:
      68                 :             :     explicit BerkeleyROCursor(const BerkeleyRODatabase& database, std::span<const std::byte> prefix = {});
      69                 :         697 :     ~BerkeleyROCursor() = default;
      70                 :             : 
      71                 :             :     Status Next(DataStream& key, DataStream& value) override;
      72                 :             : };
      73                 :             : 
      74                 :             : /** RAII class that provides access to a BerkeleyRODatabase */
      75                 :             : class BerkeleyROBatch : public DatabaseBatch
      76                 :             : {
      77                 :             : private:
      78                 :             :     const BerkeleyRODatabase& m_database;
      79                 :             : 
      80                 :             :     bool ReadKey(DataStream&& key, DataStream& value) override;
      81                 :             :     // WriteKey returns true since various automatic upgrades for older wallets will expect writing to not fail.
      82                 :             :     // It is okay for this batch type to not actually write anything as those automatic upgrades will occur again after migration.
      83                 :          34 :     bool WriteKey(DataStream&& key, DataStream&& value, bool overwrite = true) override { return true; }
      84                 :           0 :     bool EraseKey(DataStream&& key) override { return false; }
      85                 :             :     bool HasKey(DataStream&& key) override;
      86                 :           0 :     bool ErasePrefix(std::span<const std::byte> prefix) override { return false; }
      87                 :             : 
      88                 :             : public:
      89                 :         128 :     explicit BerkeleyROBatch(const BerkeleyRODatabase& database) : m_database(database) {}
      90                 :         128 :     ~BerkeleyROBatch() = default;
      91                 :             : 
      92                 :             :     BerkeleyROBatch(const BerkeleyROBatch&) = delete;
      93                 :             :     BerkeleyROBatch& operator=(const BerkeleyROBatch&) = delete;
      94                 :             : 
      95                 :           0 :     void Close() override {}
      96                 :             : 
      97                 :          32 :     std::unique_ptr<DatabaseCursor> GetNewCursor() override { return std::make_unique<BerkeleyROCursor>(m_database); }
      98                 :             :     std::unique_ptr<DatabaseCursor> GetNewPrefixCursor(std::span<const std::byte> prefix) override;
      99                 :           0 :     bool TxnBegin() override { return false; }
     100                 :           0 :     bool TxnCommit() override { return false; }
     101                 :           0 :     bool TxnAbort() override { return false; }
     102                 :           0 :     bool HasActiveTxn() override { return false; }
     103                 :             : };
     104                 :             : 
     105                 :             : //! Return object giving access to Berkeley Read Only database at specified path.
     106                 :             : std::unique_ptr<BerkeleyRODatabase> MakeBerkeleyRODatabase(const fs::path& path, const DatabaseOptions& options, DatabaseStatus& status, bilingual_str& error);
     107                 :             : } // namespace wallet
     108                 :             : 
     109                 :             : #endif // BITCOIN_WALLET_MIGRATE_H
        

Generated by: LCOV version 2.0-1