Bitcoin Core  22.99.0
P2P Digital Currency
db.h
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2021 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 <fs.h>
11 #include <streams.h>
13 
14 #include <atomic>
15 #include <memory>
16 #include <optional>
17 #include <string>
18 
19 struct bilingual_str;
20 
21 namespace wallet {
22 void SplitWalletPath(const fs::path& wallet_path, fs::path& env_directory, std::string& database_filename);
23 
26 {
27 private:
28  virtual bool ReadKey(CDataStream&& key, CDataStream& value) = 0;
29  virtual bool WriteKey(CDataStream&& key, CDataStream&& value, bool overwrite=true) = 0;
30  virtual bool EraseKey(CDataStream&& key) = 0;
31  virtual bool HasKey(CDataStream&& key) = 0;
32 
33 public:
34  explicit DatabaseBatch() {}
35  virtual ~DatabaseBatch() {}
36 
37  DatabaseBatch(const DatabaseBatch&) = delete;
38  DatabaseBatch& operator=(const DatabaseBatch&) = delete;
39 
40  virtual void Flush() = 0;
41  virtual void Close() = 0;
42 
43  template <typename K, typename T>
44  bool Read(const K& key, T& value)
45  {
47  ssKey.reserve(1000);
48  ssKey << key;
49 
51  if (!ReadKey(std::move(ssKey), ssValue)) return false;
52  try {
53  ssValue >> value;
54  return true;
55  } catch (const std::exception&) {
56  return false;
57  }
58  }
59 
60  template <typename K, typename T>
61  bool Write(const K& key, const T& value, bool fOverwrite = true)
62  {
64  ssKey.reserve(1000);
65  ssKey << key;
66 
68  ssValue.reserve(10000);
69  ssValue << value;
70 
71  return WriteKey(std::move(ssKey), std::move(ssValue), fOverwrite);
72  }
73 
74  template <typename K>
75  bool Erase(const K& key)
76  {
78  ssKey.reserve(1000);
79  ssKey << key;
80 
81  return EraseKey(std::move(ssKey));
82  }
83 
84  template <typename K>
85  bool Exists(const K& key)
86  {
88  ssKey.reserve(1000);
89  ssKey << key;
90 
91  return HasKey(std::move(ssKey));
92  }
93 
94  virtual bool StartCursor() = 0;
95  virtual bool ReadAtCursor(CDataStream& ssKey, CDataStream& ssValue, bool& complete) = 0;
96  virtual void CloseCursor() = 0;
97  virtual bool TxnBegin() = 0;
98  virtual bool TxnCommit() = 0;
99  virtual bool TxnAbort() = 0;
100 };
101 
105 {
106 public:
109  virtual ~WalletDatabase() {};
110 
112  virtual void Open() = 0;
113 
115  std::atomic<int> m_refcount{0};
117  virtual void AddRef() = 0;
119  virtual void RemoveRef() = 0;
120 
123  virtual bool Rewrite(const char* pszSkip=nullptr) = 0;
124 
127  virtual bool Backup(const std::string& strDest) const = 0;
128 
131  virtual void Flush() = 0;
135  virtual void Close() = 0;
136  /* flush the wallet passively (TRY_LOCK)
137  ideal to be called periodically */
138  virtual bool PeriodicFlush() = 0;
139 
140  virtual void IncrementUpdateCounter() = 0;
141 
142  virtual void ReloadDbEnv() = 0;
143 
145  virtual std::string Filename() = 0;
146 
147  virtual std::string Format() = 0;
148 
149  std::atomic<unsigned int> nUpdateCounter;
150  unsigned int nLastSeen;
151  unsigned int nLastFlushed;
153 
155  virtual std::unique_ptr<DatabaseBatch> MakeBatch(bool flush_on_close = true) = 0;
156 };
157 
159 class DummyBatch : public DatabaseBatch
160 {
161 private:
162  bool ReadKey(CDataStream&& key, CDataStream& value) override { return true; }
163  bool WriteKey(CDataStream&& key, CDataStream&& value, bool overwrite=true) override { return true; }
164  bool EraseKey(CDataStream&& key) override { return true; }
165  bool HasKey(CDataStream&& key) override { return true; }
166 
167 public:
168  void Flush() override {}
169  void Close() override {}
170 
171  bool StartCursor() override { return true; }
172  bool ReadAtCursor(CDataStream& ssKey, CDataStream& ssValue, bool& complete) override { return true; }
173  void CloseCursor() override {}
174  bool TxnBegin() override { return true; }
175  bool TxnCommit() override { return true; }
176  bool TxnAbort() override { return true; }
177 };
178 
182 {
183 public:
184  void Open() override {};
185  void AddRef() override {}
186  void RemoveRef() override {}
187  bool Rewrite(const char* pszSkip=nullptr) override { return true; }
188  bool Backup(const std::string& strDest) const override { return true; }
189  void Close() override {}
190  void Flush() override {}
191  bool PeriodicFlush() override { return true; }
192  void IncrementUpdateCounter() override { ++nUpdateCounter; }
193  void ReloadDbEnv() override {}
194  std::string Filename() override { return "dummy"; }
195  std::string Format() override { return "dummy"; }
196  std::unique_ptr<DatabaseBatch> MakeBatch(bool flush_on_close = true) override { return std::make_unique<DummyBatch>(); }
197 };
198 
199 enum class DatabaseFormat {
200  BERKELEY,
201  SQLITE,
202 };
203 
205  bool require_existing = false;
206  bool require_create = false;
207  std::optional<DatabaseFormat> require_format;
208  uint64_t create_flags = 0;
210  bool verify = true;
211 };
212 
213 enum class DatabaseStatus {
214  SUCCESS,
221  FAILED_LOAD,
225 };
226 
228 std::vector<fs::path> ListDatabases(const fs::path& path);
229 
230 std::unique_ptr<WalletDatabase> MakeDatabase(const fs::path& path, const DatabaseOptions& options, DatabaseStatus& status, bilingual_str& error);
231 
232 fs::path BDBDataFile(const fs::path& path);
233 fs::path SQLiteDataFile(const fs::path& path);
234 bool IsBDBFile(const fs::path& path);
235 bool IsSQLiteFile(const fs::path& path);
236 } // namespace wallet
237 
238 #endif // BITCOIN_WALLET_DB_H
wallet::WalletDatabase
An instance of this class represents one database.
Definition: db.h:104
wallet::DatabaseBatch::StartCursor
virtual bool StartCursor()=0
SER_DISK
@ SER_DISK
Definition: serialize.h:139
wallet::WalletDatabase::m_refcount
std::atomic< int > m_refcount
Counts the number of active database users to be sure that the database is not closed while someone i...
Definition: db.h:115
wallet::ListDatabases
std::vector< fs::path > ListDatabases(const fs::path &wallet_dir)
Recursively list database paths in directory.
Definition: db.cpp:14
fs.h
wallet::DatabaseBatch::ReadAtCursor
virtual bool ReadAtCursor(CDataStream &ssKey, CDataStream &ssValue, bool &complete)=0
wallet::DatabaseBatch::HasKey
virtual bool HasKey(CDataStream &&key)=0
wallet::WalletDatabase::Rewrite
virtual bool Rewrite(const char *pszSkip=nullptr)=0
Rewrite the entire database on disk, with the exception of key pszSkip if non-zero.
wallet::BDBDataFile
fs::path BDBDataFile(const fs::path &wallet_path)
Definition: db.cpp:59
streams.h
wallet::DatabaseStatus
DatabaseStatus
Definition: db.h:213
wallet::DatabaseBatch::~DatabaseBatch
virtual ~DatabaseBatch()
Definition: db.h:35
wallet::DatabaseStatus::FAILED_ENCRYPT
@ FAILED_ENCRYPT
wallet::DatabaseBatch::operator=
DatabaseBatch & operator=(const DatabaseBatch &)=delete
wallet::DummyDatabase::Open
void Open() override
Open the database if it is not already opened.
Definition: db.h:184
wallet::WalletDatabase::Close
virtual void Close()=0
Flush to the database file and close the database.
wallet::DatabaseBatch
RAII class that provides access to a WalletDatabase.
Definition: db.h:25
bilingual_str
Bilingual messages:
Definition: translation.h:16
wallet::DatabaseStatus::FAILED_VERIFY
@ FAILED_VERIFY
wallet::DummyBatch::Close
void Close() override
Definition: db.h:169
wallet::DatabaseFormat
DatabaseFormat
Definition: db.h:199
wallet::DatabaseBatch::Read
bool Read(const K &key, T &value)
Definition: db.h:44
wallet::WalletDatabase::Flush
virtual void Flush()=0
Make sure all changes are flushed to database file.
wallet::DummyBatch::StartCursor
bool StartCursor() override
Definition: db.h:171
wallet::DatabaseBatch::TxnBegin
virtual bool TxnBegin()=0
wallet::DatabaseBatch::TxnCommit
virtual bool TxnCommit()=0
clientversion.h
wallet
Definition: node.h:38
wallet::DatabaseStatus::FAILED_ALREADY_LOADED
@ FAILED_ALREADY_LOADED
wallet::DatabaseStatus::FAILED_LOAD
@ FAILED_LOAD
wallet::IsSQLiteFile
bool IsSQLiteFile(const fs::path &path)
Definition: db.cpp:103
wallet::DummyBatch::WriteKey
bool WriteKey(CDataStream &&key, CDataStream &&value, bool overwrite=true) override
Definition: db.h:163
wallet::DatabaseOptions::require_existing
bool require_existing
Definition: db.h:205
wallet::WalletDatabase::nLastWalletUpdate
int64_t nLastWalletUpdate
Definition: db.h:152
wallet::WalletDatabase::Backup
virtual bool Backup(const std::string &strDest) const =0
Back up the entire database to a file.
wallet::DummyDatabase::Filename
std::string Filename() override
Return path to main database file for logs and error messages.
Definition: db.h:194
wallet::MakeDatabase
std::unique_ptr< WalletDatabase > MakeDatabase(const fs::path &path, const DatabaseOptions &options, DatabaseStatus &status, bilingual_str &error)
Definition: walletdb.cpp:1104
wallet::DummyBatch::CloseCursor
void CloseCursor() override
Definition: db.h:173
wallet::DatabaseOptions::require_create
bool require_create
Definition: db.h:206
wallet::WalletDatabase::WalletDatabase
WalletDatabase()
Create dummy DB handle.
Definition: db.h:108
SecureString
std::basic_string< char, std::char_traits< char >, secure_allocator< char > > SecureString
Definition: secure.h:59
wallet::DatabaseStatus::SUCCESS
@ SUCCESS
wallet::WalletDatabase::IncrementUpdateCounter
virtual void IncrementUpdateCounter()=0
wallet::WalletDatabase::MakeBatch
virtual std::unique_ptr< DatabaseBatch > MakeBatch(bool flush_on_close=true)=0
Make a DatabaseBatch connected to this database.
fs::path
Path class wrapper to prepare application code for transition from boost::filesystem library to std::...
Definition: fs.h:33
wallet::DummyBatch
RAII class that provides access to a DummyDatabase.
Definition: db.h:159
wallet::DatabaseBatch::Erase
bool Erase(const K &key)
Definition: db.h:75
wallet::DummyBatch::TxnBegin
bool TxnBegin() override
Definition: db.h:174
wallet::DatabaseBatch::DatabaseBatch
DatabaseBatch()
Definition: db.h:34
wallet::DatabaseOptions::verify
bool verify
Definition: db.h:210
wallet::DatabaseOptions::require_format
std::optional< DatabaseFormat > require_format
Definition: db.h:207
wallet::DummyBatch::HasKey
bool HasKey(CDataStream &&key) override
Definition: db.h:165
wallet::SplitWalletPath
void SplitWalletPath(const fs::path &wallet_path, fs::path &env_directory, std::string &database_filename)
wallet::DatabaseBatch::TxnAbort
virtual bool TxnAbort()=0
wallet::DatabaseBatch::Write
bool Write(const K &key, const T &value, bool fOverwrite=true)
Definition: db.h:61
wallet::DummyDatabase::IncrementUpdateCounter
void IncrementUpdateCounter() override
Definition: db.h:192
wallet::DatabaseStatus::FAILED_INVALID_BACKUP_FILE
@ FAILED_INVALID_BACKUP_FILE
wallet::DatabaseStatus::FAILED_CREATE
@ FAILED_CREATE
wallet::DummyDatabase::Backup
bool Backup(const std::string &strDest) const override
Back up the entire database to a file.
Definition: db.h:188
wallet::DatabaseStatus::FAILED_BAD_FORMAT
@ FAILED_BAD_FORMAT
CDataStream::reserve
void reserve(size_type n)
Definition: streams.h:238
wallet::DatabaseStatus::FAILED_NOT_FOUND
@ FAILED_NOT_FOUND
wallet::WalletDatabase::Format
virtual std::string Format()=0
secure.h
wallet::WalletDatabase::AddRef
virtual void AddRef()=0
Indicate the a new database user has began using the database.
wallet::DatabaseBatch::WriteKey
virtual bool WriteKey(CDataStream &&key, CDataStream &&value, bool overwrite=true)=0
wallet::WalletDatabase::nUpdateCounter
std::atomic< unsigned int > nUpdateCounter
Definition: db.h:149
wallet::DummyDatabase::Rewrite
bool Rewrite(const char *pszSkip=nullptr) override
Rewrite the entire database on disk, with the exception of key pszSkip if non-zero.
Definition: db.h:187
wallet::DummyDatabase::AddRef
void AddRef() override
Indicate the a new database user has began using the database.
Definition: db.h:185
wallet::DummyDatabase::Flush
void Flush() override
Make sure all changes are flushed to database file.
Definition: db.h:190
wallet::DatabaseFormat::BERKELEY
@ BERKELEY
wallet::DummyDatabase::Close
void Close() override
Flush to the database file and close the database.
Definition: db.h:189
wallet::DummyDatabase
A dummy WalletDatabase that does nothing and never fails.
Definition: db.h:181
wallet::WalletDatabase::Filename
virtual std::string Filename()=0
Return path to main database file for logs and error messages.
wallet::WalletDatabase::ReloadDbEnv
virtual void ReloadDbEnv()=0
wallet::DatabaseStatus::FAILED_BAD_PATH
@ FAILED_BAD_PATH
wallet::DummyBatch::ReadAtCursor
bool ReadAtCursor(CDataStream &ssKey, CDataStream &ssValue, bool &complete) override
Definition: db.h:172
wallet::DatabaseFormat::SQLITE
@ SQLITE
wallet::DummyBatch::TxnAbort
bool TxnAbort() override
Definition: db.h:176
wallet::DatabaseBatch::CloseCursor
virtual void CloseCursor()=0
CLIENT_VERSION
static const int CLIENT_VERSION
bitcoind-res.rc includes this file, but it cannot cope with real c++ code.
Definition: clientversion.h:33
wallet::WalletDatabase::PeriodicFlush
virtual bool PeriodicFlush()=0
wallet::WalletDatabase::RemoveRef
virtual void RemoveRef()=0
Indicate that database user has stopped using the database and that it could be flushed or closed.
wallet::DummyDatabase::RemoveRef
void RemoveRef() override
Indicate that database user has stopped using the database and that it could be flushed or closed.
Definition: db.h:186
wallet::DatabaseBatch::Close
virtual void Close()=0
wallet::IsBDBFile
bool IsBDBFile(const fs::path &path)
Definition: db.cpp:78
wallet::DatabaseBatch::ReadKey
virtual bool ReadKey(CDataStream &&key, CDataStream &value)=0
CDataStream
Double ended buffer combining vector and stream-like interfaces.
Definition: streams.h:184
wallet::DummyBatch::ReadKey
bool ReadKey(CDataStream &&key, CDataStream &value) override
Definition: db.h:162
wallet::WalletDatabase::Open
virtual void Open()=0
Open the database if it is not already opened.
error
bool error(const char *fmt, const Args &... args)
Definition: system.h:49
wallet::DummyBatch::Flush
void Flush() override
Definition: db.h:168
wallet::DatabaseStatus::FAILED_ALREADY_EXISTS
@ FAILED_ALREADY_EXISTS
wallet::DatabaseBatch::EraseKey
virtual bool EraseKey(CDataStream &&key)=0
wallet::DatabaseOptions::create_passphrase
SecureString create_passphrase
Definition: db.h:209
wallet::DummyDatabase::MakeBatch
std::unique_ptr< DatabaseBatch > MakeBatch(bool flush_on_close=true) override
Make a DatabaseBatch connected to this database.
Definition: db.h:196
wallet::DatabaseOptions
Definition: db.h:204
wallet::DummyDatabase::PeriodicFlush
bool PeriodicFlush() override
Definition: db.h:191
wallet::WalletDatabase::nLastSeen
unsigned int nLastSeen
Definition: db.h:150
wallet::DatabaseBatch::Exists
bool Exists(const K &key)
Definition: db.h:85
wallet::DummyDatabase::ReloadDbEnv
void ReloadDbEnv() override
Definition: db.h:193
wallet::DummyBatch::EraseKey
bool EraseKey(CDataStream &&key) override
Definition: db.h:164
wallet::DummyDatabase::Format
std::string Format() override
Definition: db.h:195
wallet::DatabaseOptions::create_flags
uint64_t create_flags
Definition: db.h:208
wallet::DummyBatch::TxnCommit
bool TxnCommit() override
Definition: db.h:175
wallet::DatabaseBatch::Flush
virtual void Flush()=0
wallet::WalletDatabase::nLastFlushed
unsigned int nLastFlushed
Definition: db.h:151
wallet::WalletDatabase::~WalletDatabase
virtual ~WalletDatabase()
Definition: db.h:109
wallet::SQLiteDataFile
fs::path SQLiteDataFile(const fs::path &path)
Definition: db.cpp:73