@@ -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;
0 commit comments