Skip to content

Commit b9c1cd8

Browse files
committed
Merge #8152: [Wallet] Remove CWalletDB* parameter from CWallet::AddToWallet
5723bb4 Remove unused pwalletdb from CWallet::AddToWallet (Patrick Strateman) 867f842 Remove CWalletDB* parameter from CWallet::AddToWallet (Patrick Strateman) 00f09c9 Split CWallet::AddToWallet into AddToWallet and LoadToWallet. (Patrick Strateman)
2 parents 8ea7d31 + 5723bb4 commit b9c1cd8

File tree

5 files changed

+121
-128
lines changed

5 files changed

+121
-128
lines changed

src/wallet/rpcdump.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -309,8 +309,7 @@ UniValue importprunedfunds(const UniValue& params, bool fHelp)
309309
LOCK2(cs_main, pwalletMain->cs_wallet);
310310

311311
if (pwalletMain->IsMine(tx)) {
312-
CWalletDB walletdb(pwalletMain->strWalletFile, "r+", false);
313-
pwalletMain->AddToWallet(wtx, false, &walletdb);
312+
pwalletMain->AddToWallet(wtx, false);
314313
return NullUniValue;
315314
}
316315

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);
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);
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);
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: 114 additions & 121 deletions
Original file line numberDiff line numberDiff line change
@@ -741,138 +741,143 @@ void CWallet::MarkDirty()
741741
}
742742
}
743743

744-
bool CWallet::AddToWallet(const CWalletTx& wtxIn, bool fFromLoadWallet, CWalletDB* pwalletdb)
744+
bool CWallet::AddToWallet(const CWalletTx& wtxIn, bool fFlushOnClose)
745745
{
746+
LOCK(cs_wallet);
747+
748+
CWalletDB walletdb(strWalletFile, "r+", fFlushOnClose);
749+
746750
uint256 hash = wtxIn.GetHash();
747751

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)
749758
{
750-
mapWallet[hash] = wtxIn;
751-
CWalletTx& wtx = mapWallet[hash];
752-
wtx.BindWallet(this);
759+
wtx.nTimeReceived = GetAdjustedTime();
760+
wtx.nOrderPos = IncOrderPosNext(&walletdb);
753761
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))
780767
{
781-
if (mapBlockIndex.count(wtxIn.hashBlock))
768+
int64_t latestNow = wtx.nTimeReceived;
769+
int64_t latestEntry = 0;
782770
{
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)
785775
{
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)
790782
{
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;
811795
}
812796
}
813-
814-
int64_t blocktime = mapBlockIndex[wtxIn.hashBlock]->GetBlockTime();
815-
wtx.nTimeSmart = std::max(latestEntry, std::min(blocktime, latestNow));
816797
}
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));
821801
}
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());
823806
}
807+
AddToSpends(hash);
808+
}
824809

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)
827815
{
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;
850824
}
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+
}
851836

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" : ""));
854839

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;
859844

860-
// Break debit/credit balance caches:
861-
wtx.MarkDirty();
845+
// Break debit/credit balance caches:
846+
wtx.MarkDirty();
862847

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);
865850

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", "");
868853

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+
}
874862

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+
}
875879
}
880+
876881
return true;
877882
}
878883

@@ -909,11 +914,7 @@ bool CWallet::AddToWalletIfInvolvingMe(const CTransaction& tx, const CBlock* pbl
909914
if (pblock)
910915
wtx.SetMerkleBranch(*pblock);
911916

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);
917918
}
918919
}
919920
return false;
@@ -2446,17 +2447,12 @@ bool CWallet::CommitTransaction(CWalletTx& wtxNew, CReserveKey& reservekey)
24462447
LOCK2(cs_main, cs_wallet);
24472448
LogPrintf("CommitTransaction:\n%s", wtxNew.ToString());
24482449
{
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-
24542450
// Take key pair from key pool so it won't be used again
24552451
reservekey.KeepKey();
24562452

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

24612457
// Notify that old coins are spent
24622458
set<CWalletTx*> setCoins;
@@ -2466,9 +2462,6 @@ bool CWallet::CommitTransaction(CWalletTx& wtxNew, CReserveKey& reservekey)
24662462
coin.BindWallet(this);
24672463
NotifyTransactionChanged(this, coin.GetHash(), CT_UPDATED);
24682464
}
2469-
2470-
if (fFileBacked)
2471-
delete pwalletdb;
24722465
}
24732466

24742467
// Track how many getdata requests our transaction gets

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, bool fFlushOnClose=true);
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)