Skip to content

Commit

Permalink
[Cleanup] Fix formatting in wallet and walletdb
Browse files Browse the repository at this point in the history
  • Loading branch information
random-zebra committed May 4, 2021
1 parent d78c43a commit 97af1a1
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 53 deletions.
24 changes: 16 additions & 8 deletions src/wallet/wallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2120,8 +2120,9 @@ bool CWallet::Verify()
}

std::string strError;
if (!CWalletDB::VerifyEnvironment(walletFile, GetDataDir().string(), strError))
if (!CWalletDB::VerifyEnvironment(walletFile, GetDataDir().string(), strError)) {
return UIError(strError);
}

if (gArgs.GetBoolArg("-salvagewallet", false)) {
// Recover readable keypairs:
Expand All @@ -2132,14 +2133,16 @@ bool CWallet::Verify()
// tx status. If lock can't be taken, tx confirmation status may be not
// reliable.
LOCK(cs_main);
if (!CWalletDB::Recover(walletFile, (void *)&dummyWallet, CWalletDB::RecoverKeysOnlyFilter, backup_filename))
if (!CWalletDB::Recover(walletFile, (void *)&dummyWallet, CWalletDB::RecoverKeysOnlyFilter, backup_filename)) {
return false;
}
}

std::string strWarning;
bool dbV = CWalletDB::VerifyDatabaseFile(walletFile, GetDataDir().string(), strWarning, strError);
if (!strWarning.empty())
if (!strWarning.empty()) {
UIWarning(strWarning);
}
if (!dbV) {
return UIError(strError);
}
Expand All @@ -2152,16 +2155,19 @@ void CWallet::ResendWalletTransactions(CConnman* connman)
{
// Do this infrequently and randomly to avoid giving away
// that these are our transactions.
if (GetTime() < nNextResend)
if (GetTime() < nNextResend) {
return;
}
bool fFirst = (nNextResend == 0);
nNextResend = GetTime() + GetRand(30 * 60);
if (fFirst)
if (fFirst) {
return;
}

// Only do it if there's been a new block since last time
if (nTimeBestReceived < nLastResend)
if (nTimeBestReceived < nLastResend) {
return;
}
nLastResend = GetTime();

// Rebroadcast any of our txes that aren't in a block yet
Expand All @@ -2174,8 +2180,9 @@ void CWallet::ResendWalletTransactions(CConnman* connman)
CWalletTx& wtx = item.second;
// Don't rebroadcast until it's had plenty of time that
// it should have gotten in already by now.
if (nTimeBestReceived - (int64_t)wtx.nTimeReceived > 5 * 60)
if (nTimeBestReceived - (int64_t)wtx.nTimeReceived > 5 * 60) {
mapSorted.emplace(wtx.nTimeReceived, &wtx);
}
}
for (std::pair<const unsigned int, CWalletTx*> & item : mapSorted) {
CWalletTx& wtx = *item.second;
Expand Down Expand Up @@ -4250,8 +4257,9 @@ CWallet* CWallet::CreateWalletFromFile(const std::string walletFile)
LogPrintf("Performing wallet upgrade to %i\n", FEATURE_LATEST);
nMaxVersion = FEATURE_LATEST;
walletInstance->SetMinVersion(FEATURE_LATEST); // permanently upgrade the wallet immediately
} else
} else {
LogPrintf("Allowing wallet upgrade up to %i\n", nMaxVersion);
}
if (nMaxVersion < walletInstance->GetVersion()) {
UIError("Cannot downgrade wallet\n");
return nullptr;
Expand Down
91 changes: 51 additions & 40 deletions src/wallet/walletdb.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) 2009-2010 Satoshi Nakamoto
// Copyright (c) 2009-2014 The Bitcoin developers
// Copyright (c) 2009-2021 The Bitcoin developers
// Copyright (c) 2014-2015 The Dash developers
// Copyright (c) 2015-2020 The PIVX developers
// Copyright (c) 2015-2021 The PIVX developers
// Distributed under the MIT/X11 software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.

