Skip to content

Commit 8204e19

Browse files
committed
Merge pull request #4805
44bc988 [Wallet] Do not flush the wallet in AddToWalletIfInvolvingMe(..) (Cozz Lovan)
2 parents b01a435 + 44bc988 commit 8204e19

File tree

8 files changed

+30
-19
lines changed

8 files changed

+30
-19
lines changed

src/db.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -217,10 +217,11 @@ void CDBEnv::CheckpointLSN(const std::string& strFile)
217217
}
218218

219219

220-
CDB::CDB(const std::string& strFilename, const char* pszMode) : pdb(NULL), activeTxn(NULL)
220+
CDB::CDB(const std::string& strFilename, const char* pszMode, bool fFlushOnCloseIn) : pdb(NULL), activeTxn(NULL)
221221
{
222222
int ret;
223223
fReadOnly = (!strchr(pszMode, '+') && !strchr(pszMode, 'w'));
224+
fFlushOnClose = fFlushOnCloseIn;
224225
if (strFilename.empty())
225226
return;
226227

@@ -297,7 +298,8 @@ void CDB::Close()
297298
activeTxn = NULL;
298299
pdb = NULL;
299300

300-
Flush();
301+
if (fFlushOnClose)
302+
Flush();
301303

302304
{
303305
LOCK(bitdb.cs_db);

src/db.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,9 @@ class CDB
9797
std::string strFile;
9898
DbTxn* activeTxn;
9999
bool fReadOnly;
100+
bool fFlushOnClose;
100101

101-
explicit CDB(const std::string& strFilename, const char* pszMode = "r+");
102+
explicit CDB(const std::string& strFilename, const char* pszMode = "r+", bool fFlushOnCloseIn=true);
102103
~CDB() { Close(); }
103104

104105
public:

src/init.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1211,6 +1211,8 @@ bool AppInit2(boost::thread_group& threadGroup)
12111211
// Restore wallet transaction metadata after -zapwallettxes=1
12121212
if (GetBoolArg("-zapwallettxes", false) && GetArg("-zapwallettxes", "1") != "2")
12131213
{
1214+
CWalletDB walletdb(strWalletFile);
1215+
12141216
BOOST_FOREACH(const CWalletTx& wtxOld, vWtx)
12151217
{
12161218
uint256 hash = wtxOld.GetHash();
@@ -1226,7 +1228,7 @@ bool AppInit2(boost::thread_group& threadGroup)
12261228
copyTo->fFromMe = copyFrom->fFromMe;
12271229
copyTo->strFromAccount = copyFrom->strFromAccount;
12281230
copyTo->nOrderPos = copyFrom->nOrderPos;
1229-
copyTo->WriteToDisk();
1231+
copyTo->WriteToDisk(&walletdb);
12301232
}
12311233
}
12321234
}

src/test/accounting_tests.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ BOOST_AUTO_TEST_CASE(acc_orderupgrade)
4646
walletdb.WriteAccountingEntry(ae);
4747

4848
wtx.mapValue["comment"] = "z";
49-
pwalletMain->AddToWallet(wtx);
49+
pwalletMain->AddToWallet(wtx, false, &walletdb);
5050
vpwtx.push_back(&pwalletMain->mapWallet[wtx.GetHash()]);
5151
vpwtx[0]->nTimeReceived = (unsigned int)1333333335;
5252
vpwtx[0]->nOrderPos = -1;
@@ -88,7 +88,7 @@ BOOST_AUTO_TEST_CASE(acc_orderupgrade)
8888
--tx.nLockTime; // Just to change the hash :)
8989
*static_cast<CTransaction*>(&wtx) = CTransaction(tx);
9090
}
91-
pwalletMain->AddToWallet(wtx);
91+
pwalletMain->AddToWallet(wtx, false, &walletdb);
9292
vpwtx.push_back(&pwalletMain->mapWallet[wtx.GetHash()]);
9393
vpwtx[1]->nTimeReceived = (unsigned int)1333333336;
9494

@@ -98,7 +98,7 @@ BOOST_AUTO_TEST_CASE(acc_orderupgrade)
9898
--tx.nLockTime; // Just to change the hash :)
9999
*static_cast<CTransaction*>(&wtx) = CTransaction(tx);
100100
}
101-
pwalletMain->AddToWallet(wtx);
101+
pwalletMain->AddToWallet(wtx, false, &walletdb);
102102
vpwtx.push_back(&pwalletMain->mapWallet[wtx.GetHash()]);
103103
vpwtx[2]->nTimeReceived = (unsigned int)1333333329;
104104
vpwtx[2]->nOrderPos = -1;

src/wallet.cpp

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -555,7 +555,7 @@ void CWallet::MarkDirty()
555555
}
556556
}
557557

