Skip to content

Commit 0878128

Browse files
committed
Merge bitcoin/bitcoin#21962: wallet: refactor: dedup sqlite PRAGMA access
9938d61 wallet: refactor: dedup sqlite PRAGMA assignments (Sebastian Falbesoner) dca8ef5 wallet: refactor: dedup sqlite PRAGMA integer reads (Sebastian Falbesoner) Pull request description: This refactoring PR deduplicates repeated SQLite access to PRAGMA settings. Two functions `ReadPragmaInteger(...)` (reads a single integer value via statement `PRAGMA key`) and `SetPragma(...)` (sets a key to specified value via statement `PRAGMA key = value`) are introduced for this purpose. This should be more readable and less error-prone, e.g. in case other PRAGMA settings need to be read/set in the future or the error handling has to be adapted. ACKs for top commit: achow101: Code Review ACK 9938d61 laanwj: Looks good to me now, code review ACK 9938d61 Tree-SHA512: 5332788ead6d8d652e28cb0cef1bf0be2b22d6744f8d02dd9e04a4a68e32e14d4a21f94d9b940c37a0d815be3f0091d956c9f6e269b0a6819b62b40482d3bbd2
2 parents 4da26fb + 9938d61 commit 0878128

File tree

1 file changed

+46
-54
lines changed

1 file changed

+46
-54
lines changed

src/wallet/sqlite.cpp

Lines changed: 46 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include <sqlite3.h>
1717
#include <stdint.h>
1818

19+
#include <optional>
1920
#include <utility>
2021
#include <vector>
2122

@@ -35,6 +36,36 @@ static void ErrorLogCallback(void* arg, int code, const char* msg)
3536
LogPrintf("SQLite Error. Code: %d. Message: %s\n", code, msg);
3637
}
3738

