Skip to content

Commit e9c3215

Browse files
committed
[Wallet] sort pending wallet transactions before reaccepting
During startup, when adding pending wallet transactions, which spend outputs of other pending wallet transactions, back to the memory pool, and when they are added out of order, it appears as if they are orphans with missing inputs. Those transactions are then rejected and flagged as "conflicting" (= not in the memory pool, not in the block chain). To prevent this, transactions are explicitly sorted.
1 parent f3948a3 commit e9c3215

File tree

1 file changed

+14
-5
lines changed

1 file changed

+14
-5
lines changed

src/wallet/wallet.cpp

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1028,6 +1028,9 @@ int CWallet::ScanForWalletTransactions(CBlockIndex* pindexStart, bool fUpdate)
10281028
void CWallet::ReacceptWalletTransactions()
10291029
{
10301030
LOCK2(cs_main, cs_wallet);
1031+
std::map<int64_t, CWalletTx*> mapSorted;
1032+
1033+
// Sort pending wallet transactions based on their initial wallet insertion order
10311034
BOOST_FOREACH(PAIRTYPE(const uint256, CWalletTx)& item, mapWallet)
10321035
{
10331036
const uint256& wtxid = item.first;
@@ -1036,13 +1039,19 @@ void CWallet::ReacceptWalletTransactions()
10361039

10371040
int nDepth = wtx.GetDepthInMainChain();
10381041

1039-
if (!wtx.IsCoinBase() && nDepth < 0)
1040-
{
1041-
// Try to add to memory pool
1042-
LOCK(mempool.cs);
1043-
wtx.AcceptToMemoryPool(false);
1042+
if (!wtx.IsCoinBase() && nDepth < 0) {
1043+
mapSorted.insert(std::make_pair(wtx.nOrderPos, &wtx));
10441044
}
10451045
}
1046+
1047+
// Try to add wallet transactions to memory pool
1048+
BOOST_FOREACH(PAIRTYPE(const int64_t, CWalletTx*)& item, mapSorted)
1049+
{
1050+
CWalletTx& wtx = *(item.second);
1051+
1052+
LOCK(mempool.cs);
1053+
wtx.AcceptToMemoryPool(false);
1054+
}
10461055
}
10471056

10481057
void CWalletTx::RelayWalletTransaction()

0 commit comments

Comments
 (0)