Skip to content

Commit 00f09c9

Browse files
committed
Split CWallet::AddToWallet into AddToWallet and LoadToWallet.
This removes the fFromLoadWallet flag in AddToWallet. These were already effectively two methods.
1 parent bbcb8fd commit 00f09c9

File tree

5 files changed

+119
-116
lines changed

5 files changed

+119
-116
lines changed

src/wallet/rpcdump.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,7 @@ UniValue importprunedfunds(const UniValue& params, bool fHelp)
310310

311311
if (pwalletMain->IsMine(tx)) {
312312
CWalletDB walletdb(pwalletMain->strWalletFile, "r+", false);
313-
pwalletMain->AddToWallet(wtx, false, &walletdb);
313+
pwalletMain->AddToWallet(wtx, &walletdb);
314314
return NullUniValue;
315315
}
316316

src/wallet/test/accounting_tests.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ BOOST_AUTO_TEST_CASE(acc_orderupgrade)
4848
pwalletMain->AddAccountingEntry(ae, walletdb);
4949

5050
wtx.mapValue["comment"] = "z";
51-
pwalletMain->AddToWallet(wtx, false, &walletdb);
51+
pwalletMain->AddToWallet(wtx, &walletdb);
5252
vpwtx.push_back(&pwalletMain->mapWallet[wtx.GetHash()]);
5353
vpwtx[0]->nTimeReceived = (unsigned int)1333333335;
5454
vpwtx[0]->nOrderPos = -1;
@@ -90,7 +90,7 @@ BOOST_AUTO_TEST_CASE(acc_orderupgrade)
9090
--tx.nLockTime; // Just to change the hash :)
9191
*static_cast<CTransaction*>(&wtx) = CTransaction(tx);
9292
}
93-
pwalletMain->AddToWallet(wtx, false, &walletdb);
93+
pwalletMain->AddToWallet(wtx, &walletdb);
9494
vpwtx.push_back(&pwalletMain->mapWallet[wtx.GetHash()]);
9595
vpwtx[1]->nTimeReceived = (unsigned int)1333333336;
9696

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

src/wallet/wallet.cpp

Lines changed: 112 additions & 110 deletions
Original file line numberDiff line numberDiff line change
@@ -741,138 +741,140 @@ void CWallet::MarkDirty()
741741
}
742742
}
743743

