diff options
Diffstat (limited to 'src/wallet/sqlite.cpp')
-rw-r--r-- | src/wallet/sqlite.cpp | 217 |
1 files changed, 104 insertions, 113 deletions
diff --git a/src/wallet/sqlite.cpp b/src/wallet/sqlite.cpp index 0fb3b1d3c4..2b2181e70b 100644 --- a/src/wallet/sqlite.cpp +++ b/src/wallet/sqlite.cpp @@ -1,4 +1,4 @@ -// Copyright (c) 2020 The Bitcoin Core developers +// Copyright (c) 2020-2021 The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. @@ -8,7 +8,6 @@ #include <crypto/common.h> #include <logging.h> #include <sync.h> -#include <util/memory.h> #include <util/strencodings.h> #include <util/system.h> #include <util/translation.h> @@ -17,6 +16,11 @@ #include <sqlite3.h> #include <stdint.h> +#include <optional> +#include <utility> +#include <vector> + +namespace wallet { static constexpr int32_t WALLET_SCHEMA_VERSION = 0; static Mutex g_sqlite_mutex; @@ -33,8 +37,38 @@ static void ErrorLogCallback(void* arg, int code, const char* msg) LogPrintf("SQLite Error. Code: %d. Message: %s\n", code, msg); } +static std::optional<int> ReadPragmaInteger(sqlite3* db, const std::string& key, const std::string& description, bilingual_str& error) +{ + std::string stmt_text = strprintf("PRAGMA %s", key); + sqlite3_stmt* pragma_read_stmt{nullptr}; + int ret = sqlite3_prepare_v2(db, stmt_text.c_str(), -1, &pragma_read_stmt, nullptr); + if (ret != SQLITE_OK) { + sqlite3_finalize(pragma_read_stmt); + error = Untranslated(strprintf("SQLiteDatabase: Failed to prepare the statement to fetch %s: %s", description, sqlite3_errstr(ret))); + return std::nullopt; + } + ret = sqlite3_step(pragma_read_stmt); + if (ret != SQLITE_ROW) { + sqlite3_finalize(pragma_read_stmt); + error = Untranslated(strprintf("SQLiteDatabase: Failed to fetch %s: %s", description, sqlite3_errstr(ret))); + return std::nullopt; + } + int result = sqlite3_column_int(pragma_read_stmt, 0); + sqlite3_finalize(pragma_read_stmt); + return result; +} + +static void SetPragma(sqlite3* db, const std::string& key, const std::string& value, const std::string& err_msg) +{ + std::string stmt_text = strprintf("PRAGMA %s = %s", key, value); + int ret = sqlite3_exec(db, stmt_text.c_str(), nullptr, nullptr, nullptr); + if (ret != SQLITE_OK) { + throw std::runtime_error(strprintf("SQLiteDatabase: %s: %s\n", err_msg, sqlite3_errstr(ret))); + } +} + SQLiteDatabase::SQLiteDatabase(const fs::path& dir_path, const fs::path& file_path, bool mock) - : WalletDatabase(), m_mock(mock), m_dir_path(dir_path.string()), m_file_path(file_path.string()) + : WalletDatabase(), m_mock(mock), m_dir_path(fs::PathToString(dir_path)), m_file_path(fs::PathToString(file_path)) { { LOCK(g_sqlite_mutex); @@ -70,30 +104,21 @@ SQLiteDatabase::SQLiteDatabase(const fs::path& dir_path, const fs::path& file_pa void SQLiteBatch::SetupSQLStatements() { - int res; - if (!m_read_stmt) { - if ((res = sqlite3_prepare_v2(m_database.m_db, "SELECT value FROM main WHERE key = ?", -1, &m_read_stmt, nullptr)) != SQLITE_OK) { - throw std::runtime_error(strprintf("SQLiteDatabase: Failed to setup SQL statements: %s\n", sqlite3_errstr(res))); - } - } - if (!m_insert_stmt) { - if ((res = sqlite3_prepare_v2(m_database.m_db, "INSERT INTO main VALUES(?, ?)", -1, &m_insert_stmt, nullptr)) != SQLITE_OK) { - throw std::runtime_error(strprintf("SQLiteDatabase: Failed to setup SQL statements: %s\n", sqlite3_errstr(res))); - } - } - if (!m_overwrite_stmt) { - if ((res = sqlite3_prepare_v2(m_database.m_db, "INSERT or REPLACE into main values(?, ?)", -1, &m_overwrite_stmt, nullptr)) != SQLITE_OK) { - throw std::runtime_error(strprintf("SQLiteDatabase: Failed to setup SQL statements: %s\n", sqlite3_errstr(res))); - } - } - if (!m_delete_stmt) { - if ((res = sqlite3_prepare_v2(m_database.m_db, "DELETE FROM main WHERE key = ?", -1, &m_delete_stmt, nullptr)) != SQLITE_OK) { - throw std::runtime_error(strprintf("SQLiteDatabase: Failed to setup SQL statements: %s\n", sqlite3_errstr(res))); - } - } - if (!m_cursor_stmt) { - if ((res = sqlite3_prepare_v2(m_database.m_db, "SELECT key, value FROM main", -1, &m_cursor_stmt, nullptr)) != SQLITE_OK) { - throw std::runtime_error(strprintf("SQLiteDatabase: Failed to setup SQL statements : %s\n", sqlite3_errstr(res))); + const std::vector<std::pair<sqlite3_stmt**, const char*>> statements{ + {&m_read_stmt, "SELECT value FROM main WHERE key = ?"}, + {&m_insert_stmt, "INSERT INTO main VALUES(?, ?)"}, + {&m_overwrite_stmt, "INSERT or REPLACE into main values(?, ?)"}, + {&m_delete_stmt, "DELETE FROM main WHERE key = ?"}, + {&m_cursor_stmt, "SELECT key, value FROM main"}, + }; + + for (const auto& [stmt_prepared, stmt_text] : statements) { + if (*stmt_prepared == nullptr) { + int res = sqlite3_prepare_v2(m_database.m_db, stmt_text, -1, stmt_prepared, nullptr); + if (res != SQLITE_OK) { + throw std::runtime_error(strprintf( + "SQLiteDatabase: Failed to setup SQL statements: %s\n", sqlite3_errstr(res))); + } } } } @@ -121,21 +146,9 @@ bool SQLiteDatabase::Verify(bilingual_str& error) assert(m_db); // Check the application ID matches our network magic - sqlite3_stmt* app_id_stmt{nullptr}; - int ret = sqlite3_prepare_v2(m_db, "PRAGMA application_id", -1, &app_id_stmt, nullptr); - if (ret != SQLITE_OK) { - sqlite3_finalize(app_id_stmt); - error = strprintf(_("SQLiteDatabase: Failed to prepare the statement to fetch the application id: %s"), sqlite3_errstr(ret)); - return false; - } - ret = sqlite3_step(app_id_stmt); - if (ret != SQLITE_ROW) { - sqlite3_finalize(app_id_stmt); - error = strprintf(_("SQLiteDatabase: Failed to fetch the application id: %s"), sqlite3_errstr(ret)); - return false; - } - uint32_t app_id = static_cast<uint32_t>(sqlite3_column_int(app_id_stmt, 0)); - sqlite3_finalize(app_id_stmt); + auto read_result = ReadPragmaInteger(m_db, "application_id", "the application id", error); + if (!read_result.has_value()) return false; + uint32_t app_id = static_cast<uint32_t>(read_result.value()); uint32_t net_magic = ReadBE32(Params().MessageStart()); if (app_id != net_magic) { error = strprintf(_("SQLiteDatabase: Unexpected application id. Expected %u, got %u"), net_magic, app_id); @@ -143,28 +156,16 @@ bool SQLiteDatabase::Verify(bilingual_str& error) } // Check our schema version - sqlite3_stmt* user_ver_stmt{nullptr}; - ret = sqlite3_prepare_v2(m_db, "PRAGMA user_version", -1, &user_ver_stmt, nullptr); - if (ret != SQLITE_OK) { - sqlite3_finalize(user_ver_stmt); - error = strprintf(_("SQLiteDatabase: Failed to prepare the statement to fetch sqlite wallet schema version: %s"), sqlite3_errstr(ret)); - return false; - } - ret = sqlite3_step(user_ver_stmt); - if (ret != SQLITE_ROW) { - sqlite3_finalize(user_ver_stmt); - error = strprintf(_("SQLiteDatabase: Failed to fetch sqlite wallet schema version: %s"), sqlite3_errstr(ret)); - return false; - } - int32_t user_ver = sqlite3_column_int(user_ver_stmt, 0); - sqlite3_finalize(user_ver_stmt); + read_result = ReadPragmaInteger(m_db, "user_version", "sqlite wallet schema version", error); + if (!read_result.has_value()) return false; + int32_t user_ver = read_result.value(); if (user_ver != WALLET_SCHEMA_VERSION) { error = strprintf(_("SQLiteDatabase: Unknown sqlite wallet schema version %d. Only version %d is supported"), user_ver, WALLET_SCHEMA_VERSION); return false; } sqlite3_stmt* stmt{nullptr}; - ret = sqlite3_prepare_v2(m_db, "PRAGMA integrity_check", -1, &stmt, nullptr); + int ret = sqlite3_prepare_v2(m_db, "PRAGMA integrity_check", -1, &stmt, nullptr); if (ret != SQLITE_OK) { sqlite3_finalize(stmt); error = strprintf(_("SQLiteDatabase: Failed to prepare statement to verify database: %s"), sqlite3_errstr(ret)); @@ -206,12 +207,16 @@ void SQLiteDatabase::Open() if (m_db == nullptr) { if (!m_mock) { - TryCreateDirectories(m_dir_path); + TryCreateDirectories(fs::PathFromString(m_dir_path)); } int ret = sqlite3_open_v2(m_file_path.c_str(), &m_db, flags, nullptr); if (ret != SQLITE_OK) { throw std::runtime_error(strprintf("SQLiteDatabase: Failed to open database: %s\n", sqlite3_errstr(ret))); } + ret = sqlite3_extended_result_codes(m_db, 1); + if (ret != SQLITE_OK) { + throw std::runtime_error(strprintf("SQLiteDatabase: Failed to enable extended result codes: %s\n", sqlite3_errstr(ret))); + } } if (sqlite3_db_readonly(m_db, "main") != 0) { @@ -220,14 +225,11 @@ void SQLiteDatabase::Open() // Acquire an exclusive lock on the database // First change the locking mode to exclusive - int ret = sqlite3_exec(m_db, "PRAGMA locking_mode = exclusive", nullptr, nullptr, nullptr); - if (ret != SQLITE_OK) { - throw std::runtime_error(strprintf("SQLiteDatabase: Unable to change database locking mode to exclusive: %s\n", sqlite3_errstr(ret))); - } + SetPragma(m_db, "locking_mode", "exclusive", "Unable to change database locking mode to exclusive"); // Now begin a transaction to acquire the exclusive lock. This lock won't be released until we close because of the exclusive locking mode. - ret = sqlite3_exec(m_db, "BEGIN EXCLUSIVE TRANSACTION", nullptr, nullptr, nullptr); + int ret = sqlite3_exec(m_db, "BEGIN EXCLUSIVE TRANSACTION", nullptr, nullptr, nullptr); if (ret != SQLITE_OK) { - throw std::runtime_error("SQLiteDatabase: Unable to obtain an exclusive lock on the database, is it being used by another bitcoind?\n"); + 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"); } ret = sqlite3_exec(m_db, "COMMIT", nullptr, nullptr, nullptr); if (ret != SQLITE_OK) { @@ -235,9 +237,12 @@ void SQLiteDatabase::Open() } // Enable fullfsync for the platforms that use it - ret = sqlite3_exec(m_db, "PRAGMA fullfsync = true", nullptr, nullptr, nullptr); - if (ret != SQLITE_OK) { - throw std::runtime_error(strprintf("SQLiteDatabase: Failed to enable fullfsync: %s\n", sqlite3_errstr(ret))); + SetPragma(m_db, "fullfsync", "true", "Failed to enable fullfsync"); + + if (gArgs.GetBoolArg("-unsafesqlitesync", false)) { + // Use normal synchronous mode for the journal + LogPrintf("WARNING SQLite is configured to not wait for data to be flushed to disk. Data loss and corruption may occur.\n"); + SetPragma(m_db, "synchronous", "OFF", "Failed to set synchronous mode to OFF"); } // Make the table for our key-value pairs @@ -269,18 +274,12 @@ void SQLiteDatabase::Open() // Set the application id uint32_t app_id = ReadBE32(Params().MessageStart()); - std::string set_app_id = strprintf("PRAGMA application_id = %d", static_cast<int32_t>(app_id)); - ret = sqlite3_exec(m_db, set_app_id.c_str(), nullptr, nullptr, nullptr); - if (ret != SQLITE_OK) { - throw std::runtime_error(strprintf("SQLiteDatabase: Failed to set the application id: %s\n", sqlite3_errstr(ret))); - } + SetPragma(m_db, "application_id", strprintf("%d", static_cast<int32_t>(app_id)), + "Failed to set the application id"); // Set the user version - std::string set_user_ver = strprintf("PRAGMA user_version = %d", WALLET_SCHEMA_VERSION); - ret = sqlite3_exec(m_db, set_user_ver.c_str(), nullptr, nullptr, nullptr); - if (ret != SQLITE_OK) { - throw std::runtime_error(strprintf("SQLiteDatabase: Failed to set the wallet schema version: %s\n", sqlite3_errstr(ret))); - } + SetPragma(m_db, "user_version", strprintf("%d", WALLET_SCHEMA_VERSION), + "Failed to set the wallet schema version"); } } @@ -330,7 +329,7 @@ void SQLiteDatabase::Close() std::unique_ptr<DatabaseBatch> SQLiteDatabase::MakeBatch(bool flush_on_close) { // We ignore flush_on_close because we don't do manual flushing for SQLite - return MakeUnique<SQLiteBatch>(*this); + return std::make_unique<SQLiteBatch>(*this); } SQLiteBatch::SQLiteBatch(SQLiteDatabase& database) @@ -354,31 +353,22 @@ void SQLiteBatch::Close() } // Free all of the prepared statements - int ret = sqlite3_finalize(m_read_stmt); - if (ret != SQLITE_OK) { - LogPrintf("SQLiteBatch: Batch closed but could not finalize read statement: %s\n", sqlite3_errstr(ret)); - } - ret = sqlite3_finalize(m_insert_stmt); - if (ret != SQLITE_OK) { - LogPrintf("SQLiteBatch: Batch closed but could not finalize insert statement: %s\n", sqlite3_errstr(ret)); - } - ret = sqlite3_finalize(m_overwrite_stmt); - if (ret != SQLITE_OK) { - LogPrintf("SQLiteBatch: Batch closed but could not finalize overwrite statement: %s\n", sqlite3_errstr(ret)); - } - ret = sqlite3_finalize(m_delete_stmt); - if (ret != SQLITE_OK) { - LogPrintf("SQLiteBatch: Batch closed but could not finalize delete statement: %s\n", sqlite3_errstr(ret)); - } - ret = sqlite3_finalize(m_cursor_stmt); - if (ret != SQLITE_OK) { - LogPrintf("SQLiteBatch: Batch closed but could not finalize cursor statement: %s\n", sqlite3_errstr(ret)); + const std::vector<std::pair<sqlite3_stmt**, const char*>> statements{ + {&m_read_stmt, "read"}, + {&m_insert_stmt, "insert"}, + {&m_overwrite_stmt, "overwrite"}, + {&m_delete_stmt, "delete"}, + {&m_cursor_stmt, "cursor"}, + }; + + for (const auto& [stmt_prepared, stmt_description] : statements) { + int res = sqlite3_finalize(*stmt_prepared); + if (res != SQLITE_OK) { + LogPrintf("SQLiteBatch: Batch closed but could not finalize %s statement: %s\n", + stmt_description, sqlite3_errstr(res)); + } + *stmt_prepared = nullptr; } - m_read_stmt = nullptr; - m_insert_stmt = nullptr; - m_overwrite_stmt = nullptr; - m_delete_stmt = nullptr; - m_cursor_stmt = nullptr; } bool SQLiteBatch::ReadKey(CDataStream&& key, CDataStream& value) @@ -405,9 +395,9 @@ bool SQLiteBatch::ReadKey(CDataStream&& key, CDataStream& value) return false; } // Leftmost column in result is index 0 - const char* data = reinterpret_cast<const char*>(sqlite3_column_blob(m_read_stmt, 0)); - int data_size = sqlite3_column_bytes(m_read_stmt, 0); - value.write(data, data_size); + const std::byte* data{BytePtr(sqlite3_column_blob(m_read_stmt, 0))}; + size_t data_size(sqlite3_column_bytes(m_read_stmt, 0)); + value.write({data, data_size}); sqlite3_clear_bindings(m_read_stmt); sqlite3_reset(m_read_stmt); @@ -522,12 +512,12 @@ bool SQLiteBatch::ReadAtCursor(CDataStream& key, CDataStream& value, bool& compl } // Leftmost column in result is index 0 - const char* key_data = reinterpret_cast<const char*>(sqlite3_column_blob(m_cursor_stmt, 0)); - int key_data_size = sqlite3_column_bytes(m_cursor_stmt, 0); - key.write(key_data, key_data_size); - const char* value_data = reinterpret_cast<const char*>(sqlite3_column_blob(m_cursor_stmt, 1)); - int value_data_size = sqlite3_column_bytes(m_cursor_stmt, 1); - value.write(value_data, value_data_size); + const std::byte* key_data{BytePtr(sqlite3_column_blob(m_cursor_stmt, 0))}; + size_t key_data_size(sqlite3_column_bytes(m_cursor_stmt, 0)); + key.write({key_data, key_data_size}); + const std::byte* value_data{BytePtr(sqlite3_column_blob(m_cursor_stmt, 1))}; + size_t value_data_size(sqlite3_column_bytes(m_cursor_stmt, 1)); + value.write({value_data, value_data_size}); return true; } @@ -571,7 +561,7 @@ std::unique_ptr<SQLiteDatabase> MakeSQLiteDatabase(const fs::path& path, const D { try { fs::path data_file = SQLiteDataFile(path); - auto db = MakeUnique<SQLiteDatabase>(data_file.parent_path(), data_file); + auto db = std::make_unique<SQLiteDatabase>(data_file.parent_path(), data_file); if (options.verify && !db->Verify(error)) { status = DatabaseStatus::FAILED_VERIFY; return nullptr; @@ -589,3 +579,4 @@ std::string SQLiteDatabaseVersion() { return std::string(sqlite3_libversion()); } +} // namespace wallet |