0
0
Fork 0
mirror of https://github.com/bitcoin/bitcoin.git synced 2025-02-08 10:31:50 -05:00

wallet: refactor: dedup sqlite PRAGMA integer reads

This commit is contained in:
Sebastian Falbesoner 2021-05-15 18:45:20 +02:00
parent 1ef34ee25e
commit dca8ef586c

View file

@ -16,6 +16,7 @@
#include <sqlite3.h>
#include <stdint.h>
#include <optional>
#include <utility>
#include <vector>
@ -35,6 +36,27 @@ 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;
}
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())
{
@ -114,21 +136,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);
@ -136,28 +146,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));