744-
bool CWallet::AddToWallet(const CWalletTx& wtxIn, bool fFromLoadWallet, CWalletDB* pwalletdb)
744+
bool CWallet::AddToWallet(const CWalletTx& wtxIn, CWalletDB* pwalletdb)
745745
{
746746
uint256 hash = wtxIn.GetHash();
747747

748-
if (fFromLoadWallet)
749-
{
750-
mapWallet[hash] = wtxIn;
751-
CWalletTx& wtx = mapWallet[hash];
752-
wtx.BindWallet(this);
748+
LOCK(cs_wallet);
749+
// Inserts only if not already there, returns tx inserted or tx found
750+
pair<map<uint256, CWalletTx>::iterator, bool> ret = mapWallet.insert(make_pair(hash, wtxIn));
751+
CWalletTx& wtx = (*ret.first).second;
752+
wtx.BindWallet(this);
753+
bool fInsertedNew = ret.second;
754+
if (fInsertedNew)
755+
{
756+
wtx.nTimeReceived = GetAdjustedTime();
757+
wtx.nOrderPos = IncOrderPosNext(pwalletdb);
753758
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())
759+
760+
wtx.nTimeSmart = wtx.nTimeReceived;
761+
if (!wtxIn.hashUnset())
762+
{
763+
if (mapBlockIndex.count(wtxIn.hashBlock))
780764
{
781-
if (mapBlockIndex.count(wtxIn.hashBlock))
765+
int64_t latestNow = wtx.nTimeReceived;
766+
int64_t latestEntry = 0;
782767
{
783-
int64_t latestNow = wtx.nTimeReceived;
784-
int64_t latestEntry = 0;
768+
// Tolerate times up to the last timestamp in the wallet not more than 5 minutes into the future
769+
int64_t latestTolerated = latestNow + 300;
770+
const TxItems & txOrdered = wtxOrdered;
771+
for (TxItems::const_reverse_iterator it = txOrdered.rbegin(); it != txOrdered.rend(); ++it)
785772
{
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)
773+
CWalletTx *const pwtx = (*it).second.first;
774+
if (pwtx == &wtx)
775+
continue;
776+
CAccountingEntry *const pacentry = (*it).second.second;
777+
int64_t nSmartTime;
778+
if (pwtx)
790779
{
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-
}
780+
nSmartTime = pwtx->nTimeSmart;
781+
if (!nSmartTime)
782+
nSmartTime = pwtx->nTimeReceived;
783+
}
784+
else
785+
nSmartTime = pacentry->nTime;
786+
if (nSmartTime <= latestTolerated)
787+
{
788+
latestEntry = nSmartTime;
789+
if (nSmartTime > latestNow)
790+
latestNow = nSmartTime;
791+
break;
811792
}
812793
}
813-
814-
int64_t blocktime = mapBlockIndex[wtxIn.hashBlock]->GetBlockTime();
815-
wtx.nTimeSmart = std::max(latestEntry, std::min(blocktime, latestNow));
816794
}
817-
else
818-
LogPrintf("AddToWallet(): found %s in block %s not in index\n",
819-
wtxIn.GetHash().ToString(),
820-
wtxIn.hashBlock.ToString());
795+
796+
int64_t blocktime = mapBlockIndex[wtxIn.hashBlock]->GetBlockTime();
797+
wtx.nTimeSmart = std::max(latestEntry, std::min(blocktime, latestNow));
821798
}
822-
AddToSpends(hash);
799+
else
800+
LogPrintf("AddToWallet(): found %s in block %s not in index\n",
801+
wtxIn.GetHash().ToString(),
802+
wtxIn.hashBlock.ToString());
823803
}
804+
AddToSpends(hash);
805+
}
824806

825-
bool fUpdated = false;
826-
if (!fInsertedNew)
807+
bool fUpdated = false;
808+
if (!fInsertedNew)
809+
{
810+
// Merge
811+
if (!wtxIn.hashUnset() && wtxIn.hashBlock != wtx.hashBlock)
827812
{
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-
}
813+
wtx.hashBlock = wtxIn.hashBlock;
814+
fUpdated = true;
850815
}
816+
// If no longer abandoned, update
817+
if (wtxIn.hashBlock.IsNull() && wtx.isAbandoned())
818+
{
819+
wtx.hashBlock = wtxIn.hashBlock;
820+
fUpdated = true;
821+
}
822+
if (wtxIn.nIndex != -1 && (wtxIn.nIndex != wtx.nIndex))
823+
{
824+
wtx.nIndex = wtxIn.nIndex;
825+
fUpdated = true;
826+
}
827+
if (wtxIn.fFromMe && wtxIn.fFromMe != wtx.fFromMe)
828+
{
829+
wtx.fFromMe = wtxIn.fFromMe;
830+
fUpdated = true;
831+
}
832+
}
851833

852-
//// debug print
853-
LogPrintf("AddToWallet %s %s%s\n", wtxIn.GetHash().ToString(), (fInsertedNew ? "new" : ""), (fUpdated ? "update" : ""));
834+
//// debug print
835+
LogPrintf("AddToWallet %s %s%s\n", wtxIn.GetHash().ToString(), (fInsertedNew ? "new" : ""), (fUpdated ? "update" : ""));
854836

855-
// Write to disk
856-
if (fInsertedNew || fUpdated)
857-
if (!pwalletdb->WriteTx(wtx))
858-
return false;
837+
// Write to disk
838+
if (fInsertedNew || fUpdated)
839+
if (!pwalletdb->WriteTx(wtx))
840+
return false;
859841

860-
// Break debit/credit balance caches:
861-
wtx.MarkDirty();
842+
// Break debit/credit balance caches:
843+
wtx.MarkDirty();
862844

