@@ -811,83 +811,85 @@ void CWallet::MarkDirty()
811811 }
812812}
813813
814- bool CWallet::AddToWallet (const CWalletTx& wtxIn, bool fFromLoadWallet , CWalletDB* pwalletdb)
814+ bool CWallet::AddToWallet (const CWalletTx& wtxIn, CWalletDB* pwalletdb)
815815{
816816 uint256 hash = wtxIn.GetHash ();
817-
818- if (fFromLoadWallet ) {
819- mapWallet[hash] = wtxIn;
820- CWalletTx& wtx = mapWallet[hash];
821- wtx.BindWallet (this );
817+ LOCK (cs_wallet);
818+ // Inserts only if not already there, returns tx inserted or tx found
819+ std::pair<std::map<uint256, CWalletTx>::iterator, bool > ret = mapWallet.insert (std::make_pair (hash, wtxIn));
820+ CWalletTx& wtx = (*ret.first ).second ;
821+ wtx.BindWallet (this );
822+ bool fInsertedNew = ret.second ;
823+ if (fInsertedNew ) {
824+ wtx.nTimeReceived = GetAdjustedTime ();
825+ wtx.nOrderPos = IncOrderPosNext (pwalletdb);
822826 wtxOrdered.insert (std::make_pair (wtx.nOrderPos , TxPair (&wtx, (CAccountingEntry*)0 )));
827+ wtx.UpdateTimeSmart ();
823828 AddToSpends (hash);
824- for (const CTxIn& txin : wtx.vin ) {
825- if (mapWallet.count (txin.prevout .hash )) {
826- CWalletTx& prevtx = mapWallet[txin.prevout .hash ];
827- if (prevtx.nIndex == -1 && !prevtx.hashUnset ()) {
828- MarkConflicted (prevtx.hashBlock , wtx.GetHash ());
829- }
830- }
831- }
832- } else {
833- LOCK (cs_wallet);
834- // Inserts only if not already there, returns tx inserted or tx found
835- std::pair<std::map<uint256, CWalletTx>::iterator, bool > ret = mapWallet.insert (std::make_pair (hash, wtxIn));
836- CWalletTx& wtx = (*ret.first ).second ;
837- wtx.BindWallet (this );
838- bool fInsertedNew = ret.second ;
839- if (fInsertedNew ) {
840- wtx.nTimeReceived = GetAdjustedTime ();
841- wtx.nOrderPos = IncOrderPosNext (pwalletdb);
842- wtxOrdered.insert (std::make_pair (wtx.nOrderPos , TxPair (&wtx, (CAccountingEntry*)0 )));
829+ }
830+
831+ bool fUpdated = false ;
832+ if (!fInsertedNew ) {
833+ // Merge
834+ if (!wtxIn.hashUnset () && wtxIn.hashBlock != wtx.hashBlock ) {
835+ wtx.hashBlock = wtxIn.hashBlock ;
843836 wtx.UpdateTimeSmart ();
844- AddToSpends (hash) ;
837+ fUpdated = true ;
845838 }
846-
847- bool fUpdated = false ;
848- if (!fInsertedNew ) {
849- // Merge
850- if (!wtxIn.hashUnset () && wtxIn.hashBlock != wtx.hashBlock ) {
851- wtx.hashBlock = wtxIn.hashBlock ;
852- wtx.UpdateTimeSmart ();
853- fUpdated = true ;
854- }
855- // If no longer abandoned, update
856- if (wtxIn.hashBlock .IsNull () && wtx.isAbandoned ()) {
857- wtx.hashBlock = wtxIn.hashBlock ;
858- if (!fUpdated ) wtx.UpdateTimeSmart ();
859- fUpdated = true ;
860- }
861- if (wtxIn.nIndex != -1 && wtxIn.nIndex != wtx.nIndex ) {
862- wtx.nIndex = wtxIn.nIndex ;
863- fUpdated = true ;
864- }
865- if (wtxIn.fFromMe && wtxIn.fFromMe != wtx.fFromMe ) {
866- wtx.fFromMe = wtxIn.fFromMe ;
867- fUpdated = true ;
868- }
839+ // If no longer abandoned, update
840+ if (wtxIn.hashBlock .IsNull () && wtx.isAbandoned ()) {
841+ wtx.hashBlock = wtxIn.hashBlock ;
842+ if (!fUpdated ) wtx.UpdateTimeSmart ();
843+ fUpdated = true ;
869844 }
845+ if (wtxIn.nIndex != -1 && wtxIn.nIndex != wtx.nIndex ) {
846+ wtx.nIndex = wtxIn.nIndex ;
847+ fUpdated = true ;
848+ }
849+ if (wtxIn.fFromMe && wtxIn.fFromMe != wtx.fFromMe ) {
850+ wtx.fFromMe = wtxIn.fFromMe ;
851+ fUpdated = true ;
852+ }
853+ }
870854
871- // // debug print
872- LogPrintf (" AddToWallet %s %s%s\n " , wtxIn.GetHash ().ToString (), (fInsertedNew ? " new" : " " ), (fUpdated ? " update" : " " ));
855+ // // debug print
856+ LogPrintf (" AddToWallet %s %s%s\n " , wtxIn.GetHash ().ToString (), (fInsertedNew ? " new" : " " ), (fUpdated ? " update" : " " ));
873857
874- // Write to disk
875- if (fInsertedNew || fUpdated )
876- if (!pwalletdb->WriteTx (wtx))
877- return false ;
858+ // Write to disk
859+ if (fInsertedNew || fUpdated )
860+ if (!pwalletdb->WriteTx (wtx))
861+ return false ;
878862
879- // Break debit/credit balance caches:
880- wtx.MarkDirty ();
863+ // Break debit/credit balance caches:
864+ wtx.MarkDirty ();
881865
882- // Notify UI of new or updated transaction
883- NotifyTransactionChanged (this , hash, fInsertedNew ? CT_NEW : CT_UPDATED);
866+ // Notify UI of new or updated transaction
867+ NotifyTransactionChanged (this , hash, fInsertedNew ? CT_NEW : CT_UPDATED);
884868
885- // notify an external script when a wallet transaction comes in or is updated
886- std::string strCmd = GetArg (" -walletnotify" , " " );
869+ // notify an external script when a wallet transaction comes in or is updated
870+ std::string strCmd = GetArg (" -walletnotify" , " " );
887871
888- if (!strCmd.empty ()) {
889- boost::replace_all (strCmd, " %s" , wtxIn.GetHash ().GetHex ());
890- boost::thread t (runCommand, strCmd); // thread runs free
872+ if (!strCmd.empty ()) {
873+ boost::replace_all (strCmd, " %s" , wtxIn.GetHash ().GetHex ());
874+ boost::thread t (runCommand, strCmd); // thread runs free
875+ }
876+ return true ;
877+ }
878+
879+ bool CWallet::LoadToWallet (const CWalletTx& wtxIn)
880+ {
881+ uint256 hash = wtxIn.GetHash ();
882+ mapWallet[hash] = wtxIn;
883+ CWalletTx& wtx = mapWallet[hash];
884+ wtx.BindWallet (this );
885+ wtxOrdered.insert (std::make_pair (wtx.nOrderPos , TxPair (&wtx, (CAccountingEntry*)0 )));
886+ AddToSpends (hash);
887+ for (const CTxIn& txin : wtx.vin ) {
888+ if (mapWallet.count (txin.prevout .hash )) {
889+ CWalletTx& prevtx = mapWallet[txin.prevout .hash ];
890+ if (prevtx.nIndex == -1 && !prevtx.hashUnset ()) {
891+ MarkConflicted (prevtx.hashBlock , wtx.GetHash ());
892+ }
891893 }
892894 }
893895 return true ;
@@ -939,7 +941,7 @@ bool CWallet::AddToWalletIfInvolvingMe(const CTransaction& tx, const CBlock* pbl
939941 // this is safe, as in case of a crash, we rescan the necessary blocks on startup through our SetBestChain-mechanism
940942 CWalletDB walletdb (strWalletFile, " r+" , false );
941943
942- return AddToWallet (wtx, false , &walletdb);
944+ return AddToWallet (wtx, &walletdb);
943945 }
944946 }
945947 return false ;
@@ -2929,7 +2931,7 @@ CWallet::CommitResult CWallet::CommitTransaction(CWalletTx& wtxNew, CReserveKey&
29292931
29302932 // Add tx to wallet, because if it has change it's also ours,
29312933 // otherwise just for transaction history.
2932- AddToWallet (wtxNew, false , pwalletdb);
2934+ AddToWallet (wtxNew, pwalletdb);
29332935
29342936 // Notify that old coins are spent
29352937 if (!wtxNew.HasZerocoinSpendInputs ()) {
0 commit comments