@@ -1094,6 +1094,9 @@ CWalletTx* CWallet::AddToWallet(CTransactionRef tx, const TxState& state, const
10941094 // Break debit/credit balance caches:
10951095 wtx.MarkDirty ();
10961096
1097+ // Cache the outputs that belong to the wallet
1098+ RefreshTXOsFromTx (wtx);
1099+
10971100 // Notify UI of new or updated transaction
10981101 NotifyTransactionChanged (hash, fInsertedNew ? CT_NEW : CT_UPDATED);
10991102
@@ -1157,6 +1160,8 @@ bool CWallet::LoadToWallet(const Txid& hash, const UpdateWalletTxFn& fill_wtx)
11571160 // Update birth time when tx time is older than it.
11581161 MaybeUpdateBirthTime (wtx.GetTxTime ());
11591162
1163+ // Make sure the tx outputs are known by the wallet
1164+ RefreshTXOsFromTx (wtx);
11601165 return true ;
11611166}
11621167
@@ -2324,6 +2329,9 @@ util::Result<void> CWallet::RemoveTxs(WalletBatch& batch, std::vector<Txid>& txs
23242329 wtxOrdered.erase (it->second .m_it_wtxOrdered );
23252330 for (const auto & txin : it->second .tx ->vin )
23262331 mapTxSpends.erase (txin.prevout );
2332+ for (unsigned int i = 0 ; i < it->second .tx ->vout .size (); ++i) {
2333+ m_txos.erase (COutPoint (Txid::FromUint256 (hash), i));
2334+ }
23272335 mapWallet.erase (it);
23282336 NotifyTransactionChanged (hash, CT_DELETED);
23292337 }
@@ -4425,4 +4433,22 @@ void CWallet::WriteBestBlock() const
44254433 batch.WriteBestBlock (loc);
44264434 }
44274435}
4436+
4437+ void CWallet::RefreshTXOsFromTx (const CWalletTx& wtx)
4438+ {
4439+ AssertLockHeld (cs_wallet);
4440+ for (uint32_t i = 0 ; i < wtx.tx ->vout .size (); ++i) {
4441+ const CTxOut& txout = wtx.tx ->vout .at (i);
4442+ isminetype ismine = IsMine (txout);
4443+ if (ismine == ISMINE_NO) {
4444+ continue ;
4445+ }
4446+ COutPoint outpoint (wtx.GetHash (), i);
4447+ if (m_txos.contains (outpoint)) {
4448+ m_txos.at (outpoint).SetIsMine (ismine);
4449+ } else {
4450+ m_txos.emplace (outpoint, WalletTXO{wtx, txout, ismine});
4451+ }
4452+ }
4453+ }
44284454} // namespace wallet
0 commit comments