Bitcoin Core Fuzz Coverage Report

Coverage Report

Created: 2026-06-01 16:00

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/home/zip/work/bitcoin/src/wallet/sqlite.cpp
Line
Count
Source
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 <sync.h>
12
#include <util/check.h>
13
#include <util/fs_helpers.h>
14
#include <util/log.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
26
namespace wallet {
27
static constexpr int32_t WALLET_SCHEMA_VERSION = 0;
28
29
static std::span<const std::byte> SpanFromBlob(sqlite3_stmt* stmt, int col)
30
0
{
31
0
    return {reinterpret_cast<const std::byte*>(sqlite3_column_blob(stmt, col)),
32
0
            static_cast<size_t>(sqlite3_column_bytes(stmt, col))};
33
0
}
34
35
static void ErrorLogCallback(void* arg, int code, const char* msg)
36
0
{
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
0
    assert(arg == nullptr);
43
0
    LogWarning("SQLite Error. Code: %d. Message: %s", code, msg);
Line
Count
Source
126
0
#define LogWarning(...) detail_LogWithSrcLoc(BCLog::LogFlags::ALL, util::log::Level::Warning, __VA_ARGS__)
Line
Count
Source
119
0
#define detail_LogWithSrcLoc(category, level, ...) util::log::LogPrintFormatInternal(SourceLocation{__func__}, category, level, __VA_ARGS__)
44
0
}
45
46
static int TraceSqlCallback(unsigned code, void* context, void* param1, void* param2)
47
0
{
48
0
    auto* db = static_cast<SQLiteDatabase*>(context);
49
0
    if (code == SQLITE_TRACE_STMT) {
50
0
        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
0
        char* expanded{sqlite3_stmt_readonly(stmt) ? sqlite3_expanded_sql(stmt) : nullptr};
55
0
        LogTrace(BCLog::WALLETDB, "[%s] SQLite Statement: %s\n", db->Filename(), expanded ? expanded : sqlite3_sql(stmt));
Line
Count
Source
144
0
#define LogTrace(category, ...) detail_LogIfCategoryAndLevelEnabled(category, util::log::ShouldTraceLog, util::log::Level::Trace, __VA_ARGS__)
Line
Count
Source
136
0
    do {                                                                                      \
137
0
        if (shouldlog(category)) {                                                            \
138
0
            detail_LogWithSrcLoc((category), (level), util::log::NO_RATE_LIMIT, __VA_ARGS__); \
Line
Count
Source
119
0
#define detail_LogWithSrcLoc(category, level, ...) util::log::LogPrintFormatInternal(SourceLocation{__func__}, category, level, __VA_ARGS__)
139
0
        }                                                                                     \
140
0
    } while (0)
56
0
        if (expanded) sqlite3_free(expanded);
57
0
    }
58
0
    return SQLITE_OK;
59
0
}
60
61
static bool BindBlobToStatement(sqlite3_stmt* stmt,
62
                                int index,
63
                                std::span<const std::byte> blob,
64
                                const std::string& description)
65
0
{
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
0
    int res = sqlite3_bind_blob(stmt, index, blob.data() ? static_cast<const void*>(blob.data()) : "", blob.size(), SQLITE_STATIC);
71
0
    if (res != SQLITE_OK) {
72
0
        LogWarning("Unable to bind %s to statement: %s", description, sqlite3_errstr(res));
Line
Count
Source
126
0
#define LogWarning(...) detail_LogWithSrcLoc(BCLog::LogFlags::ALL, util::log::Level::Warning, __VA_ARGS__)
Line
Count
Source
119
0
#define detail_LogWithSrcLoc(category, level, ...) util::log::LogPrintFormatInternal(SourceLocation{__func__}, category, level, __VA_ARGS__)
73
0
        sqlite3_clear_bindings(stmt);
74
0
        sqlite3_reset(stmt);
75
0
        return false;
76
0
    }
77
78
0
    return true;
79
0
}
80
81
static std::optional<int> ReadPragmaInteger(sqlite3* db, const std::string& key, const std::string& description, bilingual_str& error)
82
0
{
83
0
    std::string stmt_text = strprintf("PRAGMA %s", key);
Line
Count
Source
1172
0
#define strprintf tfm::format
84
0
    sqlite3_stmt* pragma_read_stmt{nullptr};
85
0
    int ret = sqlite3_prepare_v2(db, stmt_text.c_str(), -1, &pragma_read_stmt, nullptr);
86
0
    if (ret != SQLITE_OK) {
87
0
        sqlite3_finalize(pragma_read_stmt);
88
0
        error = Untranslated(strprintf("SQLiteDatabase: Failed to prepare the statement to fetch %s: %s", description, sqlite3_errstr(ret)));
Line
Count
Source
1172
0
#define strprintf tfm::format
89
0
        return std::nullopt;
90
0
    }
91
0
    ret = sqlite3_step(pragma_read_stmt);
92
0
    if (ret != SQLITE_ROW) {
93
0
        sqlite3_finalize(pragma_read_stmt);
94
0
        error = Untranslated(strprintf("SQLiteDatabase: Failed to fetch %s: %s", description, sqlite3_errstr(ret)));
Line
Count
Source
1172
0
#define strprintf tfm::format
95
0
        return std::nullopt;
96
0
    }
97
0
    int result = sqlite3_column_int(pragma_read_stmt, 0);
98
0
    sqlite3_finalize(pragma_read_stmt);
99
0
    return result;
100
0
}
101
102
static void SetPragma(sqlite3* db, const std::string& key, const std::string& value, const std::string& err_msg)
103
0
{
104
0
    std::string stmt_text = strprintf("PRAGMA %s = %s", key, value);
Line
Count
Source
1172
0
#define strprintf tfm::format
105
0
    int ret = sqlite3_exec(db, stmt_text.c_str(), nullptr, nullptr, nullptr);
106
0
    if (ret != SQLITE_OK) {
107
0
        throw std::runtime_error(strprintf("SQLiteDatabase: %s: %s\n", err_msg, sqlite3_errstr(ret)));
Line
Count
Source
1172
0
#define strprintf tfm::format
108
0
    }
109
0
}
110
111
Mutex SQLiteDatabase::g_sqlite_mutex;
112
int SQLiteDatabase::g_sqlite_count = 0;
113
114
SQLiteDatabase::SQLiteDatabase(const fs::path& dir_path, const fs::path& file_path, const DatabaseOptions& options)
115
0
    : SQLiteDatabase(dir_path, file_path, options, /*additional_flags=*/0)
116
0
{}
117
118
SQLiteDatabase::SQLiteDatabase(const fs::path& dir_path, const fs::path& file_path, const DatabaseOptions& options, int additional_flags)
119
0
    : 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
0
{
121
0
    {
122
0
        LOCK(g_sqlite_mutex);
Line
Count
Source
268
0
#define LOCK(cs) UniqueLock UNIQUE_NAME(criticalblock)(MaybeCheckNotHeld(cs), #cs, __FILE__, __LINE__)
Line
Count
Source
11
0
#define UNIQUE_NAME(name) PASTE2(name, __COUNTER__)
Line
Count
Source
9
0
#define PASTE2(x, y) PASTE(x, y)
Line
Count
Source
8
0
#define PASTE(x, y) x ## y
123
0
        if (++g_sqlite_count == 1) {
124
            // Setup logging
125
0
            int ret = sqlite3_config(SQLITE_CONFIG_LOG, ErrorLogCallback, nullptr);
126
0
            if (ret != SQLITE_OK) {
127
0
                throw std::runtime_error(strprintf("SQLiteDatabase: Failed to setup error log: %s\n", sqlite3_errstr(ret)));
Line
Count
Source
1172
0
#define strprintf tfm::format
128
0
            }
129
            // Force serialized threading mode
130
0
            ret = sqlite3_config(SQLITE_CONFIG_SERIALIZED);
131
0
            if (ret != SQLITE_OK) {
132
0
                throw std::runtime_error(strprintf("SQLiteDatabase: Failed to configure serialized threading mode: %s\n", sqlite3_errstr(ret)));
Line
Count
Source
1172
0
#define strprintf tfm::format
133
0
            }
134
0
        }
135
0
        int ret = sqlite3_initialize(); // This is a no-op if sqlite3 is already initialized
136
0
        if (ret != SQLITE_OK) {
137
0
            throw std::runtime_error(strprintf("SQLiteDatabase: Failed to initialize SQLite: %s\n", sqlite3_errstr(ret)));
Line
Count
Source
1172
0
#define strprintf tfm::format
138
0
        }
139
0
    }
140
141
0
    try {
142
0
        Open(additional_flags);
143
0
    } catch (const std::runtime_error&) {
144
        // If open fails, cleanup this object and rethrow the exception
145
0
        Cleanup();
146
0
        throw;
147
0
    }
148
0
}
149
150
void SQLiteBatch::SetupSQLStatements()
151
0
{
152
0
    const std::vector<std::pair<sqlite3_stmt**, const char*>> statements{
153
0
        {&m_read_stmt, "SELECT value FROM main WHERE key = ?"},
154
0
        {&m_insert_stmt, "INSERT INTO main VALUES(?, ?)"},
155
0
        {&m_overwrite_stmt, "INSERT or REPLACE into main values(?, ?)"},
156
0
        {&m_delete_stmt, "DELETE FROM main WHERE key = ?"},
157
0
        {&m_delete_prefix_stmt, "DELETE FROM main WHERE instr(key, ?) = 1"},
158
0
    };
159
160
0
    for (const auto& [stmt_prepared, stmt_text] : statements) {
161
0
        if (*stmt_prepared == nullptr) {
162
0
            int res = sqlite3_prepare_v2(m_database.m_db, stmt_text, -1, stmt_prepared, nullptr);
163
0
            if (res != SQLITE_OK) {
164
0
                throw std::runtime_error(strprintf(
Line
Count
Source
1172
0
#define strprintf tfm::format
165
0
                    "SQLiteDatabase: Failed to setup SQL statements: %s\n", sqlite3_errstr(res)));
166
0
            }
167
0
        }
168
0
    }
169
0
}
170
171
SQLiteDatabase::~SQLiteDatabase()
172
0
{
173
0
    Cleanup();
174
0
}
175
176
void SQLiteDatabase::Cleanup() noexcept
177
0
{
178
0
    AssertLockNotHeld(g_sqlite_mutex);
Line
Count
Source
149
0
#define AssertLockNotHeld(cs) AssertLockNotHeldInline(#cs, __FILE__, __LINE__, &cs)
179
180
0
    Close();
181
182
0
    LOCK(g_sqlite_mutex);
Line
Count
Source
268
0
#define LOCK(cs) UniqueLock UNIQUE_NAME(criticalblock)(MaybeCheckNotHeld(cs), #cs, __FILE__, __LINE__)
Line
Count
Source
11
0
#define UNIQUE_NAME(name) PASTE2(name, __COUNTER__)
Line
Count
Source
9
0
#define PASTE2(x, y) PASTE(x, y)
Line
Count
Source
8
0
#define PASTE(x, y) x ## y
183
0
    if (--g_sqlite_count == 0) {
184
0
        int ret = sqlite3_shutdown();
185
0
        if (ret != SQLITE_OK) {
186
0
            LogWarning("SQLiteDatabase: Failed to shutdown SQLite: %s", sqlite3_errstr(ret));
Line
Count
Source
126
0
#define LogWarning(...) detail_LogWithSrcLoc(BCLog::LogFlags::ALL, util::log::Level::Warning, __VA_ARGS__)
Line
Count
Source
119
0
#define detail_LogWithSrcLoc(category, level, ...) util::log::LogPrintFormatInternal(SourceLocation{__func__}, category, level, __VA_ARGS__)
187
0
        }
188
0
    }
189
0
}
190
191
bool SQLiteDatabase::Verify(bilingual_str& error)
192
0
{
193
0
    assert(m_db);
194
195
    // Check the application ID matches our network magic
196
0
    auto read_result = ReadPragmaInteger(m_db, "application_id", "the application id", error);
197
0
    if (!read_result.has_value()) return false;
198
0
    uint32_t app_id = static_cast<uint32_t>(read_result.value());
199
0
    uint32_t net_magic = ReadBE32(Params().MessageStart().data());
200
0
    if (app_id != net_magic) {
201
0
        error = strprintf(_("SQLiteDatabase: Unexpected application id. Expected %u, got %u"), net_magic, app_id);
Line
Count
Source
1172
0
#define strprintf tfm::format
202
0
        return false;
203
0
    }
204
205
    // Check our schema version
206
0
    read_result = ReadPragmaInteger(m_db, "user_version", "sqlite wallet schema version", error);
207
0
    if (!read_result.has_value()) return false;
208
0
    int32_t user_ver = read_result.value();
209
0
    if (user_ver != WALLET_SCHEMA_VERSION) {
210
0
        error = strprintf(_("SQLiteDatabase: Unknown sqlite wallet schema version %d. Only version %d is supported"), user_ver, WALLET_SCHEMA_VERSION);
Line
Count
Source
1172
0
#define strprintf tfm::format
211
0
        return false;
212
0
    }
213
214
0
    sqlite3_stmt* stmt{nullptr};
215
0
    int ret = sqlite3_prepare_v2(m_db, "PRAGMA integrity_check", -1, &stmt, nullptr);
216
0
    if (ret != SQLITE_OK) {
217
0
        sqlite3_finalize(stmt);
218
0
        error = strprintf(_("SQLiteDatabase: Failed to prepare statement to verify database: %s"), sqlite3_errstr(ret));
Line
Count
Source
1172
0
#define strprintf tfm::format
219
0
        return false;
220
0
    }
221
0
    while (true) {
222
0
        ret = sqlite3_step(stmt);
223
0
        if (ret == SQLITE_DONE) {
224
0
            break;
225
0
        }
226
0
        if (ret != SQLITE_ROW) {
227
0
            error = strprintf(_("SQLiteDatabase: Failed to execute statement to verify database: %s"), sqlite3_errstr(ret));
Line
Count
Source
1172
0
#define strprintf tfm::format
228
0
            break;
229
0
        }
230
0
        const char* msg = (const char*)sqlite3_column_text(stmt, 0);
231
0
        if (!msg) {
232
0
            error = strprintf(_("SQLiteDatabase: Failed to read database verification error: %s"), sqlite3_errstr(ret));
Line
Count
Source
1172
0
#define strprintf tfm::format
233
0
            break;
234
0
        }
235
0
        std::string str_msg(msg);
236
0
        if (str_msg == "ok") {
237
0
            continue;
238
0
        }
239
0
        if (error.empty()) {
240
0
            error = _("Failed to verify database") + Untranslated("\n");
241
0
        }
242
0
        error += Untranslated(strprintf("%s\n", str_msg));
Line
Count
Source
1172
0
#define strprintf tfm::format
243
0
    }
244
0
    sqlite3_finalize(stmt);
245
0
    return error.empty();
246
0
}
247
248
void SQLiteDatabase::Open()
249
0
{
250
0
    Open(/*additional_flags*/0);
251
0
}
252
253
void SQLiteDatabase::Open(int additional_flags)
254
0
{
255
0
    int flags = SQLITE_OPEN_FULLMUTEX | SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE | additional_flags;
256
257
0
    if (m_db == nullptr) {
258
0
        if (!(flags & SQLITE_OPEN_MEMORY)) {
259
0
            TryCreateDirectories(m_dir_path);
260
0
            if (!IsDirWritable(m_dir_path)) {
261
0
                throw std::runtime_error(strprintf("SQLiteDatabase: Failed to open database in directory '%s': directory is not writable", fs::PathToString(m_dir_path)));
Line
Count
Source
1172
0
#define strprintf tfm::format
262
0
            }
263
0
        }
264
265
0
        int ret = sqlite3_open_v2(m_file_path.c_str(), &m_db, flags, nullptr);
266
0
        if (ret != SQLITE_OK) {
267
0
            throw std::runtime_error(strprintf("SQLiteDatabase: Failed to open database: %s\n", sqlite3_errstr(ret)));
Line
Count
Source
1172
0
#define strprintf tfm::format
268
0
        }
269
0
        ret = sqlite3_extended_result_codes(m_db, 1);
270
0
        if (ret != SQLITE_OK) {
271
0
            throw std::runtime_error(strprintf("SQLiteDatabase: Failed to enable extended result codes: %s\n", sqlite3_errstr(ret)));
Line
Count
Source
1172
0
#define strprintf tfm::format
272
0
        }
273
        // Trace SQL statements if tracing is enabled with -debug=walletdb -loglevel=walletdb:trace
274
0
        if (util::log::ShouldTraceLog(BCLog::WALLETDB)) {
275
0
           ret = sqlite3_trace_v2(m_db, SQLITE_TRACE_STMT, TraceSqlCallback, this);
276
0
           if (ret != SQLITE_OK) {
277
0
               LogWarning("Failed to enable SQL tracing for %s", Filename());
Line
Count
Source
126
0
#define LogWarning(...) detail_LogWithSrcLoc(BCLog::LogFlags::ALL, util::log::Level::Warning, __VA_ARGS__)
Line
Count
Source
119
0
#define detail_LogWithSrcLoc(category, level, ...) util::log::LogPrintFormatInternal(SourceLocation{__func__}, category, level, __VA_ARGS__)
278
0
           }
279
0
        }
280
0
    }
281
282
0
    if (sqlite3_db_readonly(m_db, "main") != 0) {
283
0
        throw std::runtime_error("SQLiteDatabase: Database opened in readonly mode but read-write permissions are needed");
284
0
    }
285
286
    // Acquire an exclusive lock on the database
287
    // First change the locking mode to exclusive
288
0
    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
0
    int ret = sqlite3_exec(m_db, "BEGIN EXCLUSIVE TRANSACTION", nullptr, nullptr, nullptr);
291
0
    if (ret != SQLITE_OK) {
292
0
        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
0
    }
294
0
    ret = sqlite3_exec(m_db, "COMMIT", nullptr, nullptr, nullptr);
295
0
    if (ret != SQLITE_OK) {
296
0
        throw std::runtime_error(strprintf("SQLiteDatabase: Unable to end exclusive lock transaction: %s\n", sqlite3_errstr(ret)));
Line
Count
Source
1172
0
#define strprintf tfm::format
297
0
    }
298
299
    // Enable fullfsync for the platforms that use it
300
0
    SetPragma(m_db, "fullfsync", "true", "Failed to enable fullfsync");
301
302
0
    if (m_use_unsafe_sync) {
303
        // Use normal synchronous mode for the journal
304
0
        LogWarning("SQLite is configured to not wait for data to be flushed to disk. Data loss and corruption may occur.");
Line
Count
Source
126
0
#define LogWarning(...) detail_LogWithSrcLoc(BCLog::LogFlags::ALL, util::log::Level::Warning, __VA_ARGS__)
Line
Count
Source
119
0
#define detail_LogWithSrcLoc(category, level, ...) util::log::LogPrintFormatInternal(SourceLocation{__func__}, category, level, __VA_ARGS__)
305
0
        SetPragma(m_db, "synchronous", "OFF", "Failed to set synchronous mode to OFF");
306
0
    }
307
308
    // Make the table for our key-value pairs
309
    // First check that the main table exists
310
0
    sqlite3_stmt* check_main_stmt{nullptr};
311
0
    ret = sqlite3_prepare_v2(m_db, "SELECT name FROM sqlite_master WHERE type='table' AND name='main'", -1, &check_main_stmt, nullptr);
312
0
    if (ret != SQLITE_OK) {
313
0
        throw std::runtime_error(strprintf("SQLiteDatabase: Failed to prepare statement to check table existence: %s\n", sqlite3_errstr(ret)));
Line
Count
Source
1172
0
#define strprintf tfm::format
314
0
    }
315
0
    ret = sqlite3_step(check_main_stmt);
316
0
    if (sqlite3_finalize(check_main_stmt) != SQLITE_OK) {
317
0
        throw std::runtime_error(strprintf("SQLiteDatabase: Failed to finalize statement checking table existence: %s\n", sqlite3_errstr(ret)));
Line
Count
Source
1172
0
#define strprintf tfm::format
318
0
    }
319
0
    bool table_exists;
320
0
    if (ret == SQLITE_DONE) {
321
0
        table_exists = false;
322
0
    } else if (ret == SQLITE_ROW) {
323
0
        table_exists = true;
324
0
    } else {
325
0
        throw std::runtime_error(strprintf("SQLiteDatabase: Failed to execute statement to check table existence: %s\n", sqlite3_errstr(ret)));
Line
Count
Source
1172
0
#define strprintf tfm::format
326
0
    }
327
328
    // Do the db setup things because the table doesn't exist only when we are creating a new wallet
329
0
    if (!table_exists) {
330
0
        ret = sqlite3_exec(m_db, "CREATE TABLE main(key BLOB PRIMARY KEY NOT NULL, value BLOB NOT NULL)", nullptr, nullptr, nullptr);
331
0
        if (ret != SQLITE_OK) {
332
0
            throw std::runtime_error(strprintf("SQLiteDatabase: Failed to create new database: %s\n", sqlite3_errstr(ret)));
Line
Count
Source
1172
0
#define strprintf tfm::format
333
0
        }
334
335
        // Set the application id
336
0
        uint32_t app_id = ReadBE32(Params().MessageStart().data());
337
0
        SetPragma(m_db, "application_id", strprintf("%d", static_cast<int32_t>(app_id)),
Line
Count
Source
1172
0
#define strprintf tfm::format
338
0
                  "Failed to set the application id");
339
340
        // Set the user version
341
0
        SetPragma(m_db, "user_version", strprintf("%d", WALLET_SCHEMA_VERSION),
Line
Count
Source
1172
0
#define strprintf tfm::format
342
0
                  "Failed to set the wallet schema version");
343
0
    }
344
0
}
345
346
bool SQLiteDatabase::Rewrite()
347
0
{
348
    // Rewrite the database using the VACUUM command: https://sqlite.org/lang_vacuum.html
349
0
    int ret = sqlite3_exec(m_db, "VACUUM", nullptr, nullptr, nullptr);
350
0
    return ret == SQLITE_OK;
351
0
}
352
353
bool SQLiteDatabase::Backup(const std::string& dest) const
354
0
{
355
0
    sqlite3* db_copy;
356
0
    int res = sqlite3_open(dest.c_str(), &db_copy);
357
0
    if (res != SQLITE_OK) {
358
0
        sqlite3_close(db_copy);
359
0
        return false;
360
0
    }
361
0
    sqlite3_backup* backup = sqlite3_backup_init(db_copy, "main", m_db, "main");
362
0
    if (!backup) {
363
0
        LogWarning("Unable to begin sqlite backup: %s", sqlite3_errmsg(m_db));
Line
Count
Source
126
0
#define LogWarning(...) detail_LogWithSrcLoc(BCLog::LogFlags::ALL, util::log::Level::Warning, __VA_ARGS__)
Line
Count
Source
119
0
#define detail_LogWithSrcLoc(category, level, ...) util::log::LogPrintFormatInternal(SourceLocation{__func__}, category, level, __VA_ARGS__)
364
0
        sqlite3_close(db_copy);
365
0
        return false;
366
0
    }
367
    // Specifying -1 will copy all of the pages
368
0
    res = sqlite3_backup_step(backup, -1);
369
0
    if (res != SQLITE_DONE) {
370
0
        LogWarning("Unable to continue sqlite backup: %s", sqlite3_errstr(res));
Line
Count
Source
126
0
#define LogWarning(...) detail_LogWithSrcLoc(BCLog::LogFlags::ALL, util::log::Level::Warning, __VA_ARGS__)
Line
Count
Source
119
0
#define detail_LogWithSrcLoc(category, level, ...) util::log::LogPrintFormatInternal(SourceLocation{__func__}, category, level, __VA_ARGS__)
371
0
        sqlite3_backup_finish(backup);
372
0
        sqlite3_close(db_copy);
373
0
        return false;
374
0
    }
375
0
    res = sqlite3_backup_finish(backup);
376
0
    sqlite3_close(db_copy);
377
0
    return res == SQLITE_OK;
378
0
}
379
380
void SQLiteDatabase::Close()
381
0
{
382
0
    int res = sqlite3_close(m_db);
383
0
    if (res != SQLITE_OK) {
384
0
        throw std::runtime_error(strprintf("SQLiteDatabase: Failed to close database: %s\n", sqlite3_errstr(res)));
Line
Count
Source
1172
0
#define strprintf tfm::format
385
0
    }
386
0
    m_db = nullptr;
387
0
}
388
389
bool SQLiteDatabase::HasActiveTxn()
390
0
{
391
    // 'sqlite3_get_autocommit' returns true by default, and false if a transaction has begun and not been committed or rolled back.
392
0
    return m_db && sqlite3_get_autocommit(m_db) == 0;
393
0
}
394
395
int SQliteExecHandler::Exec(SQLiteDatabase& database, const std::string& statement)
396
0
{
397
0
    return sqlite3_exec(database.m_db, statement.data(), nullptr, nullptr, nullptr);
398
0
}
399
400
std::unique_ptr<DatabaseBatch> SQLiteDatabase::MakeBatch()
401
0
{
402
    // We ignore flush_on_close because we don't do manual flushing for SQLite
403
0
    return std::make_unique<SQLiteBatch>(*this);
404
0
}
405
406
SQLiteBatch::SQLiteBatch(SQLiteDatabase& database)
407
0
    : m_database(database)
408
0
{
409
    // Make sure we have a db handle
410
0
    assert(m_database.m_db);
411
412
0
    SetupSQLStatements();
413
0
}
414
415
void SQLiteBatch::Close()
416
0
{
417
0
    bool force_conn_refresh = false;
418
419
    // If we began a transaction, and it wasn't committed, abort the transaction in progress
420
0
    if (m_txn) {
421
0
        if (TxnAbort()) {
422
0
            LogWarning("SQLiteBatch: Batch closed unexpectedly without the transaction being explicitly committed or aborted");
Line
Count
Source
126
0
#define LogWarning(...) detail_LogWithSrcLoc(BCLog::LogFlags::ALL, util::log::Level::Warning, __VA_ARGS__)
Line
Count
Source
119
0
#define detail_LogWithSrcLoc(category, level, ...) util::log::LogPrintFormatInternal(SourceLocation{__func__}, category, level, __VA_ARGS__)
423
0
        } 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
0
            force_conn_refresh = true;
428
0
            LogWarning("SQLiteBatch: Batch closed and failed to abort transaction, resetting db connection..");
Line
Count
Source
126
0
#define LogWarning(...) detail_LogWithSrcLoc(BCLog::LogFlags::ALL, util::log::Level::Warning, __VA_ARGS__)
Line
Count
Source
119
0
#define detail_LogWithSrcLoc(category, level, ...) util::log::LogPrintFormatInternal(SourceLocation{__func__}, category, level, __VA_ARGS__)
429
0
        }
430
0
    }
431
432
    // Free all of the prepared statements
433
0
    const std::vector<std::pair<sqlite3_stmt**, const char*>> statements{
434
0
        {&m_read_stmt, "read"},
435
0
        {&m_insert_stmt, "insert"},
436
0
        {&m_overwrite_stmt, "overwrite"},
437
0
        {&m_delete_stmt, "delete"},
438
0
        {&m_delete_prefix_stmt, "delete prefix"},
439
0
    };
440
441
0
    for (const auto& [stmt_prepared, stmt_description] : statements) {
442
0
        int res = sqlite3_finalize(*stmt_prepared);
443
0
        if (res != SQLITE_OK) {
444
0
            LogWarning("SQLiteBatch: Batch closed but could not finalize %s statement: %s",
Line
Count
Source
126
0
#define LogWarning(...) detail_LogWithSrcLoc(BCLog::LogFlags::ALL, util::log::Level::Warning, __VA_ARGS__)
Line
Count
Source
119
0
#define detail_LogWithSrcLoc(category, level, ...) util::log::LogPrintFormatInternal(SourceLocation{__func__}, category, level, __VA_ARGS__)
445
0
                      stmt_description, sqlite3_errstr(res));
446
0
        }
447
0
        *stmt_prepared = nullptr;
448
0
    }
449
450
0
    if (force_conn_refresh) {
451
0
        m_database.Close();
452
0
        try {
453
0
            m_database.Open();
454
            // If TxnAbort failed and we refreshed the connection, the semaphore was not released, so release it here to avoid deadlocks on future writes.
455
0
            m_database.m_write_semaphore.release();
456
0
        } catch (const std::runtime_error&) {
457
            // If open fails, cleanup this object and rethrow the exception
458
0
            m_database.Close();
459
0
            throw;
460
0
        }
461
0
    }
462
0
}
463
464
bool SQLiteBatch::ReadKey(DataStream&& key, DataStream& value)
465
0
{
466
0
    if (!m_database.m_db) return false;
467
0
    assert(m_read_stmt);
468
469
    // Bind: leftmost parameter in statement is index 1
470
0
    if (!BindBlobToStatement(m_read_stmt, 1, key, "key")) return false;
471
0
    int res = sqlite3_step(m_read_stmt);
472
0
    if (res != SQLITE_ROW) {
473
0
        if (res != SQLITE_DONE) {
474
            // SQLITE_DONE means "not found", don't log an error in that case.
475
0
            LogWarning("Unable to execute read statement: %s", sqlite3_errstr(res));
Line
Count
Source
126
0
#define LogWarning(...) detail_LogWithSrcLoc(BCLog::LogFlags::ALL, util::log::Level::Warning, __VA_ARGS__)
Line
Count
Source
119
0
#define detail_LogWithSrcLoc(category, level, ...) util::log::LogPrintFormatInternal(SourceLocation{__func__}, category, level, __VA_ARGS__)
476
0
        }
477
0
        sqlite3_clear_bindings(m_read_stmt);
478
0
        sqlite3_reset(m_read_stmt);
479
0
        return false;
480
0
    }
481
    // Leftmost column in result is index 0
482
0
    value.clear();
483
0
    value.write(SpanFromBlob(m_read_stmt, 0));
484
485
0
    sqlite3_clear_bindings(m_read_stmt);
486
0
    sqlite3_reset(m_read_stmt);
487
0
    return true;
488
0
}
489
490
bool SQLiteBatch::WriteKey(DataStream&& key, DataStream&& value, bool overwrite)
491
0
{
492
0
    if (!m_database.m_db) return false;
493
0
    assert(m_insert_stmt && m_overwrite_stmt);
494
495
0
    sqlite3_stmt* stmt;
496
0
    if (overwrite) {
497
0
        stmt = m_overwrite_stmt;
498
0
    } else {
499
0
        stmt = m_insert_stmt;
500
0
    }
501
502
    // Bind: leftmost parameter in statement is index 1
503
    // Insert index 1 is key, 2 is value
504
0
    if (!BindBlobToStatement(stmt, 1, key, "key")) return false;
505
0
    if (!BindBlobToStatement(stmt, 2, value, "value")) return false;
506
507
    // Acquire semaphore if not previously acquired when creating a transaction.
508
0
    if (!m_txn) m_database.m_write_semaphore.acquire();
509
510
    // Execute
511
0
    int res = sqlite3_step(stmt);
512
0
    sqlite3_clear_bindings(stmt);
513
0
    sqlite3_reset(stmt);
514
0
    if (res != SQLITE_DONE) {
515
0
        LogWarning("Unable to execute write statement: %s", sqlite3_errstr(res));
Line
Count
Source
126
0
#define LogWarning(...) detail_LogWithSrcLoc(BCLog::LogFlags::ALL, util::log::Level::Warning, __VA_ARGS__)
Line
Count
Source
119
0
#define detail_LogWithSrcLoc(category, level, ...) util::log::LogPrintFormatInternal(SourceLocation{__func__}, category, level, __VA_ARGS__)
516
0
    }
517
518
0
    if (!m_txn) m_database.m_write_semaphore.release();
519
520
0
    return res == SQLITE_DONE;
521
0
}
522
523
bool SQLiteBatch::ExecStatement(sqlite3_stmt* stmt, std::span<const std::byte> blob)
524
0
{
525
0
    if (!m_database.m_db) return false;
526
0
    assert(stmt);
527
528
    // Bind: leftmost parameter in statement is index 1
529
0
    if (!BindBlobToStatement(stmt, 1, blob, "key")) return false;
530
531
    // Acquire semaphore if not previously acquired when creating a transaction.
532
0
    if (!m_txn) m_database.m_write_semaphore.acquire();
533
534
    // Execute
535
0
    int res = sqlite3_step(stmt);
536
0
    sqlite3_clear_bindings(stmt);
537
0
    sqlite3_reset(stmt);
538
0
    if (res != SQLITE_DONE) {
539
0
        LogWarning("Unable to execute exec statement: %s", sqlite3_errstr(res));
Line
Count
Source
126
0
#define LogWarning(...) detail_LogWithSrcLoc(BCLog::LogFlags::ALL, util::log::Level::Warning, __VA_ARGS__)
Line
Count
Source
119
0
#define detail_LogWithSrcLoc(category, level, ...) util::log::LogPrintFormatInternal(SourceLocation{__func__}, category, level, __VA_ARGS__)
540
0
    }
541
542
0
    if (!m_txn) m_database.m_write_semaphore.release();
543
544
0
    return res == SQLITE_DONE;
545
0
}
546
547
bool SQLiteBatch::EraseKey(DataStream&& key)
548
0
{
549
0
    return ExecStatement(m_delete_stmt, key);
550
0
}
551
552
bool SQLiteBatch::ErasePrefix(std::span<const std::byte> prefix)
553
0
{
554
0
    return ExecStatement(m_delete_prefix_stmt, prefix);
555
0
}
556
557
bool SQLiteBatch::HasKey(DataStream&& key)
558
0
{
559
0
    if (!m_database.m_db) return false;
560
0
    assert(m_read_stmt);
561
562
    // Bind: leftmost parameter in statement is index 1
563
0
    if (!BindBlobToStatement(m_read_stmt, 1, key, "key")) return false;
564
0
    int res = sqlite3_step(m_read_stmt);
565
0
    sqlite3_clear_bindings(m_read_stmt);
566
0
    sqlite3_reset(m_read_stmt);
567
0
    return res == SQLITE_ROW;
568
0
}
569
570
DatabaseCursor::Status SQLiteCursor::Next(DataStream& key, DataStream& value)
571
0
{
572
0
    int res = sqlite3_step(m_cursor_stmt);
573
0
    if (res == SQLITE_DONE) {
574
0
        return Status::DONE;
575
0
    }
576
0
    if (res != SQLITE_ROW) {
577
0
        LogWarning("Unable to execute cursor step: %s", sqlite3_errstr(res));
Line
Count
Source
126
0
#define LogWarning(...) detail_LogWithSrcLoc(BCLog::LogFlags::ALL, util::log::Level::Warning, __VA_ARGS__)
Line
Count
Source
119
0
#define detail_LogWithSrcLoc(category, level, ...) util::log::LogPrintFormatInternal(SourceLocation{__func__}, category, level, __VA_ARGS__)
578
0
        return Status::FAIL;
579
0
    }
580
581
0
    key.clear();
582
0
    value.clear();
583
584
    // Leftmost column in result is index 0
585
0
    key.write(SpanFromBlob(m_cursor_stmt, 0));
586
0
    value.write(SpanFromBlob(m_cursor_stmt, 1));
587
0
    return Status::MORE;
588
0
}
589
590
SQLiteCursor::~SQLiteCursor()
591
0
{
592
0
    sqlite3_clear_bindings(m_cursor_stmt);
593
0
    sqlite3_reset(m_cursor_stmt);
594
0
    int res = sqlite3_finalize(m_cursor_stmt);
595
0
    if (res != SQLITE_OK) {
596
0
        LogWarning("Cursor closed but could not finalize cursor statement: %s",
Line
Count
Source
126
0
#define LogWarning(...) detail_LogWithSrcLoc(BCLog::LogFlags::ALL, util::log::Level::Warning, __VA_ARGS__)
Line
Count
Source
119
0
#define detail_LogWithSrcLoc(category, level, ...) util::log::LogPrintFormatInternal(SourceLocation{__func__}, category, level, __VA_ARGS__)
597
0
                   sqlite3_errstr(res));
598
0
    }
599
0
}
600
601
std::unique_ptr<DatabaseCursor> SQLiteBatch::GetNewCursor()
602
0
{
603
0
    if (!m_database.m_db) return nullptr;
604
0
    auto cursor = std::make_unique<SQLiteCursor>();
605
606
0
    const char* stmt_text = "SELECT key, value FROM main";
607
0
    int res = sqlite3_prepare_v2(m_database.m_db, stmt_text, -1, &cursor->m_cursor_stmt, nullptr);
608
0
    if (res != SQLITE_OK) {
609
0
        throw std::runtime_error(strprintf(
Line
Count
Source
1172
0
#define strprintf tfm::format
610
0
            "%s: Failed to setup cursor SQL statement: %s\n", __func__, sqlite3_errstr(res)));
611
0
    }
612
613
0
    return cursor;
614
0
}
615
616
std::unique_ptr<DatabaseCursor> SQLiteBatch::GetNewPrefixCursor(std::span<const std::byte> prefix)
617
0
{
618
0
    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
0
    std::vector<std::byte> start_range(prefix.begin(), prefix.end());
624
0
    std::vector<std::byte> end_range(prefix.begin(), prefix.end());
625
0
    auto it = end_range.rbegin();
626
0
    for (; it != end_range.rend(); ++it) {
627
0
        if (*it == std::byte(std::numeric_limits<unsigned char>::max())) {
628
0
            *it = std::byte(0);
629
0
            continue;
630
0
        }
631
0
        *it = std::byte(std::to_integer<unsigned char>(*it) + 1);
632
0
        break;
633
0
    }
634
0
    if (it == end_range.rend()) {
635
        // If the prefix is all 0xff bytes, clear end_range as we won't need it
636
0
        end_range.clear();
637
0
    }
638
639
0
    auto cursor = std::make_unique<SQLiteCursor>(start_range, end_range);
640
0
    if (!cursor) return nullptr;
641
642
0
    const char* stmt_text = end_range.empty() ? "SELECT key, value FROM main WHERE key >= ?" :
643
0
                            "SELECT key, value FROM main WHERE key >= ? AND key < ?";
644
0
    int res = sqlite3_prepare_v2(m_database.m_db, stmt_text, -1, &cursor->m_cursor_stmt, nullptr);
645
0
    if (res != SQLITE_OK) {
646
0
        throw std::runtime_error(strprintf(
Line
Count
Source
1172
0
#define strprintf tfm::format
647
0
            "SQLiteDatabase: Failed to setup cursor SQL statement: %s\n", sqlite3_errstr(res)));
648
0
    }
649
650
0
    if (!BindBlobToStatement(cursor->m_cursor_stmt, 1, cursor->m_prefix_range_start, "prefix_start")) return nullptr;
651
0
    if (!end_range.empty()) {
652
0
        if (!BindBlobToStatement(cursor->m_cursor_stmt, 2, cursor->m_prefix_range_end, "prefix_end")) return nullptr;
653
0
    }
654
655
0
    return cursor;
656
0
}
657
658
bool SQLiteBatch::TxnBegin()
659
0
{
660
0
    if (!m_database.m_db || m_txn) return false;
661
0
    m_database.m_write_semaphore.acquire();
662
0
    Assert(!m_database.HasActiveTxn());
Line
Count
Source
116
0
#define Assert(val) inline_assertion_check<true>(val, std::source_location::current(), #val)
663
0
    int res = Assert(m_exec_handler)->Exec(m_database, "BEGIN TRANSACTION");
Line
Count
Source
116
0
#define Assert(val) inline_assertion_check<true>(val, std::source_location::current(), #val)
664
0
    if (res != SQLITE_OK) {
665
0
        LogWarning("SQLiteBatch: Failed to begin the transaction");
Line
Count
Source
126
0
#define LogWarning(...) detail_LogWithSrcLoc(BCLog::LogFlags::ALL, util::log::Level::Warning, __VA_ARGS__)
Line
Count
Source
119
0
#define detail_LogWithSrcLoc(category, level, ...) util::log::LogPrintFormatInternal(SourceLocation{__func__}, category, level, __VA_ARGS__)
666
0
        m_database.m_write_semaphore.release();
667
0
    } else {
668
0
        m_txn = true;
669
0
    }
670
0
    return res == SQLITE_OK;
671
0
}
672
673
bool SQLiteBatch::TxnCommit()
674
0
{
675
0
    if (!m_database.m_db || !m_txn) return false;
676
0
    Assert(m_database.HasActiveTxn());
Line
Count
Source
116
0
#define Assert(val) inline_assertion_check<true>(val, std::source_location::current(), #val)
677
0
    int res = Assert(m_exec_handler)->Exec(m_database, "COMMIT TRANSACTION");
Line
Count
Source
116
0
#define Assert(val) inline_assertion_check<true>(val, std::source_location::current(), #val)
678
0
    if (res != SQLITE_OK) {
679
0
        LogWarning("SQLiteBatch: Failed to commit the transaction");
Line
Count
Source
126
0
#define LogWarning(...) detail_LogWithSrcLoc(BCLog::LogFlags::ALL, util::log::Level::Warning, __VA_ARGS__)
Line
Count
Source
119
0
#define detail_LogWithSrcLoc(category, level, ...) util::log::LogPrintFormatInternal(SourceLocation{__func__}, category, level, __VA_ARGS__)
680
0
    } else {
681
0
        m_txn = false;
682
0
        m_database.m_write_semaphore.release();
683
0
    }
684
0
    return res == SQLITE_OK;
685
0
}
686
687
bool SQLiteBatch::TxnAbort()
688
0
{
689
0
    if (!m_database.m_db || !m_txn) return false;
690
0
    Assert(m_database.HasActiveTxn());
Line
Count
Source
116
0
#define Assert(val) inline_assertion_check<true>(val, std::source_location::current(), #val)
691
0
    int res = Assert(m_exec_handler)->Exec(m_database, "ROLLBACK TRANSACTION");
Line
Count
Source
116
0
#define Assert(val) inline_assertion_check<true>(val, std::source_location::current(), #val)
692
0
    if (res != SQLITE_OK) {
693
0
        LogWarning("SQLiteBatch: Failed to abort the transaction");
Line
Count
Source
126
0
#define LogWarning(...) detail_LogWithSrcLoc(BCLog::LogFlags::ALL, util::log::Level::Warning, __VA_ARGS__)
Line
Count
Source
119
0
#define detail_LogWithSrcLoc(category, level, ...) util::log::LogPrintFormatInternal(SourceLocation{__func__}, category, level, __VA_ARGS__)
694
0
    } else {
695
0
        m_txn = false;
696
0
        m_database.m_write_semaphore.release();
697
0
    }
698
0
    return res == SQLITE_OK;
699
0
}
700
701
std::unique_ptr<SQLiteDatabase> MakeSQLiteDatabase(const fs::path& path, const DatabaseOptions& options, DatabaseStatus& status, bilingual_str& error)
702
0
{
703
0
    try {
704
0
        fs::path data_file = SQLiteDataFile(path);
705
0
        auto db = std::make_unique<SQLiteDatabase>(data_file.parent_path(), data_file, options);
706
0
        if (options.verify && !db->Verify(error)) {
707
0
            status = DatabaseStatus::FAILED_VERIFY;
708
0
            return nullptr;
709
0
        }
710
0
        status = DatabaseStatus::SUCCESS;
711
0
        return db;
712
0
    } catch (const std::runtime_error& e) {
713
0
        status = DatabaseStatus::FAILED_LOAD;
714
0
        error = Untranslated(e.what());
715
0
        return nullptr;
716
0
    }
717
0
}
718
719
std::string SQLiteDatabaseVersion()
720
0
{
721
0
    return std::string(sqlite3_libversion());
722
0
}
723
} // namespace wallet