Rationale / motivation
The rationale for this PR is to improve the user experience in new_db failure to migrate a wallet.
My story
After using bitcoin core for a while -- and then ignoring it for a while -- I tried to open my old wallet with v31 release build, saw I had to migrate it, attempted to do so, and the app crashed; running from a terminal, I reproduced and got an error in the terminal:
./wallet/wallet.cpp:3868: bool wallet::CWallet::MigrateToSQLite(bilingual_str&): Assertion `new_db' failed.
Explanation
Tracking down this assertion, I discovered the old BDB format was getting migrated to SQLite, but my wallet was stored on a CIFS mount (v3.1, nounix), which doesn't support the BEGIN EXCLUSIVE lock requested here. (fcntl(3, F_SETLK, {F_WRLCK, start=1073741826, len=510}) = -1 EACCES)
MigrateToSQLite() deletes the legacy BDB wallet before creating the new SQLite database, then asserts that the creation succeeded. Since this fails in my case (due to CIFS not supporting the requested lock), the wallet file is gone, leaving only the .legacy.bak copy made before migration. However, this could also be triggered locally by a full disk or permission errors.
This change attempts to populate the new database with a temporary filename, and only deletes the legacy wallet if that succeeds -- otherwise clean up the failed attempt and return an error with the underlying reason and a hint about filesystem locking. I included equivalent error handling/cleanup for other cases during migration that were also previously asserts, even though they weren't strictly part of the symptom I experienced.
Steps to reproduce
- Create an old BDB wallet (e.g., Bitcoin Core v27) on a CIFS mount (definitely not the only way, but an easy path for me), or fill up the filesystem after creating the wallet.
- Attempt to migrate the wallet with Bitcoin Core v31
Expected result: since the wallet cannot be migrated, the application should tell the user why and leave the original wallet in its original state.
Actual result: Bitcoin Core exits and leaves a 0-byte wallet.dat in its place.
Tests
I added a few new tests covering this case and behavior change. I've only tested this on Linux. Without the fix, the tests abort on master.