From eed1785f701be93ac2464e854c2a7de1f748ef84 Mon Sep 17 00:00:00 2001 From: Gavin Andresen Date: Tue, 18 Sep 2012 14:30:47 -0400 Subject: Handle corrupt wallets gracefully. Corrupt wallets used to cause a DB_RUNRECOVERY uncaught exception and a crash. This commit does three things: 1) Runs a BDB verify early in the startup process, and if there is a low-level problem with the database: + Moves the bad wallet.dat to wallet.timestamp.bak + Runs a 'salvage' operation to get key/value pairs, and writes them to a new wallet.dat + Continues with startup. 2) Much more tolerant of serialization errors. All errors in deserialization are reported by tolerated EXCEPT for errors related to reading keypairs or master key records-- those are reported and then shut down, so the user can get help (or recover from a backup). 3) Adds a new -salvagewallet option, which: + Moves the wallet.dat to wallet.timestamp.bak + extracts ONLY keypairs and master keys into a new wallet.dat + soft-sets -rescan, to recreate transaction history This was tested by randomly corrupting testnet wallets using a little python script I wrote (https://gist.github.com/3812689) --- src/db.h | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) (limited to 'src/db.h') diff --git a/src/db.h b/src/db.h index 4a08bf10a1..798913645f 100644 --- a/src/db.h +++ b/src/db.h @@ -50,6 +50,25 @@ public: ~CDBEnv(); void MakeMock(); bool IsMock() { return fMockDb; }; + + /* + * Verify that database file strFile is OK. If it is not, + * call the callback to try to recover. + * This must be called BEFORE strFile is opened. + * Returns true if strFile is OK. + */ + enum VerifyResult { VERIFY_OK, RECOVER_OK, RECOVER_FAIL }; + VerifyResult Verify(std::string strFile, bool (*recoverFunc)(CDBEnv& dbenv, std::string strFile)); + /* + * Salvage data from a file that Verify says is bad. + * fAggressive sets the DB_AGGRESSIVE flag (see berkeley DB->verify() method documentation). + * Appends binary key/value pairs to vResult, returns true if successful. + * NOTE: reads the entire database into memory, so cannot be used + * for huge databases. + */ + typedef std::pair, std::vector > KeyValPair; + bool Salvage(std::string strFile, bool fAggressive, std::vector& vResult); + bool Open(boost::filesystem::path pathEnv_); void Close(); void Flush(bool fShutdown); @@ -58,6 +77,7 @@ public: bool GetDetach() { return fDetachDB; } void CloseDb(const std::string& strFile); + bool RemoveDb(const std::string& strFile); DbTxn *TxnBegin(int flags=DB_TXN_WRITE_NOSYNC) { -- cgit v1.2.3