Skip to content

Commit ad6d0b1

Browse files
dexX7Warrows
authored andcommitted
[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 240f5b4 commit ad6d0b1

File tree

1 file changed

+14
-4
lines changed

1 file changed

+14
-4
lines changed

src/wallet/wallet.cpp

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1482,19 +1482,29 @@ int CWallet::ScanForWalletTransactions(CBlockIndex* pindexStart, bool fUpdate)
14821482
void CWallet::ReacceptWalletTransactions()
14831483
{
14841484
LOCK2(cs_main, cs_wallet);
1485-
for (PAIRTYPE(const uint256, CWalletTx) & item : mapWallet) {
1485+
std::map<int64_t, CWalletTx*> mapSorted;
1486+
1487+
// Sort pending wallet transactions based on their initial wallet insertion order
1488+
for (PAIRTYPE(const uint256, CWalletTx)& item: mapWallet) {
14861489
const uint256& wtxid = item.first;
14871490
CWalletTx& wtx = item.second;
14881491
assert(wtx.GetHash() == wtxid);
14891492

14901493
int nDepth = wtx.GetDepthInMainChain();
14911494

14921495
if (!wtx.IsCoinBase() && !wtx.IsCoinStake() && nDepth == 0 && !wtx.isAbandoned()) {
1493-
// Try to add to memory pool
1494-
LOCK(mempool.cs);
1495-
wtx.AcceptToMemoryPool(false);
1496+
mapSorted.insert(std::make_pair(wtx.nOrderPos, &wtx));
14961497
}
14971498
}
1499+
1500+
// Try to add wallet transactions to memory pool
1501+
for (PAIRTYPE(const int64_t, CWalletTx*)& item: mapSorted)
1502+
{
1503+
CWalletTx& wtx = *(item.second);
1504+
1505+
LOCK(mempool.cs);
1506+
wtx.AcceptToMemoryPool(false);
1507+
}
14981508
}
14991509

15001510
bool CWalletTx::InMempool() const

0 commit comments

Comments
 (0)