Bitcoin Core  0.20.99
P2P Digital Currency
sqlite.cpp
Go to the documentation of this file.
1 // Copyright (c) 2020 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/memory.h>
12 #include <util/strencodings.h>
13 #include <util/system.h>
14 #include <util/translation.h>
15 #include <wallet/db.h>
16 
17 #include <sqlite3.h>
18 #include <stdint.h>
19 
20 static const char* const DATABASE_FILENAME = "wallet.dat";
21 static constexpr int32_t WALLET_SCHEMA_VERSION = 0;
22 
24 static int g_sqlite_count GUARDED_BY(g_sqlite_mutex) = 0;
25 
26 static void ErrorLogCallback(void* arg, int code, const char* msg)
27 {
28  // From sqlite3_config() documentation for the SQLITE_CONFIG_LOG option:
29  // "The void pointer that is the second argument to SQLITE_CONFIG_LOG is passed through as
30  // the first parameter to the application-defined logger function whenever that function is
31  // invoked."
32  // Assert that this is the case:
33  assert(arg == nullptr);
34  LogPrintf("SQLite Error. Code: %d. Message: %s\n", code, msg);
35 }
36 
37 SQLiteDatabase::SQLiteDatabase(const fs::path& dir_path, const fs::path& file_path, bool mock)
38  : WalletDatabase(), m_mock(mock), m_dir_path(dir_path.string()), m_file_path(file_path.string())
39 {
40  {
41  LOCK(g_sqlite_mutex);
42  LogPrintf("Using SQLite Version %s\n", SQLiteDatabaseVersion());
43  LogPrintf("Using wallet %s\n", m_dir_path);
44 
45  if (++g_sqlite_count == 1) {
46  // Setup logging
47  int ret = sqlite3_config(SQLITE_CONFIG_LOG, ErrorLogCallback, nullptr);
48  if (ret != SQLITE_OK) {
49  throw std::runtime_error(strprintf("SQLiteDatabase: Failed to setup error log: %s\n", sqlite3_errstr(ret)));
50  }
51  // Force serialized threading mode
52  ret = sqlite3_config(SQLITE_CONFIG_SERIALIZED);
53  if (ret != SQLITE_OK) {
54  throw std::runtime_error(strprintf("SQLiteDatabase: Failed to configure serialized threading mode: %s\n", sqlite3_errstr(ret)));
55  }
56  }
57  int ret = sqlite3_initialize(); // This is a no-op if sqlite3 is already initialized
58  if (ret != SQLITE_OK) {
59  throw std::runtime_error(strprintf("SQLiteDatabase: Failed to initialize SQLite: %s\n", sqlite3_errstr(ret)));
60  }
61  }
62 
63  try {
64  Open();
65  } catch (const std::runtime_error&) {
66  // If open fails, cleanup this object and rethrow the exception
67  Cleanup();
68  throw;
69  }
70 }
71 
73 {
74  int res;
75  if (!m_read_stmt) {
76  if ((res = sqlite3_prepare_v2(m_database.m_db, "SELECT value FROM main WHERE key = ?", -1, &m_read_stmt, nullptr)) != SQLITE_OK) {
77  throw std::runtime_error(strprintf("SQLiteDatabase: Failed to setup SQL statements: %s\n", sqlite3_errstr(res)));
78  }
79  }
80  if (!m_insert_stmt) {
81  if ((res = sqlite3_prepare_v2(m_database.m_db, "INSERT INTO main VALUES(?, ?)", -1, &m_insert_stmt, nullptr)) != SQLITE_OK) {
82  throw std::runtime_error(strprintf("SQLiteDatabase: Failed to setup SQL statements: %s\n", sqlite3_errstr(res)));
83  }
84  }
85  if (!m_overwrite_stmt) {
86  if ((res = sqlite3_prepare_v2(m_database.m_db, "INSERT or REPLACE into main values(?, ?)", -1, &m_overwrite_stmt, nullptr)) != SQLITE_OK) {
87  throw std::runtime_error(strprintf("SQLiteDatabase: Failed to setup SQL statements: %s\n", sqlite3_errstr(res)));
88  }
89  }
90  if (!m_delete_stmt) {
91  if ((res = sqlite3_prepare_v2(m_database.m_db, "DELETE FROM main WHERE key = ?", -1, &m_delete_stmt, nullptr)) != SQLITE_OK) {
92  throw std::runtime_error(strprintf("SQLiteDatabase: Failed to setup SQL statements: %s\n", sqlite3_errstr(res)));
93  }
94  }
95  if (!m_cursor_stmt) {
96  if ((res = sqlite3_prepare_v2(m_database.m_db, "SELECT key, value FROM main", -1, &m_cursor_stmt, nullptr)) != SQLITE_OK) {
97  throw std::runtime_error(strprintf("SQLiteDatabase: Failed to setup SQL statements : %s\n", sqlite3_errstr(res)));
98  }
99  }
100 }
101 
103 {
104  Cleanup();
105 }
106 
107 void SQLiteDatabase::Cleanup() noexcept
108 {
109  Close();
110 
111  LOCK(g_sqlite_mutex);
112  if (--g_sqlite_count == 0) {
113  int ret = sqlite3_shutdown();
114  if (ret != SQLITE_OK) {
115  LogPrintf("SQLiteDatabase: Failed to shutdown SQLite: %s\n", sqlite3_errstr(ret));
116  }
117  }
118 }
119 
121 {
122  assert(m_db);
123 
124  // Check the application ID matches our network magic
125  sqlite3_stmt* app_id_stmt{nullptr};
126  int ret = sqlite3_prepare_v2(m_db, "PRAGMA application_id", -1, &app_id_stmt, nullptr);
127  if (ret != SQLITE_OK) {
128  sqlite3_finalize(app_id_stmt);
129  error = strprintf(_("SQLiteDatabase: Failed to prepare the statement to fetch the application id: %s"), sqlite3_errstr(ret));
130  return false;
131  }
132  ret = sqlite3_step(app_id_stmt);
133  if (ret != SQLITE_ROW) {
134  sqlite3_finalize(app_id_stmt);
135  error = strprintf(_("SQLiteDatabase: Failed to fetch the application id: %s"), sqlite3_errstr(ret));
136  return false;
137  }
138  uint32_t app_id = static_cast<uint32_t>(sqlite3_column_int(app_id_stmt, 0));
139  sqlite3_finalize(app_id_stmt);
140  uint32_t net_magic = ReadBE32(Params().MessageStart());
141  if (app_id != net_magic) {
142  error = strprintf(_("SQLiteDatabase: Unexpected application id. Expected %u, got %u"), net_magic, app_id);
143  return false;
144  }
145 
146  // Check our schema version
147  sqlite3_stmt* user_ver_stmt{nullptr};
148  ret = sqlite3_prepare_v2(m_db, "PRAGMA user_version", -1, &user_ver_stmt, nullptr);
149  if (ret != SQLITE_OK) {
150  sqlite3_finalize(user_ver_stmt);
151  error = strprintf(_("SQLiteDatabase: Failed to prepare the statement to fetch sqlite wallet schema version: %s"), sqlite3_errstr(ret));
152  return false;
153  }
154  ret = sqlite3_step(user_ver_stmt);
155  if (ret != SQLITE_ROW) {
156  sqlite3_finalize(user_ver_stmt);
157  error = strprintf(_("SQLiteDatabase: Failed to fetch sqlite wallet schema version: %s"), sqlite3_errstr(ret));
158  return false;
159  }
160  int32_t user_ver = sqlite3_column_int(user_ver_stmt, 0);
161  sqlite3_finalize(user_ver_stmt);
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  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) {
210  int ret = sqlite3_open_v2(m_file_path.c_str(), &m_db, flags, nullptr);
211  if (ret != SQLITE_OK) {
212  throw std::runtime_error(strprintf("SQLiteDatabase: Failed to open database: %s\n", sqlite3_errstr(ret)));
213  }
214  }
215 
216  if (sqlite3_db_readonly(m_db, "main") != 0) {
217  throw std::runtime_error("SQLiteDatabase: Database opened in readonly mode but read-write permissions are needed");
218  }
219 
220  // Acquire an exclusive lock on the database
221  // First change the locking mode to exclusive
222  int ret = sqlite3_exec(m_db, "PRAGMA locking_mode = exclusive", nullptr, nullptr, nullptr);
223  if (ret != SQLITE_OK) {
224  throw std::runtime_error(strprintf("SQLiteDatabase: Unable to change database locking mode to exclusive: %s\n", sqlite3_errstr(ret)));
225  }
226  // Now begin a transaction to acquire the exclusive lock. This lock won't be released until we close because of the exclusive locking mode.
227  ret = sqlite3_exec(m_db, "BEGIN EXCLUSIVE TRANSACTION", nullptr, nullptr, nullptr);
228  if (ret != SQLITE_OK) {
229  throw std::runtime_error("SQLiteDatabase: Unable to obtain an exclusive lock on the database, is it being used by another bitcoind?\n");
230  }
231  ret = sqlite3_exec(m_db, "COMMIT", nullptr, nullptr, nullptr);
232  if (ret != SQLITE_OK) {
233  throw std::runtime_error(strprintf("SQLiteDatabase: Unable to end exclusive lock transaction: %s\n", sqlite3_errstr(ret)));
234  }
235 
236  // Enable fullfsync for the platforms that use it
237  ret = sqlite3_exec(m_db, "PRAGMA fullfsync = true", nullptr, nullptr, nullptr);
238  if (ret != SQLITE_OK) {
239  throw std::runtime_error(strprintf("SQLiteDatabase: Failed to enable fullfsync: %s\n", sqlite3_errstr(ret)));
240  }
241 
242  // Make the table for our key-value pairs
243  // First check that the main table exists
244  sqlite3_stmt* check_main_stmt{nullptr};
245  ret = sqlite3_prepare_v2(m_db, "SELECT name FROM sqlite_master WHERE type='table' AND name='main'", -1, &check_main_stmt, nullptr);
246  if (ret != SQLITE_OK) {
247  throw std::runtime_error(strprintf("SQLiteDatabase: Failed to prepare statement to check table existence: %s\n", sqlite3_errstr(ret)));
248  }
249  ret = sqlite3_step(check_main_stmt);
250  if (sqlite3_finalize(check_main_stmt) != SQLITE_OK) {
251  throw std::runtime_error(strprintf("SQLiteDatabase: Failed to finalize statement checking table existence: %s\n", sqlite3_errstr(ret)));
252  }
253  bool table_exists;
254  if (ret == SQLITE_DONE) {
255  table_exists = false;
256  } else if (ret == SQLITE_ROW) {
257  table_exists = true;
258  } else {
259  throw std::runtime_error(strprintf("SQLiteDatabase: Failed to execute statement to check table existence: %s\n", sqlite3_errstr(ret)));
260  }
261 
262  // Do the db setup things because the table doesn't exist only when we are creating a new wallet
263  if (!table_exists) {
264  ret = sqlite3_exec(m_db, "CREATE TABLE main(key BLOB PRIMARY KEY NOT NULL, value BLOB NOT NULL)", nullptr, nullptr, nullptr);
265  if (ret != SQLITE_OK) {
266  throw std::runtime_error(strprintf("SQLiteDatabase: Failed to create new database: %s\n", sqlite3_errstr(ret)));
267  }
268 
269  // Set the application id
270  uint32_t app_id = ReadBE32(Params().MessageStart());
271  std::string set_app_id = strprintf("PRAGMA application_id = %d", static_cast<int32_t>(app_id));
272  ret = sqlite3_exec(m_db, set_app_id.c_str(), nullptr, nullptr, nullptr);
273  if (ret != SQLITE_OK) {
274  throw std::runtime_error(strprintf("SQLiteDatabase: Failed to set the application id: %s\n", sqlite3_errstr(ret)));
275  }
276 
277  // Set the user version
278  std::string set_user_ver = strprintf("PRAGMA user_version = %d", WALLET_SCHEMA_VERSION);
279  ret = sqlite3_exec(m_db, set_user_ver.c_str(), nullptr, nullptr, nullptr);
280  if (ret != SQLITE_OK) {
281  throw std::runtime_error(strprintf("SQLiteDatabase: Failed to set the wallet schema version: %s\n", sqlite3_errstr(ret)));
282  }
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 MakeUnique<SQLiteBatch>(*this);
333 }
334 
336  : m_database(database)
337 {
338  // Make sure we have a db handle
339  assert(m_database.m_db);
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  int ret = sqlite3_finalize(m_read_stmt);
357  if (ret != SQLITE_OK) {
358  LogPrintf("SQLiteBatch: Batch closed but could not finalize read statement: %s\n", sqlite3_errstr(ret));
359  }
360  ret = sqlite3_finalize(m_insert_stmt);
361  if (ret != SQLITE_OK) {
362  LogPrintf("SQLiteBatch: Batch closed but could not finalize insert statement: %s\n", sqlite3_errstr(ret));
363  }
364  ret = sqlite3_finalize(m_overwrite_stmt);
365  if (ret != SQLITE_OK) {
366  LogPrintf("SQLiteBatch: Batch closed but could not finalize overwrite statement: %s\n", sqlite3_errstr(ret));
367  }
368  ret = sqlite3_finalize(m_delete_stmt);
369  if (ret != SQLITE_OK) {
370  LogPrintf("SQLiteBatch: Batch closed but could not finalize delete statement: %s\n", sqlite3_errstr(ret));
371  }
372  ret = sqlite3_finalize(m_cursor_stmt);
373  if (ret != SQLITE_OK) {
374  LogPrintf("SQLiteBatch: Batch closed but could not finalize cursor statement: %s\n", sqlite3_errstr(ret));
375  }
376  m_read_stmt = nullptr;
377  m_insert_stmt = nullptr;
378  m_overwrite_stmt = nullptr;
379  m_delete_stmt = nullptr;
380  m_cursor_stmt = nullptr;
381 }
382 
384 {
385  if (!m_database.m_db) return false;
386  assert(m_read_stmt);
387 
388  // Bind: leftmost parameter in statement is index 1
389  int res = sqlite3_bind_blob(m_read_stmt, 1, key.data(), key.size(), SQLITE_STATIC);
390  if (res != SQLITE_OK) {
391  LogPrintf("%s: Unable to bind statement: %s\n", __func__, sqlite3_errstr(res));
392  sqlite3_clear_bindings(m_read_stmt);
393  sqlite3_reset(m_read_stmt);
394  return false;
395  }
396  res = sqlite3_step(m_read_stmt);
397  if (res != SQLITE_ROW) {
398  if (res != SQLITE_DONE) {
399  // SQLITE_DONE means "not found", don't log an error in that case.
400  LogPrintf("%s: Unable to execute statement: %s\n", __func__, sqlite3_errstr(res));
401  }
402  sqlite3_clear_bindings(m_read_stmt);
403  sqlite3_reset(m_read_stmt);
404  return false;
405  }
406  // Leftmost column in result is index 0
407  const char* data = reinterpret_cast<const char*>(sqlite3_column_blob(m_read_stmt, 0));
408  int data_size = sqlite3_column_bytes(m_read_stmt, 0);
409  value.write(data, data_size);
410 
411  sqlite3_clear_bindings(m_read_stmt);
412  sqlite3_reset(m_read_stmt);
413  return true;
414 }
415 
416 bool SQLiteBatch::WriteKey(CDataStream&& key, CDataStream&& value, bool overwrite)
417 {
418  if (!m_database.m_db) return false;
419  assert(m_insert_stmt && m_overwrite_stmt);
420 
421  sqlite3_stmt* stmt;
422  if (overwrite) {
423  stmt = m_overwrite_stmt;
424  } else {
425  stmt = m_insert_stmt;
426  }
427 
428  // Bind: leftmost parameter in statement is index 1
429  // Insert index 1 is key, 2 is value
430  int res = sqlite3_bind_blob(stmt, 1, key.data(), key.size(), SQLITE_STATIC);
431  if (res != SQLITE_OK) {
432  LogPrintf("%s: Unable to bind key to statement: %s\n", __func__, sqlite3_errstr(res));
433  sqlite3_clear_bindings(stmt);
434  sqlite3_reset(stmt);
435  return false;
436  }
437  res = sqlite3_bind_blob(stmt, 2, value.data(), value.size(), SQLITE_STATIC);
438  if (res != SQLITE_OK) {
439  LogPrintf("%s: Unable to bind value to statement: %s\n", __func__, sqlite3_errstr(res));
440  sqlite3_clear_bindings(stmt);
441  sqlite3_reset(stmt);
442  return false;
443  }
444 
445  // Execute
446  res = sqlite3_step(stmt);
447  sqlite3_clear_bindings(stmt);
448  sqlite3_reset(stmt);
449  if (res != SQLITE_DONE) {
450  LogPrintf("%s: Unable to execute statement: %s\n", __func__, sqlite3_errstr(res));
451  }
452  return res == SQLITE_DONE;
453 }
454 
456 {
457  if (!m_database.m_db) return false;
458  assert(m_delete_stmt);
459 
460  // Bind: leftmost parameter in statement is index 1
461  int res = sqlite3_bind_blob(m_delete_stmt, 1, key.data(), key.size(), SQLITE_STATIC);
462  if (res != SQLITE_OK) {
463  LogPrintf("%s: Unable to bind statement: %s\n", __func__, sqlite3_errstr(res));
464  sqlite3_clear_bindings(m_delete_stmt);
465  sqlite3_reset(m_delete_stmt);
466  return false;
467  }
468 
469  // Execute
470  res = sqlite3_step(m_delete_stmt);
471  sqlite3_clear_bindings(m_delete_stmt);
472  sqlite3_reset(m_delete_stmt);
473  if (res != SQLITE_DONE) {
474  LogPrintf("%s: Unable to execute statement: %s\n", __func__, sqlite3_errstr(res));
475  }
476  return res == SQLITE_DONE;
477 }
478 
480 {
481  if (!m_database.m_db) return false;
482  assert(m_read_stmt);
483 
484  // Bind: leftmost parameter in statement is index 1
485  bool ret = false;
486  int res = sqlite3_bind_blob(m_read_stmt, 1, key.data(), key.size(), SQLITE_STATIC);
487  if (res == SQLITE_OK) {
488  res = sqlite3_step(m_read_stmt);
489  if (res == SQLITE_ROW) {
490  ret = true;
491  }
492  }
493 
494  sqlite3_clear_bindings(m_read_stmt);
495  sqlite3_reset(m_read_stmt);
496  return ret;
497 }
498 
500 {
501  assert(!m_cursor_init);
502  if (!m_database.m_db) return false;
503  m_cursor_init = true;
504  return true;
505 }
506 
507 bool SQLiteBatch::ReadAtCursor(CDataStream& key, CDataStream& value, bool& complete)
508 {
509  complete = false;
510 
511  if (!m_cursor_init) return false;
512 
513  int res = sqlite3_step(m_cursor_stmt);
514  if (res == SQLITE_DONE) {
515  complete = true;
516  return true;
517  }
518  if (res != SQLITE_ROW) {
519  LogPrintf("SQLiteBatch::ReadAtCursor: Unable to execute cursor step: %s\n", sqlite3_errstr(res));
520  return false;
521  }
522 
523  // Leftmost column in result is index 0
524  const char* key_data = reinterpret_cast<const char*>(sqlite3_column_blob(m_cursor_stmt, 0));
525  int key_data_size = sqlite3_column_bytes(m_cursor_stmt, 0);
526  key.write(key_data, key_data_size);
527  const char* value_data = reinterpret_cast<const char*>(sqlite3_column_blob(m_cursor_stmt, 1));
528  int value_data_size = sqlite3_column_bytes(m_cursor_stmt, 1);
529  value.write(value_data, value_data_size);
530  return true;
531 }
532 
534 {
535  sqlite3_reset(m_cursor_stmt);
536  m_cursor_init = false;
537 }
538 
540 {
541  if (!m_database.m_db || sqlite3_get_autocommit(m_database.m_db) == 0) return false;
542  int res = sqlite3_exec(m_database.m_db, "BEGIN TRANSACTION", nullptr, nullptr, nullptr);
543  if (res != SQLITE_OK) {
544  LogPrintf("SQLiteBatch: Failed to begin the transaction\n");
545  }
546  return res == SQLITE_OK;
547 }
548 
550 {
551  if (!m_database.m_db || sqlite3_get_autocommit(m_database.m_db) != 0) return false;
552  int res = sqlite3_exec(m_database.m_db, "COMMIT TRANSACTION", nullptr, nullptr, nullptr);
553  if (res != SQLITE_OK) {
554  LogPrintf("SQLiteBatch: Failed to commit the transaction\n");
555  }
556  return res == SQLITE_OK;
557 }
558 
560 {
561  if (!m_database.m_db || sqlite3_get_autocommit(m_database.m_db) != 0) return false;
562  int res = sqlite3_exec(m_database.m_db, "ROLLBACK TRANSACTION", nullptr, nullptr, nullptr);
563  if (res != SQLITE_OK) {
564  LogPrintf("SQLiteBatch: Failed to abort the transaction\n");
565  }
566  return res == SQLITE_OK;
567 }
568 
569 bool ExistsSQLiteDatabase(const fs::path& path)
570 {
571  const fs::path file = path / DATABASE_FILENAME;
572  return fs::symlink_status(file).type() == fs::regular_file && IsSQLiteFile(file);
573 }
574 
575 std::unique_ptr<SQLiteDatabase> MakeSQLiteDatabase(const fs::path& path, const DatabaseOptions& options, DatabaseStatus& status, bilingual_str& error)
576 {
577  const fs::path file = path / DATABASE_FILENAME;
578  try {
579  auto db = MakeUnique<SQLiteDatabase>(path, file);
580  if (options.verify && !db->Verify(error)) {
582  return nullptr;
583  }
584  return db;
585  } catch (const std::runtime_error& e) {
587  error.original = e.what();
588  return nullptr;
589  }
590 }
591 
593 {
594  return std::string(sqlite3_libversion());
595 }
596 
597 bool IsSQLiteFile(const fs::path& path)
598 {
599  if (!fs::exists(path)) return false;
600 
601  // A SQLite Database file is at least 512 bytes.
602  boost::system::error_code ec;
603  auto size = fs::file_size(path, ec);
604  if (ec) LogPrintf("%s: %s %s\n", __func__, ec.message(), path.string());
605  if (size < 512) return false;
606 
607  fsbridge::ifstream file(path, std::ios::binary);
608  if (!file.is_open()) return false;
609 
610  // Magic is at beginning and is 16 bytes long
611  char magic[16];
612  file.read(magic, 16);
613 
614  // Application id is at offset 68 and 4 bytes long
615  file.seekg(68, std::ios::beg);
616  char app_id[4];
617  file.read(app_id, 4);
618 
619  file.close();
620 
621  // Check the magic, see https://sqlite.org/fileformat2.html
622  std::string magic_str(magic);
623  if (magic_str != std::string("SQLite format 3")) {
624  return false;
625  }
626 
627  // Check the application id matches our network magic
628  return memcmp(Params().MessageStart(), app_id, 4) == 0;
629 }
sqlite3 * m_db
Definition: sqlite.h:113
bool TxnBegin() override
Definition: sqlite.cpp:539
void CloseCursor() override
Definition: sqlite.cpp:533
bool TxnCommit() override
Definition: sqlite.cpp:549
bool Backup(const std::string &dest) const override
Back up the entire database to a file.
Definition: sqlite.cpp:293
static const char *const DATABASE_FILENAME
Definition: sqlite.cpp:20
bool WriteKey(CDataStream &&key, CDataStream &&value, bool overwrite=true) override
Definition: sqlite.cpp:416
Bilingual messages:
Definition: translation.h:16
bool empty() const
Definition: translation.h:27
#define strprintf
Format arguments and return the string or write to given std::ostream (see tinyformat::format doc for...
Definition: tinyformat.h:1164
bool ExistsSQLiteDatabase(const fs::path &path)
Definition: sqlite.cpp:569
static Mutex g_sqlite_mutex
Definition: sqlite.cpp:23
static int g_sqlite_count GUARDED_BY(g_sqlite_mutex)=0
bilingual_str Untranslated(std::string original)
Mark a bilingual_str as untranslated.
Definition: translation.h:40
bool Rewrite(const char *skip=nullptr) override
Rewrite the entire database on disk.
Definition: sqlite.cpp:286
bool TryCreateDirectories(const fs::path &p)
Ignores exceptions thrown by Boost&#39;s create_directories if the requested directory exists...
Definition: system.cpp:1009
fs::ifstream ifstream
Definition: fs.h:90
static void LogPrintf(const char *fmt, const Args &... args)
Definition: logging.h:166
Double ended buffer combining vector and stream-like interfaces.
Definition: streams.h:202
void write(const char *pch, size_t nSize)
Definition: streams.h:435
void SetupSQLStatements()
Definition: sqlite.cpp:72
void Open() override
Open the database if it is not already opened.
Definition: sqlite.cpp:201
bool EraseKey(CDataStream &&key) override
Definition: sqlite.cpp:455
std::unique_ptr< SQLiteDatabase > MakeSQLiteDatabase(const fs::path &path, const DatabaseOptions &options, DatabaseStatus &status, bilingual_str &error)
Definition: sqlite.cpp:575
sqlite3_stmt * m_read_stmt
Definition: sqlite.h:23
bool IsSQLiteFile(const fs::path &path)
Definition: sqlite.cpp:597
bool verify
Definition: db.h:210
sqlite3_stmt * m_overwrite_stmt
Definition: sqlite.h:25
const bool m_mock
Definition: sqlite.h:58
SQLiteDatabase()=delete
#define LOCK(cs)
Definition: sync.h:230
bilingual_str _(const char *psz)
Translation function.
Definition: translation.h:57
const std::string m_dir_path
Definition: sqlite.h:60
bool HasKey(CDataStream &&key) override
Definition: sqlite.cpp:479
bool ReadAtCursor(CDataStream &key, CDataStream &value, bool &complete) override
Definition: sqlite.cpp:507
void Close() override
Definition: sqlite.cpp:344
static uint32_t ReadBE32(const unsigned char *ptr)
Definition: common.h:63
static constexpr int32_t WALLET_SCHEMA_VERSION
Definition: sqlite.cpp:21
An instance of this class represents one SQLite3 database.
Definition: sqlite.h:55
bool StartCursor() override
Definition: sqlite.cpp:499
SQLiteDatabase & m_database
Definition: sqlite.h:19
static void ErrorLogCallback(void *arg, int code, const char *msg)
Definition: sqlite.cpp:26
std::string SQLiteDatabaseVersion()
Definition: sqlite.cpp:592
const std::string m_file_path
Definition: sqlite.h:62
int flags
Definition: bitcoin-tx.cpp:506
bool Verify(bilingual_str &error)
Definition: sqlite.cpp:120
void Cleanup() noexcept
Definition: sqlite.cpp:107
std::string original
Definition: translation.h:17
sqlite3_stmt * m_cursor_stmt
Definition: sqlite.h:27
const CChainParams & Params()
Return the currently selected parameters.
SQLiteBatch(SQLiteDatabase &database)
Definition: sqlite.cpp:335
sqlite3_stmt * m_delete_stmt
Definition: sqlite.h:26
bool TxnAbort() override
Definition: sqlite.cpp:559
void Close() override
Close the database.
Definition: sqlite.cpp:320
bool m_cursor_init
Definition: sqlite.h:21
bool ReadKey(CDataStream &&key, CDataStream &value) override
Definition: sqlite.cpp:383
DatabaseStatus
Definition: db.h:213
std::unique_ptr< DatabaseBatch > MakeBatch(bool flush_on_close=true) override
Make a SQLiteBatch connected to this database.
Definition: sqlite.cpp:329
bool error(const char *fmt, const Args &... args)
Definition: system.h:52
sqlite3_stmt * m_insert_stmt
Definition: sqlite.h:24
An instance of this class represents one database.
Definition: db.h:104