Skip to content

Commit db0ffe8

Browse files
committed
This eliminates the primary leak that causes the orphan map to
always grow to its maximum size. This does not go so far as to attempt to connect orphans made connectable by a new block. Keeping the orphan map less full helps improve the reliability of relaying chains of transactions.
1 parent 1b0bcc5 commit db0ffe8

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

src/main.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2345,6 +2345,7 @@ bool ConnectBlock(const CBlock& block, CValidationState& state, CBlockIndex* pin
23452345

23462346
CCheckQueueControl<CScriptCheck> control(fScriptChecks && nScriptCheckThreads ? &scriptcheckqueue : NULL);
23472347

2348+
std::vector<uint256> vOrphanErase;
23482349
std::vector<int> prevheights;
23492350
CAmount nFees = 0;
23502351
int nInputs = 0;
@@ -2377,6 +2378,17 @@ bool ConnectBlock(const CBlock& block, CValidationState& state, CBlockIndex* pin
23772378
prevheights[j] = view.AccessCoins(tx.vin[j].prevout.hash)->nHeight;
23782379
}
23792380

2381+
// Which orphan pool entries must we evict?
2382+
for (size_t j = 0; j < tx.vin.size(); j++) {
2383+
auto itByPrev = mapOrphanTransactionsByPrev.find(tx.vin[j].prevout);
2384+
if (itByPrev == mapOrphanTransactionsByPrev.end()) continue;
2385+
for (auto mi = itByPrev->second.begin(); mi != itByPrev->second.end(); ++mi) {
2386+
const CTransaction& orphanTx = (*mi)->second.tx;
2387+
const uint256& orphanHash = orphanTx.GetHash();
2388+
vOrphanErase.push_back(orphanHash);
2389+
}
2390+
}
2391+
23802392
if (!SequenceLocks(tx, nLockTimeFlags, &prevheights, *pindex)) {
23812393
return state.DoS(100, error("%s: contains a non-BIP68-final transaction", __func__),
23822394
REJECT_INVALID, "bad-txns-nonfinal");
@@ -2464,6 +2476,15 @@ bool ConnectBlock(const CBlock& block, CValidationState& state, CBlockIndex* pin
24642476
GetMainSignals().UpdatedTransaction(hashPrevBestCoinBase);
24652477
hashPrevBestCoinBase = block.vtx[0].GetHash();
24662478

2479+
// Erase orphan transactions include or precluded by this block
2480+
if (vOrphanErase.size()) {
2481+
int nErased = 0;
2482+
BOOST_FOREACH(uint256 &orphanHash, vOrphanErase) {
2483+
nErased += EraseOrphanTx(orphanHash);
2484+
}
2485+
LogPrint("mempool", "Erased %d orphan tx included or conflicted by block\n", nErased);
2486+
}
2487+
24672488
int64_t nTime6 = GetTimeMicros(); nTimeCallbacks += nTime6 - nTime5;
24682489
LogPrint("bench", " - Callbacks: %.2fms [%.2fs]\n", 0.001 * (nTime6 - nTime5), nTimeCallbacks * 0.000001);
24692490

0 commit comments

Comments
 (0)