diff options
| author | bitflash <dev@bitflash.network> | 2026-08-18 16:26:34 -0300 |
|---|---|---|
| committer | bitflash <dev@bitflash.network> | 2026-08-18 16:26:34 -0300 |
| commit | 477d57b9a01102b6f79ad9b10b2dcfd280faa1cb (patch) | |
| tree | c320e5c1f6e486affdf255a1a4ac806a01b9da7c /src | |
| parent | 3f94ec0649f935d00de49ad7b55026317dc64a27 (diff) | |
gui: offer to convert a Berkeley DB wallet to SQLite on first run
A first-run dialog for an existing wallet.dat with no backend choice yet:
"Convert to SQLite" exports the wallet and records the sqlite marker so
the next start opens it; "Keep Berkeley DB" records that choice so the
offer does not return; "Decide later" leaves no marker and asks again.
wallet.dat is never touched, and the change takes effect on restart --
matching the restart-to-apply model.
The conversion itself is DoConvertWalletToSQLite(), a headless-testable
function the button calls: it reuses the export path (extracted as
ExportActiveWalletToSQLite, shared with -walletsqliteexport, which keeps
its own specific messages), refuses if already on SQLite or if a
wallet.sqlite exists, and writes the marker only after a verified export.
New wallet-convert self-test proves the action end to end: build a
Berkeley DB wallet, convert, and confirm wallet.sqlite + the marker are
written, wallet.dat is left untouched, a second convert refuses, and a
flagless restart opens the converted SQLite wallet with its phrase intact.
All wallet suites pass. The modal itself is thin UI over this tested
action.
Diffstat (limited to 'src')
| -rw-r--r-- | src/gui.cpp | 90 | ||||
| -rw-r--r-- | src/makefile.mingw | 1 | ||||
| -rw-r--r-- | src/selftest.cpp | 108 | ||||
| -rw-r--r-- | src/walletcmd.cpp | 119 | ||||
| -rw-r--r-- | src/walletcmd.h | 9 |
5 files changed, 290 insertions, 37 deletions
diff --git a/src/gui.cpp b/src/gui.cpp index 8dcb854..89a7ab6 100644 --- a/src/gui.cpp +++ b/src/gui.cpp | |||
| @@ -1391,6 +1391,84 @@ static void DrawAboutDialog() | |||
| 1391 | ImGui::End(); | 1391 | ImGui::End(); |
| 1392 | } | 1392 | } |
| 1393 | 1393 | ||
| 1394 | // First-run offer to move a Berkeley DB wallet to the SQLite backend. Only the | ||
| 1395 | // button actions touch anything: "Convert" exports wallet.dat to wallet.sqlite | ||
| 1396 | // and records the backend marker (wallet.dat is left untouched, so a restart | ||
| 1397 | // picks up SQLite while the old file stays as a fallback); "Keep Berkeley DB" | ||
| 1398 | // just records that choice so the offer does not return; "Decide later" leaves | ||
| 1399 | // no marker and asks again next start. | ||
| 1400 | static bool g_showBackendWizard = false; | ||
| 1401 | static std::string g_wizardStatus; | ||
| 1402 | static bool g_wizardConverted = false; | ||
| 1403 | |||
| 1404 | static void DrawBackendWizardDialog() | ||
| 1405 | { | ||
| 1406 | if (!g_showBackendWizard) return; | ||
| 1407 | ImGui::SetNextWindowSize(ImVec2(560.0f, 0.0f), ImGuiCond_Always); | ||
| 1408 | ImGui::SetNextWindowPos(ImGui::GetMainViewport()->GetCenter(), | ||
| 1409 | ImGuiCond_Always, ImVec2(0.5f, 0.5f)); | ||
| 1410 | if (ImGui::Begin("Wallet Storage", &g_showBackendWizard, | ||
| 1411 | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoCollapse)) | ||
| 1412 | { | ||
| 1413 | if (g_wizardConverted) | ||
| 1414 | { | ||
| 1415 | ImGui::TextColored(ImVec4(0.35f, 1.0f, 0.45f, 1.0f), "Converted to SQLite."); | ||
| 1416 | ImGui::TextWrapped("%s", g_wizardStatus.c_str()); | ||
| 1417 | ImGui::Spacing(); | ||
| 1418 | ImGui::TextWrapped( | ||
| 1419 | "Restart Bitflash to start using the SQLite wallet. Your Berkeley " | ||
| 1420 | "DB wallet.dat is kept untouched as a fallback."); | ||
| 1421 | ImGui::Spacing(); | ||
| 1422 | if (ImGui::Button("Close", ImVec2(120.0f, 0.0f))) | ||
| 1423 | g_showBackendWizard = false; | ||
| 1424 | } | ||
| 1425 | else | ||
| 1426 | { | ||
| 1427 | ImGui::TextWrapped( | ||
| 1428 | "Bitflash can store your wallet in a new SQLite format: a single " | ||
| 1429 | "file that is easier to back up and does not depend on the " | ||
| 1430 | "database/ directory beside it."); | ||
| 1431 | ImGui::Spacing(); | ||
| 1432 | ImGui::TextWrapped( | ||
| 1433 | "Your current Berkeley DB wallet (wallet.dat) is kept exactly as it " | ||
| 1434 | "is -- nothing is deleted, and you can go back to it at any time. " | ||
| 1435 | "The change takes effect after you restart."); | ||
| 1436 | ImGui::Spacing(); | ||
| 1437 | if (ImGui::Button("Convert to SQLite", ImVec2(180.0f, 0.0f))) | ||
| 1438 | { | ||
| 1439 | int nCopied = 0; | ||
| 1440 | std::string strError; | ||
| 1441 | if (DoConvertWalletToSQLite(nCopied, strError)) | ||
| 1442 | { | ||
| 1443 | g_wizardStatus = | ||
| 1444 | strprintf("%d record(s) copied to wallet.sqlite.", nCopied); | ||
| 1445 | g_wizardConverted = true; | ||
| 1446 | } | ||
| 1447 | else | ||
| 1448 | g_wizardStatus = "Conversion failed: " + strError; | ||
| 1449 | } | ||
| 1450 | ImGui::SameLine(); | ||
| 1451 | if (ImGui::Button("Keep Berkeley DB", ImVec2(160.0f, 0.0f))) | ||
| 1452 | { | ||
| 1453 | WriteWalletBackendMarker("bdb"); | ||
| 1454 | g_showBackendWizard = false; | ||
| 1455 | } | ||
| 1456 | ImGui::SameLine(); | ||
| 1457 | if (ImGui::Button("Decide later", ImVec2(120.0f, 0.0f))) | ||
| 1458 | g_showBackendWizard = false; | ||
| 1459 | |||
| 1460 | if (!g_wizardStatus.empty()) | ||
| 1461 | { | ||
| 1462 | ImGui::Spacing(); | ||
| 1463 | ImGui::PushStyleColor(ImGuiCol_Text, ImVec4(1.0f, 0.45f, 0.35f, 1.0f)); | ||
| 1464 | ImGui::TextWrapped("%s", g_wizardStatus.c_str()); | ||
| 1465 | ImGui::PopStyleColor(); | ||
| 1466 | } | ||
| 1467 | } | ||
| 1468 | } | ||
| 1469 | ImGui::End(); | ||
| 1470 | } | ||
| 1471 | |||
| 1394 | // --------------------------------------------------------------------------- | 1472 | // --------------------------------------------------------------------------- |
| 1395 | // Entry point | 1473 | // Entry point |
| 1396 | // --------------------------------------------------------------------------- | 1474 | // --------------------------------------------------------------------------- |
| @@ -1457,6 +1535,17 @@ int RunGUI(int argc, char* argv[]) | |||
| 1457 | g_mineRadio = nMineMode; | 1535 | g_mineRadio = nMineMode; |
| 1458 | strncpy(g_participantPool, strParticipantPool.c_str(), sizeof(g_participantPool)-1); | 1536 | strncpy(g_participantPool, strParticipantPool.c_str(), sizeof(g_participantPool)-1); |
| 1459 | 1537 | ||
| 1538 | // First run on a Berkeley DB wallet with no recorded choice yet: offer to | ||
| 1539 | // convert to SQLite. Never when already on SQLite, once wallet.sqlite exists, | ||
| 1540 | // or once a choice has been recorded in the backend marker. | ||
| 1541 | if (!WalletSQLiteRuntimeActive() && | ||
| 1542 | FileExists((GetAppDir() + "/wallet.dat").c_str()) && | ||
| 1543 | !FileExists((GetAppDir() + "/wallet.sqlite").c_str()) && | ||
| 1544 | ReadWalletBackendMarker().empty()) | ||
| 1545 | { | ||
| 1546 | g_showBackendWizard = true; | ||
| 1547 | } | ||
| 1548 | |||
| 1460 | int frame = 0; | 1549 | int frame = 0; |
| 1461 | while (!glfwWindowShouldClose(window) && !fShutdown) | 1550 | while (!glfwWindowShouldClose(window) && !fShutdown) |
| 1462 | { | 1551 | { |
| @@ -1480,6 +1569,7 @@ int RunGUI(int argc, char* argv[]) | |||
| 1480 | DrawRestorePhraseDialog(); | 1569 | DrawRestorePhraseDialog(); |
| 1481 | DrawDiagnosticsDialog(); | 1570 | DrawDiagnosticsDialog(); |
| 1482 | DrawAboutDialog(); | 1571 | DrawAboutDialog(); |
| 1572 | DrawBackendWizardDialog(); | ||
| 1483 | DrawStatusBar(); | 1573 | DrawStatusBar(); |
| 1484 | 1574 | ||
| 1485 | ImGui::Render(); | 1575 | ImGui::Render(); |
diff --git a/src/makefile.mingw b/src/makefile.mingw index 06c9211..55f186b 100644 --- a/src/makefile.mingw +++ b/src/makefile.mingw | |||
| @@ -114,6 +114,7 @@ tests: test_bip32.exe bitflash.exe | |||
| 114 | ./bitflash.exe -selftest=wallet-encrypt | 114 | ./bitflash.exe -selftest=wallet-encrypt |
| 115 | ./bitflash.exe -selftest=wallet-sqlite-encrypt | 115 | ./bitflash.exe -selftest=wallet-sqlite-encrypt |
| 116 | ./bitflash.exe -selftest=wallet-backend-default | 116 | ./bitflash.exe -selftest=wallet-backend-default |
| 117 | ./bitflash.exe -selftest=wallet-convert | ||
| 117 | ./bitflash.exe -selftest=net-message | 118 | ./bitflash.exe -selftest=net-message |
| 118 | ./bitflash.exe -selftest=consensus-limits | 119 | ./bitflash.exe -selftest=consensus-limits |
| 119 | ./bitflash.exe -selftest=pool-stratum | 120 | ./bitflash.exe -selftest=pool-stratum |
diff --git a/src/selftest.cpp b/src/selftest.cpp index 1960003..a9c73fe 100644 --- a/src/selftest.cpp +++ b/src/selftest.cpp | |||
| @@ -2664,6 +2664,110 @@ static int RunWalletBackendDefaultSelfTest() | |||
| 2664 | return nFail == 0 ? 0 : 1; | 2664 | return nFail == 0 ? 0 : 1; |
| 2665 | } | 2665 | } |
| 2666 | 2666 | ||
| 2667 | // The wizard's conversion action, headless: build a Berkeley DB wallet, run | ||
| 2668 | // DoConvertWalletToSQLite() (what the GUI button calls), and prove it produced a | ||
| 2669 | // usable wallet.sqlite + backend marker while leaving wallet.dat untouched, then | ||
| 2670 | // that a flagless restart opens the converted SQLite wallet. | ||
| 2671 | static int RunWalletConvertSelfTest() | ||
| 2672 | { | ||
| 2673 | fflush(stdout); | ||
| 2674 | printf("wallet-convert self-test\n"); | ||
| 2675 | |||
| 2676 | std::string tmp, cwd; | ||
| 2677 | if (!MakeTempDir(tmp)) | ||
| 2678 | { | ||
| 2679 | printf(" FAIL could not create a temporary data directory\n"); | ||
| 2680 | return 1; | ||
| 2681 | } | ||
| 2682 | if (!GetCurrentDir(cwd) || !SetCurrentDir(tmp)) | ||
| 2683 | { | ||
| 2684 | printf(" FAIL could not move into the temporary data directory\n"); | ||
| 2685 | RemoveTree(tmp); | ||
| 2686 | return 1; | ||
| 2687 | } | ||
| 2688 | |||
| 2689 | int nFail = 0; | ||
| 2690 | strSetDataDir = tmp; | ||
| 2691 | printf(" temp datadir: %s\n", tmp.c_str()); | ||
| 2692 | |||
| 2693 | #ifdef _WIN32 | ||
| 2694 | string strExe = cwd + "\\bitflash.exe"; | ||
| 2695 | #else | ||
| 2696 | string strExe = cwd + "/bitflash-node"; | ||
| 2697 | #endif | ||
| 2698 | |||
| 2699 | try | ||
| 2700 | { | ||
| 2701 | if (!LoadWallet()) | ||
| 2702 | throw std::runtime_error("LoadWallet failed"); | ||
| 2703 | |||
| 2704 | string strError; | ||
| 2705 | nFail += Check(SetHDSeedFromMnemonic( | ||
| 2706 | "abandon abandon abandon abandon abandon abandon abandon " | ||
| 2707 | "abandon abandon abandon abandon about", strError), | ||
| 2708 | "a phrase can be installed before conversion") ? 0 : 1; | ||
| 2709 | TopUpKeyPool(); | ||
| 2710 | |||
| 2711 | vector<unsigned char> vchDefaultPubKey = keyUser.GetPubKey(); | ||
| 2712 | CPrivKey vchDefaultPrivKey; | ||
| 2713 | nFail += Check(GetWalletPrivKey(vchDefaultPubKey, vchDefaultPrivKey, strError), | ||
| 2714 | "the default private key is readable before conversion") ? 0 : 1; | ||
| 2715 | vector<unsigned char> vchDefaultPrivBytes(vchDefaultPrivKey.begin(), | ||
| 2716 | vchDefaultPrivKey.end()); | ||
| 2717 | |||
| 2718 | // The conversion action the GUI button invokes. | ||
| 2719 | int nCopied = 0; | ||
| 2720 | string strConvertError; | ||
| 2721 | bool fConverted = DoConvertWalletToSQLite(nCopied, strConvertError); | ||
| 2722 | nFail += Check(fConverted && nCopied > 0, | ||
| 2723 | "DoConvertWalletToSQLite exports the wallet") ? 0 : 1; | ||
| 2724 | nFail += Check(FileExists((tmp + "/wallet.sqlite").c_str()) && | ||
| 2725 | FileContainsText(tmp + "/wallet-backend", "sqlite"), | ||
| 2726 | "conversion writes wallet.sqlite and records the sqlite marker") ? 0 : 1; | ||
| 2727 | nFail += Check(FileContainsBytes(tmp + "/wallet.dat", vchDefaultPrivBytes), | ||
| 2728 | "wallet.dat is left untouched by the conversion") ? 0 : 1; | ||
| 2729 | |||
| 2730 | // Idempotence guard: a second conversion refuses rather than overwrite. | ||
| 2731 | int nCopied2 = 0; | ||
| 2732 | string strSecondError; | ||
| 2733 | nFail += Check(!DoConvertWalletToSQLite(nCopied2, strSecondError), | ||
| 2734 | "a second conversion refuses to overwrite wallet.sqlite") ? 0 : 1; | ||
| 2735 | |||
| 2736 | DBFlush(true); | ||
| 2737 | |||
| 2738 | // A flagless restart now opens the converted SQLite wallet (the marker | ||
| 2739 | // routes it) and finds the same recovery phrase. | ||
| 2740 | string strAudit = tmp + "/converted-recovery-audit.txt"; | ||
| 2741 | vector<string> vAuditArgs; | ||
| 2742 | vAuditArgs.push_back("-datadir=" + tmp); | ||
| 2743 | vAuditArgs.push_back("-nomanagedtor"); | ||
| 2744 | vAuditArgs.push_back("-nogui"); | ||
| 2745 | vAuditArgs.push_back("-recoveryaudit"); | ||
| 2746 | int nAuditRet = RunBitflashChild(strExe, vAuditArgs, NULL, &strAudit); | ||
| 2747 | nFail += Check(nAuditRet == 0 && | ||
| 2748 | FileContainsText(strAudit, "recovery phrase: present"), | ||
| 2749 | "a flagless restart opens the converted SQLite wallet") ? 0 : 1; | ||
| 2750 | } | ||
| 2751 | catch (const std::exception& e) | ||
| 2752 | { | ||
| 2753 | printf(" FAIL exception: %s\n", e.what()); | ||
| 2754 | nFail++; | ||
| 2755 | } | ||
| 2756 | catch (...) | ||
| 2757 | { | ||
| 2758 | printf(" FAIL unknown exception\n"); | ||
| 2759 | nFail++; | ||
| 2760 | } | ||
| 2761 | |||
| 2762 | DBFlush(true); | ||
| 2763 | SetCurrentDir(cwd); | ||
| 2764 | RemoveTree(tmp); | ||
| 2765 | printf("%s (%d failure%s)\n", nFail == 0 ? "ALL TESTS PASSED" : "TESTS FAILED", | ||
| 2766 | nFail, nFail == 1 ? "" : "s"); | ||
| 2767 | fflush(stdout); | ||
| 2768 | return nFail == 0 ? 0 : 1; | ||
| 2769 | } | ||
| 2770 | |||
| 2667 | static int RunWalletPortabilitySelfTest() | 2771 | static int RunWalletPortabilitySelfTest() |
| 2668 | { | 2772 | { |
| 2669 | fflush(stdout); | 2773 | fflush(stdout); |
| @@ -3398,6 +3502,8 @@ int RunSelfTest(const std::string& name) | |||
| 3398 | return RunWalletSQLiteEncryptSelfTest(); | 3502 | return RunWalletSQLiteEncryptSelfTest(); |
| 3399 | if (name == "wallet-backend-default") | 3503 | if (name == "wallet-backend-default") |
| 3400 | return RunWalletBackendDefaultSelfTest(); | 3504 | return RunWalletBackendDefaultSelfTest(); |
| 3505 | if (name == "wallet-convert") | ||
| 3506 | return RunWalletConvertSelfTest(); | ||
| 3401 | if (name == "wallet-portability") | 3507 | if (name == "wallet-portability") |
| 3402 | return RunWalletPortabilitySelfTest(); | 3508 | return RunWalletPortabilitySelfTest(); |
| 3403 | if (name == "net-message") | 3509 | if (name == "net-message") |
| @@ -3414,6 +3520,6 @@ int RunSelfTest(const std::string& name) | |||
| 3414 | return RunManagedTorSelfTest(); | 3520 | return RunManagedTorSelfTest(); |
| 3415 | 3521 | ||
| 3416 | printf("Unknown self-test '%s'\n", name.c_str()); | 3522 | printf("Unknown self-test '%s'\n", name.c_str()); |
| 3417 | printf("Known self-tests: wallet-keypool, wallet-hd, wallet-format, wallet-storage-sanity, db-env-reopen, wallet-sqlite, wallet-sqlite-migration, wallet-crypto, wallet-encrypt, wallet-sqlite-encrypt, wallet-backend-default, wallet-portability, net-message, consensus-limits, pool-stratum, parse-money, socks5-proxy, managed-tor\n"); | 3523 | printf("Known self-tests: wallet-keypool, wallet-hd, wallet-format, wallet-storage-sanity, db-env-reopen, wallet-sqlite, wallet-sqlite-migration, wallet-crypto, wallet-encrypt, wallet-sqlite-encrypt, wallet-backend-default, wallet-convert, wallet-portability, net-message, consensus-limits, pool-stratum, parse-money, socks5-proxy, managed-tor\n"); |
| 3418 | return 1; | 3524 | return 1; |
| 3419 | } | 3525 | } |
diff --git a/src/walletcmd.cpp b/src/walletcmd.cpp index 374b2cc..1f1fd27 100644 --- a/src/walletcmd.cpp +++ b/src/walletcmd.cpp | |||
| @@ -1070,79 +1070,126 @@ public: | |||
| 1070 | 1070 | ||
| 1071 | } // namespace | 1071 | } // namespace |
| 1072 | 1072 | ||
| 1073 | int CmdWalletSQLiteExport(const std::string& strDest) | 1073 | // Export the currently-loaded wallet to a SQLite file. Shared by the |
| 1074 | // -walletsqliteexport command and the GUI conversion wizard: opens the | ||
| 1075 | // destination, copies every record inside one transaction, checkpoints, and | ||
| 1076 | // verifies the stored count matches. Returns false with strErrorRet set on any | ||
| 1077 | // failure and leaves no partial file behind; the source wallet is only read. | ||
| 1078 | bool ExportActiveWalletToSQLite(const std::string& strDest, int& nCopiedRet, | ||
| 1079 | std::string& strErrorRet) | ||
| 1074 | { | 1080 | { |
| 1075 | AttachTerminal(); | 1081 | nCopiedRet = 0; |
| 1076 | |||
| 1077 | if (strDest.empty()) | 1082 | if (strDest.empty()) |
| 1078 | { | 1083 | { |
| 1079 | fprintf(stderr, "Missing SQLite wallet export path.\n"); | 1084 | strErrorRet = "no SQLite export path was given"; |
| 1080 | return 1; | 1085 | return false; |
| 1081 | } | 1086 | } |
| 1082 | if (FileExists(strDest.c_str())) | 1087 | if (FileExists(strDest.c_str())) |
| 1083 | { | 1088 | { |
| 1084 | fprintf(stderr, "Refusing to overwrite existing SQLite wallet export: %s\n", | 1089 | strErrorRet = strprintf("refusing to overwrite existing file: %s", strDest.c_str()); |
| 1085 | strDest.c_str()); | 1090 | return false; |
| 1086 | return 1; | ||
| 1087 | } | 1091 | } |
| 1088 | 1092 | ||
| 1089 | string strError; | ||
| 1090 | CWalletDBSQLite db; | 1093 | CWalletDBSQLite db; |
| 1091 | if (!db.Open(strDest, strError)) | 1094 | if (!db.Open(strDest, strErrorRet)) |
| 1092 | { | 1095 | { |
| 1093 | RemoveSQLiteExportFiles(strDest); | 1096 | RemoveSQLiteExportFiles(strDest); |
| 1094 | fprintf(stderr, "Cannot open SQLite wallet export: %s\n", strError.c_str()); | 1097 | return false; |
| 1095 | return 1; | ||
| 1096 | } | 1098 | } |
| 1097 | if (!db.BeginTransaction(strError)) | 1099 | if (!db.BeginTransaction(strErrorRet)) |
| 1098 | { | 1100 | { |
| 1099 | db.Close(); | 1101 | db.Close(); |
| 1100 | RemoveSQLiteExportFiles(strDest); | 1102 | RemoveSQLiteExportFiles(strDest); |
| 1101 | fprintf(stderr, "Cannot begin SQLite wallet export: %s\n", strError.c_str()); | 1103 | return false; |
| 1102 | return 1; | ||
| 1103 | } | 1104 | } |
| 1104 | 1105 | ||
| 1105 | int nCopied = 0; | 1106 | CWalletSQLiteExportVisitor visitor(db, nCopiedRet); |
| 1106 | CWalletSQLiteExportVisitor visitor(db, nCopied); | 1107 | if (!ScanWalletRecords(visitor, strErrorRet)) |
| 1107 | if (!ScanWalletRecords(visitor, strError)) | ||
| 1108 | { | 1108 | { |
| 1109 | string strRollbackError; | 1109 | string strRollbackError; |
| 1110 | db.RollbackTransaction(strRollbackError); | 1110 | db.RollbackTransaction(strRollbackError); |
| 1111 | db.Close(); | 1111 | db.Close(); |
| 1112 | RemoveSQLiteExportFiles(strDest); | 1112 | RemoveSQLiteExportFiles(strDest); |
| 1113 | fprintf(stderr, "Cannot export wallet.dat to SQLite: %s\n", strError.c_str()); | 1113 | return false; |
| 1114 | return 1; | ||
| 1115 | } | 1114 | } |
| 1116 | if (!db.CommitTransaction(strError)) | 1115 | if (!db.CommitTransaction(strErrorRet) || !db.Checkpoint(strErrorRet)) |
| 1117 | { | 1116 | { |
| 1118 | db.Close(); | 1117 | db.Close(); |
| 1119 | RemoveSQLiteExportFiles(strDest); | 1118 | RemoveSQLiteExportFiles(strDest); |
| 1120 | fprintf(stderr, "Cannot commit SQLite wallet export: %s\n", strError.c_str()); | 1119 | return false; |
| 1121 | return 1; | ||
| 1122 | } | 1120 | } |
| 1123 | if (!db.Checkpoint(strError)) | 1121 | |
| 1122 | int nCount = 0; | ||
| 1123 | if (!db.CountRecords(nCount, strErrorRet)) | ||
| 1124 | { | 1124 | { |
| 1125 | db.Close(); | 1125 | db.Close(); |
| 1126 | RemoveSQLiteExportFiles(strDest); | 1126 | RemoveSQLiteExportFiles(strDest); |
| 1127 | fprintf(stderr, "Cannot checkpoint SQLite wallet export: %s\n", strError.c_str()); | 1127 | return false; |
| 1128 | return 1; | ||
| 1129 | } | 1128 | } |
| 1130 | 1129 | if (nCount != nCopiedRet) | |
| 1131 | int nCount = 0; | ||
| 1132 | if (!db.CountRecords(nCount, strError)) | ||
| 1133 | { | 1130 | { |
| 1131 | strErrorRet = strprintf("export count mismatch: copied %d, stored %d", | ||
| 1132 | nCopiedRet, nCount); | ||
| 1134 | db.Close(); | 1133 | db.Close(); |
| 1135 | RemoveSQLiteExportFiles(strDest); | 1134 | RemoveSQLiteExportFiles(strDest); |
| 1136 | fprintf(stderr, "Cannot verify SQLite wallet export count: %s\n", | 1135 | return false; |
| 1137 | strError.c_str()); | 1136 | } |
| 1137 | db.Close(); | ||
| 1138 | return true; | ||
| 1139 | } | ||
| 1140 | |||
| 1141 | // Convert the running Berkeley DB wallet to SQLite for the GUI wizard: export | ||
| 1142 | // wallet.dat to <datadir>/wallet.sqlite, then record the backend marker so the | ||
| 1143 | // next start opens SQLite. wallet.dat is never modified -- it stays as the | ||
| 1144 | // fallback. Refuses if already on SQLite or if a wallet.sqlite already exists. | ||
| 1145 | bool DoConvertWalletToSQLite(int& nCopiedRet, std::string& strErrorRet) | ||
| 1146 | { | ||
| 1147 | nCopiedRet = 0; | ||
| 1148 | if (WalletSQLiteRuntimeActive()) | ||
| 1149 | { | ||
| 1150 | strErrorRet = "this wallet is already on the SQLite backend"; | ||
| 1151 | return false; | ||
| 1152 | } | ||
| 1153 | string strDest = GetAppDir() + "/wallet.sqlite"; | ||
| 1154 | if (FileExists(strDest.c_str())) | ||
| 1155 | { | ||
| 1156 | strErrorRet = strprintf("a SQLite wallet already exists at %s", strDest.c_str()); | ||
| 1157 | return false; | ||
| 1158 | } | ||
| 1159 | if (!ExportActiveWalletToSQLite(strDest, nCopiedRet, strErrorRet)) | ||
| 1160 | return false; | ||
| 1161 | if (!WriteWalletBackendMarker("sqlite")) | ||
| 1162 | { | ||
| 1163 | strErrorRet = "the wallet was exported but the backend choice could not be saved"; | ||
| 1164 | return false; | ||
| 1165 | } | ||
| 1166 | return true; | ||
| 1167 | } | ||
| 1168 | |||
| 1169 | int CmdWalletSQLiteExport(const std::string& strDest) | ||
| 1170 | { | ||
| 1171 | AttachTerminal(); | ||
| 1172 | |||
| 1173 | // Keep the command's own specific messages for the two conditions the | ||
| 1174 | // shared helper folds into a generic error, so scripts and the tests that | ||
| 1175 | // read this output see the same wording as before. | ||
| 1176 | if (strDest.empty()) | ||
| 1177 | { | ||
| 1178 | fprintf(stderr, "Missing SQLite wallet export path.\n"); | ||
| 1138 | return 1; | 1179 | return 1; |
| 1139 | } | 1180 | } |
| 1140 | if (nCount != nCopied) | 1181 | if (FileExists(strDest.c_str())) |
| 1141 | { | 1182 | { |
| 1142 | db.Close(); | 1183 | fprintf(stderr, "Refusing to overwrite existing SQLite wallet export: %s\n", |
| 1143 | RemoveSQLiteExportFiles(strDest); | 1184 | strDest.c_str()); |
| 1144 | fprintf(stderr, "SQLite wallet export count mismatch: copied %d, stored %d\n", | 1185 | return 1; |
| 1145 | nCopied, nCount); | 1186 | } |
| 1187 | |||
| 1188 | int nCopied = 0; | ||
| 1189 | string strError; | ||
| 1190 | if (!ExportActiveWalletToSQLite(strDest, nCopied, strError)) | ||
| 1191 | { | ||
| 1192 | fprintf(stderr, "Cannot export wallet to SQLite: %s\n", strError.c_str()); | ||
| 1146 | return 1; | 1193 | return 1; |
| 1147 | } | 1194 | } |
| 1148 | 1195 | ||
diff --git a/src/walletcmd.h b/src/walletcmd.h index d993856..38cbfdb 100644 --- a/src/walletcmd.h +++ b/src/walletcmd.h | |||
| @@ -75,6 +75,15 @@ int CmdWalletStorageCheck(); | |||
| 75 | // not change which backend the runtime wallet opens. | 75 | // not change which backend the runtime wallet opens. |
| 76 | int CmdWalletSQLiteExport(const std::string& strDest); | 76 | int CmdWalletSQLiteExport(const std::string& strDest); |
| 77 | 77 | ||
| 78 | // Export the currently-loaded wallet to a SQLite file (shared by the command | ||
| 79 | // above and the GUI wizard). Returns false with an error on any failure. | ||
| 80 | bool ExportActiveWalletToSQLite(const std::string& strDest, int& nCopiedRet, | ||
| 81 | std::string& strErrorRet); | ||
| 82 | |||
| 83 | // Convert the running Berkeley DB wallet to SQLite and record the backend | ||
| 84 | // marker so the next start opens it. wallet.dat is left untouched as fallback. | ||
| 85 | bool DoConvertWalletToSQLite(int& nCopiedRet, std::string& strErrorRet); | ||
| 86 | |||
| 78 | // Diagnostic/migration staging: compare a SQLite wallet export with the current | 87 | // Diagnostic/migration staging: compare a SQLite wallet export with the current |
| 79 | // wallet.dat raw records. Prints counts only, never record keys or values. | 88 | // wallet.dat raw records. Prints counts only, never record keys or values. |
| 80 | int CmdWalletSQLiteVerify(const std::string& strPath); | 89 | int CmdWalletSQLiteVerify(const std::string& strPath); |