From 8faa5dec89d1a3e51e58a9631edd965b1b6e007a Mon Sep 17 00:00:00 2001 From: Duke Leto Date: Wed, 22 Dec 2021 12:12:13 -0500 Subject: [PATCH] Erase old unconfirmed wallet transactions that are too old to be accepted by network consensus rules --- src/wallet/wallet.cpp | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/src/wallet/wallet.cpp b/src/wallet/wallet.cpp index 4217e3825..be3da40db 100644 --- a/src/wallet/wallet.cpp +++ b/src/wallet/wallet.cpp @@ -1662,6 +1662,9 @@ void CWallet::EraseFromWallet(const uint256 &hash) if (mapWallet.erase(hash)) CWalletDB(strWalletFile).EraseTx(hash); } + if(fDebug) { + LogPrintf("%s: erased txid %s\n", __func__, hash.ToString().c_str() ); + } return; } @@ -3052,6 +3055,8 @@ std::vector CWallet::ResendWalletTransactionsBefore(int64_t nTime) multimap mapSorted; uint32_t now = (uint32_t)time(NULL); std::vector vwtxh; + uint32_t erased = 0; + BOOST_FOREACH(PAIRTYPE(const uint256, CWalletTx)& item, mapWallet) { CWalletTx& wtx = item.second; @@ -3061,12 +3066,16 @@ std::vector CWallet::ResendWalletTransactionsBefore(int64_t nTime) if ( (wtx.nLockTime >= LOCKTIME_THRESHOLD && wtx.nLockTime < now-HUSH_MAXMEMPOOLTIME) ) { - LogPrintf("%s: skip Relaying wtx %s nLockTime %u vs now.%u\n", __func__, wtx.GetHash().ToString(),(uint32_t)wtx.nLockTime,now); - //vwtxh.push_back(wtx.GetHash()); + if(fDebug) { + LogPrintf("%s: skip Relaying wtx %s nLockTime %u vs now.%u\n", __func__, wtx.GetHash().ToString(),(uint32_t)wtx.nLockTime,now); + } + erased++; + vwtxh.push_back(wtx.GetHash()); continue; } mapSorted.insert(make_pair(wtx.nTimeReceived, &wtx)); } + BOOST_FOREACH(PAIRTYPE(const unsigned int, CWalletTx*)& item, mapSorted) { if ( item.second != 0 ) @@ -3076,10 +3085,19 @@ std::vector CWallet::ResendWalletTransactionsBefore(int64_t nTime) result.push_back(wtx.GetHash()); } } + + // Unless we remove these unconfirmed txs from the wallet, they will + // persist there forever. They are too old to be accepted by network + // consensus rules, so we erase them. for (auto hash : vwtxh) { - EraseFromWallets(hash); + EraseFromWallet(hash); } + + if(erased > 0) { + LogPrintf("%s: Prevented relaying and erased %d transactions which are too old\n", __func__, erased); + } + return result; }