Skip to content

Commit f16d28c

Browse files
committed
[wallet] Get rid of CWalletTx default constructor
No change in behavior. This makes CWalletTx construction more explicit and will now trigger compiler errors on mapWallet[hash] lookups that could inadvertently create dummy transaction entries.
1 parent 0fdca91 commit f16d28c

File tree

4 files changed

+18
-20
lines changed

4 files changed

+18
-20
lines changed

src/wallet/test/accounting_tests.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ GetResults(CWallet *wallet, std::map<CAmount, CAccountingEntry>& results)
2929
BOOST_AUTO_TEST_CASE(acc_orderupgrade)
3030
{
3131
std::vector<CWalletTx*> vpwtx;
32-
CWalletTx wtx;
32+
CWalletTx wtx(nullptr /* pwallet */, MakeTransactionRef());
3333
CAccountingEntry ae;
3434
std::map<CAmount, CAccountingEntry> results;
3535

@@ -44,7 +44,7 @@ BOOST_AUTO_TEST_CASE(acc_orderupgrade)
4444

4545
wtx.mapValue["comment"] = "z";
4646
pwalletMain->AddToWallet(wtx);
47-
vpwtx.push_back(&pwalletMain->mapWallet[wtx.GetHash()]);
47+
vpwtx.push_back(&pwalletMain->mapWallet.at(wtx.GetHash()));
4848
vpwtx[0]->nTimeReceived = (unsigned int)1333333335;
4949
vpwtx[0]->nOrderPos = -1;
5050

@@ -86,7 +86,7 @@ BOOST_AUTO_TEST_CASE(acc_orderupgrade)
8686
wtx.SetTx(MakeTransactionRef(std::move(tx)));
8787
}
8888
pwalletMain->AddToWallet(wtx);
89-
vpwtx.push_back(&pwalletMain->mapWallet[wtx.GetHash()]);
89+
vpwtx.push_back(&pwalletMain->mapWallet.at(wtx.GetHash()));
9090
vpwtx[1]->nTimeReceived = (unsigned int)1333333336;
9191

9292
wtx.mapValue["comment"] = "x";
@@ -96,7 +96,7 @@ BOOST_AUTO_TEST_CASE(acc_orderupgrade)
9696
wtx.SetTx(MakeTransactionRef(std::move(tx)));
9797
}
9898
pwalletMain->AddToWallet(wtx);
99-
vpwtx.push_back(&pwalletMain->mapWallet[wtx.GetHash()]);
99+
vpwtx.push_back(&pwalletMain->mapWallet.at(wtx.GetHash()));
100100
vpwtx[2]->nTimeReceived = (unsigned int)1333333329;
101101
vpwtx[2]->nOrderPos = -1;
102102