39+
static std::optional<int> ReadPragmaInteger(sqlite3* db, const std::string& key, const std::string& description, bilingual_str& error)
40+
{
41+
std::string stmt_text = strprintf("PRAGMA %s", key);
42+
sqlite3_stmt* pragma_read_stmt{nullptr};
43+
int ret = sqlite3_prepare_v2(db, stmt_text.c_str(), -1, &pragma_read_stmt, nullptr);
44+
if (ret != SQLITE_OK) {
45+
sqlite3_finalize(pragma_read_stmt);
46+
error = Untranslated(strprintf("SQLiteDatabase: Failed to prepare the statement to fetch %s: %s", description, sqlite3_errstr(ret)));
47+
return std::nullopt;
48+
}
49+
ret = sqlite3_step(pragma_read_stmt);
50+
if (ret != SQLITE_ROW) {
51+
sqlite3_finalize(pragma_read_stmt);
52+
error = Untranslated(strprintf("SQLiteDatabase: Failed to fetch %s: %s", description, sqlite3_errstr(ret)));
53+
return std::nullopt;
54+
}
55+
int result = sqlite3_column_int(pragma_read_stmt, 0);
56+
sqlite3_finalize(pragma_read_stmt);
57+
return result;
58+
}
59+
60+
static void SetPragma(sqlite3* db, const std::string& key, const std::string& value, const std::string& err_msg)
61+
{
62+
std::string stmt_text = strprintf("PRAGMA %s = %s", key, value);
63+
int ret = sqlite3_exec(db, stmt_text.c_str(), nullptr, nullptr, nullptr);
64+
if (ret != SQLITE_OK) {
65+
throw std::runtime_error(strprintf("SQLiteDatabase: %s: %s\n", err_msg, sqlite3_errstr(ret)));
66+
}
67+
}
68+
3869
SQLiteDatabase::SQLiteDatabase(const fs::path& dir_path, const fs::path& file_path, bool mock)
3970
: WalletDatabase(), m_mock(mock), m_dir_path(dir_path.string()), m_file_path(file_path.string())
4071
{
@@ -114,50 +145,26 @@ bool SQLiteDatabase::Verify(bilingual_str& error)
114145
assert(m_db);
115146

116147
// Check the application ID matches our network magic
117-
sqlite3_stmt* app_id_stmt{nullptr};
118-
int ret = sqlite3_prepare_v2(m_db, "PRAGMA application_id", -1, &app_id_stmt, nullptr);
119-
if (ret != SQLITE_OK) {
120-
sqlite3_finalize(app_id_stmt);
121-
error = strprintf(_("SQLiteDatabase: Failed to prepare the statement to fetch the application id: %s"), sqlite3_errstr(ret));
122-
return false;
123-
}
124-
ret = sqlite3_step(app_id_stmt);
125-
if (ret != SQLITE_ROW) {
126-
sqlite3_finalize(app_id_stmt);
127-
error = strprintf(_("SQLiteDatabase: Failed to fetch the application id: %s"), sqlite3_errstr(ret));
128-
return false;
129-
}
130-
uint32_t app_id = static_cast<uint32_t>(sqlite3_column_int(app_id_stmt, 0));
131-
sqlite3_finalize(app_id_stmt);
148+
auto read_result = ReadPragmaInteger(m_db, "application_id", "the application id", error);
149+
if (!read_result.has_value()) return false;
150+
uint32_t app_id = static_cast<uint32_t>(read_result.value());
132151
uint32_t net_magic = ReadBE32(Params().MessageStart());
133152
if (app_id != net_magic) {
134153
error = strprintf(_("SQLiteDatabase: Unexpected application id. Expected %u, got %u"), net_magic, app_id);
135154
return false;
136155
}
137156

138157
// Check our schema version
139-
sqlite3_stmt* user_ver_stmt{nullptr};
140-
ret = sqlite3_prepare_v2(m_db, "PRAGMA user_version", -1, &user_ver_stmt, nullptr);
141-
if (ret != SQLITE_OK) {
142-
sqlite3_finalize(user_ver_stmt);
143-
error = strprintf(_("SQLiteDatabase: Failed to prepare the statement to fetch sqlite wallet schema version: %s"), sqlite3_errstr(ret));
144-
return false;
145-
}
146-
ret = sqlite3_step(user_ver_stmt);
147-
if (ret != SQLITE_ROW) {
148-
sqlite3_finalize(user_ver_stmt);
149-
error = strprintf(_("SQLiteDatabase: Failed to fetch sqlite wallet schema version: %s"), sqlite3_errstr(ret));
150-
return false;
151-
}
152-
int32_t user_ver = sqlite3_column_int(user_ver_stmt, 0);
153-
sqlite3_finalize(user_ver_stmt);
158+
read_result = ReadPragmaInteger(m_db, "user_version", "sqlite wallet schema version", error);
159+
if (!read_result.has_value()) return false;
160+
int32_t user_ver = read_result.value();
154161
if (user_ver != WALLET_SCHEMA_VERSION) {
155162
error = strprintf(_("SQLiteDatabase: Unknown sqlite wallet schema version %d. Only version %d is supported"), user_ver, WALLET_SCHEMA_VERSION);
156163
return false;
157164
}
158165

159166
sqlite3_stmt* stmt{nullptr};
160-
ret = sqlite3_prepare_v2(m_db, "PRAGMA integrity_check", -1, &stmt, nullptr);
167+
int ret = sqlite3_prepare_v2(m_db, "PRAGMA integrity_check", -1, &stmt, nullptr);
161168
if (ret != SQLITE_OK) {
162169
sqlite3_finalize(stmt);
163170
error = strprintf(_("SQLiteDatabase: Failed to prepare statement to verify database: %s"), sqlite3_errstr(ret));
@@ -213,12 +220,9 @@ void SQLiteDatabase::Open()
213220

214221
// Acquire an exclusive lock on the database
215222
// First change the locking mode to exclusive
216-
int ret = sqlite3_exec(m_db, "PRAGMA locking_mode = exclusive", nullptr, nullptr, nullptr);
217-
if (ret != SQLITE_OK) {
218-
throw std::runtime_error(strprintf("SQLiteDatabase: Unable to change database locking mode to exclusive: %s\n", sqlite3_errstr(ret)));
219-
}
223+
SetPragma(m_db, "locking_mode", "exclusive", "Unable to change database locking mode to exclusive");
220224
// Now begin a transaction to acquire the exclusive lock. This lock won't be released until we close because of the exclusive locking mode.
221-
ret = sqlite3_exec(m_db, "BEGIN EXCLUSIVE TRANSACTION", nullptr, nullptr, nullptr);
225+
int ret = sqlite3_exec(m_db, "BEGIN EXCLUSIVE TRANSACTION", nullptr, nullptr, nullptr);
222226
if (ret != SQLITE_OK) {
223227
throw std::runtime_error("SQLiteDatabase: Unable to obtain an exclusive lock on the database, is it being used by another bitcoind?\n");
224228
}
@@ -228,18 +232,12 @@ void SQLiteDatabase::Open()
228232
}
229233

230234
// Enable fullfsync for the platforms that use it
231-
ret = sqlite3_exec(m_db, "PRAGMA fullfsync = true", nullptr, nullptr, nullptr);
232-
if (ret != SQLITE_OK) {
233-
throw std::runtime_error(strprintf("SQLiteDatabase: Failed to enable fullfsync: %s\n", sqlite3_errstr(ret)));
234-
}
235+
SetPragma(m_db, "fullfsync", "true", "Failed to enable fullfsync");
235236

236237
if (gArgs.GetBoolArg("-unsafesqlitesync", false)) {
237238
// Use normal synchronous mode for the journal
238239
LogPrintf("WARNING SQLite is configured to not wait for data to be flushed to disk. Data loss and corruption may occur.\n");
239-
ret = sqlite3_exec(m_db, "PRAGMA synchronous = OFF", nullptr, nullptr, nullptr);
240-
if (ret != SQLITE_OK) {
241-
throw std::runtime_error(strprintf("SQLiteDatabase: Failed to set synchronous mode to OFF: %s\n", sqlite3_errstr(ret)));
242-
}
240+
SetPragma(m_db, "synchronous", "OFF", "Failed to set synchronous mode to OFF");
243241
}
244242

245243
// Make the table for our key-value pairs
@@ -271,18 +269,12 @@ void SQLiteDatabase::Open()
271269

272270
// Set the application id
273271
uint32_t app_id = ReadBE32(Params().MessageStart());
274-
std::string set_app_id = strprintf("PRAGMA application_id = %d", static_cast<int32_t>(app_id));
275-
ret = sqlite3_exec(m_db, set_app_id.c_str(), nullptr, nullptr, nullptr);
276-
if (ret != SQLITE_OK) {
277-
throw std::runtime_error(strprintf("SQLiteDatabase: Failed to set the application id: %s\n", sqlite3_errstr(ret)));
278-
}
272+
SetPragma(m_db, "application_id", strprintf("%d", static_cast<int32_t>(app_id)),
273+
"Failed to set the application id");
279274

280275
// Set the user version
281-
std::string set_user_ver = strprintf("PRAGMA user_version = %d", WALLET_SCHEMA_VERSION);
282-
ret = sqlite3_exec(m_db, set_user_ver.c_str(), nullptr, nullptr, nullptr);
283-
if (ret != SQLITE_OK) {
284-
throw std::runtime_error(strprintf("SQLiteDatabase: Failed to set the wallet schema version: %s\n", sqlite3_errstr(ret)));
285-
}
276+
SetPragma(m_db, "user_version", strprintf("%d", WALLET_SCHEMA_VERSION),
277+
"Failed to set the wallet schema version");
286278
}
287279
}
288280

0 commit comments

Comments
 (0)