@@ -923,11 +923,10 @@ bool CWallet::AddToWallet(const CWalletTx& wtxIn, bool fFlushOnClose)
923
923
CWalletTx& wtx = (*ret.first ).second ;
924
924
wtx.BindWallet (this );
925
925
bool fInsertedNew = ret.second ;
926
- if (fInsertedNew )
927
- {
926
+ if (fInsertedNew ) {
928
927
wtx.nTimeReceived = GetAdjustedTime ();
929
928
wtx.nOrderPos = IncOrderPosNext (&batch);
930
- wtxOrdered.insert (std::make_pair (wtx.nOrderPos , TxPair (&wtx, nullptr )));
929
+ wtx. m_it_wtxOrdered = wtxOrdered.insert (std::make_pair (wtx.nOrderPos , TxPair (&wtx, nullptr )));
931
930
wtx.nTimeSmart = ComputeTimeSmart (wtx);
932
931
AddToSpends (hash);
933
932
}
@@ -998,9 +997,12 @@ bool CWallet::AddToWallet(const CWalletTx& wtxIn, bool fFlushOnClose)
998
997
bool CWallet::LoadToWallet (const CWalletTx& wtxIn)
999
998
{
1000
999
uint256 hash = wtxIn.GetHash ();
1001
- CWalletTx& wtx = mapWallet.emplace (hash, wtxIn).first ->second ;
1000
+ const auto & ins = mapWallet.emplace (hash, wtxIn);
1001
+ CWalletTx& wtx = ins.first ->second ;
1002
1002
wtx.BindWallet (this );
1003
- wtxOrdered.insert (std::make_pair (wtx.nOrderPos , TxPair (&wtx, nullptr )));
1003
+ if (/* insertion took place */ ins.second ) {
1004
+ wtx.m_it_wtxOrdered = wtxOrdered.insert (std::make_pair (wtx.nOrderPos , TxPair (&wtx, nullptr )));
1005
+ }
1004
1006
AddToSpends (hash);
1005
1007
for (const CTxIn& txin : wtx.tx ->vin ) {
1006
1008
auto it = mapWallet.find (txin.prevout .hash );
@@ -3206,8 +3208,11 @@ DBErrors CWallet::ZapSelectTx(std::vector<uint256>& vHashIn, std::vector<uint256
3206
3208
{
3207
3209
AssertLockHeld (cs_wallet); // mapWallet
3208
3210
DBErrors nZapSelectTxRet = WalletBatch (*database," cr+" ).ZapSelectTx (vHashIn, vHashOut);
3209
- for (uint256 hash : vHashOut)
3210
- mapWallet.erase (hash);
3211
+ for (uint256 hash : vHashOut) {
3212
+ const auto & it = mapWallet.find (hash);
3213
+ wtxOrdered.erase (it->second .m_it_wtxOrdered );
3214
+ mapWallet.erase (it);
3215
+ }
3211
3216
3212
3217
if (nZapSelectTxRet == DBErrors::NEED_REWRITE)
3213
3218
{
0 commit comments