src/wallet/wallet.cpp

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -526,11 +526,11 @@ void CWallet::SyncMetaData(std::pair<TxSpends::iterator, TxSpends::iterator> ran
526526
for (TxSpends::iterator it = range.first; it != range.second; ++it)
527527
{
528528
const uint256& hash = it->second;
529-
int n = mapWallet[hash].nOrderPos;
529+
int n = mapWallet.at(hash).nOrderPos;
530530
if (n < nMinOrderPos)
531531
{
532532
nMinOrderPos = n;
533-
copyFrom = &mapWallet[hash];
533+
copyFrom = &mapWallet.at(hash);
534534
}
535535
}
536536

@@ -540,7 +540,7 @@ void CWallet::SyncMetaData(std::pair<TxSpends::iterator, TxSpends::iterator> ran
540540
for (TxSpends::iterator it = range.first; it != range.second; ++it)
541541
{
542542
const uint256& hash = it->second;
543-
CWalletTx* copyTo = &mapWallet[hash];
543+
CWalletTx* copyTo = &mapWallet.at(hash);
544544
if (copyFrom == copyTo) continue;
545545
assert(copyFrom && "Oldest wallet transaction in range assumed to have been found.");
546546
if (!copyFrom->IsEquivalentTo(*copyTo)) continue;
@@ -979,8 +979,11 @@ bool CWallet::LoadToWallet(const CWalletTx& wtxIn)
979979
{
980980
uint256 hash = wtxIn.GetHash();
981981

982-
mapWallet[hash] = wtxIn;
983-
CWalletTx& wtx = mapWallet[hash];
982+
auto inserted = mapWallet.emplace(hash, wtxIn);
983+
CWalletTx& wtx = inserted.first->second;
984+
if (!inserted.second) {
985+
wtx = wtxIn;
986+
}
984987
wtx.BindWallet(this);
985988
wtxOrdered.insert(std::make_pair(wtx.nOrderPos, TxPair(&wtx, nullptr)));
986989
AddToSpends(hash);
@@ -3023,7 +3026,7 @@ bool CWallet::CommitTransaction(CTransactionRef tx, mapValue_t mapValue, std::ve
30233026
// Notify that old coins are spent
30243027
for (const CTxIn& txin : wtxNew.tx->vin)
30253028
{
3026-
CWalletTx &coin = mapWallet[txin.prevout.hash];
3029+
CWalletTx &coin = mapWallet.at(txin.prevout.hash);
30273030
coin.BindWallet(this);
30283031
NotifyTransactionChanged(this, coin.GetHash(), CT_UPDATED);
30293032
}
@@ -3034,7 +3037,7 @@ bool CWallet::CommitTransaction(CTransactionRef tx, mapValue_t mapValue, std::ve
30343037

30353038
// Get the inserted-CWalletTx from mapWallet so that the
30363039
// fInMempool flag is cached properly
3037-
CWalletTx& wtx = mapWallet[wtxNew.GetHash()];
3040+
CWalletTx& wtx = mapWallet.at(wtxNew.GetHash());
30383041

30393042
if (fBroadcastTransactions)
30403043
{
@@ -3490,7 +3493,7 @@ std::set< std::set<CTxDestination> > CWallet::GetAddressGroupings()
34903493
CTxDestination address;
34913494
if(!IsMine(txin)) /* If this input isn't mine, ignore it */
34923495
continue;
3493-
if(!ExtractDestination(mapWallet[txin.prevout.hash].tx->vout[txin.prevout.n].scriptPubKey, address))
3496+
if(!ExtractDestination(mapWallet.at(txin.prevout.hash).tx->vout[txin.prevout.n].scriptPubKey, address))
34943497
continue;
34953498
grouping.insert(address);
34963499
any_mine = true;

src/wallet/wallet.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -335,11 +335,6 @@ class CWalletTx : public CMerkleTx
335335
mutable CAmount nAvailableWatchCreditCached;
336336
mutable CAmount nChangeCached;
337337

338-
CWalletTx()
339-
{
340-
Init(nullptr);
341-
}
342-
343338
CWalletTx(const CWallet* pwalletIn, CTransactionRef arg) : CMerkleTx(std::move(arg))
344339
{
345340
Init(pwalletIn);

src/wallet/walletdb.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ ReadKeyValue(CWallet* pwallet, CDataStream& ssKey, CDataStream& ssValue,
265265
{
266266
uint256 hash;
267267
ssKey >> hash;
268-
CWalletTx wtx;
268+
CWalletTx wtx(nullptr /* pwallet */, MakeTransactionRef());
269269
ssValue >> wtx;
270270
CValidationState state;
271271
if (!(CheckTransaction(*wtx.tx, state) && (wtx.GetHash() == hash) && state.IsValid()))
@@ -607,7 +607,7 @@ DBErrors CWalletDB::LoadWallet(CWallet* pwallet)
607607
pwallet->UpdateTimeFirstKey(1);
608608

609609
for (uint256 hash : wss.vWalletUpgrade)
610-
WriteTx(pwallet->mapWallet[hash]);
610+
WriteTx(pwallet->mapWallet.at(hash));
611611

612612
// Rewrite encrypted wallets of versions 0.4.0 and 0.5.0rc:
613613
if (wss.fIsEncrypted && (wss.nFileVersion == 40000 || wss.nFileVersion == 50000))
@@ -669,7 +669,7 @@ DBErrors CWalletDB::FindWalletTx(std::vector<uint256>& vTxHash, std::vector<CWal
669669
uint256 hash;
670670
ssKey >> hash;
671671

672-
CWalletTx wtx;
672+
CWalletTx wtx(nullptr /* pwallet */, MakeTransactionRef());
673673
ssValue >> wtx;
674674

675675
vTxHash.push_back(hash);

0 commit comments

Comments
 (0)