| 142 | } |
| 143 | |
| 144 | bool SQLiteDatabase::Verify(bilingual_str& error) |
| 145 | { |
| 146 | assert(m_db); |
| 147 | |
| 148 | // Check the application ID matches our network magic |
| 149 | auto read_result = ReadPragmaInteger(m_db, "application_id", "the application id", error); |
| 150 | if (!read_result.has_value()) return false; |
| 151 | uint32_t app_id = static_cast<uint32_t>(read_result.value()); |
| 152 | uint32_t net_magic = ReadBE32(Params().MessageStart()); |
| 153 | if (app_id != net_magic) { |
| 154 | error = strprintf(_("SQLiteDatabase: Unexpected application id. Expected %u, got %u"), net_magic, app_id); |
| 155 | return false; |
| 156 | } |
| 157 | |
| 158 | // Check our schema version |
| 159 | read_result = ReadPragmaInteger(m_db, "user_version", "sqlite wallet schema version", error); |
| 160 | if (!read_result.has_value()) return false; |
| 161 | int32_t user_ver = read_result.value(); |
| 162 | if (user_ver != WALLET_SCHEMA_VERSION) { |
| 163 | error = strprintf(_("SQLiteDatabase: Unknown sqlite wallet schema version %d. Only version %d is supported"), user_ver, WALLET_SCHEMA_VERSION); |
| 164 | return false; |
| 165 | } |
| 166 | |
| 167 | sqlite3_stmt* stmt{nullptr}; |
| 168 | int ret = sqlite3_prepare_v2(m_db, "PRAGMA integrity_check", -1, &stmt, nullptr); |
| 169 | if (ret != SQLITE_OK) { |
| 170 | sqlite3_finalize(stmt); |
| 171 | error = strprintf(_("SQLiteDatabase: Failed to prepare statement to verify database: %s"), sqlite3_errstr(ret)); |
| 172 | return false; |
| 173 | } |
| 174 | while (true) { |
| 175 | ret = sqlite3_step(stmt); |
| 176 | if (ret == SQLITE_DONE) { |
| 177 | break; |
| 178 | } |
| 179 | if (ret != SQLITE_ROW) { |
| 180 | error = strprintf(_("SQLiteDatabase: Failed to execute statement to verify database: %s"), sqlite3_errstr(ret)); |
| 181 | break; |
| 182 | } |
| 183 | const char* msg = (const char*)sqlite3_column_text(stmt, 0); |
| 184 | if (!msg) { |
| 185 | error = strprintf(_("SQLiteDatabase: Failed to read database verification error: %s"), sqlite3_errstr(ret)); |
| 186 | break; |
| 187 | } |
| 188 | std::string str_msg(msg); |
| 189 | if (str_msg == "ok") { |
| 190 | continue; |
| 191 | } |
| 192 | if (error.empty()) { |
| 193 | error = _("Failed to verify database") + Untranslated("\n"); |
| 194 | } |
| 195 | error += Untranslated(strprintf("%s\n", str_msg)); |
| 196 | } |
| 197 | sqlite3_finalize(stmt); |
| 198 | return error.empty(); |
| 199 | } |
| 200 | |
| 201 | void SQLiteDatabase::Open() |
no test coverage detected