Expand Down Expand Up @@ -100,8 +100,9 @@ bool CWalletDB::EraseTx(uint256 hash)

bool CWalletDB::WriteKey(const CPubKey& vchPubKey, const CPrivKey& vchPrivKey, const CKeyMetadata& keyMeta)
{
if (!WriteIC(std::make_pair(std::string(DBKeys::KEYMETA), vchPubKey), keyMeta, false))
if (!WriteIC(std::make_pair(std::string(DBKeys::KEYMETA), vchPubKey), keyMeta, false)) {
return false;
}

// hash pubkey/privkey to accelerate wallet load
std::vector<unsigned char> vchKey;
Expand All @@ -113,16 +114,18 @@ bool CWalletDB::WriteKey(const CPubKey& vchPubKey, const CPrivKey& vchPrivKey, c
}

bool CWalletDB::WriteCryptedKey(const CPubKey& vchPubKey,
const std::vector<unsigned char>& vchCryptedSecret,
const CKeyMetadata& keyMeta)
const std::vector<unsigned char>& vchCryptedSecret,
const CKeyMetadata& keyMeta)
{
const bool fEraseUnencryptedKey = true;

if (!WriteIC(std::make_pair(std::string(DBKeys::KEYMETA), vchPubKey), keyMeta))
if (!WriteIC(std::make_pair(std::string(DBKeys::KEYMETA), vchPubKey), keyMeta)) {
return false;
}

if (!WriteIC(std::make_pair(std::string(DBKeys::CRYPTED_KEY), vchPubKey), vchCryptedSecret, false))
if (!WriteIC(std::make_pair(std::string(DBKeys::CRYPTED_KEY), vchPubKey), vchCryptedSecret, false)) {
return false;
}
if (fEraseUnencryptedKey) {
EraseIC(std::make_pair(std::string(DBKeys::KEY), vchPubKey));
}
Expand All @@ -134,32 +137,34 @@ bool CWalletDB::WriteSaplingZKey(const libzcash::SaplingIncomingViewingKey &ivk,
const libzcash::SaplingExtendedSpendingKey &key,
const CKeyMetadata &keyMeta)
{
if (!WriteIC(std::make_pair(std::string(DBKeys::SAP_KEYMETA), ivk), keyMeta))
if (!WriteIC(std::make_pair(std::string(DBKeys::SAP_KEYMETA), ivk), keyMeta)) {
return false;
}

return WriteIC(std::make_pair(std::string(DBKeys::SAP_KEY), ivk), key, false);
}

bool CWalletDB::WriteSaplingPaymentAddress(
const libzcash::SaplingPaymentAddress &addr,
const libzcash::SaplingIncomingViewingKey &ivk)
bool CWalletDB::WriteSaplingPaymentAddress(const libzcash::SaplingPaymentAddress &addr,
const libzcash::SaplingIncomingViewingKey &ivk)
{
return WriteIC(std::make_pair(std::string(DBKeys::SAP_ADDR), addr), ivk, false);
}

bool CWalletDB::WriteCryptedSaplingZKey(
const libzcash::SaplingExtendedFullViewingKey &extfvk,
const std::vector<unsigned char>& vchCryptedSecret,
const CKeyMetadata &keyMeta)
bool CWalletDB::WriteCryptedSaplingZKey(const libzcash::SaplingExtendedFullViewingKey &extfvk,
const std::vector<unsigned char>& vchCryptedSecret,
const CKeyMetadata &keyMeta)
{
const bool fEraseUnencryptedKey = true;
auto ivk = extfvk.fvk.in_viewing_key();

if (!WriteIC(std::make_pair(std::string(DBKeys::SAP_KEYMETA), ivk), keyMeta))
if (!WriteIC(std::make_pair(std::string(DBKeys::SAP_KEYMETA), ivk), keyMeta)) {
return false;
}

if (!WriteIC(std::make_pair(std::string(DBKeys::SAP_KEY_CRIPTED), ivk), std::make_pair(extfvk, vchCryptedSecret), false))
if (!WriteIC(std::make_pair(std::string(DBKeys::SAP_KEY_CRIPTED), ivk),
std::make_pair(extfvk, vchCryptedSecret), false)) {
return false;
}

if (fEraseUnencryptedKey) {
EraseIC(std::make_pair(std::string(DBKeys::SAP_KEY), ivk));
Expand Down Expand Up @@ -210,7 +215,9 @@ bool CWalletDB::WriteBestBlock(const CBlockLocator& locator)

bool CWalletDB::ReadBestBlock(CBlockLocator& locator)
{
if (batch.Read(std::string(DBKeys::BESTBLOCK), locator) && !locator.vHave.empty()) return true;
if (batch.Read(std::string(DBKeys::BESTBLOCK), locator) && !locator.vHave.empty()) {
return true;
}
return batch.Read(std::string(DBKeys::BESTBLOCK_NOMERKLE), locator);
}

Expand Down Expand Up @@ -279,7 +286,7 @@ DBErrors CWalletDB::ReorderTransactions(CWallet* pwallet)
typedef std::multimap<int64_t, CWalletTx*> TxItems;
TxItems txByTime;

for (std::map<uint256, CWalletTx>::iterator it = pwallet->mapWallet.begin(); it != pwallet->mapWallet.end(); ++it) {
for (auto it = pwallet->mapWallet.begin(); it != pwallet->mapWallet.end(); ++it) {
CWalletTx* wtx = &((*it).second);
txByTime.insert(std::make_pair(wtx->nTimeReceived, wtx));
}
Expand All @@ -295,8 +302,8 @@ DBErrors CWalletDB::ReorderTransactions(CWallet* pwallet)
nOrderPos = nOrderPosNext++;
nOrderPosOffsets.push_back(nOrderPos);

if (!WriteTx(*pwtx))
return DB_LOAD_FAIL;
if (!WriteTx(*pwtx)) return DB_LOAD_FAIL;

} else {
int64_t nOrderPosOff = 0;
for (const int64_t& nOffsetStart : nOrderPosOffsets) {
Expand All @@ -306,12 +313,10 @@ DBErrors CWalletDB::ReorderTransactions(CWallet* pwallet)
nOrderPos += nOrderPosOff;
nOrderPosNext = std::max(nOrderPosNext, nOrderPos + 1);

if (!nOrderPosOff)
continue;
if (!nOrderPosOff) continue;

// Since we're changing the order, write it back
if (!WriteTx(*pwtx))
return DB_LOAD_FAIL;
if (!WriteTx(*pwtx)) return DB_LOAD_FAIL;
}
}
WriteOrderPosNext(nOrderPosNext);
Expand Down Expand Up @@ -623,8 +628,9 @@ DBErrors CWalletDB::LoadWallet(CWallet* pwallet)
try {
int nMinVersion = 0;
if (batch.Read((std::string) DBKeys::MINVERSION, nMinVersion)) {
if (nMinVersion > CLIENT_VERSION)
if (nMinVersion > CLIENT_VERSION) {
return DB_TOO_NEW;
}
pwallet->LoadMinVersion(nMinVersion);
}

Expand All @@ -640,9 +646,9 @@ DBErrors CWalletDB::LoadWallet(CWallet* pwallet)
CDataStream ssKey(SER_DISK, CLIENT_VERSION);
CDataStream ssValue(SER_DISK, CLIENT_VERSION);
int ret = batch.ReadAtCursor(pcursor, ssKey, ssValue);
if (ret == DB_NOTFOUND)
if (ret == DB_NOTFOUND) {
break;
else if (ret != 0) {
} else if (ret != 0) {
LogPrintf("Error reading next record from wallet database\n");
return DB_CORRUPT;
}
Expand All @@ -652,9 +658,9 @@ DBErrors CWalletDB::LoadWallet(CWallet* pwallet)
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) || strType == DBKeys::DEFAULTKEY)
if (IsKeyType(strType) || strType == DBKeys::DEFAULTKEY) {
result = DB_CORRUPT;
else {
} else {
// Leave other errors alone, if we try to fix them we might make things worse.
fNoncriticalErrors = true; // ... but do warn the user there is something wrong.
if (strType == DBKeys::TX)
Expand Down Expand Up @@ -692,18 +698,22 @@ DBErrors CWalletDB::LoadWallet(CWallet* pwallet)
if ((wss.nKeys + wss.nCKeys) != wss.nKeyMeta)
pwallet->nTimeFirstKey = 1; // 0 would be considered 'no value'

for (const uint256& hash : wss.vWalletUpgrade)
for (const uint256& hash : wss.vWalletUpgrade) {
WriteTx(pwallet->mapWallet.at(hash));
}

// Rewrite encrypted wallets of versions 0.4.0 and 0.5.0rc:
if (wss.fIsEncrypted && (wss.nFileVersion == 40000 || wss.nFileVersion == 50000))
if (wss.fIsEncrypted && (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)
if (wss.fAnyUnordered) {
result = ReorderTransactions(pwallet);
}

return result;
}
Expand All @@ -717,8 +727,9 @@ DBErrors CWalletDB::FindWalletTx(CWallet* pwallet, std::vector<uint256>& vTxHash
LOCK(pwallet->cs_wallet);
int nMinVersion = 0;
if (batch.Read((std::string) DBKeys::MINVERSION, nMinVersion)) {
if (nMinVersion > CLIENT_VERSION)
if (nMinVersion > CLIENT_VERSION) {
return DB_TOO_NEW;
}
pwallet->LoadMinVersion(nMinVersion);
}

Expand All @@ -734,9 +745,9 @@ DBErrors CWalletDB::FindWalletTx(CWallet* pwallet, std::vector<uint256>& vTxHash
CDataStream ssKey(SER_DISK, CLIENT_VERSION);
CDataStream ssValue(SER_DISK, CLIENT_VERSION);
int ret = batch.ReadAtCursor(pcursor, ssKey, ssValue);
if (ret == DB_NOTFOUND)
if (ret == DB_NOTFOUND) {
break;
else if (ret != 0) {
} else if (ret != 0) {
LogPrintf("Error reading next record from wallet database\n");
return DB_CORRUPT;
}
Expand Down Expand Up @@ -772,13 +783,13 @@ DBErrors CWalletDB::ZapWalletTx(CWallet* pwallet, std::vector<CWalletTx>& vWtx)
// build list of wallet TXs
std::vector<uint256> vTxHash;
DBErrors err = FindWalletTx(pwallet, vTxHash, vWtx);
if (err != DB_LOAD_OK)
if (err != DB_LOAD_OK) {
return err;
}

// erase each wallet TX
for (uint256& hash : vTxHash) {
if (!EraseTx(hash))
return DB_CORRUPT;
if (!EraseTx(hash)) return DB_CORRUPT;
}

return DB_LOAD_OK;
Expand Down
10 changes: 5 additions & 5 deletions src/wallet/walletdb.h
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
// Copyright (c) 2009-2010 Satoshi Nakamoto
// Copyright (c) 2009-2013 The Bitcoin developers
// Copyright (c) 2016-2020 The PIVX developers
// Copyright (c) 2009-2021 The Bitcoin developers
// Copyright (c) 2016-2021 The PIVX developers
// Distributed under the MIT/X11 software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.

#ifndef BITCOIN_WALLETDB_H
#define BITCOIN_WALLETDB_H
#ifndef PIVX_WALLETDB_H
#define PIVX_WALLETDB_H

#include "amount.h"
#include "wallet/db.h"
Expand Down Expand Up @@ -247,4 +247,4 @@ bool AutoBackupWallet(const std::string& strWalletFile, std::string& strBackupWa
//! Compacts BDB state so that wallet.dat is self-contained (if there are changes)
void MaybeCompactWalletDB();

#endif // BITCOIN_WALLETDB_H
#endif // PIVX_WALLETDB_H

0 comments on commit 97af1a1

Please sign in to comment.