/Users/eugenesiegel/btc/bitcoin/src/wallet/migrate.h
| Line | Count | Source (jump to first uncovered line) | 
| 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 | 0 |     BerkeleyRODatabase(const fs::path& filepath, bool open = true) : WalletDatabase(), m_filepath(filepath) | 
| 28 | 0 |     { | 
| 29 | 0 |         if (open) Open(); | 
| 30 | 0 |     } | 
| 31 | 0 |     ~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 | 
| 39 |  |      */ | 
| 40 | 0 |     bool Rewrite() 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 | 0 |     void Close() override {} | 
| 50 |  |  | 
| 51 |  |     /** Return path to main database file for logs and error messages. */ | 
| 52 | 0 |     std::string Filename() override { return fs::PathToString(m_filepath); } | 
| 53 | 0 |     std::vector<fs::path> Files() override { return {m_filepath}; } | 
| 54 |  |  | 
| 55 | 0 |     std::string Format() override { return "bdb_ro"; } | 
| 56 |  |  | 
| 57 |  |     /** Make a DatabaseBatch connected to this database */ | 
| 58 |  |     std::unique_ptr<DatabaseBatch> MakeBatch() override; | 
| 59 |  | }; | 
| 60 |  |  | 
| 61 |  | class BerkeleyROCursor : public DatabaseCursor | 
| 62 |  | { | 
| 63 |  | private: | 
| 64 |  |     const BerkeleyRODatabase& m_database; | 
| 65 |  |     BerkeleyROData::const_iterator m_cursor; | 
| 66 |  |     BerkeleyROData::const_iterator m_cursor_end; | 
| 67 |  |  | 
| 68 |  | public: | 
| 69 |  |     explicit BerkeleyROCursor(const BerkeleyRODatabase& database, std::span<const std::byte> prefix = {}); | 
| 70 | 0 |     ~BerkeleyROCursor() = default; | 
| 71 |  |  | 
| 72 |  |     Status Next(DataStream& key, DataStream& value) override; | 
| 73 |  | }; | 
| 74 |  |  | 
| 75 |  | /** RAII class that provides access to a BerkeleyRODatabase */ | 
| 76 |  | class BerkeleyROBatch : public DatabaseBatch | 
| 77 |  | { | 
| 78 |  | private: | 
| 79 |  |     const BerkeleyRODatabase& m_database; | 
| 80 |  |  | 
| 81 |  |     bool ReadKey(DataStream&& key, DataStream& value) override; | 
| 82 |  |     // WriteKey returns true since various automatic upgrades for older wallets will expect writing to not fail. | 
| 83 |  |     // It is okay for this batch type to not actually write anything as those automatic upgrades will occur again after migration. | 
| 84 | 0 |     bool WriteKey(DataStream&& key, DataStream&& value, bool overwrite = true) override { return true; } | 
| 85 | 0 |     bool EraseKey(DataStream&& key) override { return false; } | 
| 86 |  |     bool HasKey(DataStream&& key) override; | 
| 87 | 0 |     bool ErasePrefix(std::span<const std::byte> prefix) override { return false; } | 
| 88 |  |  | 
| 89 |  | public: | 
| 90 | 0 |     explicit BerkeleyROBatch(const BerkeleyRODatabase& database) : m_database(database) {} | 
| 91 | 0 |     ~BerkeleyROBatch() = default; | 
| 92 |  |  | 
| 93 |  |     BerkeleyROBatch(const BerkeleyROBatch&) = delete; | 
| 94 |  |     BerkeleyROBatch& operator=(const BerkeleyROBatch&) = delete; | 
| 95 |  |  | 
| 96 | 0 |     void Close() override {} | 
| 97 |  |  | 
| 98 | 0 |     std::unique_ptr<DatabaseCursor> GetNewCursor() override { return std::make_unique<BerkeleyROCursor>(m_database); } | 
| 99 |  |     std::unique_ptr<DatabaseCursor> GetNewPrefixCursor(std::span<const std::byte> prefix) override; | 
| 100 | 0 |     bool TxnBegin() override { return false; } | 
| 101 | 0 |     bool TxnCommit() override { return false; } | 
| 102 | 0 |     bool TxnAbort() override { return false; } | 
| 103 | 0 |     bool HasActiveTxn() override { return false; } | 
| 104 |  | }; | 
| 105 |  |  | 
| 106 |  | //! Return object giving access to Berkeley Read Only database at specified path. | 
| 107 |  | std::unique_ptr<BerkeleyRODatabase> MakeBerkeleyRODatabase(const fs::path& path, const DatabaseOptions& options, DatabaseStatus& status, bilingual_str& error); | 
| 108 |  | } // namespace wallet | 
| 109 |  |  | 
| 110 |  | #endif // BITCOIN_WALLET_MIGRATE_H |