Bitcoin ABC 0.26.3
P2P Digital Currency
Loading...
Searching...
No Matches
sqlite.cpp
Go to the documentation of this file.
1// Copyright (c) 2020 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 <wallet/sqlite.h>
6
7#include <logging.h>
8#include <sync.h>
9#include <util/fs_helpers.h>
10#include <util/strencodings.h>
11#include <util/translation.h>
12#include <wallet/db.h>
13
14#include <cstdint>
15#include <sqlite3.h>
16
17static const char *const DATABASE_FILENAME = "wallet.dat";
18
21
22static void ErrorLogCallback(void *arg, int code, const char *msg) {
23 // From sqlite3_config() documentation for the SQLITE_CONFIG_LOG option:
24 // "The void pointer that is the second argument to SQLITE_CONFIG_LOG is
25 // passed through as the first parameter to the application-defined logger
26 // function whenever that function is invoked."
27 // Assert that this is the case:
28 assert(arg == nullptr);
29 LogPrintf("SQLite Error. Code: %d. Message: %s\n", code, msg);
30}
31
33 const fs::path &file_path, bool mock)
34 : WalletDatabase(), m_mock(mock), m_dir_path(fs::PathToString(dir_path)),
35 m_file_path(fs::PathToString(file_path)) {
36 {
38 LogPrintf("Using SQLite Version %s\n", SQLiteDatabaseVersion());
39 LogPrintf("Using wallet %s\n", m_dir_path);
40
41 if (++g_sqlite_count == 1) {
42 // Setup logging
43 int ret =
45 if (ret != SQLITE_OK) {
46 throw std::runtime_error(
47 strprintf("SQLiteDatabase: Failed to setup error log: %s\n",
49 }
50 }
51 // This is a no-op if sqlite3 is already initialized
52 int ret = sqlite3_initialize();
53 if (ret != SQLITE_OK) {
54 throw std::runtime_error(
55 strprintf("SQLiteDatabase: Failed to initialize SQLite: %s\n",
57 }
58 }
59
60 try {
61 Open();
62 } catch (const std::runtime_error &) {
63 // If open fails, cleanup this object and rethrow the exception
64 Cleanup();
65 throw;
66 }
67}
68
70 int res;
71 if (!m_read_stmt) {
73 m_database.m_db, "SELECT value FROM main WHERE key = ?", -1,
74 &m_read_stmt, nullptr)) != SQLITE_OK) {
75 throw std::runtime_error(strprintf(
76 "SQLiteDatabase: Failed to setup SQL statements: %s\n",
78 }
79 }
80 if (!m_insert_stmt) {
82 "INSERT INTO main VALUES(?, ?)", -1,
83 &m_insert_stmt, nullptr)) != SQLITE_OK) {
84 throw std::runtime_error(strprintf(
85 "SQLiteDatabase: Failed to setup SQL statements: %s\n",
87 }
88 }
89 if (!m_overwrite_stmt) {
91 m_database.m_db, "INSERT or REPLACE into main values(?, ?)",
92 -1, &m_overwrite_stmt, nullptr)) != SQLITE_OK) {
93 throw std::runtime_error(strprintf(
94 "SQLiteDatabase: Failed to setup SQL statements: %s\n",
96 }
97 }
98 if (!m_delete_stmt) {
100 "DELETE FROM main WHERE key = ?", -1,
101 &m_delete_stmt, nullptr)) != SQLITE_OK) {
102 throw std::runtime_error(strprintf(
103 "SQLiteDatabase: Failed to setup SQL statements: %s\n",
105 }
106 }
107 if (!m_cursor_stmt) {
109 "SELECT key, value FROM main", -1,
110 &m_cursor_stmt, nullptr)) != SQLITE_OK) {
111 throw std::runtime_error(strprintf(
112 "SQLiteDatabase: Failed to setup SQL statements : %s\n",
114 }
115 }
116}
117
121
122void SQLiteDatabase::Cleanup() noexcept {
123 Close();
124
126 if (--g_sqlite_count == 0) {
127 int ret = sqlite3_shutdown();
128 if (ret != SQLITE_OK) {
129 LogPrintf("SQLiteDatabase: Failed to shutdown SQLite: %s\n",
131 }
132 }
133}
134
136 assert(m_db);
137
138 sqlite3_stmt *stmt{nullptr};
139 int ret =
140 sqlite3_prepare_v2(m_db, "PRAGMA integrity_check", -1, &stmt, nullptr);
141 if (ret != SQLITE_OK) {
143 error = strprintf(_("SQLiteDatabase: Failed to prepare statement to "
144 "verify database: %s"),
146 return false;
147 }
148 while (true) {
150 if (ret == SQLITE_DONE) {
151 break;
152 }
153 if (ret != SQLITE_ROW) {
154 error = strprintf(_("SQLiteDatabase: Failed to execute statement "
155 "to verify database: %s"),
157 break;
158 }
159 const char *msg = (const char *)sqlite3_column_text(stmt, 0);
160 if (!msg) {
161 error = strprintf(_("SQLiteDatabase: Failed to read database "
162 "verification error: %s"),
164 break;
165 }
166 std::string str_msg(msg);
167 if (str_msg == "ok") {
168 continue;
169 }
170 if (error.empty()) {
171 error = _("Failed to verify database") + Untranslated("\n");
172 }
173 error += Untranslated(strprintf("%s\n", str_msg));
174 }
176 return error.empty();
177}
178
180 int flags =
182 if (m_mock) {
183 // In memory database for mock db
185 }
186
187 if (m_db == nullptr) {
189 int ret = sqlite3_open_v2(m_file_path.c_str(), &m_db, flags, nullptr);
190 if (ret != SQLITE_OK) {
191 throw std::runtime_error(
192 strprintf("SQLiteDatabase: Failed to open database: %s\n",
194 }
195 }
196
197 if (sqlite3_db_readonly(m_db, "main") != 0) {
198 throw std::runtime_error("SQLiteDatabase: Database opened in readonly "
199 "mode but read-write permissions are needed");
200 }
201
202 // Acquire an exclusive lock on the database
203 // First change the locking mode to exclusive
204 int ret = sqlite3_exec(m_db, "PRAGMA locking_mode = exclusive", nullptr,
205 nullptr, nullptr);
206 if (ret != SQLITE_OK) {
207 throw std::runtime_error(
208 strprintf("SQLiteDatabase: Unable to change database locking mode "
209 "to exclusive: %s\n",
211 }
212 // Now begin a transaction to acquire the exclusive lock. This lock won't be
213 // released until we close because of the exclusive locking mode.
214 ret = sqlite3_exec(m_db, "BEGIN EXCLUSIVE TRANSACTION", nullptr, nullptr,
215 nullptr);
216 if (ret != SQLITE_OK) {
217 throw std::runtime_error(
218 "SQLiteDatabase: Unable to obtain an exclusive lock on the "
219 "database, is it being used by another bitcoind?\n");
220 }
221 ret = sqlite3_exec(m_db, "COMMIT", nullptr, nullptr, nullptr);
222 if (ret != SQLITE_OK) {
223 throw std::runtime_error(strprintf(
224 "SQLiteDatabase: Unable to end exclusive lock transaction: %s\n",
226 }
227
228 // Enable fullfsync for the platforms that use it
229 ret = sqlite3_exec(m_db, "PRAGMA fullfsync = true", nullptr, nullptr,
230 nullptr);
231 if (ret != SQLITE_OK) {
232 throw std::runtime_error(
233 strprintf("SQLiteDatabase: Failed to enable fullfsync: %s\n",
235 }
236
237 // Make the table for our key-value pairs
238 // First check that the main table exists
241 m_db,
242 "SELECT name FROM sqlite_master WHERE type='table' AND name='main'", -1,
243 &check_main_stmt, nullptr);
244 if (ret != SQLITE_OK) {
245 throw std::runtime_error(
246 strprintf("SQLiteDatabase: Failed to prepare statement to check "
247 "table existence: %s\n",
249 }
252 throw std::runtime_error(
253 strprintf("SQLiteDatabase: Failed to finalize statement checking "
254 "table existence: %s\n",
256 }
257 bool table_exists;
258 if (ret == SQLITE_DONE) {
259 table_exists = false;
260 } else if (ret == SQLITE_ROW) {
261 table_exists = true;
262 } else {
263 throw std::runtime_error(
264 strprintf("SQLiteDatabase: Failed to execute statement to check "
265 "table existence: %s\n",
267 }
268
269 // Do the db setup things because the table doesn't exist only when we are
270 // creating a new wallet
271 if (!table_exists) {
273 "CREATE TABLE main(key BLOB PRIMARY KEY NOT NULL, "
274 "value BLOB NOT NULL)",
275 nullptr, nullptr, nullptr);
276 if (ret != SQLITE_OK) {
277 throw std::runtime_error(
278 strprintf("SQLiteDatabase: Failed to create new database: %s\n",
280 }
281 }
282}
283
284bool SQLiteDatabase::Rewrite(const char *skip) {
285 // Rewrite the database using the VACUUM command:
286 // https://sqlite.org/lang_vacuum.html
287 int ret = sqlite3_exec(m_db, "VACUUM", nullptr, nullptr, nullptr);
288 return ret == SQLITE_OK;
289}
290
291bool SQLiteDatabase::Backup(const std::string &dest) const {
293 int res = sqlite3_open(dest.c_str(), &db_copy);
294 if (res != SQLITE_OK) {
296 return false;
297 }
299 if (!backup) {
300 LogPrintf("%s: Unable to begin backup: %s\n", __func__,
303 return false;
304 }
305 // Specifying -1 will copy all of the pages
307 if (res != SQLITE_DONE) {
308 LogPrintf("%s: Unable to backup: %s\n", __func__, sqlite3_errstr(res));
311 return false;
312 }
315 return res == SQLITE_OK;
316}
317
319 int res = sqlite3_close(m_db);
320 if (res != SQLITE_OK) {
321 throw std::runtime_error(
322 strprintf("SQLiteDatabase: Failed to close database: %s\n",
324 }
325 m_db = nullptr;
326}
327
328std::unique_ptr<DatabaseBatch> SQLiteDatabase::MakeBatch(bool flush_on_close) {
329 // We ignore flush_on_close because we don't do manual flushing for SQLite
330 return std::make_unique<SQLiteBatch>(*this);
331}
332
333SQLiteBatch::SQLiteBatch(SQLiteDatabase &database) : m_database(database) {
334 // Make sure we have a db handle
336
338}
339
341 // If m_db is in a transaction (i.e. not in autocommit mode), then abort the
342 // transaction in progress
344 if (TxnAbort()) {
345 LogPrintf("SQLiteBatch: Batch closed unexpectedly without the "
346 "transaction being explicitly committed or aborted\n");
347 } else {
348 LogPrintf(
349 "SQLiteBatch: Batch closed and failed to abort transaction\n");
350 }
351 }
352
353 // Free all of the prepared statements
355 if (ret != SQLITE_OK) {
356 LogPrintf("SQLiteBatch: Batch closed but could not finalize read "
357 "statement: %s\n",
359 }
361 if (ret != SQLITE_OK) {
362 LogPrintf("SQLiteBatch: Batch closed but could not finalize insert "
363 "statement: %s\n",
365 }
367 if (ret != SQLITE_OK) {
368 LogPrintf("SQLiteBatch: Batch closed but could not finalize overwrite "
369 "statement: %s\n",
371 }
373 if (ret != SQLITE_OK) {
374 LogPrintf("SQLiteBatch: Batch closed but could not finalize delete "
375 "statement: %s\n",
377 }
379 if (ret != SQLITE_OK) {
380 LogPrintf("SQLiteBatch: Batch closed but could not finalize cursor "
381 "statement: %s\n",
383 }
384 m_read_stmt = nullptr;
385 m_insert_stmt = nullptr;
386 m_overwrite_stmt = nullptr;
387 m_delete_stmt = nullptr;
388 m_cursor_stmt = nullptr;
389}
390
392 if (!m_database.m_db) {
393 return false;
394 }
396
397 // Bind: leftmost parameter in statement is index 1
398 int res = sqlite3_bind_blob(m_read_stmt, 1, key.data(), key.size(),
400 if (res != SQLITE_OK) {
401 LogPrintf("%s: Unable to bind statement: %s\n", __func__,
405 return false;
406 }
408 if (res != SQLITE_ROW) {
409 if (res != SQLITE_DONE) {
410 // SQLITE_DONE means "not found", don't log an error in that case.
411 LogPrintf("%s: Unable to execute statement: %s\n", __func__,
413 }
416 return false;
417 }
418 // Leftmost column in result is index 0
419 const std::byte *data{BytePtr(sqlite3_column_blob(m_read_stmt, 0))};
421 value.write({data, data_size});
422
425 return true;
426}
427
429 bool overwrite) {
430 if (!m_database.m_db) {
431 return false;
432 }
434
436 if (overwrite) {
438 } else {
440 }
441
442 // Bind: leftmost parameter in statement is index 1
443 // Insert index 1 is key, 2 is value
444 int res = sqlite3_bind_blob(stmt, 1, key.data(), key.size(), SQLITE_STATIC);
445 if (res != SQLITE_OK) {
446 LogPrintf("%s: Unable to bind key to statement: %s\n", __func__,
450 return false;
451 }
452 res = sqlite3_bind_blob(stmt, 2, value.data(), value.size(), SQLITE_STATIC);
453 if (res != SQLITE_OK) {
454 LogPrintf("%s: Unable to bind value to statement: %s\n", __func__,
458 return false;
459 }
460
461 // Execute
465 if (res != SQLITE_DONE) {
466 LogPrintf("%s: Unable to execute statement: %s\n", __func__,
468 }
469 return res == SQLITE_DONE;
470}
471
473 if (!m_database.m_db) {
474 return false;
475 }
477
478 // Bind: leftmost parameter in statement is index 1
479 int res = sqlite3_bind_blob(m_delete_stmt, 1, key.data(), key.size(),
481 if (res != SQLITE_OK) {
482 LogPrintf("%s: Unable to bind statement: %s\n", __func__,
486 return false;
487 }
488
489 // Execute
493 if (res != SQLITE_DONE) {
494 LogPrintf("%s: Unable to execute statement: %s\n", __func__,
496 }
497 return res == SQLITE_DONE;
498}
499
501 if (!m_database.m_db) {
502 return false;
503 }
505
506 // Bind: leftmost parameter in statement is index 1
507 bool ret = false;
508 int res = sqlite3_bind_blob(m_read_stmt, 1, key.data(), key.size(),
510 if (res == SQLITE_OK) {
512 if (res == SQLITE_ROW) {
513 ret = true;
514 }
515 }
516
519 return ret;
520}
521
524 if (!m_database.m_db) {
525 return false;
526 }
527 m_cursor_init = true;
528 return true;
529}
530
532 bool &complete) {
533 complete = false;
534
535 if (!m_cursor_init) {
536 return false;
537 }
538
540 if (res == SQLITE_DONE) {
541 complete = true;
542 return true;
543 }
544 if (res != SQLITE_ROW) {
545 LogPrintf(
546 "SQLiteBatch::ReadAtCursor: Unable to execute cursor step: %s\n",
548 return false;
549 }
550
551 // Leftmost column in result is index 0
552 const std::byte *key_data{BytePtr(sqlite3_column_blob(m_cursor_stmt, 0))};
558 return true;
559}
560
565
568 return false;
569 }
570 int res = sqlite3_exec(m_database.m_db, "BEGIN TRANSACTION", nullptr,
571 nullptr, nullptr);
572 if (res != SQLITE_OK) {
573 LogPrintf("SQLiteBatch: Failed to begin the transaction\n");
574 }
575 return res == SQLITE_OK;
576}
577
580 return false;
581 }
582 int res = sqlite3_exec(m_database.m_db, "COMMIT TRANSACTION", nullptr,
583 nullptr, nullptr);
584 if (res != SQLITE_OK) {
585 LogPrintf("SQLiteBatch: Failed to commit the transaction\n");
586 }
587 return res == SQLITE_OK;
588}
589
592 return false;
593 }
594 int res = sqlite3_exec(m_database.m_db, "ROLLBACK TRANSACTION", nullptr,
595 nullptr, nullptr);
596 if (res != SQLITE_OK) {
597 LogPrintf("SQLiteBatch: Failed to abort the transaction\n");
598 }
599 return res == SQLITE_OK;
600}
601
603 return false;
604}
605
606std::unique_ptr<SQLiteDatabase>
607MakeSQLiteDatabase(const fs::path &path, const DatabaseOptions &options,
609 const fs::path file = path / DATABASE_FILENAME;
610 try {
611 auto db = std::make_unique<SQLiteDatabase>(path, file);
612 if (options.verify && !db->Verify(error)) {
614 return nullptr;
615 }
616 return db;
617 } catch (const std::runtime_error &e) {
619 error.original = e.what();
620 return nullptr;
621 }
622}
623
625 return std::string(sqlite3_libversion());
626}
int flags
Double ended buffer combining vector and stream-like interfaces.
Definition streams.h:177
void write(Span< const value_type > src)
Definition streams.h:379
Different type to mark Mutex at global scope.
Definition sync.h:144
bool WriteKey(CDataStream &&key, CDataStream &&value, bool overwrite=true) override
Definition sqlite.cpp:428
sqlite3_stmt * m_read_stmt
Definition sqlite.h:22
bool TxnAbort() override
Definition sqlite.cpp:590
bool m_cursor_init
Definition sqlite.h:20
sqlite3_stmt * m_cursor_stmt
Definition sqlite.h:26
void SetupSQLStatements()
Definition sqlite.cpp:69
bool HasKey(CDataStream &&key) override
Definition sqlite.cpp:500
sqlite3_stmt * m_insert_stmt
Definition sqlite.h:23
sqlite3_stmt * m_overwrite_stmt
Definition sqlite.h:24
bool StartCursor() override
Definition sqlite.cpp:522
bool EraseKey(CDataStream &&key) override
Definition sqlite.cpp:472
bool ReadAtCursor(CDataStream &key, CDataStream &value, bool &complete) override
Definition sqlite.cpp:531
bool TxnBegin() override
Definition sqlite.cpp:566
void Close() override
Definition sqlite.cpp:340
sqlite3_stmt * m_delete_stmt
Definition sqlite.h:25
bool ReadKey(CDataStream &&key, CDataStream &value) override
Definition sqlite.cpp:391
bool TxnCommit() override
Definition sqlite.cpp:578
SQLiteDatabase & m_database
Definition sqlite.h:18
void CloseCursor() override
Definition sqlite.cpp:561
SQLiteBatch(SQLiteDatabase &database)
Definition sqlite.cpp:333
An instance of this class represents one SQLite3 database.
Definition sqlite.h:55
void Close() override
Close the database.
Definition sqlite.cpp:318
void Open() override
Open the database if it is not already opened.
Definition sqlite.cpp:179
sqlite3 * m_db
Definition sqlite.h:114
std::unique_ptr< DatabaseBatch > MakeBatch(bool flush_on_close=true) override
Make a SQLiteBatch connected to this database.
Definition sqlite.cpp:328
const std::string m_dir_path
Definition sqlite.h:59
void Cleanup() noexcept
Definition sqlite.cpp:122
bool Verify(bilingual_str &error)
Definition sqlite.cpp:135
SQLiteDatabase()=delete
const bool m_mock
Definition sqlite.h:57
const std::string m_file_path
Definition sqlite.h:61
bool Backup(const std::string &dest) const override
Back up the entire database to a file.
Definition sqlite.cpp:291
bool Rewrite(const char *skip=nullptr) override
Rewrite the entire database on disk.
Definition sqlite.cpp:284
An instance of this class represents one database.
Definition db.h:100
Path class wrapper to block calls to the fs::path(std::string) implicit constructor and the fs::path:...
Definition fs.h:30
bool TryCreateDirectories(const fs::path &p)
Ignores exceptions thrown by create_directories if the requested directory exists.
bool error(const char *fmt, const Args &...args)
Definition logging.h:226
#define LogPrintf(...)
Definition logging.h:207
Filesystem operations and types.
Definition fs.h:20
static path PathFromString(const std::string &string)
Convert byte string to path object.
Definition fs.h:165
T GetRand(T nMax=std::numeric_limits< T >::max()) noexcept
Generate a uniform random integer of type T in the range [0..nMax) nMax defaults to std::numeric_limi...
Definition random.h:85
CAddrDb db
Definition main.cpp:35
const std::byte * BytePtr(const void *data)
Convert a data pointer to a std::byte data pointer.
Definition span.h:286
static void ErrorLogCallback(void *arg, int code, const char *msg)
Definition sqlite.cpp:22
std::string SQLiteDatabaseVersion()
Definition sqlite.cpp:624
static GlobalMutex g_sqlite_mutex
Definition sqlite.cpp:19
std::unique_ptr< SQLiteDatabase > MakeSQLiteDatabase(const fs::path &path, const DatabaseOptions &options, DatabaseStatus &status, bilingual_str &error)
Definition sqlite.cpp:607
static const char *const DATABASE_FILENAME
Definition sqlite.cpp:17
bool ExistsSQLiteDatabase(const fs::path &path)
Definition sqlite.cpp:602
std::string SQLiteDatabaseVersion()
Definition sqlite.cpp:624
bool verify
Definition db.h:226
Bilingual messages:
Definition translation.h:17
#define LOCK(cs)
Definition sync.h:306
#define GUARDED_BY(x)
#define strprintf
Format arguments and return the string or write to given std::ostream (see tinyformat::format doc for...
bilingual_str _(const char *psz)
Translation function.
Definition translation.h:68
bilingual_str Untranslated(std::string original)
Mark a bilingual_str as untranslated.
Definition translation.h:36
assert(!tx.IsCoinBase())
DatabaseStatus
Definition db.h:229