Bitcoin Core  31.0.0
P2P Digital Currency
sqlite.cpp
Go to the documentation of this file.
1 // Copyright (c) 2020-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 #include <bitcoin-build-config.h> // IWYU pragma: keep
6 
7 #include <wallet/sqlite.h>
8 
9 #include <chainparams.h>
10 #include <crypto/common.h>
11 #include <logging.h>
12 #include <sync.h>
13 #include <util/check.h>
14 #include <util/fs_helpers.h>
15 #include <util/strencodings.h>
16 #include <util/translation.h>
17 #include <wallet/db.h>
18 
19 #include <sqlite3.h>
20 
21 #include <cstdint>
22 #include <optional>
23 #include <utility>
24 #include <vector>
25 
26 namespace wallet {
27 static constexpr int32_t WALLET_SCHEMA_VERSION = 0;
28 
29 static std::span<const std::byte> SpanFromBlob(sqlite3_stmt* stmt, int col)
30 {
31  return {reinterpret_cast<const std::byte*>(sqlite3_column_blob(stmt, col)),
32  static_cast<size_t>(sqlite3_column_bytes(stmt, col))};
33 }
34 
35 static void ErrorLogCallback(void* arg, int code, const char* msg)
36 {
37  // From sqlite3_config() documentation for the SQLITE_CONFIG_LOG option:
38  // "The void pointer that is the second argument to SQLITE_CONFIG_LOG is passed through as
39  // the first parameter to the application-defined logger function whenever that function is
40  // invoked."
41  // Assert that this is the case:
42  assert(arg == nullptr);
43  LogWarning("SQLite Error. Code: %d. Message: %s", code, msg);
44 }
45 
46 static int TraceSqlCallback(unsigned code, void* context, void* param1, void* param2)
47 {
48  auto* db = static_cast<SQLiteDatabase*>(context);
49  if (code == SQLITE_TRACE_STMT) {
50  auto* stmt = static_cast<sqlite3_stmt*>(param1);
51  // To be conservative and avoid leaking potentially secret information
52  // in the log file, only expand statements that query the database, not
53  // statements that update the database.
54  char* expanded{sqlite3_stmt_readonly(stmt) ? sqlite3_expanded_sql(stmt) : nullptr};
55  LogTrace(BCLog::WALLETDB, "[%s] SQLite Statement: %s\n", db->Filename(), expanded ? expanded : sqlite3_sql(stmt));
56  if (expanded) sqlite3_free(expanded);
57  }
58  return SQLITE_OK;
59 }
60 
61 static bool BindBlobToStatement(sqlite3_stmt* stmt,
62  int index,
63  std::span<const std::byte> blob,
64  const std::string& description)
65 {
66  // Pass a pointer to the empty string "" below instead of passing the
67  // blob.data() pointer if the blob.data() pointer is null. Passing a null
68  // data pointer to bind_blob would cause sqlite to bind the SQL NULL value
69  // instead of the empty blob value X'', which would mess up SQL comparisons.
70  int res = sqlite3_bind_blob(stmt, index, blob.data() ? static_cast<const void*>(blob.data()) : "", blob.size(), SQLITE_STATIC);
71  if (res != SQLITE_OK) {
72  LogWarning("Unable to bind %s to statement: %s", description, sqlite3_errstr(res));
73  sqlite3_clear_bindings(stmt);
74  sqlite3_reset(stmt);
75  return false;
76  }
77 
78  return true;
79 }
80 
81 static std::optional<int> ReadPragmaInteger(sqlite3* db, const std::string& key, const std::string& description, bilingual_str& error)
82 {
83  std::string stmt_text = strprintf("PRAGMA %s", key);
84  sqlite3_stmt* pragma_read_stmt{nullptr};
85  int ret = sqlite3_prepare_v2(db, stmt_text.c_str(), -1, &pragma_read_stmt, nullptr);
86  if (ret != SQLITE_OK) {
87  sqlite3_finalize(pragma_read_stmt);
88  error = Untranslated(strprintf("SQLiteDatabase: Failed to prepare the statement to fetch %s: %s", description, sqlite3_errstr(ret)));
89  return std::nullopt;
90  }
91  ret = sqlite3_step(pragma_read_stmt);
92  if (ret != SQLITE_ROW) {
93  sqlite3_finalize(pragma_read_stmt);
94  error = Untranslated(strprintf("SQLiteDatabase: Failed to fetch %s: %s", description, sqlite3_errstr(ret)));
95  return std::nullopt;
96  }
97  int result = sqlite3_column_int(pragma_read_stmt, 0);
98  sqlite3_finalize(pragma_read_stmt);
99  return result;
100 }
101 
102 static void SetPragma(sqlite3* db, const std::string& key, const std::string& value, const std::string& err_msg)
103 {
104  std::string stmt_text = strprintf("PRAGMA %s = %s", key, value);
105  int ret = sqlite3_exec(db, stmt_text.c_str(), nullptr, nullptr, nullptr);
106  if (ret != SQLITE_OK) {
107  throw std::runtime_error(strprintf("SQLiteDatabase: %s: %s\n", err_msg, sqlite3_errstr(ret)));
108  }
109 }
110 
112 int SQLiteDatabase::g_sqlite_count = 0;
113 
114 SQLiteDatabase::SQLiteDatabase(const fs::path& dir_path, const fs::path& file_path, const DatabaseOptions& options, bool mock)
115  : WalletDatabase(), m_mock(mock), m_dir_path(dir_path), m_file_path(fs::PathToString(file_path)), m_write_semaphore(1), m_use_unsafe_sync(options.use_unsafe_sync)
116 {
117  {
119  if (++g_sqlite_count == 1) {
120  // Setup logging
121  int ret = sqlite3_config(SQLITE_CONFIG_LOG, ErrorLogCallback, nullptr);
122  if (ret != SQLITE_OK) {
123  throw std::runtime_error(strprintf("SQLiteDatabase: Failed to setup error log: %s\n", sqlite3_errstr(ret)));
124  }
125  // Force serialized threading mode
126  ret = sqlite3_config(SQLITE_CONFIG_SERIALIZED);
127  if (ret != SQLITE_OK) {
128  throw std::runtime_error(strprintf("SQLiteDatabase: Failed to configure serialized threading mode: %s\n", sqlite3_errstr(ret)));
129  }
130  }
131  int ret = sqlite3_initialize(); // This is a no-op if sqlite3 is already initialized
132  if (ret != SQLITE_OK) {
133  throw std::runtime_error(strprintf("SQLiteDatabase: Failed to initialize SQLite: %s\n", sqlite3_errstr(ret)));
134  }
135  }
136 
137  try {
138  Open();
139  } catch (const std::runtime_error&) {
140  // If open fails, cleanup this object and rethrow the exception
141  Cleanup();
142  throw;
143  }
144 }
145 
147 {
148  const std::vector<std::pair<sqlite3_stmt**, const char*>> statements{
149  {&m_read_stmt, "SELECT value FROM main WHERE key = ?"},
150  {&m_insert_stmt, "INSERT INTO main VALUES(?, ?)"},
151  {&m_overwrite_stmt, "INSERT or REPLACE into main values(?, ?)"},
152  {&m_delete_stmt, "DELETE FROM main WHERE key = ?"},
153  {&m_delete_prefix_stmt, "DELETE FROM main WHERE instr(key, ?) = 1"},
154  };
155 
156  for (const auto& [stmt_prepared, stmt_text] : statements) {
157  if (*stmt_prepared == nullptr) {
158  int res = sqlite3_prepare_v2(m_database.m_db, stmt_text, -1, stmt_prepared, nullptr);
159  if (res != SQLITE_OK) {
160  throw std::runtime_error(strprintf(
161  "SQLiteDatabase: Failed to setup SQL statements: %s\n", sqlite3_errstr(res)));
162  }
163  }
164  }
165 }
166 
168 {
169  Cleanup();
170 }
171 
172 void SQLiteDatabase::Cleanup() noexcept
173 {
175 
176  Close();
177 
179  if (--g_sqlite_count == 0) {
180  int ret = sqlite3_shutdown();
181  if (ret != SQLITE_OK) {
182  LogWarning("SQLiteDatabase: Failed to shutdown SQLite: %s", sqlite3_errstr(ret));
183  }
184  }
185 }
186 
188 {
189  assert(m_db);
190 
191  // Check the application ID matches our network magic
192  auto read_result = ReadPragmaInteger(m_db, "application_id", "the application id", error);
193  if (!read_result.has_value()) return false;
194  uint32_t app_id = static_cast<uint32_t>(read_result.value());
195  uint32_t net_magic = ReadBE32(Params().MessageStart().data());
196  if (app_id != net_magic) {
197  error = strprintf(_("SQLiteDatabase: Unexpected application id. Expected %u, got %u"), net_magic, app_id);
198  return false;
199  }
200 
201  // Check our schema version
202  read_result = ReadPragmaInteger(m_db, "user_version", "sqlite wallet schema version", error);
203  if (!read_result.has_value()) return false;
204  int32_t user_ver = read_result.value();
205  if (user_ver != WALLET_SCHEMA_VERSION) {
206  error = strprintf(_("SQLiteDatabase: Unknown sqlite wallet schema version %d. Only version %d is supported"), user_ver, WALLET_SCHEMA_VERSION);
207  return false;
208  }
209 
210  sqlite3_stmt* stmt{nullptr};
211  int ret = sqlite3_prepare_v2(m_db, "PRAGMA integrity_check", -1, &stmt, nullptr);
212  if (ret != SQLITE_OK) {
213  sqlite3_finalize(stmt);
214  error = strprintf(_("SQLiteDatabase: Failed to prepare statement to verify database: %s"), sqlite3_errstr(ret));
215  return false;
216  }
217  while (true) {
218  ret = sqlite3_step(stmt);
219  if (ret == SQLITE_DONE) {
220  break;
221  }
222  if (ret != SQLITE_ROW) {
223  error = strprintf(_("SQLiteDatabase: Failed to execute statement to verify database: %s"), sqlite3_errstr(ret));
224  break;
225  }
226  const char* msg = (const char*)sqlite3_column_text(stmt, 0);
227  if (!msg) {
228  error = strprintf(_("SQLiteDatabase: Failed to read database verification error: %s"), sqlite3_errstr(ret));
229  break;
230  }
231  std::string str_msg(msg);
232  if (str_msg == "ok") {
233  continue;
234  }
235  if (error.empty()) {
236  error = _("Failed to verify database") + Untranslated("\n");
237  }
238  error += Untranslated(strprintf("%s\n", str_msg));
239  }
240  sqlite3_finalize(stmt);
241  return error.empty();
242 }
243 
245 {
246  int flags = SQLITE_OPEN_FULLMUTEX | SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE;
247  if (m_mock) {
248  flags |= SQLITE_OPEN_MEMORY; // In memory database for mock db
249  }
250 
251  if (m_db == nullptr) {
252  if (!m_mock) {
254  }
255  int ret = sqlite3_open_v2(m_file_path.c_str(), &m_db, flags, nullptr);
256  if (ret != SQLITE_OK) {
257  throw std::runtime_error(strprintf("SQLiteDatabase: Failed to open database: %s\n", sqlite3_errstr(ret)));
258  }
259  ret = sqlite3_extended_result_codes(m_db, 1);
260  if (ret != SQLITE_OK) {
261  throw std::runtime_error(strprintf("SQLiteDatabase: Failed to enable extended result codes: %s\n", sqlite3_errstr(ret)));
262  }
263  // Trace SQL statements if tracing is enabled with -debug=walletdb -loglevel=walletdb:trace
264  if (LogAcceptCategory(BCLog::WALLETDB, BCLog::Level::Trace)) {
265  ret = sqlite3_trace_v2(m_db, SQLITE_TRACE_STMT, TraceSqlCallback, this);
266  if (ret != SQLITE_OK) {
267  LogWarning("Failed to enable SQL tracing for %s", Filename());
268  }
269  }
270  }
271 
272  if (sqlite3_db_readonly(m_db, "main") != 0) {
273  throw std::runtime_error("SQLiteDatabase: Database opened in readonly mode but read-write permissions are needed");
274  }
275 
276  // Acquire an exclusive lock on the database
277  // First change the locking mode to exclusive
278  SetPragma(m_db, "locking_mode", "exclusive", "Unable to change database locking mode to exclusive");
279  // Now begin a transaction to acquire the exclusive lock. This lock won't be released until we close because of the exclusive locking mode.
280  int ret = sqlite3_exec(m_db, "BEGIN EXCLUSIVE TRANSACTION", nullptr, nullptr, nullptr);
281  if (ret != SQLITE_OK) {
282  throw std::runtime_error("SQLiteDatabase: Unable to obtain an exclusive lock on the database, is it being used by another instance of " CLIENT_NAME "?\n");
283  }
284  ret = sqlite3_exec(m_db, "COMMIT", nullptr, nullptr, nullptr);
285  if (ret != SQLITE_OK) {
286  throw std::runtime_error(strprintf("SQLiteDatabase: Unable to end exclusive lock transaction: %s\n", sqlite3_errstr(ret)));
287  }
288 
289  // Enable fullfsync for the platforms that use it
290  SetPragma(m_db, "fullfsync", "true", "Failed to enable fullfsync");
291 
292  if (m_use_unsafe_sync) {
293  // Use normal synchronous mode for the journal
294  LogWarning("SQLite is configured to not wait for data to be flushed to disk. Data loss and corruption may occur.");
295  SetPragma(m_db, "synchronous", "OFF", "Failed to set synchronous mode to OFF");
296  }
297 
298  // Make the table for our key-value pairs
299  // First check that the main table exists
300  sqlite3_stmt* check_main_stmt{nullptr};
301  ret = sqlite3_prepare_v2(m_db, "SELECT name FROM sqlite_master WHERE type='table' AND name='main'", -1, &check_main_stmt, nullptr);
302  if (ret != SQLITE_OK) {
303  throw std::runtime_error(strprintf("SQLiteDatabase: Failed to prepare statement to check table existence: %s\n", sqlite3_errstr(ret)));
304  }
305  ret = sqlite3_step(check_main_stmt);
306  if (sqlite3_finalize(check_main_stmt) != SQLITE_OK) {
307  throw std::runtime_error(strprintf("SQLiteDatabase: Failed to finalize statement checking table existence: %s\n", sqlite3_errstr(ret)));
308  }
309  bool table_exists;
310  if (ret == SQLITE_DONE) {
311  table_exists = false;
312  } else if (ret == SQLITE_ROW) {
313  table_exists = true;
314  } else {
315  throw std::runtime_error(strprintf("SQLiteDatabase: Failed to execute statement to check table existence: %s\n", sqlite3_errstr(ret)));
316  }
317 
318  // Do the db setup things because the table doesn't exist only when we are creating a new wallet
319  if (!table_exists) {
320  ret = sqlite3_exec(m_db, "CREATE TABLE main(key BLOB PRIMARY KEY NOT NULL, value BLOB NOT NULL)", nullptr, nullptr, nullptr);
321  if (ret != SQLITE_OK) {
322  throw std::runtime_error(strprintf("SQLiteDatabase: Failed to create new database: %s\n", sqlite3_errstr(ret)));
323  }
324 
325  // Set the application id
326  uint32_t app_id = ReadBE32(Params().MessageStart().data());
327  SetPragma(m_db, "application_id", strprintf("%d", static_cast<int32_t>(app_id)),
328  "Failed to set the application id");
329 
330  // Set the user version
331  SetPragma(m_db, "user_version", strprintf("%d", WALLET_SCHEMA_VERSION),
332  "Failed to set the wallet schema version");
333  }
334 }
335 
337 {
338  // Rewrite the database using the VACUUM command: https://sqlite.org/lang_vacuum.html
339  int ret = sqlite3_exec(m_db, "VACUUM", nullptr, nullptr, nullptr);
340  return ret == SQLITE_OK;
341 }
342 
343 bool SQLiteDatabase::Backup(const std::string& dest) const
344 {
345  sqlite3* db_copy;
346  int res = sqlite3_open(dest.c_str(), &db_copy);
347  if (res != SQLITE_OK) {
348  sqlite3_close(db_copy);
349  return false;
350  }
351  sqlite3_backup* backup = sqlite3_backup_init(db_copy, "main", m_db, "main");
352  if (!backup) {
353  LogWarning("Unable to begin sqlite backup: %s", sqlite3_errmsg(m_db));
354  sqlite3_close(db_copy);
355  return false;
356  }
357  // Specifying -1 will copy all of the pages
358  res = sqlite3_backup_step(backup, -1);
359  if (res != SQLITE_DONE) {
360  LogWarning("Unable to continue sqlite backup: %s", sqlite3_errstr(res));
361  sqlite3_backup_finish(backup);
362  sqlite3_close(db_copy);
363  return false;
364  }
365  res = sqlite3_backup_finish(backup);
366  sqlite3_close(db_copy);
367  return res == SQLITE_OK;
368 }
369 
371 {
372  int res = sqlite3_close(m_db);
373  if (res != SQLITE_OK) {
374  throw std::runtime_error(strprintf("SQLiteDatabase: Failed to close database: %s\n", sqlite3_errstr(res)));
375  }
376  m_db = nullptr;
377 }
378 
380 {
381  // 'sqlite3_get_autocommit' returns true by default, and false if a transaction has begun and not been committed or rolled back.
382  return m_db && sqlite3_get_autocommit(m_db) == 0;
383 }
384 
385 int SQliteExecHandler::Exec(SQLiteDatabase& database, const std::string& statement)
386 {
387  return sqlite3_exec(database.m_db, statement.data(), nullptr, nullptr, nullptr);
388 }
389 
390 std::unique_ptr<DatabaseBatch> SQLiteDatabase::MakeBatch()
391 {
392  // We ignore flush_on_close because we don't do manual flushing for SQLite
393  return std::make_unique<SQLiteBatch>(*this);
394 }
395 
397  : m_database(database)
398 {
399  // Make sure we have a db handle
401 
403 }
404 
406 {
407  bool force_conn_refresh = false;
408 
409  // If we began a transaction, and it wasn't committed, abort the transaction in progress
410  if (m_txn) {
411  if (TxnAbort()) {
412  LogWarning("SQLiteBatch: Batch closed unexpectedly without the transaction being explicitly committed or aborted");
413  } else {
414  // If transaction cannot be aborted, it means there is a bug or there has been data corruption. Try to recover in this case
415  // by closing and reopening the database. Closing the database should also ensure that any changes made since the transaction
416  // was opened will be rolled back and future transactions can succeed without committing old data.
417  force_conn_refresh = true;
418  LogWarning("SQLiteBatch: Batch closed and failed to abort transaction, resetting db connection..");
419  }
420  }
421 
422  // Free all of the prepared statements
423  const std::vector<std::pair<sqlite3_stmt**, const char*>> statements{
424  {&m_read_stmt, "read"},
425  {&m_insert_stmt, "insert"},
426  {&m_overwrite_stmt, "overwrite"},
427  {&m_delete_stmt, "delete"},
428  {&m_delete_prefix_stmt, "delete prefix"},
429  };
430 
431  for (const auto& [stmt_prepared, stmt_description] : statements) {
432  int res = sqlite3_finalize(*stmt_prepared);
433  if (res != SQLITE_OK) {
434  LogWarning("SQLiteBatch: Batch closed but could not finalize %s statement: %s",
435  stmt_description, sqlite3_errstr(res));
436  }
437  *stmt_prepared = nullptr;
438  }
439 
440  if (force_conn_refresh) {
441  m_database.Close();
442  try {
443  m_database.Open();
444  // If TxnAbort failed and we refreshed the connection, the semaphore was not released, so release it here to avoid deadlocks on future writes.
445  m_database.m_write_semaphore.release();
446  } catch (const std::runtime_error&) {
447  // If open fails, cleanup this object and rethrow the exception
448  m_database.Close();
449  throw;
450  }
451  }
452 }
453 
455 {
456  if (!m_database.m_db) return false;
458 
459  // Bind: leftmost parameter in statement is index 1
460  if (!BindBlobToStatement(m_read_stmt, 1, key, "key")) return false;
461  int res = sqlite3_step(m_read_stmt);
462  if (res != SQLITE_ROW) {
463  if (res != SQLITE_DONE) {
464  // SQLITE_DONE means "not found", don't log an error in that case.
465  LogWarning("Unable to execute read statement: %s", sqlite3_errstr(res));
466  }
467  sqlite3_clear_bindings(m_read_stmt);
468  sqlite3_reset(m_read_stmt);
469  return false;
470  }
471  // Leftmost column in result is index 0
472  value.clear();
473  value.write(SpanFromBlob(m_read_stmt, 0));
474 
475  sqlite3_clear_bindings(m_read_stmt);
476  sqlite3_reset(m_read_stmt);
477  return true;
478 }
479 
480 bool SQLiteBatch::WriteKey(DataStream&& key, DataStream&& value, bool overwrite)
481 {
482  if (!m_database.m_db) return false;
484 
485  sqlite3_stmt* stmt;
486  if (overwrite) {
487  stmt = m_overwrite_stmt;
488  } else {
489  stmt = m_insert_stmt;
490  }
491 
492  // Bind: leftmost parameter in statement is index 1
493  // Insert index 1 is key, 2 is value
494  if (!BindBlobToStatement(stmt, 1, key, "key")) return false;
495  if (!BindBlobToStatement(stmt, 2, value, "value")) return false;
496 
497  // Acquire semaphore if not previously acquired when creating a transaction.
498  if (!m_txn) m_database.m_write_semaphore.acquire();
499 
500  // Execute
501  int res = sqlite3_step(stmt);
502  sqlite3_clear_bindings(stmt);
503  sqlite3_reset(stmt);
504  if (res != SQLITE_DONE) {
505  LogWarning("Unable to execute write statement: %s", sqlite3_errstr(res));
506  }
507 
508  if (!m_txn) m_database.m_write_semaphore.release();
509 
510  return res == SQLITE_DONE;
511 }
512 
513 bool SQLiteBatch::ExecStatement(sqlite3_stmt* stmt, std::span<const std::byte> blob)
514 {
515  if (!m_database.m_db) return false;
516  assert(stmt);
517 
518  // Bind: leftmost parameter in statement is index 1
519  if (!BindBlobToStatement(stmt, 1, blob, "key")) return false;
520 
521  // Acquire semaphore if not previously acquired when creating a transaction.
522  if (!m_txn) m_database.m_write_semaphore.acquire();
523 
524  // Execute
525  int res = sqlite3_step(stmt);
526  sqlite3_clear_bindings(stmt);
527  sqlite3_reset(stmt);
528  if (res != SQLITE_DONE) {
529  LogWarning("Unable to execute exec statement: %s", sqlite3_errstr(res));
530  }
531 
532  if (!m_txn) m_database.m_write_semaphore.release();
533 
534  return res == SQLITE_DONE;
535 }
536 
538 {
539  return ExecStatement(m_delete_stmt, key);
540 }
541 
542 bool SQLiteBatch::ErasePrefix(std::span<const std::byte> prefix)
543 {
545 }
546 
548 {
549  if (!m_database.m_db) return false;
551 
552  // Bind: leftmost parameter in statement is index 1
553  if (!BindBlobToStatement(m_read_stmt, 1, key, "key")) return false;
554  int res = sqlite3_step(m_read_stmt);
555  sqlite3_clear_bindings(m_read_stmt);
556  sqlite3_reset(m_read_stmt);
557  return res == SQLITE_ROW;
558 }
559 
561 {
562  int res = sqlite3_step(m_cursor_stmt);
563  if (res == SQLITE_DONE) {
564  return Status::DONE;
565  }
566  if (res != SQLITE_ROW) {
567  LogWarning("Unable to execute cursor step: %s", sqlite3_errstr(res));
568  return Status::FAIL;
569  }
570 
571  key.clear();
572  value.clear();
573 
574  // Leftmost column in result is index 0
576  value.write(SpanFromBlob(m_cursor_stmt, 1));
577  return Status::MORE;
578 }
579 
581 {
582  sqlite3_clear_bindings(m_cursor_stmt);
583  sqlite3_reset(m_cursor_stmt);
584  int res = sqlite3_finalize(m_cursor_stmt);
585  if (res != SQLITE_OK) {
586  LogWarning("Cursor closed but could not finalize cursor statement: %s",
587  sqlite3_errstr(res));
588  }
589 }
590 
591 std::unique_ptr<DatabaseCursor> SQLiteBatch::GetNewCursor()
592 {
593  if (!m_database.m_db) return nullptr;
594  auto cursor = std::make_unique<SQLiteCursor>();
595 
596  const char* stmt_text = "SELECT key, value FROM main";
597  int res = sqlite3_prepare_v2(m_database.m_db, stmt_text, -1, &cursor->m_cursor_stmt, nullptr);
598  if (res != SQLITE_OK) {
599  throw std::runtime_error(strprintf(
600  "%s: Failed to setup cursor SQL statement: %s\n", __func__, sqlite3_errstr(res)));
601  }
602 
603  return cursor;
604 }
605 
606 std::unique_ptr<DatabaseCursor> SQLiteBatch::GetNewPrefixCursor(std::span<const std::byte> prefix)
607 {
608  if (!m_database.m_db) return nullptr;
609 
610  // To get just the records we want, the SQL statement does a comparison of the binary data
611  // where the data must be greater than or equal to the prefix, and less than
612  // the prefix incremented by one (when interpreted as an integer)
613  std::vector<std::byte> start_range(prefix.begin(), prefix.end());
614  std::vector<std::byte> end_range(prefix.begin(), prefix.end());
615  auto it = end_range.rbegin();
616  for (; it != end_range.rend(); ++it) {
617  if (*it == std::byte(std::numeric_limits<unsigned char>::max())) {
618  *it = std::byte(0);
619  continue;
620  }
621  *it = std::byte(std::to_integer<unsigned char>(*it) + 1);
622  break;
623  }
624  if (it == end_range.rend()) {
625  // If the prefix is all 0xff bytes, clear end_range as we won't need it
626  end_range.clear();
627  }
628 
629  auto cursor = std::make_unique<SQLiteCursor>(start_range, end_range);
630  if (!cursor) return nullptr;
631 
632  const char* stmt_text = end_range.empty() ? "SELECT key, value FROM main WHERE key >= ?" :
633  "SELECT key, value FROM main WHERE key >= ? AND key < ?";
634  int res = sqlite3_prepare_v2(m_database.m_db, stmt_text, -1, &cursor->m_cursor_stmt, nullptr);
635  if (res != SQLITE_OK) {
636  throw std::runtime_error(strprintf(
637  "SQLiteDatabase: Failed to setup cursor SQL statement: %s\n", sqlite3_errstr(res)));
638  }
639 
640  if (!BindBlobToStatement(cursor->m_cursor_stmt, 1, cursor->m_prefix_range_start, "prefix_start")) return nullptr;
641  if (!end_range.empty()) {
642  if (!BindBlobToStatement(cursor->m_cursor_stmt, 2, cursor->m_prefix_range_end, "prefix_end")) return nullptr;
643  }
644 
645  return cursor;
646 }
647 
649 {
650  if (!m_database.m_db || m_txn) return false;
651  m_database.m_write_semaphore.acquire();
653  int res = Assert(m_exec_handler)->Exec(m_database, "BEGIN TRANSACTION");
654  if (res != SQLITE_OK) {
655  LogWarning("SQLiteBatch: Failed to begin the transaction");
656  m_database.m_write_semaphore.release();
657  } else {
658  m_txn = true;
659  }
660  return res == SQLITE_OK;
661 }
662 
664 {
665  if (!m_database.m_db || !m_txn) return false;
667  int res = Assert(m_exec_handler)->Exec(m_database, "COMMIT TRANSACTION");
668  if (res != SQLITE_OK) {
669  LogWarning("SQLiteBatch: Failed to commit the transaction");
670  } else {
671  m_txn = false;
672  m_database.m_write_semaphore.release();
673  }
674  return res == SQLITE_OK;
675 }
676 
678 {
679  if (!m_database.m_db || !m_txn) return false;
681  int res = Assert(m_exec_handler)->Exec(m_database, "ROLLBACK TRANSACTION");
682  if (res != SQLITE_OK) {
683  LogWarning("SQLiteBatch: Failed to abort the transaction");
684  } else {
685  m_txn = false;
686  m_database.m_write_semaphore.release();
687  }
688  return res == SQLITE_OK;
689 }
690 
691 std::unique_ptr<SQLiteDatabase> MakeSQLiteDatabase(const fs::path& path, const DatabaseOptions& options, DatabaseStatus& status, bilingual_str& error)
692 {
693  try {
694  fs::path data_file = SQLiteDataFile(path);
695  auto db = std::make_unique<SQLiteDatabase>(data_file.parent_path(), data_file, options);
696  if (options.verify && !db->Verify(error)) {
698  return nullptr;
699  }
700  status = DatabaseStatus::SUCCESS;
701  return db;
702  } catch (const std::runtime_error& e) {
704  error = Untranslated(e.what());
705  return nullptr;
706  }
707 }
708 
710 {
711  return std::string(sqlite3_libversion());
712 }
713 } // namespace wallet
bool TryCreateDirectories(const fs::path &p)
Ignores exceptions thrown by create_directories if the requested directory exists.
Definition: fs_helpers.cpp:255
void write(std::span< const value_type > src)
Definition: streams.h:236
int ret
assert(!tx.IsCoinBase())
uint32_t ReadBE32(const B *ptr)
Definition: common.h:72
void Cleanup() noexcept EXCLUSIVE_LOCKS_REQUIRED(!g_sqlite_mutex)
Definition: sqlite.cpp:172
Bilingual messages:
Definition: translation.h:24
#define LogWarning(...)
Definition: log.h:96
static Mutex g_sqlite_mutex
This mutex protects SQLite initialization and shutdown.
Definition: sqlite.h:117
std::unique_ptr< SQLiteDatabase > MakeSQLiteDatabase(const fs::path &path, const DatabaseOptions &options, DatabaseStatus &status, bilingual_str &error)
Definition: sqlite.cpp:691
bool empty() const
Definition: translation.h:35
#define strprintf
Format arguments and return the string or write to given std::ostream (see tinyformat::format doc for...
Definition: tinyformat.h:1172
bilingual_str Untranslated(std::string original)
Mark a bilingual_str as untranslated.
Definition: translation.h:82
bool TxnCommit() override
Definition: sqlite.cpp:663
const char * prefix
Definition: rest.cpp:1141
const std::string m_file_path
Definition: sqlite.h:109
void SetupSQLStatements()
Definition: sqlite.cpp:146
bool m_txn
Whether this batch has started a database transaction and whether it owns SQLiteDatabase::m_write_sem...
Definition: sqlite.h:73
SQLiteDatabase & m_database
Definition: sqlite.h:54
bool HasKey(DataStream &&key) override
Definition: sqlite.cpp:547
std::unique_ptr< DatabaseBatch > MakeBatch() override
Make a SQLiteBatch connected to this database.
Definition: sqlite.cpp:390
std::string SQLiteDatabaseVersion()
Definition: sqlite.cpp:709
static int TraceSqlCallback(unsigned code, void *context, void *param1, void *param2)
Definition: sqlite.cpp:46
#define LogTrace(category,...)
Definition: log.h:116
sqlite3_stmt * m_overwrite_stmt
Definition: sqlite.h:59
sqlite3_stmt * m_insert_stmt
Definition: sqlite.h:58
consteval auto _(util::TranslatedLiteral str)
Definition: translation.h:79
std::unique_ptr< DatabaseCursor > GetNewCursor() override
Definition: sqlite.cpp:591
static std::optional< int > ReadPragmaInteger(sqlite3 *db, const std::string &key, const std::string &description, bilingual_str &error)
Definition: sqlite.cpp:81
fs::path SQLiteDataFile(const fs::path &path)
Definition: db.cpp:89
virtual int Exec(SQLiteDatabase &database, const std::string &statement)
Definition: sqlite.cpp:385
static bool BindBlobToStatement(sqlite3_stmt *stmt, int index, std::span< const std::byte > blob, const std::string &description)
Definition: sqlite.cpp:61
std::unique_ptr< SQliteExecHandler > m_exec_handler
Definition: sqlite.h:55
bool Verify(bilingual_str &error)
Definition: sqlite.cpp:187
std::binary_semaphore m_write_semaphore
Definition: sqlite.h:132
const bool m_mock
Definition: sqlite.h:105
bool ErasePrefix(std::span< const std::byte > prefix) override
Definition: sqlite.cpp:542
bool TxnAbort() override
Definition: sqlite.cpp:677
#define LOCK(cs)
Definition: sync.h:258
Double ended buffer combining vector and stream-like interfaces.
Definition: streams.h:132
void Open() override
Open the database if it is not already opened.
Definition: sqlite.cpp:244
bool WriteKey(DataStream &&key, DataStream &&value, bool overwrite=true) override
Definition: sqlite.cpp:480
bool Rewrite() override
Rewrite the entire database on disk.
Definition: sqlite.cpp:336
bool HasActiveTxn()
Return true if there is an on-going txn in this connection.
Definition: sqlite.cpp:379
bool EraseKey(DataStream &&key) override
Definition: sqlite.cpp:537
bool ExecStatement(sqlite3_stmt *stmt, std::span< const std::byte > blob)
Definition: sqlite.cpp:513
void Close() override
Definition: sqlite.cpp:405
static constexpr int32_t WALLET_SCHEMA_VERSION
Definition: sqlite.cpp:27
SQLiteBatch(SQLiteDatabase &database)
Definition: sqlite.cpp:396
Status Next(DataStream &key, DataStream &value) override
Definition: sqlite.cpp:560
int flags
Definition: bitcoin-tx.cpp:529
DatabaseStatus
Definition: db.h:186
~SQLiteCursor() override
Definition: sqlite.cpp:580
std::unique_ptr< DatabaseCursor > GetNewPrefixCursor(std::span< const std::byte > prefix) override
Definition: sqlite.cpp:606
std::string Filename() override
Return path to main database file for logs and error messages.
Definition: sqlite.h:149
static std::span< const std::byte > SpanFromBlob(sqlite3_stmt *stmt, int col)
Definition: sqlite.cpp:29
void clear()
Definition: streams.h:173
auto result
Definition: common-types.h:74
sqlite3_stmt * m_cursor_stmt
Definition: sqlite.h:25
bool ReadKey(DataStream &&key, DataStream &value) override
Definition: sqlite.cpp:454
const CChainParams & Params()
Return the currently selected parameters.
bool TxnBegin() override
Definition: sqlite.cpp:648
static void ErrorLogCallback(void *arg, int code, const char *msg)
Definition: sqlite.cpp:35
#define AssertLockNotHeld(cs)
Definition: sync.h:141
An instance of this class represents one SQLite3 database.
Definition: sqlite.h:102
static bool LogAcceptCategory(BCLog::LogFlags category, BCLog::Level level)
Return true if log accepts specified category, at the specified level.
Definition: logging.h:294
const fs::path m_dir_path
Definition: sqlite.h:107
sqlite3_stmt * m_delete_stmt
Definition: sqlite.h:60
sqlite3_stmt * m_delete_prefix_stmt
Definition: sqlite.h:61
static void SetPragma(sqlite3 *db, const std::string &key, const std::string &value, const std::string &err_msg)
Definition: sqlite.cpp:102
Path class wrapper to block calls to the fs::path(std::string) implicit constructor and the fs::path:...
Definition: fs.h:33
bool verify
Check data integrity on load.
Definition: db.h:180
bool Backup(const std::string &dest) const override
Back up the entire database to a file.
Definition: sqlite.cpp:343
An instance of this class represents one database.
Definition: db.h:129
sqlite3_stmt * m_read_stmt
Definition: sqlite.h:57
void Close() override
Close the database.
Definition: sqlite.cpp:370
#define Assert(val)
Identity function.
Definition: check.h:113