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