@@ -741,138 +741,143 @@ void CWallet::MarkDirty()
741
741
}
742
742
}
743
743
744
- bool CWallet::AddToWallet (const CWalletTx& wtxIn, bool fFromLoadWallet , CWalletDB* pwalletdb )
744
+ bool CWallet::AddToWallet (const CWalletTx& wtxIn, bool fFlushOnClose )
745
745
{
746
+ LOCK (cs_wallet);
747
+
748
+ CWalletDB walletdb (strWalletFile, " r+" , fFlushOnClose );
749
+
746
750
uint256 hash = wtxIn.GetHash ();
747
751
748
- if (fFromLoadWallet )
752
+ // Inserts only if not already there, returns tx inserted or tx found
753
+ pair<map<uint256, CWalletTx>::iterator, bool > ret = mapWallet.insert (make_pair (hash, wtxIn));
754
+ CWalletTx& wtx = (*ret.first ).second ;
755
+ wtx.BindWallet (this );
756
+ bool fInsertedNew = ret.second ;
757
+ if (fInsertedNew )
749
758
{
750
- mapWallet[hash] = wtxIn;
751
- CWalletTx& wtx = mapWallet[hash];
752
- wtx.BindWallet (this );
759
+ wtx.nTimeReceived = GetAdjustedTime ();
760
+ wtx.nOrderPos = IncOrderPosNext (&walletdb);
753
761
wtxOrdered.insert (make_pair (wtx.nOrderPos , TxPair (&wtx, (CAccountingEntry*)0 )));
754
- AddToSpends (hash);
755
- BOOST_FOREACH (const CTxIn& txin, wtx.vin ) {
756
- if (mapWallet.count (txin.prevout .hash )) {
757
- CWalletTx& prevtx = mapWallet[txin.prevout .hash ];
758
- if (prevtx.nIndex == -1 && !prevtx.hashUnset ()) {
759
- MarkConflicted (prevtx.hashBlock , wtx.GetHash ());
760
- }
761
- }
762
- }
763
- }
764
- else
765
- {
766
- LOCK (cs_wallet);
767
- // Inserts only if not already there, returns tx inserted or tx found
768
- pair<map<uint256, CWalletTx>::iterator, bool > ret = mapWallet.insert (make_pair (hash, wtxIn));
769
- CWalletTx& wtx = (*ret.first ).second ;
770
- wtx.BindWallet (this );
771
- bool fInsertedNew = ret.second ;
772
- if (fInsertedNew )
773
- {
774
- wtx.nTimeReceived = GetAdjustedTime ();
775
- wtx.nOrderPos = IncOrderPosNext (pwalletdb);
776
- wtxOrdered.insert (make_pair (wtx.nOrderPos , TxPair (&wtx, (CAccountingEntry*)0 )));
777
-
778
- wtx.nTimeSmart = wtx.nTimeReceived ;
779
- if (!wtxIn.hashUnset ())
762
+
763
+ wtx.nTimeSmart = wtx.nTimeReceived ;
764
+ if (!wtxIn.hashUnset ())
765
+ {
766
+ if (mapBlockIndex.count (wtxIn.hashBlock ))
780
767
{
781
- if (mapBlockIndex.count (wtxIn.hashBlock ))
768
+ int64_t latestNow = wtx.nTimeReceived ;
769
+ int64_t latestEntry = 0 ;
782
770
{
783
- int64_t latestNow = wtx.nTimeReceived ;
784
- int64_t latestEntry = 0 ;
771
+ // Tolerate times up to the last timestamp in the wallet not more than 5 minutes into the future
772
+ int64_t latestTolerated = latestNow + 300 ;
773
+ const TxItems & txOrdered = wtxOrdered;
774
+ for (TxItems::const_reverse_iterator it = txOrdered.rbegin (); it != txOrdered.rend (); ++it)
785
775
{
786
- // Tolerate times up to the last timestamp in the wallet not more than 5 minutes into the future
787
- int64_t latestTolerated = latestNow + 300 ;
788
- const TxItems & txOrdered = wtxOrdered;
789
- for (TxItems::const_reverse_iterator it = txOrdered.rbegin (); it != txOrdered.rend (); ++it)
776
+ CWalletTx *const pwtx = (*it).second .first ;
777
+ if (pwtx == &wtx)
778
+ continue ;
779
+ CAccountingEntry *const pacentry = (*it).second .second ;
780
+ int64_t nSmartTime;
781
+ if (pwtx)
790
782
{
791
- CWalletTx *const pwtx = (*it).second .first ;
792
- if (pwtx == &wtx)
793
- continue ;
794
- CAccountingEntry *const pacentry = (*it).second .second ;
795
- int64_t nSmartTime;
796
- if (pwtx)
797
- {
798
- nSmartTime = pwtx->nTimeSmart ;
799
- if (!nSmartTime)
800
- nSmartTime = pwtx->nTimeReceived ;
801
- }
802
- else
803
- nSmartTime = pacentry->nTime ;
804
- if (nSmartTime <= latestTolerated)
805
- {
806
- latestEntry = nSmartTime;
807
- if (nSmartTime > latestNow)
808
- latestNow = nSmartTime;
809
- break ;
810
- }
783
+ nSmartTime = pwtx->nTimeSmart ;
784
+ if (!nSmartTime)
785
+ nSmartTime = pwtx->nTimeReceived ;
786
+ }
787
+ else
788
+ nSmartTime = pacentry->nTime ;
789
+ if (nSmartTime <= latestTolerated)
790
+ {
791
+ latestEntry = nSmartTime;
792
+ if (nSmartTime > latestNow)
793
+ latestNow = nSmartTime;
794
+ break ;
811
795
}
812
796
}
813
-
814
- int64_t blocktime = mapBlockIndex[wtxIn.hashBlock ]->GetBlockTime ();
815
- wtx.nTimeSmart = std::max (latestEntry, std::min (blocktime, latestNow));
816
797
}
817
- else
818
- LogPrintf (" AddToWallet(): found %s in block %s not in index\n " ,
819
- wtxIn.GetHash ().ToString (),
820
- wtxIn.hashBlock .ToString ());
798
+
799
+ int64_t blocktime = mapBlockIndex[wtxIn.hashBlock ]->GetBlockTime ();
800
+ wtx.nTimeSmart = std::max (latestEntry, std::min (blocktime, latestNow));
821
801
}
822
- AddToSpends (hash);
802
+ else
803
+ LogPrintf (" AddToWallet(): found %s in block %s not in index\n " ,
804
+ wtxIn.GetHash ().ToString (),
805
+ wtxIn.hashBlock .ToString ());
823
806
}
807
+ AddToSpends (hash);
808
+ }
824
809
825
- bool fUpdated = false ;
826
- if (!fInsertedNew )
810
+ bool fUpdated = false ;
811
+ if (!fInsertedNew )
812
+ {
813
+ // Merge
814
+ if (!wtxIn.hashUnset () && wtxIn.hashBlock != wtx.hashBlock )
827
815
{
828
- // Merge
829
- if (!wtxIn.hashUnset () && wtxIn.hashBlock != wtx.hashBlock )
830
- {
831
- wtx.hashBlock = wtxIn.hashBlock ;
832
- fUpdated = true ;
833
- }
834
- // If no longer abandoned, update
835
- if (wtxIn.hashBlock .IsNull () && wtx.isAbandoned ())
836
- {
837
- wtx.hashBlock = wtxIn.hashBlock ;
838
- fUpdated = true ;
839
- }
840
- if (wtxIn.nIndex != -1 && (wtxIn.nIndex != wtx.nIndex ))
841
- {
842
- wtx.nIndex = wtxIn.nIndex ;
843
- fUpdated = true ;
844
- }
845
- if (wtxIn.fFromMe && wtxIn.fFromMe != wtx.fFromMe )
846
- {
847
- wtx.fFromMe = wtxIn.fFromMe ;
848
- fUpdated = true ;
849
- }
816
+ wtx.hashBlock = wtxIn.hashBlock ;
817
+ fUpdated = true ;
818
+ }
819
+ // If no longer abandoned, update
820
+ if (wtxIn.hashBlock .IsNull () && wtx.isAbandoned ())
821
+ {
822
+ wtx.hashBlock = wtxIn.hashBlock ;
823
+ fUpdated = true ;
850
824
}
825
+ if (wtxIn.nIndex != -1 && (wtxIn.nIndex != wtx.nIndex ))
826
+ {
827
+ wtx.nIndex = wtxIn.nIndex ;
828
+ fUpdated = true ;
829
+ }
830
+ if (wtxIn.fFromMe && wtxIn.fFromMe != wtx.fFromMe )
831
+ {
832
+ wtx.fFromMe = wtxIn.fFromMe ;
833
+ fUpdated = true ;
834
+ }
835
+ }
851
836
852
- // // debug print
853
- LogPrintf (" AddToWallet %s %s%s\n " , wtxIn.GetHash ().ToString (), (fInsertedNew ? " new" : " " ), (fUpdated ? " update" : " " ));
837
+ // // debug print
838
+ LogPrintf (" AddToWallet %s %s%s\n " , wtxIn.GetHash ().ToString (), (fInsertedNew ? " new" : " " ), (fUpdated ? " update" : " " ));
854
839
855
- // Write to disk
856
- if (fInsertedNew || fUpdated )
857
- if (!pwalletdb-> WriteTx (wtx))
858
- return false ;
840
+ // Write to disk
841
+ if (fInsertedNew || fUpdated )
842
+ if (!walletdb. WriteTx (wtx))
843
+ return false ;
859
844
860
- // Break debit/credit balance caches:
861
- wtx.MarkDirty ();
845
+ // Break debit/credit balance caches:
846
+ wtx.MarkDirty ();
862
847
863
- // Notify UI of new or updated transaction
864
- NotifyTransactionChanged (this , hash, fInsertedNew ? CT_NEW : CT_UPDATED);
848
+ // Notify UI of new or updated transaction
849
+ NotifyTransactionChanged (this , hash, fInsertedNew ? CT_NEW : CT_UPDATED);
865
850
866
- // notify an external script when a wallet transaction comes in or is updated
867
- std::string strCmd = GetArg (" -walletnotify" , " " );
851
+ // notify an external script when a wallet transaction comes in or is updated
852
+ std::string strCmd = GetArg (" -walletnotify" , " " );
868
853
869
- if ( !strCmd.empty ())
870
- {
871
- boost::replace_all (strCmd, " %s" , wtxIn.GetHash ().GetHex ());
872
- boost::thread t (runCommand, strCmd); // thread runs free
873
- }
854
+ if ( !strCmd.empty ())
855
+ {
856
+ boost::replace_all (strCmd, " %s" , wtxIn.GetHash ().GetHex ());
857
+ boost::thread t (runCommand, strCmd); // thread runs free
858
+ }
859
+
860
+ return true ;
861
+ }
874
862
863
+ bool CWallet::LoadToWallet (const CWalletTx& wtxIn)
864
+ {
865
+ uint256 hash = wtxIn.GetHash ();
866
+
867
+ mapWallet[hash] = wtxIn;
868
+ CWalletTx& wtx = mapWallet[hash];
869
+ wtx.BindWallet (this );
870
+ wtxOrdered.insert (make_pair (wtx.nOrderPos , TxPair (&wtx, (CAccountingEntry*)0 )));
871
+ AddToSpends (hash);
872
+ BOOST_FOREACH (const CTxIn& txin, wtx.vin ) {
873
+ if (mapWallet.count (txin.prevout .hash )) {
874
+ CWalletTx& prevtx = mapWallet[txin.prevout .hash ];
875
+ if (prevtx.nIndex == -1 && !prevtx.hashUnset ()) {
876
+ MarkConflicted (prevtx.hashBlock , wtx.GetHash ());
877
+ }
878
+ }
875
879
}
880
+
876
881
return true ;
877
882
}
878
883
@@ -909,11 +914,7 @@ bool CWallet::AddToWalletIfInvolvingMe(const CTransaction& tx, const CBlock* pbl
909
914
if (pblock)
910
915
wtx.SetMerkleBranch (*pblock);
911
916
912
- // Do not flush the wallet here for performance reasons
913
- // this is safe, as in case of a crash, we rescan the necessary blocks on startup through our SetBestChain-mechanism
914
- CWalletDB walletdb (strWalletFile, " r+" , false );
915
-
916
- return AddToWallet (wtx, false , &walletdb);
917
+ return AddToWallet (wtx, false );
917
918
}
918
919
}
919
920
return false ;
@@ -2446,17 +2447,12 @@ bool CWallet::CommitTransaction(CWalletTx& wtxNew, CReserveKey& reservekey)
2446
2447
LOCK2 (cs_main, cs_wallet);
2447
2448
LogPrintf (" CommitTransaction:\n %s" , wtxNew.ToString ());
2448
2449
{
2449
- // This is only to keep the database open to defeat the auto-flush for the
2450
- // duration of this scope. This is the only place where this optimization
2451
- // maybe makes sense; please don't do it anywhere else.
2452
- CWalletDB* pwalletdb = fFileBacked ? new CWalletDB (strWalletFile," r+" ) : NULL ;
2453
-
2454
2450
// Take key pair from key pool so it won't be used again
2455
2451
reservekey.KeepKey ();
2456
2452
2457
2453
// Add tx to wallet, because if it has change it's also ours,
2458
2454
// otherwise just for transaction history.
2459
- AddToWallet (wtxNew, false , pwalletdb );
2455
+ AddToWallet (wtxNew);
2460
2456
2461
2457
// Notify that old coins are spent
2462
2458
set<CWalletTx*> setCoins;
@@ -2466,9 +2462,6 @@ bool CWallet::CommitTransaction(CWalletTx& wtxNew, CReserveKey& reservekey)
2466
2462
coin.BindWallet (this );
2467
2463
NotifyTransactionChanged (this , coin.GetHash (), CT_UPDATED);
2468
2464
}
2469
-
2470
- if (fFileBacked )
2471
- delete pwalletdb;
2472
2465
}
2473
2466
2474
2467
// Track how many getdata requests our transaction gets
0 commit comments