Skip to content

Commit

Permalink
Split CWallet::AddToWallet into AddToWallet and LoadToWallet.
Browse files Browse the repository at this point in the history
This removes the fFromLoadWallet flag in AddToWallet.  These were already
effectively two methods.

Coming from btc@00f09c920c2e8906d2260251be6d1d2fa1bbb29d

Github-Pull: #1717
Rebased-From: c3779c5
  • Loading branch information
furszy authored and Fuzzbawls committed Jul 22, 2020
1 parent 9aacb38 commit 609407d
Show file tree
Hide file tree
Showing 8 changed files with 80 additions and 77 deletions.
2 changes: 1 addition & 1 deletion src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2440,7 +2440,7 @@ bool ConnectBlock(const CBlock& block, CValidationState& state, CBlockIndex* pin
CWalletTx wtx(pwalletMain, tx);
wtx.nTimeReceived = pindex->GetBlockTime();
wtx.SetMerkleBranch(block);
pwalletMain->AddToWallet(wtx, false, nullptr);
pwalletMain->AddToWallet(wtx, nullptr);
setAddedTx.insert(pSpend.second);
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/test/accounting_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ BOOST_AUTO_TEST_CASE(acc_orderupgrade)
pwalletMain->AddAccountingEntry(ae, walletdb);

wtx.mapValue["comment"] = "z";
pwalletMain->AddToWallet(wtx, false, &walletdb);
pwalletMain->AddToWallet(wtx, &walletdb);
vpwtx.push_back(&pwalletMain->mapWallet[wtx.GetHash()]);
vpwtx[0]->nTimeReceived = (unsigned int)1333333335;
vpwtx[0]->nOrderPos = -1;
Expand Down Expand Up @@ -90,7 +90,7 @@ BOOST_AUTO_TEST_CASE(acc_orderupgrade)
--tx.nLockTime; // Just to change the hash :)
*static_cast<CTransaction*>(&wtx) = CTransaction(tx);
}
pwalletMain->AddToWallet(wtx, false, &walletdb);
pwalletMain->AddToWallet(wtx, &walletdb);
vpwtx.push_back(&pwalletMain->mapWallet[wtx.GetHash()]);
vpwtx[1]->nTimeReceived = (unsigned int)1333333336;

Expand All @@ -100,7 +100,7 @@ BOOST_AUTO_TEST_CASE(acc_orderupgrade)
--tx.nLockTime; // Just to change the hash :)
*static_cast<CTransaction*>(&wtx) = CTransaction(tx);
}
pwalletMain->AddToWallet(wtx, false, &walletdb);
pwalletMain->AddToWallet(wtx, &walletdb);
vpwtx.push_back(&pwalletMain->mapWallet[wtx.GetHash()]);
vpwtx[2]->nTimeReceived = (unsigned int)1333333329;
vpwtx[2]->nOrderPos = -1;
Expand Down
134 changes: 68 additions & 66 deletions src/wallet/wallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -811,83 +811,85 @@ void CWallet::MarkDirty()
}
}

