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