diff --git a/src/wallet/db.h b/src/wallet/db.h --- a/src/wallet/db.h +++ b/src/wallet/db.h @@ -109,7 +109,9 @@ protected: template bool Read(const K &key, T &value) { - if (!pdb) return false; + if (!pdb) { + return false; + } // Key CDataStream ssKey(SER_DISK, CLIENT_VERSION); @@ -122,7 +124,9 @@ datValue.set_flags(DB_DBT_MALLOC); int ret = pdb->get(activeTxn, &datKey, &datValue, 0); memset(datKey.get_data(), 0, datKey.get_size()); - if (datValue.get_data() == nullptr) return false; + if (datValue.get_data() == nullptr) { + return false; + } // Unserialize value try { @@ -143,8 +147,12 @@ template bool Write(const K &key, const T &value, bool fOverwrite = true) { - if (!pdb) return false; - if (fReadOnly) assert(!"Write called on database in read-only mode"); + if (!pdb) { + return false; + } + if (fReadOnly) { + assert(!"Write called on database in read-only mode"); + } // Key CDataStream ssKey(SER_DISK, CLIENT_VERSION); @@ -169,8 +177,12 @@ } template bool Erase(const K &key) { - if (!pdb) return false; - if (fReadOnly) assert(!"Erase called on database in read-only mode"); + if (!pdb) { + return false; + } + if (fReadOnly) { + assert(!"Erase called on database in read-only mode"); + } // Key CDataStream ssKey(SER_DISK, CLIENT_VERSION); @@ -187,7 +199,9 @@ } template bool Exists(const K &key) { - if (!pdb) return false; + if (!pdb) { + return false; + } // Key CDataStream ssKey(SER_DISK, CLIENT_VERSION); @@ -204,10 +218,14 @@ } Dbc *GetCursor() { - if (!pdb) return nullptr; + if (!pdb) { + return nullptr; + } Dbc *pcursor = nullptr; int ret = pdb->cursor(nullptr, &pcursor, 0); - if (ret != 0) return nullptr; + if (ret != 0) { + return nullptr; + } return pcursor; } @@ -250,22 +268,30 @@ public: bool TxnBegin() { - if (!pdb || activeTxn) return false; + if (!pdb || activeTxn) { + return false; + } DbTxn *ptxn = bitdb.TxnBegin(); - if (!ptxn) return false; + if (!ptxn) { + return false; + } activeTxn = ptxn; return true; } bool TxnCommit() { - if (!pdb || !activeTxn) return false; + if (!pdb || !activeTxn) { + return false; + } int ret = activeTxn->commit(0); activeTxn = nullptr; return (ret == 0); } bool TxnAbort() { - if (!pdb || !activeTxn) return false; + if (!pdb || !activeTxn) { + return false; + } int ret = activeTxn->abort(); activeTxn = nullptr; return (ret == 0); diff --git a/src/wallet/db.cpp b/src/wallet/db.cpp --- a/src/wallet/db.cpp +++ b/src/wallet/db.cpp @@ -173,7 +173,9 @@ assert(mapFileUseCount.count(strFile) == 0); u_int32_t flags = DB_SALVAGE; - if (fAggressive) flags |= DB_AGGRESSIVE; + if (fAggressive) { + flags |= DB_AGGRESSIVE; + } std::stringstream strDump; @@ -212,7 +214,9 @@ while (!strDump.eof() && keyHex != DATA_END) { getline(strDump, keyHex); if (keyHex != DATA_END) { - if (strDump.eof()) break; + if (strDump.eof()) { + break; + } getline(strDump, valueHex); if (valueHex == DATA_END) { LogPrintf("CDBEnv::Salvage: WARNING: Number of keys in data " @@ -234,7 +238,9 @@ void CDBEnv::CheckpointLSN(const std::string &strFile) { dbenv->txn_checkpoint(0, 0, 0); - if (fMockDb) return; + if (fMockDb) { + return; + } dbenv->lsn_reset(strFile.c_str(), 0); } @@ -309,11 +315,15 @@ } void CDB::Flush() { - if (activeTxn) return; + if (activeTxn) { + return; + } // Flush database activity from memory pool to disk log unsigned int nMinutes = 0; - if (fReadOnly) nMinutes = 1; + if (fReadOnly) { + nMinutes = 1; + } bitdb.dbenv->txn_checkpoint( nMinutes ? GetArg("-dblogsize", DEFAULT_WALLET_DBLOGSIZE) * 1024 : 0, @@ -321,17 +331,21 @@ } void CDB::Close() { - if (!pdb) return; - if (activeTxn) activeTxn->abort(); + if (!pdb) { + return; + } + if (activeTxn) { + activeTxn->abort(); + } activeTxn = nullptr; pdb = nullptr; - if (fFlushOnClose) Flush(); - - { - LOCK(bitdb.cs_db); - --bitdb.mapFileUseCount[strFile]; + if (fFlushOnClose) { + Flush(); } + + LOCK(bitdb.cs_db); + --bitdb.mapFileUseCount[strFile]; } void CDBEnv::CloseDb(const std::string &strFile) { @@ -393,7 +407,8 @@ if (ret1 == DB_NOTFOUND) { pcursor->close(); break; - } else if (ret1 != 0) { + } + if (ret1 != 0) { pcursor->close(); fSuccess = false; break; @@ -401,8 +416,9 @@ if (pszSkip && strncmp(ssKey.data(), pszSkip, std::min(ssKey.size(), - strlen(pszSkip))) == 0) + strlen(pszSkip))) == 0) { continue; + } if (strncmp(ssKey.data(), "\x07version", 8) == 0) { // Update version: ssValue.clear(); @@ -412,28 +428,35 @@ Dbt datValue(ssValue.data(), ssValue.size()); int ret2 = pdbCopy->put(nullptr, &datKey, &datValue, DB_NOOVERWRITE); - if (ret2 > 0) fSuccess = false; + if (ret2 > 0) { + fSuccess = false; + } } if (fSuccess) { db.Close(); bitdb.CloseDb(strFile); - if (pdbCopy->close(0)) fSuccess = false; + if (pdbCopy->close(0)) { + fSuccess = false; + } delete pdbCopy; } } if (fSuccess) { Db dbA(bitdb.dbenv, 0); - if (dbA.remove(strFile.c_str(), nullptr, 0)) + if (dbA.remove(strFile.c_str(), nullptr, 0)) { fSuccess = false; + } Db dbB(bitdb.dbenv, 0); if (dbB.rename(strFileRes.c_str(), nullptr, strFile.c_str(), - 0)) + 0)) { fSuccess = false; + } } - if (!fSuccess) + if (!fSuccess) { LogPrintf( "CDB::Rewrite: Failed to rewrite database file %s\n", strFileRes); + } return fSuccess; } } @@ -466,11 +489,14 @@ LogPrint(BCLog::DB, "CDBEnv::Flush: %s checkpoint\n", strFile); dbenv->txn_checkpoint(0, 0, 0); LogPrint(BCLog::DB, "CDBEnv::Flush: %s detach\n", strFile); - if (!fMockDb) dbenv->lsn_reset(strFile.c_str(), 0); + if (!fMockDb) { + dbenv->lsn_reset(strFile.c_str(), 0); + } LogPrint(BCLog::DB, "CDBEnv::Flush: %s closed\n", strFile); mapFileUseCount.erase(mi++); - } else + } else { mi++; + } } LogPrint(BCLog::DB, "CDBEnv::Flush: Flush(%s)%s took %15dms\n", fShutdown ? "true" : "false", diff --git a/src/wallet/walletdb.cpp b/src/wallet/walletdb.cpp --- a/src/wallet/walletdb.cpp +++ b/src/wallet/walletdb.cpp @@ -89,8 +89,9 @@ nWalletDBUpdateCounter++; if (!Write(std::make_pair(std::string("keymeta"), vchPubKey), keyMeta, - false)) + false)) { return false; + } // hash pubkey/privkey to accelerate wallet load std::vector vchKey; @@ -109,12 +110,15 @@ const bool fEraseUnencryptedKey = true; nWalletDBUpdateCounter++; - if (!Write(std::make_pair(std::string("keymeta"), vchPubKey), keyMeta)) + if (!Write(std::make_pair(std::string("keymeta"), vchPubKey), keyMeta)) { return false; + } if (!Write(std::make_pair(std::string("ckey"), vchPubKey), vchCryptedSecret, - false)) + false)) { return false; + } + if (fEraseUnencryptedKey) { Erase(std::make_pair(std::string("key"), vchPubKey)); Erase(std::make_pair(std::string("wkey"), vchPubKey)); @@ -138,8 +142,10 @@ nWalletDBUpdateCounter++; if (!Write(std::make_pair(std::string("watchmeta"), *(const CScriptBase *)(&dest)), - keyMeta)) + keyMeta)) { return false; + } + return Write( std::make_pair(std::string("watchs"), *(const CScriptBase *)(&dest)), '1'); @@ -148,8 +154,10 @@ bool CWalletDB::EraseWatchOnly(const CScript &dest) { nWalletDBUpdateCounter++; if (!Erase(std::make_pair(std::string("watchmeta"), - *(const CScriptBase *)(&dest)))) + *(const CScriptBase *)(&dest)))) { return false; + } + return Erase( std::make_pair(std::string("watchs"), *(const CScriptBase *)(&dest))); } @@ -163,8 +171,10 @@ } bool CWalletDB::ReadBestBlock(CBlockLocator &locator) { - if (Read(std::string("bestblock"), locator) && !locator.vHave.empty()) + if (Read(std::string("bestblock"), locator) && !locator.vHave.empty()) { return true; + } + return Read(std::string("bestblock_nomerkle"), locator); } @@ -235,9 +245,11 @@ bool fAllAccounts = (strAccount == "*"); Dbc *pcursor = GetCursor(); - if (!pcursor) + if (!pcursor) { throw std::runtime_error(std::string(__func__) + ": cannot create DB cursor"); + } + bool setRange = true; while (true) { // Read next record @@ -250,9 +262,11 @@ CDataStream ssValue(SER_DISK, CLIENT_VERSION); int ret = ReadAtCursor(pcursor, ssKey, ssValue, setRange); setRange = false; - if (ret == DB_NOTFOUND) + if (ret == DB_NOTFOUND) { break; - else if (ret != 0) { + } + + if (ret != 0) { pcursor->close(); throw std::runtime_error(std::string(__func__) + ": error scanning DB"); @@ -261,10 +275,14 @@ // Unserialize std::string strType; ssKey >> strType; - if (strType != "acentry") break; + if (strType != "acentry") { + break; + } CAccountingEntry acentry; ssKey >> acentry.strAccount; - if (!fAllAccounts && acentry.strAccount != strAccount) break; + if (!fAllAccounts && acentry.strAccount != strAccount) { + break; + } ssValue >> acentry; ssKey >> acentry.nEntryNo; @@ -320,7 +338,9 @@ bool isValid = wtx.IsCoinBase() ? CheckCoinbase(wtx, state) : CheckRegularTransaction(wtx, state); - if (wtx.GetId() != hash || !isValid) return false; + if (wtx.GetId() != hash || !isValid) { + return false; + } // Undo serialize changes in 31600 if (31404 <= wtx.fTimeReceivedIsTxTime && @@ -343,7 +363,9 @@ wss.vWalletUpgrade.push_back(hash); } - if (wtx.nOrderPos == -1) wss.fAnyUnordered = true; + if (wtx.nOrderPos == -1) { + wss.fAnyUnordered = true; + } pwallet->LoadToWallet(wtx); } else if (strType == "acentry") { @@ -351,13 +373,16 @@ ssKey >> strAccount; uint64_t nNumber; ssKey >> nNumber; - if (nNumber > nAccountingEntryNumber) + if (nNumber > nAccountingEntryNumber) { nAccountingEntryNumber = nNumber; + } if (!wss.fAnyUnordered) { CAccountingEntry acentry; ssValue >> acentry; - if (acentry.nOrderPos == -1) wss.fAnyUnordered = true; + if (acentry.nOrderPos == -1) { + wss.fAnyUnordered = true; + } } } else if (strType == "watchs") { wss.nWatchKeys++; @@ -365,7 +390,9 @@ ssKey >> *(CScriptBase *)(&script); char fYes; ssValue >> fYes; - if (fYes == '1') pwallet->LoadWatchOnly(script); + if (fYes == '1') { + pwallet->LoadWatchOnly(script); + } } else if (strType == "key" || strType == "wkey") { CPubKey vchPubKey; ssKey >> vchPubKey; @@ -435,7 +462,9 @@ return false; } pwallet->mapMasterKeys[nID] = kMasterKey; - if (pwallet->nMasterKeyMaxID < nID) pwallet->nMasterKeyMaxID = nID; + if (pwallet->nMasterKeyMaxID < nID) { + pwallet->nMasterKeyMaxID = nID; + } } else if (strType == "ckey") { CPubKey vchPubKey; ssKey >> vchPubKey; @@ -480,7 +509,9 @@ pwallet->LoadKeyPool(nIndex, keypool); } else if (strType == "version") { ssValue >> wss.nFileVersion; - if (wss.nFileVersion == 10300) wss.nFileVersion = 300; + if (wss.nFileVersion == 10300) { + wss.nFileVersion = 300; + } } else if (strType == "cscript") { uint160 hash; ssKey >> hash; @@ -531,7 +562,9 @@ try { int nMinVersion = 0; if (Read((std::string) "minversion", nMinVersion)) { - if (nMinVersion > CLIENT_VERSION) return DB_TOO_NEW; + if (nMinVersion > CLIENT_VERSION) { + return DB_TOO_NEW; + } pwallet->LoadMinVersion(nMinVersion); } @@ -547,9 +580,11 @@ CDataStream ssKey(SER_DISK, CLIENT_VERSION); CDataStream ssValue(SER_DISK, CLIENT_VERSION); int ret = ReadAtCursor(pcursor, ssKey, ssValue); - if (ret == DB_NOTFOUND) + if (ret == DB_NOTFOUND) { break; - else if (ret != 0) { + } + + if (ret != 0) { LogPrintf("Error reading next record from wallet database\n"); return DB_CORRUPT; } @@ -559,19 +594,22 @@ if (!ReadKeyValue(pwallet, ssKey, ssValue, wss, strType, strErr)) { // losing keys is considered a catastrophic error, anything else // we assume the user can live with: - if (IsKeyType(strType)) + if (IsKeyType(strType)) { result = DB_CORRUPT; - else { + } else { // Leave other errors alone, if we try to fix them we might // make things worse. But do warn the user there is // something wrong. fNoncriticalErrors = true; - if (strType == "tx") + if (strType == "tx") { // Rescan if there is a bad transaction record: SoftSetBoolArg("-rescan", true); + } } } - if (!strErr.empty()) LogPrintf("%s\n", strErr); + if (!strErr.empty()) { + LogPrintf("%s\n", strErr); + } } pcursor->close(); } catch (const boost::thread_interrupted &) { @@ -580,12 +618,15 @@ result = DB_CORRUPT; } - if (fNoncriticalErrors && result == DB_LOAD_OK) + if (fNoncriticalErrors && result == DB_LOAD_OK) { result = DB_NONCRITICAL_ERROR; + } // Any wallet corruption at all: skip any rewriting or upgrading, we don't // want to make it worse. - if (result != DB_LOAD_OK) return result; + if (result != DB_LOAD_OK) { + return result; + } LogPrintf("nFileVersion = %d\n", wss.nFileVersion); @@ -593,8 +634,9 @@ wss.nKeys, wss.nCKeys, wss.nKeyMeta, wss.nKeys + wss.nCKeys); // nTimeFirstKey is only reliable if all keys have metadata - if ((wss.nKeys + wss.nCKeys + wss.nWatchKeys) != wss.nKeyMeta) + if ((wss.nKeys + wss.nCKeys + wss.nWatchKeys) != wss.nKeyMeta) { pwallet->UpdateTimeFirstKey(1); + } for (uint256 hash : wss.vWalletUpgrade) { WriteTx(pwallet->mapWallet[hash]); @@ -602,13 +644,18 @@ // Rewrite encrypted wallets of versions 0.4.0 and 0.5.0rc: if (wss.fIsEncrypted && - (wss.nFileVersion == 40000 || wss.nFileVersion == 50000)) + (wss.nFileVersion == 40000 || wss.nFileVersion == 50000)) { return DB_NEED_REWRITE; + } - if (wss.nFileVersion < CLIENT_VERSION) // Update + if (wss.nFileVersion < CLIENT_VERSION) { + // Update WriteVersion(CLIENT_VERSION); + } - if (wss.fAnyUnordered) result = pwallet->ReorderTransactions(); + if (wss.fAnyUnordered) { + result = pwallet->ReorderTransactions(); + } pwallet->laccentries.clear(); ListAccountCreditDebit("*", pwallet->laccentries); @@ -630,8 +677,10 @@ try { LOCK(pwallet->cs_wallet); int nMinVersion = 0; - if (Read((std::string) "minversion", nMinVersion)) { - if (nMinVersion > CLIENT_VERSION) return DB_TOO_NEW; + if (Read(std::string("minversion"), nMinVersion)) { + if (nMinVersion > CLIENT_VERSION) { + return DB_TOO_NEW; + } pwallet->LoadMinVersion(nMinVersion); } @@ -647,9 +696,11 @@ CDataStream ssKey(SER_DISK, CLIENT_VERSION); CDataStream ssValue(SER_DISK, CLIENT_VERSION); int ret = ReadAtCursor(pcursor, ssKey, ssValue); - if (ret == DB_NOTFOUND) + if (ret == DB_NOTFOUND) { break; - else if (ret != 0) { + } + + if (ret != 0) { LogPrintf("Error reading next record from wallet database\n"); return DB_CORRUPT; } @@ -674,8 +725,9 @@ result = DB_CORRUPT; } - if (fNoncriticalErrors && result == DB_LOAD_OK) + if (fNoncriticalErrors && result == DB_LOAD_OK) { result = DB_NONCRITICAL_ERROR; + } return result; } @@ -703,7 +755,9 @@ } if (it == vTxHashIn.end()) { break; - } else if ((*it) == hash) { + } + + if ((*it) == hash) { pwallet->mapWallet.erase(hash); if (!EraseTx(hash)) { LogPrint(BCLog::DB, "Transaction was found for deletion but " @@ -745,9 +799,14 @@ RenameThread("bitcoin-wallet"); static bool fOneThread; - if (fOneThread) return; + if (fOneThread) { + return; + } + fOneThread = true; - if (!GetBoolArg("-flushwallet", DEFAULT_FLUSHWALLET)) return; + if (!GetBoolArg("-flushwallet", DEFAULT_FLUSHWALLET)) { + return; + } unsigned int nLastSeen = CWalletDB::GetUpdateCounter(); unsigned int nLastFlushed = CWalletDB::GetUpdateCounter(); @@ -812,9 +871,9 @@ int result = dbenv.dbenv->dbrename(nullptr, filename.c_str(), nullptr, newFilename.c_str(), DB_AUTO_COMMIT); - if (result == 0) + if (result == 0) { LogPrintf("Renamed %s to %s\n", filename, newFilename); - else { + } else { LogPrintf("Failed to rename %s to %s\n", filename, newFilename); return false; } @@ -854,7 +913,9 @@ fReadOK = ReadKeyValue(&dummyWallet, ssKey, ssValue, wss, strType, strErr); } - if (!IsKeyType(strType) && strType != "hdchain") continue; + if (!IsKeyType(strType) && strType != "hdchain") { + continue; + } if (!fReadOK) { LogPrintf("WARNING: CWalletDB::Recover skipping %s: %s\n", strType, strErr); @@ -864,7 +925,9 @@ Dbt datKey(&row.first[0], row.first.size()); Dbt datValue(&row.second[0], row.second.size()); int ret2 = pdbCopy->put(ptxn, &datKey, &datValue, DB_NOOVERWRITE); - if (ret2 > 0) fSuccess = false; + if (ret2 > 0) { + fSuccess = false; + } } ptxn->commit(0); pdbCopy->close(0);