bool CWallet::AddToWallet(const CWalletTx& wtxIn, bool fFromLoadWallet, CWalletDB* pwalletdb)
bool CWallet::AddToWallet(const CWalletTx& wtxIn, CWalletDB* pwalletdb)
{
uint256 hash = wtxIn.GetHash();

if (fFromLoadWallet) {
mapWallet[hash] = wtxIn;
CWalletTx& wtx = mapWallet[hash];
wtx.BindWallet(this);
LOCK(cs_wallet);
// Inserts only if not already there, returns tx inserted or tx found
std::pair<std::map<uint256, CWalletTx>::iterator, bool> ret = mapWallet.insert(std::make_pair(hash, wtxIn));
CWalletTx& wtx = (*ret.first).second;
wtx.BindWallet(this);
bool fInsertedNew = ret.second;
if (fInsertedNew) {
wtx.nTimeReceived = GetAdjustedTime();
wtx.nOrderPos = IncOrderPosNext(pwalletdb);
wtxOrdered.insert(std::make_pair(wtx.nOrderPos, TxPair(&wtx, (CAccountingEntry*)0)));
wtx.UpdateTimeSmart();
AddToSpends(hash);
for (const CTxIn& txin : wtx.vin) {
if (mapWallet.count(txin.prevout.hash)) {
CWalletTx& prevtx = mapWallet[txin.prevout.hash];
if (prevtx.nIndex == -1 && !prevtx.hashUnset()) {
MarkConflicted(prevtx.hashBlock, wtx.GetHash());
}
}
}
} else {
LOCK(cs_wallet);
// Inserts only if not already there, returns tx inserted or tx found
std::pair<std::map<uint256, CWalletTx>::iterator, bool> ret = mapWallet.insert(std::make_pair(hash, wtxIn));
CWalletTx& wtx = (*ret.first).second;
wtx.BindWallet(this);
bool fInsertedNew = ret.second;
if (fInsertedNew) {
wtx.nTimeReceived = GetAdjustedTime();
wtx.nOrderPos = IncOrderPosNext(pwalletdb);
wtxOrdered.insert(std::make_pair(wtx.nOrderPos, TxPair(&wtx, (CAccountingEntry*)0)));
}

bool fUpdated = false;
if (!fInsertedNew) {
// Merge
if (!wtxIn.hashUnset() && wtxIn.hashBlock != wtx.hashBlock) {
wtx.hashBlock = wtxIn.hashBlock;
wtx.UpdateTimeSmart();
AddToSpends(hash);
fUpdated = true;
}

bool fUpdated = false;
if (!fInsertedNew) {
// Merge
if (!wtxIn.hashUnset() && wtxIn.hashBlock != wtx.hashBlock) {
wtx.hashBlock = wtxIn.hashBlock;
wtx.UpdateTimeSmart();
fUpdated = true;
}
// If no longer abandoned, update
if (wtxIn.hashBlock.IsNull() && wtx.isAbandoned()) {
wtx.hashBlock = wtxIn.hashBlock;
if (!fUpdated) wtx.UpdateTimeSmart();
fUpdated = true;
}
if (wtxIn.nIndex != -1 && wtxIn.nIndex != wtx.nIndex) {
wtx.nIndex = wtxIn.nIndex;
fUpdated = true;
}
if (wtxIn.fFromMe && wtxIn.fFromMe != wtx.fFromMe) {
wtx.fFromMe = wtxIn.fFromMe;
fUpdated = true;
}
// If no longer abandoned, update
if (wtxIn.hashBlock.IsNull() && wtx.isAbandoned()) {
wtx.hashBlock = wtxIn.hashBlock;
if (!fUpdated) wtx.UpdateTimeSmart();
fUpdated = true;
}
if (wtxIn.nIndex != -1 && wtxIn.nIndex != wtx.nIndex) {
wtx.nIndex = wtxIn.nIndex;
fUpdated = true;
}
if (wtxIn.fFromMe && wtxIn.fFromMe != wtx.fFromMe) {
wtx.fFromMe = wtxIn.fFromMe;
fUpdated = true;
}
}

//// debug print
LogPrintf("AddToWallet %s %s%s\n", wtxIn.GetHash().ToString(), (fInsertedNew ? "new" : ""), (fUpdated ? "update" : ""));
//// debug print
LogPrintf("AddToWallet %s %s%s\n", wtxIn.GetHash().ToString(), (fInsertedNew ? "new" : ""), (fUpdated ? "update" : ""));

// Write to disk
if (fInsertedNew || fUpdated)
if (!pwalletdb->WriteTx(wtx))
return false;
// Write to disk
if (fInsertedNew || fUpdated)
if (!pwalletdb->WriteTx(wtx))
return false;

// Break debit/credit balance caches:
wtx.MarkDirty();
// Break debit/credit balance caches:
wtx.MarkDirty();

// Notify UI of new or updated transaction
NotifyTransactionChanged(this, hash, fInsertedNew ? CT_NEW : CT_UPDATED);
// Notify UI of new or updated transaction
NotifyTransactionChanged(this, hash, fInsertedNew ? CT_NEW : CT_UPDATED);

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

if (!strCmd.empty()) {
boost::replace_all(strCmd, "%s", wtxIn.GetHash().GetHex());
boost::thread t(runCommand, strCmd); // thread runs free
if (!strCmd.empty()) {
boost::replace_all(strCmd, "%s", wtxIn.GetHash().GetHex());
boost::thread t(runCommand, strCmd); // thread runs free
}
return true;
}

bool CWallet::LoadToWallet(const CWalletTx& wtxIn)
{
uint256 hash = wtxIn.GetHash();
mapWallet[hash] = wtxIn;
CWalletTx& wtx = mapWallet[hash];
wtx.BindWallet(this);
wtxOrdered.insert(std::make_pair(wtx.nOrderPos, TxPair(&wtx, (CAccountingEntry*)0)));
AddToSpends(hash);
for (const CTxIn& txin : wtx.vin) {
if (mapWallet.count(txin.prevout.hash)) {
CWalletTx& prevtx = mapWallet[txin.prevout.hash];
if (prevtx.nIndex == -1 && !prevtx.hashUnset()) {
MarkConflicted(prevtx.hashBlock, wtx.GetHash());
}
}
}
return true;
Expand Down Expand Up @@ -939,7 +941,7 @@ bool CWallet::AddToWalletIfInvolvingMe(const CTransaction& tx, const CBlock* pbl
// this is safe, as in case of a crash, we rescan the necessary blocks on startup through our SetBestChain-mechanism
CWalletDB walletdb(strWalletFile, "r+", false);

return AddToWallet(wtx, false, &walletdb);
return AddToWallet(wtx, &walletdb);
}
}
return false;
Expand Down Expand Up @@ -2943,7 +2945,7 @@ CWallet::CommitResult CWallet::CommitTransaction(CWalletTx& wtxNew, CReserveKey&

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

// Notify that old coins are spent
if (!wtxNew.HasZerocoinSpendInputs()) {
Expand Down
3 changes: 2 additions & 1 deletion src/wallet/wallet.h
Original file line number Diff line number Diff line change
Expand Up @@ -433,7 +433,8 @@ class CWallet : public CCryptoKeyStore, public CValidationInterface

bool GetLabelDestination(CTxDestination& dest, const std::string& label, bool bForceNew = false);
void MarkDirty();
bool AddToWallet(const CWalletTx& wtxIn, bool fFromLoadWallet, CWalletDB* pwalletdb);
bool AddToWallet(const CWalletTx& wtxIn, CWalletDB* pwalletdb);
bool LoadToWallet(const CWalletTx& wtxIn);
void SyncTransaction(const CTransaction& tx, const CBlock* pblock);
bool AddToWalletIfInvolvingMe(const CTransaction& tx, const CBlock* pblock, bool fUpdate);
void EraseFromWallet(const uint256& hash);
Expand Down
4 changes: 2 additions & 2 deletions src/wallet/wallet_zerocoin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ void CWallet::doZPivRescan(const CBlockIndex* pindex, const CBlock& block,
CWalletTx wtx(this, tx);
wtx.nTimeReceived = block.GetBlockTime();
wtx.SetMerkleBranch(block);
AddToWallet(wtx, false, &walletdb);
AddToWallet(wtx, &walletdb);
setAddedToWallet.insert(txid);
}
}
Expand All @@ -137,7 +137,7 @@ void CWallet::doZPivRescan(const CBlockIndex* pindex, const CBlock& block,
wtx.SetMerkleBranch(blockSpend);

wtx.nTimeReceived = pindexSpend->nTime;
AddToWallet(wtx, false, &walletdb);
AddToWallet(wtx, &walletdb);
setAddedToWallet.emplace(txidSpend);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/wallet/walletdb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -491,7 +491,7 @@ bool ReadKeyValue(CWallet* pwallet, CDataStream& ssKey, CDataStream& ssValue, CW
if (wtx.nOrderPos == -1)
wss.fAnyUnordered = true;

pwallet->AddToWallet(wtx, true, nullptr);
pwallet->LoadToWallet(wtx);
} else if (strType == "acentry") {
std::string strAccount;
ssKey >> strAccount;
Expand Down
4 changes: 2 additions & 2 deletions src/zpiv/zpivwallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ void CzPIVWallet::SyncWithChain(bool fGenerateMintPool)

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

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

wtx.nTimeReceived = pindex->nTime;
CWalletDB walletdb(wallet->strWalletFile);
wallet->AddToWallet(wtx, false, &walletdb);
wallet->AddToWallet(wtx, &walletdb);
}

// Add to zpivTracker which also adds to database
Expand Down
2 changes: 1 addition & 1 deletion src/zpivchain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -443,7 +443,7 @@ bool UpdateZPIVSupplyConnect(const CBlock& block, CBlockIndex* pindex, bool fJus
CWalletTx wtx(pwalletMain, tx);
wtx.nTimeReceived = block.GetBlockTime();
wtx.SetMerkleBranch(block);
pwalletMain->AddToWallet(wtx, false, nullptr);
pwalletMain->AddToWallet(wtx, nullptr);
setAddedToWallet.insert(txid);
}
}
Expand Down

0 comments on commit 609407d

Please sign in to comment.