Skip to content

Commit c3779c5

Browse files
committed
Split CWallet::AddToWallet into AddToWallet and LoadToWallet.
This removes the fFromLoadWallet flag in AddToWallet. These were already effectively two methods. Coming from btc@00f09c920c2e8906d2260251be6d1d2fa1bbb29d
1 parent 49f4124 commit c3779c5

File tree

8 files changed

+80
-77
lines changed

8 files changed

+80
-77
lines changed

src/main.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2440,7 +2440,7 @@ bool ConnectBlock(const CBlock& block, CValidationState& state, CBlockIndex* pin
24402440
CWalletTx wtx(pwalletMain, tx);
24412441
wtx.nTimeReceived = pindex->GetBlockTime();
24422442
wtx.SetMerkleBranch(block);
2443-
pwalletMain->AddToWallet(wtx, false, nullptr);
2443+
pwalletMain->AddToWallet(wtx, nullptr);
24442444
setAddedTx.insert(pSpend.second);
24452445
}
24462446
}

src/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: 68 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -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()) {

src/wallet/wallet.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -432,7 +432,8 @@ class CWallet : public CCryptoKeyStore, public CValidationInterface
432432

433433
bool GetLabelDestination(CTxDestination& dest, const std::string& label, bool bForceNew = false);
434434
void MarkDirty();
435-
bool AddToWallet(const CWalletTx& wtxIn, bool fFromLoadWallet, CWalletDB* pwalletdb);
435+
bool AddToWallet(const CWalletTx& wtxIn, CWalletDB* pwalletdb);
436+
bool LoadToWallet(const CWalletTx& wtxIn);
436437
void SyncTransaction(const CTransaction& tx, const CBlock* pblock);
437438
bool AddToWalletIfInvolvingMe(const CTransaction& tx, const CBlock* pblock, bool fUpdate);
438439
void EraseFromWallet(const uint256& hash);

src/wallet/wallet_zerocoin.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ void CWallet::doZPivRescan(const CBlockIndex* pindex, const CBlock& block,
117117
CWalletTx wtx(this, tx);
118118
wtx.nTimeReceived = block.GetBlockTime();
119119
wtx.SetMerkleBranch(block);
120-
AddToWallet(wtx, false, &walletdb);
120+
AddToWallet(wtx, &walletdb);
121121
setAddedToWallet.insert(txid);
122122
}
123123
}
@@ -137,7 +137,7 @@ void CWallet::doZPivRescan(const CBlockIndex* pindex, const CBlock& block,
137137
wtx.SetMerkleBranch(blockSpend);
138138

139139
wtx.nTimeReceived = pindexSpend->nTime;
140-
AddToWallet(wtx, false, &walletdb);
140+
AddToWallet(wtx, &walletdb);
141141
setAddedToWallet.emplace(txidSpend);
142142
}
143143
}

src/wallet/walletdb.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -491,7 +491,7 @@ bool ReadKeyValue(CWallet* pwallet, CDataStream& ssKey, CDataStream& ssValue, CW
491491
if (wtx.nOrderPos == -1)
492492
wss.fAnyUnordered = true;
493493

494-
pwallet->AddToWallet(wtx, true, nullptr);
494+
pwallet->LoadToWallet(wtx);
495495
} else if (strType == "acentry") {
496496
std::string strAccount;
497497
ssKey >> strAccount;

src/zpiv/zpivwallet.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ void CzPIVWallet::SyncWithChain(bool fGenerateMintPool)
267267

268268
//Fill out wtx so that a transaction record can be created
269269
wtx.nTimeReceived = pindex->GetBlockTime();
270-
wallet->AddToWallet(wtx, false, &walletdb);
270+
wallet->AddToWallet(wtx, &walletdb);
271271
setAddedTx.insert(txHash);
272272
}
273273

@@ -324,7 +324,7 @@ bool CzPIVWallet::SetMintSeen(const CBigNum& bnValue, const int& nHeight, const
324324

325325
wtx.nTimeReceived = pindex->nTime;
326326
CWalletDB walletdb(wallet->strWalletFile);
327-
wallet->AddToWallet(wtx, false, &walletdb);
327+
wallet->AddToWallet(wtx, &walletdb);
328328
}
329329

330330
// Add to zpivTracker which also adds to database

src/zpivchain.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -443,7 +443,7 @@ bool UpdateZPIVSupplyConnect(const CBlock& block, CBlockIndex* pindex, bool fJus
443443
CWalletTx wtx(pwalletMain, tx);
444444
wtx.nTimeReceived = block.GetBlockTime();
445445
wtx.SetMerkleBranch(block);
446-
pwalletMain->AddToWallet(wtx, false, nullptr);
446+
pwalletMain->AddToWallet(wtx, nullptr);
447447
setAddedToWallet.insert(txid);
448448
}
449449
}

0 commit comments

Comments
 (0)