In commit "wallet: use RunWithinTxn within MigrateToSQlite" (038d663391621f14e2dcb08ce1a6d9296646689e)
Using RunWithinTxn here is nice: it makes the write loop much easier to follow and puts the begin, write and commit failures in one place. But this commit is hard to review because it isn't a refactoring, it changes how failures are handled, and the next commit replaces that failure handling again. I think the simplest fix is to restructure the commits; otherwise there are a few problems to fix here.
Suggested restructuring: keep only the walletdb.cpp/walletdb.h part in this commit, which is a real refactoring, and move the MigrateToSQLite change into the next commit "wallet: migration, make sqlite creation robust against failures" (b9486540d774f2e07ecce93af0637d6e685a93e0). That commit rewrites this block anyway, so its diff would then go directly from the old asserts to the new error returns, and the cleanup problems below would be covered by the comment on fail(). Commit message for this commit could be:
wallet: add RunWithinTxn overload taking a DatabaseBatch
Refactoring, no behavior change. Make the static RunWithinTxn helper a template so it works with both WalletBatch and DatabaseBatch, and add a WalletDatabase overload whose callback takes a DatabaseBatch. The next commit uses it in MigrateToSQLite to write the migrated records in one transaction and return an error if that fails.
If you keep this commit as it is, these should be fixed:
The database is no longer closed before its file is deleted, and only wallet.dat is deleted. Deleting the data file of an open database can leave wallet.dat-journal behind as a hot journal, and since the new database is created at the wallet's own path, SQLite could roll it back into a new wallet.dat created there later, for example when the migration is retried after restoring the backup.
fs::remove is the throwing overload. If it throws (likely on Windows, which can't delete an open file), the exception skips the assert(false) that is meant to stop the node, and at this commit nothing in the migration catches it, so the RPC returns an error and the node keeps running.
assert(false) normally means a programming bug, but here runtime errors (full disk, I/O error, corrupt database) reach it, which isn't obvious to readers.
Something like this would fix all three:
if (!written) {
// Can be reached by runtime errors such as a full disk, an I/O error or a corrupt database.
const std::vector<fs::path> files{new_db->Files()};
new_db->Close();
for (const fs::path& file : files) {
std::error_code ec;
fs::remove(file, ec);
}
assert(false); // This is a critical error, the new db could not be written to. The original db exists as a backup, but we should not continue execution.
}
- The commit message should say this isn't a pure refactoring, and that the next commit replaces these asserts. For example:
wallet: use RunWithinTxn within MigrateToSQLite
Write the migrated records with RunWithinTxn so failures to begin, write or commit the transaction are handled in one place. The next commit uses this to return an error instead of asserting.
This is a refactoring except on failure paths that still end in assert(false): the new database is now closed and deleted when the transaction can't be started or committed, not only when a write fails, and the failure is logged.