5#include <bitcoin-build-config.h>
9#include <chainparams.h>
31 return {
reinterpret_cast<const std::byte*
>(sqlite3_column_blob(stmt, col)),
32 static_cast<size_t>(sqlite3_column_bytes(stmt, col))};
43 LogPrintf(
"SQLite Error. Code: %d. Message: %s\n", code,
msg);
49 if (code == SQLITE_TRACE_STMT) {
50 auto* stmt =
static_cast<sqlite3_stmt*
>(param1);
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);
64 const std::string& description)
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 LogPrintf(
"Unable to bind %s to statement: %s\n", description, sqlite3_errstr(res));
73 sqlite3_clear_bindings(stmt);
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)));
91 ret = sqlite3_step(pragma_read_stmt);
92 if (
ret != SQLITE_ROW) {
93 sqlite3_finalize(pragma_read_stmt);
97 int result = sqlite3_column_int(pragma_read_stmt, 0);
98 sqlite3_finalize(pragma_read_stmt);
102static void SetPragma(sqlite3* db,
const std::string& key,
const std::string& value,
const std::string& err_msg)
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)));
112int SQLiteDatabase::g_sqlite_count = 0;
122 if (++g_sqlite_count == 1) {
125 if (
ret != SQLITE_OK) {
126 throw std::runtime_error(
strprintf(
"SQLiteDatabase: Failed to setup error log: %s\n", sqlite3_errstr(
ret)));
129 ret = sqlite3_config(SQLITE_CONFIG_SERIALIZED);
130 if (
ret != SQLITE_OK) {
131 throw std::runtime_error(
strprintf(
"SQLiteDatabase: Failed to configure serialized threading mode: %s\n", sqlite3_errstr(
ret)));
134 int ret = sqlite3_initialize();
135 if (
ret != SQLITE_OK) {
136 throw std::runtime_error(
strprintf(
"SQLiteDatabase: Failed to initialize SQLite: %s\n", sqlite3_errstr(
ret)));
142 }
catch (
const std::runtime_error&) {
151 const std::vector<std::pair<sqlite3_stmt**, const char*>> statements{
152 {&
m_read_stmt,
"SELECT value FROM main WHERE key = ?"},
159 for (
const auto& [stmt_prepared, stmt_text] : statements) {
160 if (*stmt_prepared ==
nullptr) {
161 int res = sqlite3_prepare_v2(
m_database.
m_db, stmt_text, -1, stmt_prepared,
nullptr);
162 if (res != SQLITE_OK) {
164 "SQLiteDatabase: Failed to setup SQL statements: %s\n", sqlite3_errstr(res)));
182 if (--g_sqlite_count == 0) {
183 int ret = sqlite3_shutdown();
184 if (
ret != SQLITE_OK) {
185 LogPrintf(
"SQLiteDatabase: Failed to shutdown SQLite: %s\n", sqlite3_errstr(
ret));
196 if (!read_result.has_value())
return false;
197 uint32_t app_id =
static_cast<uint32_t
>(read_result.value());
199 if (app_id != net_magic) {
200 error =
strprintf(
_(
"SQLiteDatabase: Unexpected application id. Expected %u, got %u"), net_magic, app_id);
206 if (!read_result.has_value())
return false;
207 int32_t user_ver = read_result.value();
213 sqlite3_stmt* stmt{
nullptr};
214 int ret = sqlite3_prepare_v2(
m_db,
"PRAGMA integrity_check", -1, &stmt,
nullptr);
215 if (
ret != SQLITE_OK) {
216 sqlite3_finalize(stmt);
217 error =
strprintf(
_(
"SQLiteDatabase: Failed to prepare statement to verify database: %s"), sqlite3_errstr(
ret));
221 ret = sqlite3_step(stmt);
222 if (
ret == SQLITE_DONE) {
225 if (
ret != SQLITE_ROW) {
226 error =
strprintf(
_(
"SQLiteDatabase: Failed to execute statement to verify database: %s"), sqlite3_errstr(
ret));
229 const char*
msg = (
const char*)sqlite3_column_text(stmt, 0);
231 error =
strprintf(
_(
"SQLiteDatabase: Failed to read database verification error: %s"), sqlite3_errstr(
ret));
234 std::string str_msg(
msg);
235 if (str_msg ==
"ok") {
243 sqlite3_finalize(stmt);
244 return error.
empty();
249 int flags = SQLITE_OPEN_FULLMUTEX | SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE;
251 flags |= SQLITE_OPEN_MEMORY;
254 if (
m_db ==
nullptr) {
259 if (
ret != SQLITE_OK) {
260 throw std::runtime_error(
strprintf(
"SQLiteDatabase: Failed to open database: %s\n", sqlite3_errstr(
ret)));
262 ret = sqlite3_extended_result_codes(
m_db, 1);
263 if (
ret != SQLITE_OK) {
264 throw std::runtime_error(
strprintf(
"SQLiteDatabase: Failed to enable extended result codes: %s\n", sqlite3_errstr(
ret)));
269 if (
ret != SQLITE_OK) {
275 if (sqlite3_db_readonly(
m_db,
"main") != 0) {
276 throw std::runtime_error(
"SQLiteDatabase: Database opened in readonly mode but read-write permissions are needed");
281 SetPragma(
m_db,
"locking_mode",
"exclusive",
"Unable to change database locking mode to exclusive");
283 int ret = sqlite3_exec(
m_db,
"BEGIN EXCLUSIVE TRANSACTION",
nullptr,
nullptr,
nullptr);
284 if (
ret != SQLITE_OK) {
285 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");
287 ret = sqlite3_exec(
m_db,
"COMMIT",
nullptr,
nullptr,
nullptr);
288 if (
ret != SQLITE_OK) {
289 throw std::runtime_error(
strprintf(
"SQLiteDatabase: Unable to end exclusive lock transaction: %s\n", sqlite3_errstr(
ret)));
293 SetPragma(
m_db,
"fullfsync",
"true",
"Failed to enable fullfsync");
297 LogPrintf(
"WARNING SQLite is configured to not wait for data to be flushed to disk. Data loss and corruption may occur.\n");
298 SetPragma(
m_db,
"synchronous",
"OFF",
"Failed to set synchronous mode to OFF");
303 sqlite3_stmt* check_main_stmt{
nullptr};
304 ret = sqlite3_prepare_v2(
m_db,
"SELECT name FROM sqlite_master WHERE type='table' AND name='main'", -1, &check_main_stmt,
nullptr);
305 if (
ret != SQLITE_OK) {
306 throw std::runtime_error(
strprintf(
"SQLiteDatabase: Failed to prepare statement to check table existence: %s\n", sqlite3_errstr(
ret)));
308 ret = sqlite3_step(check_main_stmt);
309 if (sqlite3_finalize(check_main_stmt) != SQLITE_OK) {
310 throw std::runtime_error(
strprintf(
"SQLiteDatabase: Failed to finalize statement checking table existence: %s\n", sqlite3_errstr(
ret)));
313 if (
ret == SQLITE_DONE) {
314 table_exists =
false;
315 }
else if (
ret == SQLITE_ROW) {
318 throw std::runtime_error(
strprintf(
"SQLiteDatabase: Failed to execute statement to check table existence: %s\n", sqlite3_errstr(
ret)));
323 ret = sqlite3_exec(
m_db,
"CREATE TABLE main(key BLOB PRIMARY KEY NOT NULL, value BLOB NOT NULL)",
nullptr,
nullptr,
nullptr);
324 if (
ret != SQLITE_OK) {
325 throw std::runtime_error(
strprintf(
"SQLiteDatabase: Failed to create new database: %s\n", sqlite3_errstr(
ret)));
331 "Failed to set the application id");
335 "Failed to set the wallet schema version");
342 int ret = sqlite3_exec(
m_db,
"VACUUM",
nullptr,
nullptr,
nullptr);
343 return ret == SQLITE_OK;
349 int res = sqlite3_open(dest.c_str(), &db_copy);
350 if (res != SQLITE_OK) {
351 sqlite3_close(db_copy);
354 sqlite3_backup* backup = sqlite3_backup_init(db_copy,
"main",
m_db,
"main");
356 LogPrintf(
"%s: Unable to begin backup: %s\n", __func__, sqlite3_errmsg(
m_db));
357 sqlite3_close(db_copy);
361 res = sqlite3_backup_step(backup, -1);
362 if (res != SQLITE_DONE) {
363 LogPrintf(
"%s: Unable to backup: %s\n", __func__, sqlite3_errstr(res));
364 sqlite3_backup_finish(backup);
365 sqlite3_close(db_copy);
368 res = sqlite3_backup_finish(backup);
369 sqlite3_close(db_copy);
370 return res == SQLITE_OK;
375 int res = sqlite3_close(
m_db);
376 if (res != SQLITE_OK) {
377 throw std::runtime_error(
strprintf(
"SQLiteDatabase: Failed to close database: %s\n", sqlite3_errstr(res)));
385 return m_db && sqlite3_get_autocommit(
m_db) == 0;
390 return sqlite3_exec(database.
m_db, statement.data(),
nullptr,
nullptr,
nullptr);
396 return std::make_unique<SQLiteBatch>(*
this);
400 : m_database(database)
410 bool force_conn_refresh =
false;
415 LogPrintf(
"SQLiteBatch: Batch closed unexpectedly without the transaction being explicitly committed or aborted\n");
420 force_conn_refresh =
true;
421 LogPrintf(
"SQLiteBatch: Batch closed and failed to abort transaction, resetting db connection..\n");
426 const std::vector<std::pair<sqlite3_stmt**, const char*>> statements{
434 for (
const auto& [stmt_prepared, stmt_description] : statements) {
435 int res = sqlite3_finalize(*stmt_prepared);
436 if (res != SQLITE_OK) {
437 LogPrintf(
"SQLiteBatch: Batch closed but could not finalize %s statement: %s\n",
438 stmt_description, sqlite3_errstr(res));
440 *stmt_prepared =
nullptr;
443 if (force_conn_refresh) {
449 }
catch (
const std::runtime_error&) {
465 if (res != SQLITE_ROW) {
466 if (res != SQLITE_DONE) {
468 LogPrintf(
"%s: Unable to execute statement: %s\n", __func__, sqlite3_errstr(res));
504 int res = sqlite3_step(stmt);
505 sqlite3_clear_bindings(stmt);
507 if (res != SQLITE_DONE) {
508 LogPrintf(
"%s: Unable to execute statement: %s\n", __func__, sqlite3_errstr(res));
513 return res == SQLITE_DONE;
528 int res = sqlite3_step(stmt);
529 sqlite3_clear_bindings(stmt);
531 if (res != SQLITE_DONE) {
532 LogPrintf(
"%s: Unable to execute statement: %s\n", __func__, sqlite3_errstr(res));
537 return res == SQLITE_DONE;
560 return res == SQLITE_ROW;
566 if (res == SQLITE_DONE) {
569 if (res != SQLITE_ROW) {
570 LogPrintf(
"%s: Unable to execute cursor step: %s\n", __func__, sqlite3_errstr(res));
588 if (res != SQLITE_OK) {
589 LogPrintf(
"%s: cursor closed but could not finalize cursor statement: %s\n",
590 __func__, sqlite3_errstr(res));
597 auto cursor = std::make_unique<SQLiteCursor>();
599 const char* stmt_text =
"SELECT key, value FROM main";
600 int res = sqlite3_prepare_v2(
m_database.
m_db, stmt_text, -1, &cursor->m_cursor_stmt,
nullptr);
601 if (res != SQLITE_OK) {
603 "%s: Failed to setup cursor SQL statement: %s\n", __func__, sqlite3_errstr(res)));
616 std::vector<std::byte> start_range(
prefix.begin(),
prefix.end());
617 std::vector<std::byte> end_range(
prefix.begin(),
prefix.end());
618 auto it = end_range.rbegin();
619 for (; it != end_range.rend(); ++it) {
620 if (*it == std::byte(std::numeric_limits<unsigned char>::max())) {
624 *it = std::byte(std::to_integer<unsigned char>(*it) + 1);
627 if (it == end_range.rend()) {
632 auto cursor = std::make_unique<SQLiteCursor>(start_range, end_range);
633 if (!cursor)
return nullptr;
635 const char* stmt_text = end_range.empty() ?
"SELECT key, value FROM main WHERE key >= ?" :
636 "SELECT key, value FROM main WHERE key >= ? AND key < ?";
637 int res = sqlite3_prepare_v2(
m_database.
m_db, stmt_text, -1, &cursor->m_cursor_stmt,
nullptr);
638 if (res != SQLITE_OK) {
640 "SQLiteDatabase: Failed to setup cursor SQL statement: %s\n", sqlite3_errstr(res)));
643 if (!
BindBlobToStatement(cursor->m_cursor_stmt, 1, cursor->m_prefix_range_start,
"prefix_start"))
return nullptr;
644 if (!end_range.empty()) {
645 if (!
BindBlobToStatement(cursor->m_cursor_stmt, 2, cursor->m_prefix_range_end,
"prefix_end"))
return nullptr;
657 if (res != SQLITE_OK) {
658 LogPrintf(
"SQLiteBatch: Failed to begin the transaction\n");
663 return res == SQLITE_OK;
671 if (res != SQLITE_OK) {
672 LogPrintf(
"SQLiteBatch: Failed to commit the transaction\n");
677 return res == SQLITE_OK;
685 if (res != SQLITE_OK) {
686 LogPrintf(
"SQLiteBatch: Failed to abort the transaction\n");
691 return res == SQLITE_OK;
698 auto db = std::make_unique<SQLiteDatabase>(data_file.parent_path(), data_file, options);
699 if (options.
verify && !db->Verify(error)) {
705 }
catch (
const std::runtime_error& e) {
714 return std::string(sqlite3_libversion());
const CChainParams & Params()
Return the currently selected parameters.
#define Assert(val)
Identity function.
Double ended buffer combining vector and stream-like interfaces.
void write(Span< const value_type > src)
constexpr std::size_t size() const noexcept
constexpr C * data() const noexcept
Path class wrapper to block calls to the fs::path(std::string) implicit constructor and the fs::path:...
bool ReadKey(DataStream &&key, DataStream &value) override
bool TxnCommit() override
SQLiteBatch(SQLiteDatabase &database)
std::unique_ptr< SQliteExecHandler > m_exec_handler
bool HasKey(DataStream &&key) override
bool m_txn
Whether this batch has started a database transaction and whether it owns SQLiteDatabase::m_write_sem...
std::unique_ptr< DatabaseCursor > GetNewCursor() override
bool ErasePrefix(Span< const std::byte > prefix) override
bool EraseKey(DataStream &&key) override
sqlite3_stmt * m_delete_stmt
sqlite3_stmt * m_read_stmt
sqlite3_stmt * m_overwrite_stmt
bool ExecStatement(sqlite3_stmt *stmt, Span< const std::byte > blob)
std::unique_ptr< DatabaseCursor > GetNewPrefixCursor(Span< const std::byte > prefix) override
sqlite3_stmt * m_delete_prefix_stmt
void SetupSQLStatements()
sqlite3_stmt * m_insert_stmt
SQLiteDatabase & m_database
bool WriteKey(DataStream &&key, DataStream &&value, bool overwrite=true) override
Status Next(DataStream &key, DataStream &value) override
sqlite3_stmt * m_cursor_stmt
An instance of this class represents one SQLite3 database.
static Mutex g_sqlite_mutex
This mutex protects SQLite initialization and shutdown.
CSemaphore m_write_semaphore
void Open() override
Open the database if it is not already opened.
std::string Filename() override
Return path to main database file for logs and error messages.
bool Rewrite(const char *skip=nullptr) override
Rewrite the entire database on disk.
void Cleanup() noexcept EXCLUSIVE_LOCKS_REQUIRED(!g_sqlite_mutex)
void Close() override
Close the database.
bool Backup(const std::string &dest) const override
Back up the entire database to a file.
const std::string m_dir_path
const std::string m_file_path
std::unique_ptr< DatabaseBatch > MakeBatch(bool flush_on_close=true) override
Make a SQLiteBatch connected to this database.
bool Verify(bilingual_str &error)
bool HasActiveTxn()
Return true if there is an on-going txn in this connection.
virtual int Exec(SQLiteDatabase &database, const std::string &statement)
An instance of this class represents one database.
static uint32_t ReadBE32(const unsigned char *ptr)
bool TryCreateDirectories(const fs::path &p)
Ignores exceptions thrown by create_directories if the requested directory exists.
static bool LogAcceptCategory(BCLog::LogFlags category, BCLog::Level level)
Return true if log accepts specified category, at the specified level.
#define LogTrace(category,...)
Filesystem operations and types.
static std::string PathToString(const path &path)
Convert path object to a byte string.
static path PathFromString(const std::string &string)
Convert byte string to path object.
static Span< const std::byte > SpanFromBlob(sqlite3_stmt *stmt, int col)
std::unique_ptr< SQLiteDatabase > MakeSQLiteDatabase(const fs::path &path, const DatabaseOptions &options, DatabaseStatus &status, bilingual_str &error)
fs::path SQLiteDataFile(const fs::path &path)
static std::optional< int > ReadPragmaInteger(sqlite3 *db, const std::string &key, const std::string &description, bilingual_str &error)
static constexpr int32_t WALLET_SCHEMA_VERSION
static int TraceSqlCallback(unsigned code, void *context, void *param1, void *param2)
static bool BindBlobToStatement(sqlite3_stmt *stmt, int index, Span< const std::byte > blob, const std::string &description)
static void SetPragma(sqlite3 *db, const std::string &key, const std::string &value, const std::string &err_msg)
std::string SQLiteDatabaseVersion()
static void ErrorLogCallback(void *arg, int code, const char *msg)
bool verify
Check data integrity on load.
#define AssertLockNotHeld(cs)
bilingual_str _(ConstevalStringLiteral str)
Translation function.
bilingual_str Untranslated(std::string original)
Mark a bilingual_str as untranslated.