/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, 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 | 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 | | |
54 | 0 | 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 | 0 | ~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 | 0 | 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 | 0 | explicit BerkeleyROBatch(const BerkeleyRODatabase& database) : m_database(database) {} |
90 | 0 | ~BerkeleyROBatch() = default; |
91 | | |
92 | | BerkeleyROBatch(const BerkeleyROBatch&) = delete; |
93 | | BerkeleyROBatch& operator=(const BerkeleyROBatch&) = delete; |
94 | | |
95 | 0 | void Close() override {} |
96 | | |
97 | 0 | 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 |