Skip to content

Commit fe84e57

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 7b44eb8 commit fe84e57

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
@@ -532,11 +532,11 @@ void CWallet::SyncMetaData(std::pair<TxSpends::iterator, TxSpends::iterator> ran
532532
for (TxSpends::iterator it = range.first; it != range.second; ++it)
533533
{
534534
const uint256& hash = it->second;
535-
int n = mapWallet[hash].nOrderPos;
535+
int n = mapWallet.at(hash).nOrderPos;
536536
if (n < nMinOrderPos)
537537
{
538538
nMinOrderPos = n;
539-
copyFrom = &mapWallet[hash];
539+
copyFrom = &mapWallet.at(hash);
540540
}
541541
}
542542

@@ -546,7 +546,7 @@ void CWallet::SyncMetaData(std::pair<TxSpends::iterator, TxSpends::iterator> ran
546546
for (TxSpends::iterator it = range.first; it != range.second; ++it)
547547
{
548548
const uint256& hash = it->second;
549-
CWalletTx* copyTo = &mapWallet[hash];
549+
CWalletTx* copyTo = &mapWallet.at(hash);
550550
if (copyFrom == copyTo) continue;
551551
assert(copyFrom && "Oldest wallet transaction in range assumed to have been found.");
552552
if (!copyFrom->IsEquivalentTo(*copyTo)) continue;
@@ -985,8 +985,11 @@ bool CWallet::LoadToWallet(const CWalletTx& wtxIn)
985985
{
986986
uint256 hash = wtxIn.GetHash();
987987

988-
mapWallet[hash] = wtxIn;
989-
CWalletTx& wtx = mapWallet[hash];
988+
auto inserted = mapWallet.emplace(hash, wtxIn);
989+
CWalletTx& wtx = inserted.first->second;
990+
if (!inserted.second) {
991+
wtx = wtxIn;
992+
}
990993
wtx.BindWallet(this);
991994
wtxOrdered.insert(std::make_pair(wtx.nOrderPos, TxPair(&wtx, nullptr)));
992995
AddToSpends(hash);
@@ -3027,7 +3030,7 @@ bool CWallet::CommitTransaction(CTransactionRef tx, mapValue_t mapValue, std::ve
30273030
// Notify that old coins are spent
30283031
for (const CTxIn& txin : wtxNew.tx->vin)
30293032
{
3030-
CWalletTx &coin = mapWallet[txin.prevout.hash];
3033+
CWalletTx &coin = mapWallet.at(txin.prevout.hash);
30313034
coin.BindWallet(this);
30323035
NotifyTransactionChanged(this, coin.GetHash(), CT_UPDATED);
30333036
}
@@ -3038,7 +3041,7 @@ bool CWallet::CommitTransaction(CTransactionRef tx, mapValue_t mapValue, std::ve
30383041

30393042
// Get the inserted-CWalletTx from mapWallet so that the
30403043
// fInMempool flag is cached properly
3041-
CWalletTx& wtx = mapWallet[wtxNew.GetHash()];
3044+
CWalletTx& wtx = mapWallet.at(wtxNew.GetHash());
30423045

30433046
if (fBroadcastTransactions)
30443047
{
@@ -3494,7 +3497,7 @@ std::set< std::set<CTxDestination> > CWallet::GetAddressGroupings()
34943497
CTxDestination address;
34953498
if(!IsMine(txin)) /* If this input isn't mine, ignore it */
34963499
continue;
3497-
if(!ExtractDestination(mapWallet[txin.prevout.hash].tx->vout[txin.prevout.n].scriptPubKey, address))
3500+
if(!ExtractDestination(mapWallet.at(txin.prevout.hash).tx->vout[txin.prevout.n].scriptPubKey, address))
34983501
continue;
34993502
grouping.insert(address);
35003503
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()))
@@ -603,7 +603,7 @@ DBErrors CWalletDB::LoadWallet(CWallet* pwallet)
603603
pwallet->UpdateTimeFirstKey(1);
604604

605605
for (uint256 hash : wss.vWalletUpgrade)
606-
WriteTx(pwallet->mapWallet[hash]);
606+
WriteTx(pwallet->mapWallet.at(hash));
607607

608608
// Rewrite encrypted wallets of versions 0.4.0 and 0.5.0rc:
609609
if (wss.fIsEncrypted && (wss.nFileVersion == 40000 || wss.nFileVersion == 50000))
@@ -665,7 +665,7 @@ DBErrors CWalletDB::FindWalletTx(std::vector<uint256>& vTxHash, std::vector<CWal
665665
uint256 hash;
666666
ssKey >> hash;
667667

668-
CWalletTx wtx;
668+
CWalletTx wtx(nullptr /* pwallet */, MakeTransactionRef());
669669
ssValue >> wtx;
670670

671671
vTxHash.push_back(hash);

0 commit comments

Comments
 (0)