Branch data Line data Source code
1 : : // Copyright (c) 2009-2010 Satoshi Nakamoto
2 : : // Copyright (c) 2009-present The Bitcoin Core developers
3 : : // Distributed under the MIT software license, see the accompanying
4 : : // file COPYING or http://www.opensource.org/licenses/mit-license.php.
5 : :
6 : : #ifndef BITCOIN_WALLET_DB_H
7 : : #define BITCOIN_WALLET_DB_H
8 : :
9 : : #include <clientversion.h>
10 : : #include <streams.h>
11 : : #include <support/allocators/secure.h>
12 : : #include <util/fs.h>
13 : : #include <util/result.h>
14 : :
15 : : #include <memory>
16 : : #include <optional>
17 : : #include <string>
18 : :
19 : : class ArgsManager;
20 : : struct bilingual_str;
21 : :
22 : : namespace wallet {
23 : : // BytePrefix compares equality with other byte spans that begin with the same prefix.
24 : : struct BytePrefix {
25 : : std::span<const std::byte> prefix;
26 : : };
27 : : bool operator<(BytePrefix a, std::span<const std::byte> b);
28 : : bool operator<(std::span<const std::byte> a, BytePrefix b);
29 : :
30 : : class DatabaseCursor
31 : : {
32 : : public:
33 [ + - ]: 34299 : explicit DatabaseCursor() = default;
34 : 34299 : virtual ~DatabaseCursor() = default;
35 : :
36 : : DatabaseCursor(const DatabaseCursor&) = delete;
37 : : DatabaseCursor& operator=(const DatabaseCursor&) = delete;
38 : :
39 : : enum class Status
40 : : {
41 : : FAIL,
42 : : MORE,
43 : : DONE,
44 : : };
45 : :
46 : 0 : virtual Status Next(DataStream& key, DataStream& value) { return Status::FAIL; }
47 : : };
48 : :
49 : : /** RAII class that provides access to a WalletDatabase */
50 : : class DatabaseBatch
51 : : {
52 : : private:
53 : : virtual bool ReadKey(DataStream&& key, DataStream& value) = 0;
54 : : virtual bool WriteKey(DataStream&& key, DataStream&& value, bool overwrite = true) = 0;
55 : : virtual bool EraseKey(DataStream&& key) = 0;
56 : : virtual bool HasKey(DataStream&& key) = 0;
57 : :
58 : : public:
59 [ + - ]: 177679 : explicit DatabaseBatch() = default;
60 : 177679 : virtual ~DatabaseBatch() = default;
61 : :
62 : : DatabaseBatch(const DatabaseBatch&) = delete;
63 : : DatabaseBatch& operator=(const DatabaseBatch&) = delete;
64 : :
65 : : virtual void Close() = 0;
66 : :
67 : : template <typename K, typename T>
68 : 5980 : bool Read(const K& key, T& value)
69 : : {
70 [ + - ]: 5980 : DataStream ssKey{};
71 [ + - ]: 5980 : ssKey.reserve(1000);
72 : 5980 : ssKey << key;
73 : :
74 : 5980 : DataStream ssValue{};
75 [ + - + + ]: 5980 : if (!ReadKey(std::move(ssKey), ssValue)) return false;
76 : : try {
77 : 5980 : ssValue >> value;
78 : : return true;
79 [ - - ]: 0 : } catch (const std::exception&) {
80 : : return false;
81 : : }
82 : 5980 : }
83 : :
84 : : template <typename K, typename T>
85 : 314456 : bool Write(const K& key, const T& value, bool fOverwrite = true)
86 : : {
87 [ + - ]: 314456 : DataStream ssKey{};
88 [ + - ]: 314456 : ssKey.reserve(1000);
89 : 314456 : ssKey << key;
90 : :
91 [ + - ]: 314456 : DataStream ssValue{};
92 [ + - ]: 314456 : ssValue.reserve(10000);
93 : 314456 : ssValue << value;
94 : :
95 [ + - ]: 314456 : return WriteKey(std::move(ssKey), std::move(ssValue), fOverwrite);
96 : 314456 : }
97 : :
98 : : template <typename K>
99 : 504 : bool Erase(const K& key)
100 : : {
101 [ + - ]: 504 : DataStream ssKey{};
102 [ + - ]: 504 : ssKey.reserve(1000);
103 : 504 : ssKey << key;
104 : :
105 [ + - ]: 504 : return EraseKey(std::move(ssKey));
106 : 504 : }
107 : :
108 : : template <typename K>
109 : 92 : bool Exists(const K& key)
110 : : {
111 [ + - ]: 92 : DataStream ssKey{};
112 [ + - ]: 92 : ssKey.reserve(1000);
113 : 92 : ssKey << key;
114 : :
115 [ + - ]: 92 : return HasKey(std::move(ssKey));
116 : 92 : }
117 : : virtual bool ErasePrefix(std::span<const std::byte> prefix) = 0;
118 : :
119 : : virtual std::unique_ptr<DatabaseCursor> GetNewCursor() = 0;
120 : : virtual std::unique_ptr<DatabaseCursor> GetNewPrefixCursor(std::span<const std::byte> prefix) = 0;
121 : : virtual bool TxnBegin() = 0;
122 : : virtual bool TxnCommit() = 0;
123 : : virtual bool TxnAbort() = 0;
124 : : virtual bool HasActiveTxn() = 0;
125 : : };
126 : :
127 : : /** An instance of this class represents one database.
128 : : **/
129 : : class WalletDatabase
130 : : {
131 : : public:
132 : : /** Create dummy DB handle */
133 [ + - ]: 1485 : WalletDatabase() = default;
134 : 1485 : virtual ~WalletDatabase() = default;
135 : :
136 : : /** Open the database if it is not already opened. */
137 : : virtual void Open() = 0;
138 : :
139 : : /** Rewrite the entire database on disk
140 : : */
141 : : virtual bool Rewrite() = 0;
142 : :
143 : : /** Back up the entire database to a file.
144 : : */
145 : : virtual bool Backup(const std::string& strDest) const = 0;
146 : :
147 : : /** Flush to the database file and close the database.
148 : : * Also close the environment if no other databases are open in it.
149 : : */
150 : : virtual void Close() = 0;
151 : :
152 : : /** Return path to main database file for logs and error messages. */
153 : : virtual std::string Filename() = 0;
154 : :
155 : : /** Return paths to all database created files */
156 : : virtual std::vector<fs::path> Files() = 0;
157 : :
158 : : virtual std::string Format() = 0;
159 : :
160 : : /** Make a DatabaseBatch connected to this database */
161 : : virtual std::unique_ptr<DatabaseBatch> MakeBatch() = 0;
162 : :
163 : : /** Close the database and erase all its files */
164 : : util::Result<void> Erase();
165 : : };
166 : :
167 : : enum class DatabaseFormat {
168 : : SQLITE,
169 : : BERKELEY_RO,
170 : : };
171 : :
172 [ + - + + : 3621 : struct DatabaseOptions {
+ - + + +
- ][ + - ]
[ + - + -
+ - ][ - +
+ - + - +
- # # ][ +
- + - # #
# # ]
173 : : bool require_existing = false;
174 : : bool require_create = false;
175 : : std::optional<DatabaseFormat> require_format;
176 : : uint64_t create_flags = 0;
177 : : SecureString create_passphrase;
178 : :
179 : : // Specialized options. Not every option is supported by every backend.
180 : : bool verify = true; //!< Check data integrity on load.
181 : : bool use_unsafe_sync = false; //!< Disable file sync for faster performance.
182 : : };
183 : :
184 : : enum class DatabaseStatus {
185 : : SUCCESS,
186 : : FAILED_BAD_PATH,
187 : : FAILED_BAD_FORMAT,
188 : : FAILED_LEGACY_DISABLED,
189 : : FAILED_ALREADY_LOADED,
190 : : FAILED_ALREADY_EXISTS,
191 : : FAILED_NOT_FOUND,
192 : : FAILED_CREATE,
193 : : FAILED_LOAD,
194 : : FAILED_VERIFY,
195 : : FAILED_ENCRYPT,
196 : : FAILED_INVALID_BACKUP_FILE,
197 : : FAILED_NEW_UNNAMED,
198 : : };
199 : :
200 : : /** Recursively list database paths in directory. */
201 : : std::vector<std::pair<fs::path, std::string>> ListDatabases(const fs::path& path);
202 : :
203 : : void ReadDatabaseArgs(const ArgsManager& args, DatabaseOptions& options);
204 : : std::unique_ptr<WalletDatabase> MakeDatabase(const fs::path& path, const DatabaseOptions& options, DatabaseStatus& status, bilingual_str& error);
205 : :
206 : : /**
207 : : * Move 'db' into the 'dst_path' directory by renaming its data file, and reopen it from there.
208 : : * 'dst_path' must be on the same filesystem as the database. An existing data file at the
209 : : * destination is replaced. If another file of the db, such as a leftover journal, is already
210 : : * at the destination, nothing is moved and an error is returned.
211 : : * On failure, 'db' is left closed, and a moved data file that can't be reopened is erased.
212 : : */
213 : : util::Result<void> MoveDatabase(std::unique_ptr<WalletDatabase>& db, const fs::path& dst_path, DatabaseOptions& opts);
214 : :
215 : : fs::path BDBDataFile(const fs::path& path);
216 : : fs::path SQLiteDataFile(const fs::path& path);
217 : : bool IsBDBFile(const fs::path& path);
218 : : bool IsSQLiteFile(const fs::path& path);
219 : : } // namespace wallet
220 : :
221 : : #endif // BITCOIN_WALLET_DB_H
|