Skip to content

Commit 44bc988

Browse files
committed
[Wallet] Do not flush the wallet in AddToWalletIfInvolvingMe(..)
1 parent 29f96e8 commit 44bc988

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
@@ -218,10 +218,11 @@ void CDBEnv::CheckpointLSN(const std::string& strFile)
218218
}
219219

220220

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

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

301-
Flush();
302+
if (fFlushOnClose)
303+
Flush();
302304

303305
{
304306
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
@@ -1177,6 +1177,8 @@ bool AppInit2(boost::thread_group& threadGroup)
11771177
// Restore wallet transaction metadata after -zapwallettxes=1
11781178
if (GetBoolArg("-zapwallettxes", false) && GetArg("-zapwallettxes", "1") != "2")
11791179
{
1180+
CWalletDB walletdb(strWalletFile);
1181+
11801182
BOOST_FOREACH(const CWalletTx& wtxOld, vWtx)
11811183
{
11821184
uint256 hash = wtxOld.GetHash();
@@ -1192,7 +1194,7 @@ bool AppInit2(boost::thread_group& threadGroup)
11921194
copyTo->fFromMe = copyFrom->fFromMe;
11931195
copyTo->strFromAccount = copyFrom->strFromAccount;
11941196
copyTo->nOrderPos = copyFrom->nOrderPos;
1195-
copyTo->WriteToDisk();
1197+
copyTo->WriteToDisk(&walletdb);
11961198
}
11971199
}
11981200
}

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
@@ -512,7 +512,7 @@ void CWallet::MarkDirty()
512512
}
513513
}
514514

515-
bool CWallet::AddToWallet(const CWalletTx& wtxIn, bool fFromLoadWallet)
515+
bool CWallet::AddToWallet(const CWalletTx& wtxIn, bool fFromLoadWallet, CWalletDB* pwalletdb)
516516
{
517517
uint256 hash = wtxIn.GetHash();
518518

@@ -533,7 +533,7 @@ bool CWallet::AddToWallet(const CWalletTx& wtxIn, bool fFromLoadWallet)
533533
if (fInsertedNew)
534534
{
535535
wtx.nTimeReceived = GetAdjustedTime();
536-
wtx.nOrderPos = IncOrderPosNext();
536+
wtx.nOrderPos = IncOrderPosNext(pwalletdb);
537537

538538
wtx.nTimeSmart = wtx.nTimeReceived;
539539
if (wtxIn.hashBlock != 0)
@@ -610,7 +610,7 @@ bool CWallet::AddToWallet(const CWalletTx& wtxIn, bool fFromLoadWallet)
610610

611611
// Write to disk
612612
if (fInsertedNew || fUpdated)
613-
if (!wtx.WriteToDisk())
613+
if (!wtx.WriteToDisk(pwalletdb))
614614
return false;
615615

616616
// Break debit/credit balance caches:
@@ -644,10 +644,16 @@ bool CWallet::AddToWalletIfInvolvingMe(const CTransaction& tx, const CBlock* pbl
644644
if (fExisted || IsMine(tx) || IsFromMe(tx))
645645
{
646646
CWalletTx wtx(this,tx);
647+
647648
// Get merkle branch if transaction was found in a block
648649
if (pblock)
649650
wtx.SetMerkleBranch(*pblock);
650-
return AddToWallet(wtx);
651+
652+
// Do not flush the wallet here for performance reasons
653+
// this is safe, as in case of a crash, we rescan the necessary blocks on startup through our SetBestChain-mechanism
654+
CWalletDB walletdb(strWalletFile, "r+", false);
655+
656+
return AddToWallet(wtx, false, &walletdb);
651657
}
652658
}
653659
return false;
@@ -871,9 +877,9 @@ void CWalletTx::GetAccountAmounts(const string& strAccount, CAmount& nReceived,
871877
}
872878

873879

874-
bool CWalletTx::WriteToDisk()
880+
bool CWalletTx::WriteToDisk(CWalletDB *pwalletdb)
875881
{
876-
return CWalletDB(pwallet->strWalletFile).WriteTx(GetHash(), *this);
882+
return pwalletdb->WriteTx(GetHash(), *this);
877883
}
878884

879885
// Scan the block chain (starting in pindexStart) for transactions
@@ -1499,14 +1505,14 @@ bool CWallet::CommitTransaction(CWalletTx& wtxNew, CReserveKey& reservekey)
14991505
// This is only to keep the database open to defeat the auto-flush for the
15001506
// duration of this scope. This is the only place where this optimization
15011507
// maybe makes sense; please don't do it anywhere else.
1502-
CWalletDB* pwalletdb = fFileBacked ? new CWalletDB(strWalletFile,"r") : NULL;
1508+
CWalletDB* pwalletdb = fFileBacked ? new CWalletDB(strWalletFile,"r+") : NULL;
15031509

15041510
// Take key pair from key pool so it won't be used again
15051511
reservekey.KeepKey();
15061512

15071513
// Add tx to wallet, because if it has change it's also ours,
15081514
// otherwise just for transaction history.
1509-
AddToWallet(wtxNew);
1515+
AddToWallet(wtxNew, false, pwalletdb);
15101516

15111517
// Notify that old coins are spent
15121518
set<CWalletTx*> setCoins;

src/wallet.h

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

248248
void MarkDirty();
249-
bool AddToWallet(const CWalletTx& wtxIn, bool fFromLoadWallet=false);
249+
bool AddToWallet(const CWalletTx& wtxIn, bool fFromLoadWallet, CWalletDB* pwalletdb);
250250
void SyncTransaction(const CTransaction& tx, const CBlock* pblock);
251251
bool AddToWalletIfInvolvingMe(const CTransaction& tx, const CBlock* pblock, bool fUpdate);
252252
void EraseFromWallet(const uint256 &hash);
@@ -849,7 +849,7 @@ class CWalletTx : public CMerkleTx
849849
return true;
850850
}
851851

852-
bool WriteToDisk();
852+
bool WriteToDisk(CWalletDB *pwalletdb);
853853

854854
int64_t GetTxTime() const;
855855
int GetRequestCount() const;

src/walletdb.cpp

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

393-
pwallet->AddToWallet(wtx, true);
393+
pwallet->AddToWallet(wtx, true, NULL);
394394
}
395395
else if (strType == "acentry")
396396
{

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)