Bitcoin Core  22.99.0
P2P Digital Currency
sqlite.cpp
Go to the documentation of this file.
1 // Copyright (c) 2020-2021 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 #include <wallet/sqlite.h>
6 
7 #include <chainparams.h>
8 #include <crypto/common.h>
9 #include <logging.h>
10 #include <sync.h>
11 #include <util/strencodings.h>
12 #include <util/system.h>
13 #include <util/translation.h>
14 #include <wallet/db.h>
15 
16 #include <sqlite3.h>
17 #include <stdint.h>
18 
19 #include <optional>
20 #include <utility>
21 #include <vector>
22 
23 namespace wallet {
24 static constexpr int32_t WALLET_SCHEMA_VERSION = 0;
25 
27 static int g_sqlite_count GUARDED_BY(g_sqlite_mutex) = 0;
28 
29 static void ErrorLogCallback(void* arg, int code, const char* msg)
30 {
31  // From sqlite3_config() documentation for the SQLITE_CONFIG_LOG option:
32  // "The void pointer that is the second argument to SQLITE_CONFIG_LOG is passed through as
33  // the first parameter to the application-defined logger function whenever that function is
34  // invoked."
35  // Assert that this is the case:
36  assert(arg == nullptr);
37  LogPrintf("SQLite Error. Code: %d. Message: %s\n", code, msg);
38 }
39 
40 static std::optional<int> ReadPragmaInteger(sqlite3* db, const std::string& key, const std::string& description, bilingual_str& error)
41 {
42  std::string stmt_text = strprintf("PRAGMA %s", key);
43  sqlite3_stmt* pragma_read_stmt{nullptr};
44  int ret = sqlite3_prepare_v2(db, stmt_text.c_str(), -1, &pragma_read_stmt, nullptr);
45  if (ret != SQLITE_OK) {
46  sqlite3_finalize(pragma_read_stmt);
47  error = Untranslated(strprintf("SQLiteDatabase: Failed to prepare the statement to fetch %s: %s", description, sqlite3_errstr(ret)));
48  return std::nullopt;
49  }
50  ret = sqlite3_step(pragma_read_stmt);
51  if (ret != SQLITE_ROW) {
52  sqlite3_finalize(pragma_read_stmt);
53  error = Untranslated(strprintf("SQLiteDatabase: Failed to fetch %s: %s", description, sqlite3_errstr(ret)));
54  return std::nullopt;
55  }
56  int result = sqlite3_column_int(pragma_read_stmt, 0);
57  sqlite3_finalize(pragma_read_stmt);
58  return result;
59 }
60 
61 static void SetPragma(sqlite3* db, const std::string& key, const std::string& value, const std::string& err_msg)
62 {
63  std::string stmt_text = strprintf("PRAGMA %s = %s", key, value);
64  int ret = sqlite3_exec(db, stmt_text.c_str(), nullptr, nullptr, nullptr);
65  if (ret != SQLITE_OK) {
66  throw std::runtime_error(strprintf("SQLiteDatabase: %s: %s\n", err_msg, sqlite3_errstr(ret)));
67  }
68 }
69 
70 SQLiteDatabase::SQLiteDatabase(const fs::path& dir_path, const fs::path& file_path, bool mock)
71  : WalletDatabase(), m_mock(mock), m_dir_path(fs::PathToString(dir_path)), m_file_path(fs::PathToString(file_path))
72 {
73  {
75  LogPrintf("Using SQLite Version %s\n", SQLiteDatabaseVersion());
76  LogPrintf("Using wallet %s\n", m_dir_path);
77 
78  if (++g_sqlite_count == 1) {
79  // Setup logging
80  int ret = sqlite3_config(SQLITE_CONFIG_LOG, ErrorLogCallback, nullptr);
81  if (ret != SQLITE_OK) {
82  throw std::runtime_error(strprintf("SQLiteDatabase: Failed to setup error log: %s\n", sqlite3_errstr(ret)));
83  }
84  // Force serialized threading mode
85  ret = sqlite3_config(SQLITE_CONFIG_SERIALIZED);
86  if (ret != SQLITE_OK) {
87  throw std::runtime_error(strprintf("SQLiteDatabase: Failed to configure serialized threading mode: %s\n", sqlite3_errstr(ret)));
88  }
89  }
90  int ret = sqlite3_initialize(); // This is a no-op if sqlite3 is already initialized
91  if (ret != SQLITE_OK) {
92  throw std::runtime_error(strprintf("SQLiteDatabase: Failed to initialize SQLite: %s\n", sqlite3_errstr(ret)));
93  }
94  }
95 
96  try {
97  Open();
98  } catch (const std::runtime_error&) {
99  // If open fails, cleanup this object and rethrow the exception
100  Cleanup();
101  throw;
102  }
103 }
104 
106 {
107  const std::vector<std::pair<sqlite3_stmt**, const char*>> statements{
108  {&m_read_stmt, "SELECT value FROM main WHERE key = ?"},
109  {&m_insert_stmt, "INSERT INTO main VALUES(?, ?)"},
110  {&m_overwrite_stmt, "INSERT or REPLACE into main values(?, ?)"},
111  {&m_delete_stmt, "DELETE FROM main WHERE key = ?"},
112  {&m_cursor_stmt, "SELECT key, value FROM main"},
113  };
114 
115  for (const auto& [stmt_prepared, stmt_text] : statements) {
116  if (*stmt_prepared == nullptr) {
117  int res = sqlite3_prepare_v2(m_database.m_db, stmt_text, -1, stmt_prepared, nullptr);
118  if (res != SQLITE_OK) {
119  throw std::runtime_error(strprintf(
120  "SQLiteDatabase: Failed to setup SQL statements: %s\n", sqlite3_errstr(res)));
121  }
122  }
123  }
124 }
125 
127 {
128  Cleanup();
129 }
130 
131 void SQLiteDatabase::Cleanup() noexcept
132 {
133  Close();
134 
136  if (--g_sqlite_count == 0) {
137  int ret = sqlite3_shutdown();
138  if (ret != SQLITE_OK) {
139  LogPrintf("SQLiteDatabase: Failed to shutdown SQLite: %s\n", sqlite3_errstr(ret));
140  }
141  }
142 }
143 
145 {
146  assert(m_db);
147 
148  // Check the application ID matches our network magic
149  auto read_result = ReadPragmaInteger(m_db, "application_id", "the application id", error);
150  if (!read_result.has_value()) return false;
151  uint32_t app_id = static_cast<uint32_t>(read_result.value());
152  uint32_t net_magic = ReadBE32(Params().MessageStart());
153  if (app_id != net_magic) {
154  error = strprintf(_("SQLiteDatabase: Unexpected application id. Expected %u, got %u"), net_magic, app_id);
155  return false;
156  }
157 
158  // Check our schema version
159  read_result = ReadPragmaInteger(m_db, "user_version", "sqlite wallet schema version", error);
160  if (!read_result.has_value()) return false;
161  int32_t user_ver = read_result.value();
162  if (user_ver != WALLET_SCHEMA_VERSION) {
163  error = strprintf(_("SQLiteDatabase: Unknown sqlite wallet schema version %d. Only version %d is supported"), user_ver, WALLET_SCHEMA_VERSION);
164  return false;
165  }
166 
167  sqlite3_stmt* stmt{nullptr};
168  int ret = sqlite3_prepare_v2(m_db, "PRAGMA integrity_check", -1, &stmt, nullptr);
169  if (ret != SQLITE_OK) {
170  sqlite3_finalize(stmt);
171  error = strprintf(_("SQLiteDatabase: Failed to prepare statement to verify database: %s"), sqlite3_errstr(ret));
172  return false;
173  }
174  while (true) {
175  ret = sqlite3_step(stmt);
176  if (ret == SQLITE_DONE) {
177  break;
178  }
179  if (ret != SQLITE_ROW) {
180  error = strprintf(_("SQLiteDatabase: Failed to execute statement to verify database: %s"), sqlite3_errstr(ret));
181  break;
182  }
183  const char* msg = (const char*)sqlite3_column_text(stmt, 0);
184  if (!msg) {
185  error = strprintf(_("SQLiteDatabase: Failed to read database verification error: %s"), sqlite3_errstr(ret));
186  break;
187  }
188  std::string str_msg(msg);
189  if (str_msg == "ok") {
190  continue;
191  }
192  if (error.empty()) {
193  error = _("Failed to verify database") + Untranslated("\n");
194  }
195  error += Untranslated(strprintf("%s\n", str_msg));
196  }
197  sqlite3_finalize(stmt);
198  return error.empty();
199 }
200 
202 {
203  int flags = SQLITE_OPEN_FULLMUTEX | SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE;
204  if (m_mock) {
205  flags |= SQLITE_OPEN_MEMORY; // In memory database for mock db
206  }
207 
208  if (m_db == nullptr) {
209  if (!m_mock) {
211  }
212  int ret = sqlite3_open_v2(m_file_path.c_str(), &m_db, flags, nullptr);
213  if (ret != SQLITE_OK) {
214  throw std::runtime_error(strprintf("SQLiteDatabase: Failed to open database: %s\n", sqlite3_errstr(ret)));
215  }
216  ret = sqlite3_extended_result_codes(m_db, 1);
217  if (ret != SQLITE_OK) {
218  throw std::runtime_error(strprintf("SQLiteDatabase: Failed to enable extended result codes: %s\n", sqlite3_errstr(ret)));
219  }
220  }
221 
222  if (sqlite3_db_readonly(m_db, "main") != 0) {
223  throw std::runtime_error("SQLiteDatabase: Database opened in readonly mode but read-write permissions are needed");
224  }
225 
226  // Acquire an exclusive lock on the database
227  // First change the locking mode to exclusive
228  SetPragma(m_db, "locking_mode", "exclusive", "Unable to change database locking mode to exclusive");
229  // Now begin a transaction to acquire the exclusive lock. This lock won't be released until we close because of the exclusive locking mode.
230  int ret = sqlite3_exec(m_db, "BEGIN EXCLUSIVE TRANSACTION", nullptr, nullptr, nullptr);
231  if (ret != SQLITE_OK) {
232  throw std::runtime_error("SQLiteDatabase: Unable to obtain an exclusive lock on the database, is it being used by another instance of " PACKAGE_NAME "?\n");
233  }
234  ret = sqlite3_exec(m_db, "COMMIT", nullptr, nullptr, nullptr);
235  if (ret != SQLITE_OK) {
236  throw std::runtime_error(strprintf("SQLiteDatabase: Unable to end exclusive lock transaction: %s\n", sqlite3_errstr(ret)));
237  }
238 
239  // Enable fullfsync for the platforms that use it
240  SetPragma(m_db, "fullfsync", "true", "Failed to enable fullfsync");
241 
242  if (gArgs.GetBoolArg("-unsafesqlitesync", false)) {
243  // Use normal synchronous mode for the journal
244  LogPrintf("WARNING SQLite is configured to not wait for data to be flushed to disk. Data loss and corruption may occur.\n");
245  SetPragma(m_db, "synchronous", "OFF", "Failed to set synchronous mode to OFF");
246  }
247 
248  // Make the table for our key-value pairs
249  // First check that the main table exists
250  sqlite3_stmt* check_main_stmt{nullptr};
251  ret = sqlite3_prepare_v2(m_db, "SELECT name FROM sqlite_master WHERE type='table' AND name='main'", -1, &check_main_stmt, nullptr);
252  if (ret != SQLITE_OK) {
253  throw std::runtime_error(strprintf("SQLiteDatabase: Failed to prepare statement to check table existence: %s\n", sqlite3_errstr(ret)));
254  }
255  ret = sqlite3_step(check_main_stmt);
256  if (sqlite3_finalize(check_main_stmt) != SQLITE_OK) {
257  throw std::runtime_error(strprintf("SQLiteDatabase: Failed to finalize statement checking table existence: %s\n", sqlite3_errstr(ret)));
258  }
259  bool table_exists;
260  if (ret == SQLITE_DONE) {
261  table_exists = false;
262  } else if (ret == SQLITE_ROW) {
263  table_exists = true;
264  } else {
265  throw std::runtime_error(strprintf("SQLiteDatabase: Failed to execute statement to check table existence: %s\n", sqlite3_errstr(ret)));
266  }
267 
268  // Do the db setup things because the table doesn't exist only when we are creating a new wallet
269  if (!table_exists) {
270  ret = sqlite3_exec(m_db, "CREATE TABLE main(key BLOB PRIMARY KEY NOT NULL, value BLOB NOT NULL)", nullptr, nullptr, nullptr);
271  if (ret != SQLITE_OK) {
272  throw std::runtime_error(strprintf("SQLiteDatabase: Failed to create new database: %s\n", sqlite3_errstr(ret)));
273  }
274 
275  // Set the application id
276  uint32_t app_id = ReadBE32(Params().MessageStart());
277  SetPragma(m_db, "application_id", strprintf("%d", static_cast<int32_t>(app_id)),
278  "Failed to set the application id");
279 
280  // Set the user version
281  SetPragma(m_db, "user_version", strprintf("%d", WALLET_SCHEMA_VERSION),
282  "Failed to set the wallet schema version");
283  }
284 }
285 
286 bool SQLiteDatabase::Rewrite(const char* skip)
287 {
288  // Rewrite the database using the VACUUM command: https://sqlite.org/lang_vacuum.html
289  int ret = sqlite3_exec(m_db, "VACUUM", nullptr, nullptr, nullptr);
290  return ret == SQLITE_OK;
291 }
292 
293 bool SQLiteDatabase::Backup(const std::string& dest) const
294 {
295  sqlite3* db_copy;
296  int res = sqlite3_open(dest.c_str(), &db_copy);
297  if (res != SQLITE_OK) {
298  sqlite3_close(db_copy);
299  return false;
300  }
301  sqlite3_backup* backup = sqlite3_backup_init(db_copy, "main", m_db, "main");
302  if (!backup) {
303  LogPrintf("%s: Unable to begin backup: %s\n", __func__, sqlite3_errmsg(m_db));
304  sqlite3_close(db_copy);
305  return false;
306  }
307  // Specifying -1 will copy all of the pages
308  res = sqlite3_backup_step(backup, -1);
309  if (res != SQLITE_DONE) {
310  LogPrintf("%s: Unable to backup: %s\n", __func__, sqlite3_errstr(res));
311  sqlite3_backup_finish(backup);
312  sqlite3_close(db_copy);
313  return false;
314  }
315  res = sqlite3_backup_finish(backup);
316  sqlite3_close(db_copy);
317  return res == SQLITE_OK;
318 }
319 
321 {
322  int res = sqlite3_close(m_db);
323  if (res != SQLITE_OK) {
324  throw std::runtime_error(strprintf("SQLiteDatabase: Failed to close database: %s\n", sqlite3_errstr(res)));
325  }
326  m_db = nullptr;
327 }
328 
329 std::unique_ptr<DatabaseBatch> SQLiteDatabase::MakeBatch(bool flush_on_close)
330 {
331  // We ignore flush_on_close because we don't do manual flushing for SQLite
332  return std::make_unique<SQLiteBatch>(*this);
333 }
334 
336  : m_database(database)
337 {
338  // Make sure we have a db handle
340 
342 }
343 
345 {
346  // If m_db is in a transaction (i.e. not in autocommit mode), then abort the transaction in progress
347  if (m_database.m_db && sqlite3_get_autocommit(m_database.m_db) == 0) {
348  if (TxnAbort()) {
349  LogPrintf("SQLiteBatch: Batch closed unexpectedly without the transaction being explicitly committed or aborted\n");
350  } else {
351  LogPrintf("SQLiteBatch: Batch closed and failed to abort transaction\n");
352  }
353  }
354 
355  // Free all of the prepared statements
356  const std::vector<std::pair<sqlite3_stmt**, const char*>> statements{
357  {&m_read_stmt, "read"},
358  {&m_insert_stmt, "insert"},
359  {&m_overwrite_stmt, "overwrite"},
360  {&m_delete_stmt, "delete"},
361  {&m_cursor_stmt, "cursor"},
362  };
363 
364  for (const auto& [stmt_prepared, stmt_description] : statements) {
365  int res = sqlite3_finalize(*stmt_prepared);
366  if (res != SQLITE_OK) {
367  LogPrintf("SQLiteBatch: Batch closed but could not finalize %s statement: %s\n",
368  stmt_description, sqlite3_errstr(res));
369  }
370  *stmt_prepared = nullptr;
371  }
372 }
373 
375 {
376  if (!m_database.m_db) return false;
378 
379  // Bind: leftmost parameter in statement is index 1
380  int res = sqlite3_bind_blob(m_read_stmt, 1, key.data(), key.size(), SQLITE_STATIC);
381  if (res != SQLITE_OK) {
382  LogPrintf("%s: Unable to bind statement: %s\n", __func__, sqlite3_errstr(res));
383  sqlite3_clear_bindings(m_read_stmt);
384  sqlite3_reset(m_read_stmt);
385  return false;
386  }
387  res = sqlite3_step(m_read_stmt);
388  if (res != SQLITE_ROW) {
389  if (res != SQLITE_DONE) {
390  // SQLITE_DONE means "not found", don't log an error in that case.
391  LogPrintf("%s: Unable to execute statement: %s\n", __func__, sqlite3_errstr(res));
392  }
393  sqlite3_clear_bindings(m_read_stmt);
394  sqlite3_reset(m_read_stmt);
395  return false;
396  }
397  // Leftmost column in result is index 0
398  const char* data = reinterpret_cast<const char*>(sqlite3_column_blob(m_read_stmt, 0));
399  int data_size = sqlite3_column_bytes(m_read_stmt, 0);
400  value.write(data, data_size);
401 
402  sqlite3_clear_bindings(m_read_stmt);
403  sqlite3_reset(m_read_stmt);
404  return true;
405 }
406 
407 bool SQLiteBatch::WriteKey(CDataStream&& key, CDataStream&& value, bool overwrite)
408 {
409  if (!m_database.m_db) return false;
411 
412  sqlite3_stmt* stmt;
413  if (overwrite) {
414  stmt = m_overwrite_stmt;
415  } else {
416  stmt = m_insert_stmt;
417  }
418 
419  // Bind: leftmost parameter in statement is index 1
420  // Insert index 1 is key, 2 is value
421  int res = sqlite3_bind_blob(stmt, 1, key.data(), key.size(), SQLITE_STATIC);
422  if (res != SQLITE_OK) {
423  LogPrintf("%s: Unable to bind key to statement: %s\n", __func__, sqlite3_errstr(res));
424  sqlite3_clear_bindings(stmt);
425  sqlite3_reset(stmt);
426  return false;
427  }
428  res = sqlite3_bind_blob(stmt, 2, value.data(), value.size(), SQLITE_STATIC);
429  if (res != SQLITE_OK) {
430  LogPrintf("%s: Unable to bind value to statement: %s\n", __func__, sqlite3_errstr(res));
431  sqlite3_clear_bindings(stmt);
432  sqlite3_reset(stmt);
433  return false;
434  }
435 
436  // Execute
437  res = sqlite3_step(stmt);
438  sqlite3_clear_bindings(stmt);
439  sqlite3_reset(stmt);
440  if (res != SQLITE_DONE) {
441  LogPrintf("%s: Unable to execute statement: %s\n", __func__, sqlite3_errstr(res));
442  }
443  return res == SQLITE_DONE;
444 }
445 
447 {
448  if (!m_database.m_db) return false;
450 
451  // Bind: leftmost parameter in statement is index 1
452  int res = sqlite3_bind_blob(m_delete_stmt, 1, key.data(), key.size(), SQLITE_STATIC);
453  if (res != SQLITE_OK) {
454  LogPrintf("%s: Unable to bind statement: %s\n", __func__, sqlite3_errstr(res));
455  sqlite3_clear_bindings(m_delete_stmt);
456  sqlite3_reset(m_delete_stmt);
457  return false;
458  }
459 
460  // Execute
461  res = sqlite3_step(m_delete_stmt);
462  sqlite3_clear_bindings(m_delete_stmt);
463  sqlite3_reset(m_delete_stmt);
464  if (res != SQLITE_DONE) {
465  LogPrintf("%s: Unable to execute statement: %s\n", __func__, sqlite3_errstr(res));
466  }
467  return res == SQLITE_DONE;
468 }
469 
471 {
472  if (!m_database.m_db) return false;
474 
475  // Bind: leftmost parameter in statement is index 1
476  bool ret = false;
477  int res = sqlite3_bind_blob(m_read_stmt, 1, key.data(), key.size(), SQLITE_STATIC);
478  if (res == SQLITE_OK) {
479  res = sqlite3_step(m_read_stmt);
480  if (res == SQLITE_ROW) {
481  ret = true;
482  }
483  }
484 
485  sqlite3_clear_bindings(m_read_stmt);
486  sqlite3_reset(m_read_stmt);
487  return ret;
488 }
489 
491 {
493  if (!m_database.m_db) return false;
494  m_cursor_init = true;
495  return true;
496 }
497 
498 bool SQLiteBatch::ReadAtCursor(CDataStream& key, CDataStream& value, bool& complete)
499 {
500  complete = false;
501 
502  if (!m_cursor_init) return false;
503 
504  int res = sqlite3_step(m_cursor_stmt);
505  if (res == SQLITE_DONE) {
506  complete = true;
507  return true;
508  }
509  if (res != SQLITE_ROW) {
510  LogPrintf("SQLiteBatch::ReadAtCursor: Unable to execute cursor step: %s\n", sqlite3_errstr(res));
511  return false;
512  }
513 
514  // Leftmost column in result is index 0
515  const char* key_data = reinterpret_cast<const char*>(sqlite3_column_blob(m_cursor_stmt, 0));
516  int key_data_size = sqlite3_column_bytes(m_cursor_stmt, 0);
517  key.write(key_data, key_data_size);
518  const char* value_data = reinterpret_cast<const char*>(sqlite3_column_blob(m_cursor_stmt, 1));
519  int value_data_size = sqlite3_column_bytes(m_cursor_stmt, 1);
520  value.write(value_data, value_data_size);
521  return true;
522 }
523 
525 {
526  sqlite3_reset(m_cursor_stmt);
527  m_cursor_init = false;
528 }
529 
531 {
532  if (!m_database.m_db || sqlite3_get_autocommit(m_database.m_db) == 0) return false;
533  int res = sqlite3_exec(m_database.m_db, "BEGIN TRANSACTION", nullptr, nullptr, nullptr);
534  if (res != SQLITE_OK) {
535  LogPrintf("SQLiteBatch: Failed to begin the transaction\n");
536  }
537  return res == SQLITE_OK;
538 }
539 
541 {
542  if (!m_database.m_db || sqlite3_get_autocommit(m_database.m_db) != 0) return false;
543  int res = sqlite3_exec(m_database.m_db, "COMMIT TRANSACTION", nullptr, nullptr, nullptr);
544  if (res != SQLITE_OK) {
545  LogPrintf("SQLiteBatch: Failed to commit the transaction\n");
546  }
547  return res == SQLITE_OK;
548 }
549 
551 {
552  if (!m_database.m_db || sqlite3_get_autocommit(m_database.m_db) != 0) return false;
553  int res = sqlite3_exec(m_database.m_db, "ROLLBACK TRANSACTION", nullptr, nullptr, nullptr);
554  if (res != SQLITE_OK) {
555  LogPrintf("SQLiteBatch: Failed to abort the transaction\n");
556  }
557  return res == SQLITE_OK;
558 }
559 
560 std::unique_ptr<SQLiteDatabase> MakeSQLiteDatabase(const fs::path& path, const DatabaseOptions& options, DatabaseStatus& status, bilingual_str& error)
561 {
562  try {
563  fs::path data_file = SQLiteDataFile(path);
564  auto db = std::make_unique<SQLiteDatabase>(data_file.parent_path(), data_file);
565  if (options.verify && !db->Verify(error)) {
567  return nullptr;
568  }
569  status = DatabaseStatus::SUCCESS;
570  return db;
571  } catch (const std::runtime_error& e) {
573  error = Untranslated(e.what());
574  return nullptr;
575  }
576 }
577 
579 {
580  return std::string(sqlite3_libversion());
581 }
582 } // namespace wallet
wallet::SQLiteBatch::Close
void Close() override
Definition: sqlite.cpp:344
wallet::WalletDatabase
An instance of this class represents one database.
Definition: db.h:104
ArgsManager::GetBoolArg
bool GetBoolArg(const std::string &strArg, bool fDefault) const
Return boolean argument or default value.
Definition: system.cpp:602
wallet::SQLiteBatch::TxnAbort
bool TxnAbort() override
Definition: sqlite.cpp:550
wallet::SQLiteBatch::m_cursor_init
bool m_cursor_init
Definition: sqlite.h:23
_
bilingual_str _(const char *psz)
Translation function.
Definition: translation.h:63
assert
assert(!tx.IsCoinBase())
wallet::SQLiteBatch::m_database
SQLiteDatabase & m_database
Definition: sqlite.h:21
wallet::SQLiteBatch::HasKey
bool HasKey(CDataStream &&key) override
Definition: sqlite.cpp:470
wallet::SetPragma
static void SetPragma(sqlite3 *db, const std::string &key, const std::string &value, const std::string &err_msg)
Definition: sqlite.cpp:61
wallet::SQLiteDatabase
An instance of this class represents one SQLite3 database.
Definition: sqlite.h:57
wallet::SQLiteDatabase::~SQLiteDatabase
~SQLiteDatabase()
Definition: sqlite.cpp:126
flags
int flags
Definition: bitcoin-tx.cpp:525
wallet::DatabaseStatus
DatabaseStatus
Definition: db.h:213
wallet::SQLiteBatch::SetupSQLStatements
void SetupSQLStatements()
Definition: sqlite.cpp:105
sync.h
bilingual_str
Bilingual messages:
Definition: translation.h:16
wallet::SQLiteBatch::StartCursor
bool StartCursor() override
Definition: sqlite.cpp:490
wallet::DatabaseStatus::FAILED_VERIFY
@ FAILED_VERIFY
AnnotatedMixin< std::mutex >
ReadBE32
static uint32_t ReadBE32(const unsigned char *ptr)
Definition: common.h:63
wallet
Definition: node.h:38
wallet::SQLiteBatch::m_cursor_stmt
sqlite3_stmt * m_cursor_stmt
Definition: sqlite.h:29
wallet::DatabaseStatus::FAILED_LOAD
@ FAILED_LOAD
wallet::SQLiteBatch::TxnBegin
bool TxnBegin() override
Definition: sqlite.cpp:530
fs::PathToString
static std::string PathToString(const path &path)
Convert path object to byte string.
Definition: fs.h:120
common.h
chainparams.h
wallet::SQLiteBatch::m_read_stmt
sqlite3_stmt * m_read_stmt
Definition: sqlite.h:25
wallet::SQLiteDatabase::Open
void Open() override
Open the database if it is not already opened.
Definition: sqlite.cpp:201
sqlite.h
db.h
CDataStream::write
void write(const char *pch, size_t nSize)
Definition: streams.h:382
strencodings.h
wallet::DatabaseStatus::SUCCESS
@ SUCCESS
wallet::SQLiteBatch::EraseKey
bool EraseKey(CDataStream &&key) override
Definition: sqlite.cpp:446
wallet::SQLiteDatabaseVersion
std::string SQLiteDatabaseVersion()
Definition: sqlite.cpp:578
Untranslated
bilingual_str Untranslated(std::string original)
Mark a bilingual_str as untranslated.
Definition: translation.h:46
wallet::SQLiteDatabase::MakeBatch
std::unique_ptr< DatabaseBatch > MakeBatch(bool flush_on_close=true) override
Make a SQLiteBatch connected to this database.
Definition: sqlite.cpp:329
fs::path
Path class wrapper to prepare application code for transition from boost::filesystem library to std::...
Definition: fs.h:33
PACKAGE_NAME
#define PACKAGE_NAME
Definition: bitcoin-config.h:363
wallet::g_sqlite_mutex
static Mutex g_sqlite_mutex
Definition: sqlite.cpp:26
wallet::SQLiteBatch::m_delete_stmt
sqlite3_stmt * m_delete_stmt
Definition: sqlite.h:28
wallet::DatabaseOptions::verify
bool verify
Definition: db.h:210
wallet::ReadPragmaInteger
static std::optional< int > ReadPragmaInteger(sqlite3 *db, const std::string &key, const std::string &description, bilingual_str &error)
Definition: sqlite.cpp:40
wallet::SQLiteDatabase::m_mock
const bool m_mock
Definition: sqlite.h:60
wallet::ErrorLogCallback
static void ErrorLogCallback(void *arg, int code, const char *msg)
Definition: sqlite.cpp:29
LogPrintf
#define LogPrintf(...)
Definition: logging.h:187
wallet::SQLiteDatabase::SQLiteDatabase
SQLiteDatabase()=delete
fs::PathFromString
static path PathFromString(const std::string &string)
Convert byte string to path object.
Definition: fs.h:143
wallet::SQLiteDatabase::m_db
sqlite3 * m_db
Definition: sqlite.h:115
wallet::SQLiteDatabase::m_dir_path
const std::string m_dir_path
Definition: sqlite.h:62
gArgs
ArgsManager gArgs
Definition: system.cpp:85
system.h
wallet::SQLiteDatabase::Rewrite
bool Rewrite(const char *skip=nullptr) override
Rewrite the entire database on disk.
Definition: sqlite.cpp:286
wallet::SQLiteBatch::m_overwrite_stmt
sqlite3_stmt * m_overwrite_stmt
Definition: sqlite.h:27
strprintf
#define strprintf
Format arguments and return the string or write to given std::ostream (see tinyformat::format doc for...
Definition: tinyformat.h:1164
wallet::SQLiteBatch::ReadKey
bool ReadKey(CDataStream &&key, CDataStream &value) override
Definition: sqlite.cpp:374
wallet::SQLiteBatch::m_insert_stmt
sqlite3_stmt * m_insert_stmt
Definition: sqlite.h:26
wallet::SQLiteBatch::WriteKey
bool WriteKey(CDataStream &&key, CDataStream &&value, bool overwrite=true) override
Definition: sqlite.cpp:407
translation.h
wallet::SQLiteBatch::CloseCursor
void CloseCursor() override
Definition: sqlite.cpp:524
LOCK
#define LOCK(cs)
Definition: sync.h:226
fs
Filesystem operations and types.
Definition: fs.h:19
wallet::WALLET_SCHEMA_VERSION
static constexpr int32_t WALLET_SCHEMA_VERSION
Definition: sqlite.cpp:24
wallet::SQLiteDatabase::Backup
bool Backup(const std::string &dest) const override
Back up the entire database to a file.
Definition: sqlite.cpp:293
wallet::MakeSQLiteDatabase
std::unique_ptr< SQLiteDatabase > MakeSQLiteDatabase(const fs::path &path, const DatabaseOptions &options, DatabaseStatus &status, bilingual_str &error)
Definition: sqlite.cpp:560
Params
const CChainParams & Params()
Return the currently selected parameters.
Definition: chainparams.cpp:561
wallet::GUARDED_BY
static int g_sqlite_count GUARDED_BY(g_sqlite_mutex)=0
CDataStream
Double ended buffer combining vector and stream-like interfaces.
Definition: streams.h:184
error
bool error(const char *fmt, const Args &... args)
Definition: system.h:49
wallet::SQLiteBatch::ReadAtCursor
bool ReadAtCursor(CDataStream &key, CDataStream &value, bool &complete) override
Definition: sqlite.cpp:498
wallet::SQLiteDatabase::Close
void Close() override
Close the database.
Definition: sqlite.cpp:320
wallet::DatabaseOptions
Definition: db.h:204
wallet::SQLiteDatabase::m_file_path
const std::string m_file_path
Definition: sqlite.h:64
wallet::SQLiteDatabase::Verify
bool Verify(bilingual_str &error)
Definition: sqlite.cpp:144
wallet::SQLiteBatch::TxnCommit
bool TxnCommit() override
Definition: sqlite.cpp:540
wallet::SQLiteBatch::SQLiteBatch
SQLiteBatch(SQLiteDatabase &database)
Definition: sqlite.cpp:335
wallet::SQLiteDatabase::Cleanup
void Cleanup() noexcept
Definition: sqlite.cpp:131
wallet::SQLiteDataFile
fs::path SQLiteDataFile(const fs::path &path)
Definition: db.cpp:73
TryCreateDirectories
bool TryCreateDirectories(const fs::path &p)
Ignores exceptions thrown by Boost's create_directories if the requested directory exists.
Definition: system.cpp:1081