863-
// Notify UI of new or updated transaction
864-
NotifyTransactionChanged(this, hash, fInsertedNew ? CT_NEW : CT_UPDATED);
845+
// Notify UI of new or updated transaction
846+
NotifyTransactionChanged(this, hash, fInsertedNew ? CT_NEW : CT_UPDATED);
865847

866-
// notify an external script when a wallet transaction comes in or is updated
867-
std::string strCmd = GetArg("-walletnotify", "");
848+
// notify an external script when a wallet transaction comes in or is updated
849+
std::string strCmd = GetArg("-walletnotify", "");
868850

869-
if ( !strCmd.empty())
870-
{
871-
boost::replace_all(strCmd, "%s", wtxIn.GetHash().GetHex());
872-
boost::thread t(runCommand, strCmd); // thread runs free
873-
}
851+
if ( !strCmd.empty())
852+
{
853+
boost::replace_all(strCmd, "%s", wtxIn.GetHash().GetHex());
854+
boost::thread t(runCommand, strCmd); // thread runs free
855+
}
856+
857+
return true;
858+
}
859+
860+
bool CWallet::LoadToWallet(const CWalletTx& wtxIn)
861+
{
862+
uint256 hash = wtxIn.GetHash();
874863

864+
mapWallet[hash] = wtxIn;
865+
CWalletTx& wtx = mapWallet[hash];
866+
wtx.BindWallet(this);
867+
wtxOrdered.insert(make_pair(wtx.nOrderPos, TxPair(&wtx, (CAccountingEntry*)0)));
868+
AddToSpends(hash);
869+
BOOST_FOREACH(const CTxIn& txin, wtx.vin) {
870+
if (mapWallet.count(txin.prevout.hash)) {
871+
CWalletTx& prevtx = mapWallet[txin.prevout.hash];
872+
if (prevtx.nIndex == -1 && !prevtx.hashUnset()) {
873+
MarkConflicted(prevtx.hashBlock, wtx.GetHash());
874+
}
875+
}
875876
}
877+
876878
return true;
877879
}
878880

@@ -913,7 +915,7 @@ bool CWallet::AddToWalletIfInvolvingMe(const CTransaction& tx, const CBlock* pbl
913915
// this is safe, as in case of a crash, we rescan the necessary blocks on startup through our SetBestChain-mechanism
914916
CWalletDB walletdb(strWalletFile, "r+", false);
915917

916-
return AddToWallet(wtx, false, &walletdb);
918+
return AddToWallet(wtx, &walletdb);
917919
}
918920
}
919921
return false;
@@ -2456,7 +2458,7 @@ bool CWallet::CommitTransaction(CWalletTx& wtxNew, CReserveKey& reservekey)
24562458

24572459
// Add tx to wallet, because if it has change it's also ours,
24582460
// otherwise just for transaction history.
2459-
AddToWallet(wtxNew, false, pwalletdb);
2461+
AddToWallet(wtxNew, pwalletdb);
24602462

24612463
// Notify that old coins are spent
24622464
set<CWalletTx*> setCoins;

src/wallet/wallet.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -729,7 +729,8 @@ class CWallet : public CCryptoKeyStore, public CValidationInterface
729729
bool GetAccountPubkey(CPubKey &pubKey, std::string strAccount, bool bForceNew = false);
730730

731731
void MarkDirty();
732-
bool AddToWallet(const CWalletTx& wtxIn, bool fFromLoadWallet, CWalletDB* pwalletdb);
732+
bool AddToWallet(const CWalletTx& wtxIn, CWalletDB* pwalletdb);
733+
bool LoadToWallet(const CWalletTx& wtxIn);
733734
void SyncTransaction(const CTransaction& tx, const CBlockIndex *pindex, const CBlock* pblock);
734735
bool AddToWalletIfInvolvingMe(const CTransaction& tx, const CBlock* pblock, bool fUpdate);
735736
int ScanForWalletTransactions(CBlockIndex* pindexStart, bool fUpdate = false);

src/wallet/walletdb.cpp

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

403-
pwallet->AddToWallet(wtx, true, NULL);
403+
pwallet->LoadToWallet(wtx);
404404
}
405405
else if (strType == "acentry")
406406
{

0 commit comments

Comments
 (0)