/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 | | /** Indicate the a new database user has began using the database. Increments m_refcount */ |
39 | 0 | void AddRef() override {} |
40 | | /** Indicate that database user has stopped using the database and that it could be flushed or closed. Decrement m_refcount */ |
41 | 0 | void RemoveRef() override {} |
42 | | |
43 | | /** Rewrite the entire database on disk, with the exception of key pszSkip if non-zero |
44 | | */ |
45 | 0 | bool Rewrite(const char* pszSkip = nullptr) override { return false; } |
46 | | |
47 | | /** Back up the entire database to a file. |
48 | | */ |
49 | | bool Backup(const std::string& strDest) const override; |
50 | | |
51 | | /** Make sure all changes are flushed to database file. |
52 | | */ |
53 | 0 | void Flush() override {} |
54 | | /** Flush to the database file and close the database. |
55 | | * Also close the environment if no other databases are open in it. |
56 | | */ |
57 | 0 | void Close() override {} |
58 | | /* flush the wallet passively (TRY_LOCK) |
59 | | ideal to be called periodically */ |
60 | 0 | bool PeriodicFlush() override { return false; } |
61 | | |
62 | 0 | void IncrementUpdateCounter() override {} |
63 | | |
64 | 0 | void ReloadDbEnv() override {} |
65 | | |
66 | | /** Return path to main database file for logs and error messages. */ |
67 | 0 | std::string Filename() override { return fs::PathToString(m_filepath); } |
68 | | |
69 | 0 | std::string Format() override { return "bdb_ro"; } |
70 | | |
71 | | /** Make a DatabaseBatch connected to this database */ |
72 | | std::unique_ptr<DatabaseBatch> MakeBatch(bool flush_on_close = true) override; |
73 | | }; |
74 | | |
75 | | class BerkeleyROCursor : public DatabaseCursor |
76 | | { |
77 | | private: |
78 | | const BerkeleyRODatabase& m_database; |
79 | | BerkeleyROData::const_iterator m_cursor; |
80 | | BerkeleyROData::const_iterator m_cursor_end; |
81 | | |
82 | | public: |
83 | | explicit BerkeleyROCursor(const BerkeleyRODatabase& database, std::span<const std::byte> prefix = {}); |
84 | 0 | ~BerkeleyROCursor() = default; |
85 | | |
86 | | Status Next(DataStream& key, DataStream& value) override; |
87 | | }; |
88 | | |
89 | | /** RAII class that provides access to a BerkeleyRODatabase */ |
90 | | class BerkeleyROBatch : public DatabaseBatch |
91 | | { |
92 | | private: |
93 | | const BerkeleyRODatabase& m_database; |
94 | | |
95 | | bool ReadKey(DataStream&& key, DataStream& value) override; |
96 | | // WriteKey returns true since various automatic upgrades for older wallets will expect writing to not fail. |
97 | | // It is okay for this batch type to not actually write anything as those automatic upgrades will occur again after migration. |
98 | 0 | bool WriteKey(DataStream&& key, DataStream&& value, bool overwrite = true) override { return true; } |
99 | 0 | bool EraseKey(DataStream&& key) override { return false; } |
100 | | bool HasKey(DataStream&& key) override; |
101 | 0 | bool ErasePrefix(std::span<const std::byte> prefix) override { return false; } |
102 | | |
103 | | public: |
104 | 0 | explicit BerkeleyROBatch(const BerkeleyRODatabase& database) : m_database(database) {} |
105 | 0 | ~BerkeleyROBatch() = default; |
106 | | |
107 | | BerkeleyROBatch(const BerkeleyROBatch&) = delete; |
108 | | BerkeleyROBatch& operator=(const BerkeleyROBatch&) = delete; |
109 | | |
110 | 0 | void Flush() override {} |
111 | 0 | void Close() override {} |
112 | | |
113 | 0 | std::unique_ptr<DatabaseCursor> GetNewCursor() override { return std::make_unique<BerkeleyROCursor>(m_database); } |
114 | | std::unique_ptr<DatabaseCursor> GetNewPrefixCursor(std::span<const std::byte> prefix) override; |
115 | 0 | bool TxnBegin() override { return false; } |
116 | 0 | bool TxnCommit() override { return false; } |
117 | 0 | bool TxnAbort() override { return false; } |
118 | 0 | bool HasActiveTxn() override { return false; } |
119 | | }; |
120 | | |
121 | | //! Return object giving access to Berkeley Read Only database at specified path. |
122 | | std::unique_ptr<BerkeleyRODatabase> MakeBerkeleyRODatabase(const fs::path& path, const DatabaseOptions& options, DatabaseStatus& status, bilingual_str& error); |
123 | | } // namespace wallet |
124 | | |
125 | | #endif // BITCOIN_WALLET_MIGRATE_H |