Remove transactions when ensuring size limit

This commit is contained in:
Eirik Ogilvie-Wigley 2019-09-30 10:46:15 -06:00
parent 0b2c5cfcc2
commit 1407fd10a1
3 changed files with 16 additions and 15 deletions

View File

@ -1637,12 +1637,7 @@ bool AcceptToMemoryPool(CTxMemPool& pool, CValidationState &state, const CTransa
pool.addSpentIndex(entry, view); pool.addSpentIndex(entry, view);
} }
// In normal circumstances the following should be boost::none pool.ensureSizeLimit();
boost::optional<uint256> maybeDropTxId;
list<CTransaction> removed;
while ((maybeDropTxId = pool.ensureSizeLimit()).is_initialized()) {
pool.remove(pool.mapTx.find(maybeDropTxId.get())->GetTx(), removed, true);
}
} }
} }

View File

@ -68,6 +68,8 @@ CTxMemPool::CTxMemPool(const CFeeRate& _minRelayFee) :
CTxMemPool::~CTxMemPool() CTxMemPool::~CTxMemPool()
{ {
delete minerPolicyEstimator; delete minerPolicyEstimator;
delete recentlyEvicted;
delete weightedTxList;
} }
void CTxMemPool::pruneSpent(const uint256 &hashTx, CCoins &coins) void CTxMemPool::pruneSpent(const uint256 &hashTx, CCoins &coins)
@ -819,16 +821,20 @@ void CTxMemPool::setMempoolCostLimit(int64_t totalCostLimit, int64_t evictionMem
} }
bool CTxMemPool::isRecentlyEvicted(const uint256& txId) { bool CTxMemPool::isRecentlyEvicted(const uint256& txId) {
if (!recentlyEvicted) { LOCK(cs);
return false; assert(recentlyEvicted);
}
return recentlyEvicted->contains(txId); return recentlyEvicted->contains(txId);
} }
boost::optional<uint256> CTxMemPool::ensureSizeLimit() { void CTxMemPool::ensureSizeLimit() {
auto maybeDrop = weightedTxList->maybeDropRandom(false); AssertLockHeld(cs);
if (maybeDrop) { assert(recentlyEvicted);
return maybeDrop.get().txId; assert(weightedTxList);
boost::optional<WeightedTxInfo> maybeDrop;
std::list<CTransaction> removed;
while ((maybeDrop = weightedTxList->maybeDropRandom(false)).is_initialized()) {
uint256 txId = maybeDrop.get().txId;
recentlyEvicted->add(txId);
remove(mapTx.find(txId)->GetTx(), removed, true);
} }
return boost::none;
} }

View File

@ -268,7 +268,7 @@ public:
// Returns true if a transaction has been recently evicted // Returns true if a transaction has been recently evicted
bool isRecentlyEvicted(const uint256& txId); bool isRecentlyEvicted(const uint256& txId);
// Returns a txid if a transaction is evicted from the mempool // Returns a txid if a transaction is evicted from the mempool
boost::optional<uint256> ensureSizeLimit(); void ensureSizeLimit();
}; };
/** /**