558-
bool CWallet::AddToWallet(const CWalletTx& wtxIn, bool fFromLoadWallet)
558+
bool CWallet::AddToWallet(const CWalletTx& wtxIn, bool fFromLoadWallet, CWalletDB* pwalletdb)
559559
{
560560
uint256 hash = wtxIn.GetHash();
561561

@@ -576,7 +576,7 @@ bool CWallet::AddToWallet(const CWalletTx& wtxIn, bool fFromLoadWallet)
576576
if (fInsertedNew)
577577
{
578578
wtx.nTimeReceived = GetAdjustedTime();
579-
wtx.nOrderPos = IncOrderPosNext();
579+
wtx.nOrderPos = IncOrderPosNext(pwalletdb);
580580

581581
wtx.nTimeSmart = wtx.nTimeReceived;
582582
if (!wtxIn.hashBlock.IsNull())
@@ -653,7 +653,7 @@ bool CWallet::AddToWallet(const CWalletTx& wtxIn, bool fFromLoadWallet)
653653

654654
// Write to disk
655655
if (fInsertedNew || fUpdated)
656-
if (!wtx.WriteToDisk())
656+
if (!wtx.WriteToDisk(pwalletdb))
657657
return false;
658658

659659
// Break debit/credit balance caches:
@@ -689,10 +689,16 @@ bool CWallet::AddToWalletIfInvolvingMe(const CTransaction& tx, const CBlock* pbl
689689
if (fExisted || IsMine(tx) || IsFromMe(tx))
690690
{
691691
CWalletTx wtx(this,tx);
692+
692693
// Get merkle branch if transaction was found in a block
693694
if (pblock)
694695
wtx.SetMerkleBranch(*pblock);
695-
return AddToWallet(wtx);
696+
697+
// Do not flush the wallet here for performance reasons
698+
// this is safe, as in case of a crash, we rescan the necessary blocks on startup through our SetBestChain-mechanism
699+
CWalletDB walletdb(strWalletFile, "r+", false);
700+
701+
return AddToWallet(wtx, false, &walletdb);
696702
}
697703
}
698704
return false;
@@ -916,9 +922,9 @@ void CWalletTx::GetAccountAmounts(const string& strAccount, CAmount& nReceived,
916922
}
917923

918924

919-
bool CWalletTx::WriteToDisk()
925+
bool CWalletTx::WriteToDisk(CWalletDB *pwalletdb)
920926
{
921-
return CWalletDB(pwallet->strWalletFile).WriteTx(GetHash(), *this);
927+
return pwalletdb->WriteTx(GetHash(), *this);
922928
}
923929

924930
/**
@@ -1581,14 +1587,14 @@ bool CWallet::CommitTransaction(CWalletTx& wtxNew, CReserveKey& reservekey)
15811587
// This is only to keep the database open to defeat the auto-flush for the
15821588
// duration of this scope. This is the only place where this optimization
15831589
// maybe makes sense; please don't do it anywhere else.
1584-
CWalletDB* pwalletdb = fFileBacked ? new CWalletDB(strWalletFile,"r") : NULL;
1590+
CWalletDB* pwalletdb = fFileBacked ? new CWalletDB(strWalletFile,"r+") : NULL;
15851591

15861592
// Take key pair from key pool so it won't be used again
15871593
reservekey.KeepKey();
15881594

15891595
// Add tx to wallet, because if it has change it's also ours,
15901596
// otherwise just for transaction history.
1591-
AddToWallet(wtxNew);
1597+
AddToWallet(wtxNew, false, pwalletdb);
15921598

15931599
// Notify that old coins are spent
15941600
set<CWalletTx*> setCoins;

src/wallet.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ class CWallet : public CCryptoKeyStore, public CValidationInterface
275275
TxItems OrderedTxItems(std::list<CAccountingEntry>& acentries, std::string strAccount = "");
276276

277277
void MarkDirty();
278-
bool AddToWallet(const CWalletTx& wtxIn, bool fFromLoadWallet=false);
278+
bool AddToWallet(const CWalletTx& wtxIn, bool fFromLoadWallet, CWalletDB* pwalletdb);
279279
void SyncTransaction(const CTransaction& tx, const CBlock* pblock);
280280
bool AddToWalletIfInvolvingMe(const CTransaction& tx, const CBlock* pblock, bool fUpdate);
281281
void EraseFromWallet(const uint256 &hash);
@@ -903,7 +903,7 @@ class CWalletTx : public CMerkleTx
903903
return true;
904904
}
905905

906-
bool WriteToDisk();
906+
bool WriteToDisk(CWalletDB *pwalletdb);
907907

908908
int64_t GetTxTime() const;
909909
int GetRequestCount() const;

src/walletdb.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -395,7 +395,7 @@ ReadKeyValue(CWallet* pwallet, CDataStream& ssKey, CDataStream& ssValue,
395395
if (wtx.nOrderPos == -1)
396396
wss.fAnyUnordered = true;
397397

398-
pwallet->AddToWallet(wtx, true);
398+
pwallet->AddToWallet(wtx, true, NULL);
399399
}
400400
else if (strType == "acentry")
401401
{

src/walletdb.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ class CKeyMetadata
7676
class CWalletDB : public CDB
7777
{
7878
public:
79-
CWalletDB(const std::string& strFilename, const char* pszMode = "r+") : CDB(strFilename, pszMode)
79+
CWalletDB(const std::string& strFilename, const char* pszMode = "r+", bool fFlushOnClose = true) : CDB(strFilename, pszMode, fFlushOnClose)
8080
{
8181
}
8282

0 commit comments

Comments
 (0)