From 99f7c1dfebb1a15dc6f77a338a8b0aaa3962564b Mon Sep 17 00:00:00 2001 From: Bitflash Date: Thu, 30 Jul 2026 11:27:49 -0300 Subject: wallet: make wallet.dat a file you can actually back up (#46) * wallet: make wallet.dat a file you can actually back up Issue #40: a wallet.dat copied out on its own will not open anywhere else. The file is intact -- same bytes, same keys -- and the node refuses it with "Db::put: Invalid argument", thrown from a path that aborted the process. The reporter believes they lost funds to this, having saved the one file anybody would think to save. The chain, from the bottom: _endthread() is pthread_exit() on POSIX, and glibc implements that by throwing abi::__forced_unwind through the stack. That is not a real exception; catching it without rethrowing is a fatal error by contract. CATCH_PRINT_EXCEPTION ended in a bare catch(...), so every Linux shutdown ended: UNKNOWN EXCEPTION bitflash-node in ThreadSocketHandler() FATAL: exception not rethrown An abort, mid-shutdown, so nothing after StopNode() ran. Which mattered because Berkeley DB stamps every page with a log sequence number belonging to the environment that wrote it. Carried elsewhere, those numbers point at logs that do not exist and BDB refuses to write. DBFlush(true) calls lsn_reset, which is precisely the cure -- and DBFlush has been declared since 2009 and called from nowhere. Even had it been called, the abort stood in front of it. So the wallet was never released from its directory, and the one file users copy was the one file that could not travel. Three fixes, each covering a different case: - Rethrow abi::__forced_unwind, and call DBFlush(true) on both shutdown paths. Shutdown completes, and wallet.dat is left portable by construction. - Adopt orphaned files on open: a freshly created database environment beside pre-existing .dat files means they came from elsewhere, so reset their sequence numbers. This is what recovers wallets already stranded by older builds -- including, if they still hold the file, the reporter's. - BackupWallet(), reachable as /backupwallet=FILE, which checkpoints, copies, and clears the copy's sequence numbers. Bitcoin 0.1.0 had a backup command; this tree lost it in the wxWidgets removal and has shipped with no way to take a backup since. DBFlush's own txn_checkpoint and lsn_reset are wrapped too -- they throw, and throwing on the way out is how this started. Verified on Linux and Windows: shutdown exits 0 rather than 134, DBFlush runs, and a lone wallet.dat opens in an empty directory with no exceptions and byte-identical key records (defaultkey, key, name, setting, version) before and after. Not addressed here, and the larger half of #40: keys are generated as they are needed rather than derived from a seed, so any backup is a snapshot and coins paid to addresses created after it are not in it. /backupwallet says so on every run. The real answer is deterministic derivation, which is a wallet format change and its own piece of work. * README: document what a wallet backup actually requires The PR description claimed this section existed and it did not. Covers the two things that cost money and are not obvious: a lone wallet.dat is refused elsewhere because Berkeley DB ties it to the database/ directory beside it, and every backup is a snapshot because keys are made on demand rather than derived from a seed. --- src/main_gui.cpp | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) (limited to 'src/main_gui.cpp') diff --git a/src/main_gui.cpp b/src/main_gui.cpp index c64dca9..976cd42 100644 --- a/src/main_gui.cpp +++ b/src/main_gui.cpp @@ -67,6 +67,10 @@ static void PrintUsage() printf(" /port=N (P2P listen port, default 8433)\n"); printf(" /btfseed=ADDRESS:ENCHEX (extra bootstrap peer, repeatable)\n"); printf("\n"); + printf("Wallet:\n"); + printf(" /backupwallet=FILE (write a wallet.dat that opens on its own,\n"); + printf(" then exit; run it again after new addresses)\n"); + printf("\n"); printf("Each option also accepts '-' instead of '/'.\n"); } @@ -219,6 +223,28 @@ int main(int argc, char* argv[]) return 1; } printf("Height=%d\n", nBestHeight); + + // Before the node opens sockets or touches anything: the wallet is loaded, + // which is all a backup needs, and finishing here means the copy is taken + // from a quiet directory rather than from under a running node. + string strBackup = argval2(argc, argv, "/backupwallet", "-backupwallet"); + if (!strBackup.empty()) + { + bool fOk = BackupWallet(strBackup); + if (fOk) + { + printf("Backed up to %s\n", strBackup.c_str()); + printf("This file opens on its own -- it does not need the database/ " + "directory beside it.\n"); + printf("It is a snapshot: coins paid to addresses created after now are " + "not in it, so back up again whenever you receive to a new address.\n"); + } + else + fprintf(stderr, "Backup failed -- nothing was written.\n"); + DBFlush(true); + return fOk ? 0 : 1; + } + ReacceptWalletTransactions(); if (!StartNode(strErrors)) { fprintf(stderr,"StartNode: %s\n",strErrors.c_str()); return 1; } @@ -249,6 +275,11 @@ int main(int argc, char* argv[]) printf("Running headless. Ctrl-C to stop.\n"); while (!fShutdown) Sleep(500); StopNode(); + // Checkpoints, releases wallet.dat from this directory's Berkeley DB + // environment, and closes it. Declared since 2009 and never once + // called here, which is why a wallet.dat copied elsewhere would not + // open -- issue #40. Not optional. + DBFlush(true); return 0; } @@ -258,6 +289,7 @@ int main(int argc, char* argv[]) int ret = RunGUI(argc, argv); fShutdown = true; StopNode(); + DBFlush(true); return ret; #endif } -- cgit v1.2.3