Skip to content

Commit c5b36b1

Browse files
committed
Mempool Update Cut-Through Optimization
Often when we're updating mempool entries we update entries that we ultimately end up removing the updated entries shortly thereafter. This patch makes it so that we filter for such entries a bit earlier in processing, which yields a mild improvement for these cases, and is negligible overhead otherwise.
1 parent c49daf9 commit c5b36b1

File tree

3 files changed

+72
-32
lines changed

3 files changed

+72
-32
lines changed

src/txmempool.cpp

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -126,10 +126,9 @@ size_t CTxMemPoolEntry::GetTxSize() const
126126
return GetVirtualTransactionSize(nTxWeight, sigOpCost);
127127
}
128128

129-
// Update the given tx for any in-mempool descendants.
130-
// Assumes that CTxMemPool::m_children is correct for the given tx and all
131-
// descendants.
132-
void CTxMemPool::UpdateForDescendants(txiter updateIt, cacheMap &cachedDescendants, const std::set<uint256> &setExclude)
129+
void CTxMemPool::UpdateForDescendants(txiter updateIt, cacheMap& cachedDescendants,
130+
const std::set<uint256>& setExclude, std::set<uint256>& descendants_to_remove,
131+
uint64_t ancestor_size_limit, uint64_t ancestor_count_limit)
133132
{
134133
CTxMemPoolEntry::Children stageEntries, descendants;
135134
stageEntries = updateIt->GetMemPoolChildrenConst();
@@ -166,17 +165,18 @@ void CTxMemPool::UpdateForDescendants(txiter updateIt, cacheMap &cachedDescendan
166165
cachedDescendants[updateIt].insert(mapTx.iterator_to(descendant));
167166
// Update ancestor state for each descendant
168167
mapTx.modify(mapTx.iterator_to(descendant), update_ancestor_state(updateIt->GetTxSize(), updateIt->GetModifiedFee(), 1, updateIt->GetSigOpCost()));
168+
// Don't directly remove the transaction here -- doing so would
169+
// invalidate iterators in cachedDescendants. Mark it for removal
170+
// by inserting into descendants_to_remove.
171+
if (descendant.GetCountWithAncestors() > ancestor_count_limit || descendant.GetSizeWithAncestors() > ancestor_size_limit) {
172+
descendants_to_remove.insert(descendant.GetTx().GetHash());
173+
}
169174
}
170175
}
171176
mapTx.modify(updateIt, update_descendant_state(modifySize, modifyFee, modifyCount));
172177
}
173178

174-
// vHashesToUpdate is the set of transaction hashes from a disconnected block
175-
// which has been re-added to the mempool.
176-
// for each entry, look for descendants that are outside vHashesToUpdate, and
177-
// add fee/size information for such descendants to the parent.
178-
// for each such descendant, also update the ancestor state to include the parent.
179-
void CTxMemPool::UpdateTransactionsFromBlock(const std::vector<uint256> &vHashesToUpdate)
179+
void CTxMemPool::UpdateTransactionsFromBlock(const std::vector<uint256> &vHashesToUpdate, uint64_t ancestor_size_limit, uint64_t ancestor_count_limit)
180180
{
181181
AssertLockHeld(cs);
182182
// For each entry in vHashesToUpdate, store the set of in-mempool, but not
@@ -188,6 +188,8 @@ void CTxMemPool::UpdateTransactionsFromBlock(const std::vector<uint256> &vHashes
188188
// accounted for in the state of their ancestors)
189189
std::set<uint256> setAlreadyIncluded(vHashesToUpdate.begin(), vHashesToUpdate.end());
190190

191+
std::set<uint256> descendants_to_remove;
192+
191193
// Iterate in reverse, so that whenever we are looking at a transaction
192194
// we are sure that all in-mempool descendants have already been processed.
193195
// This maximizes the benefit of the descendant cache and guarantees that
@@ -217,7 +219,15 @@ void CTxMemPool::UpdateTransactionsFromBlock(const std::vector<uint256> &vHashes
217219
}
218220
}
219221
} // release epoch guard for UpdateForDescendants
220-
UpdateForDescendants(it, mapMemPoolDescendantsToUpdate, setAlreadyIncluded);
222+
UpdateForDescendants(it, mapMemPoolDescendantsToUpdate, setAlreadyIncluded, descendants_to_remove, ancestor_size_limit, ancestor_count_limit);
223+
}
224+
225+
for (const auto& txid : descendants_to_remove) {
226+
// This txid may have been removed already in a prior call to removeRecursive.
227+
// Therefore we ensure it is not yet removed already.
228+
if (const std::optional<txiter> txiter = GetIter(txid)) {
229+
removeRecursive((*txiter)->GetTx(), MemPoolRemovalReason::SIZELIMIT);
230+
}
221231
}
222232
}
223233

src/txmempool.h

Lines changed: 48 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -632,16 +632,25 @@ class CTxMemPool
632632
*/
633633
void RemoveStaged(setEntries& stage, bool updateDescendants, MemPoolRemovalReason reason) EXCLUSIVE_LOCKS_REQUIRED(cs);
634634

635-
/** When adding transactions from a disconnected block back to the mempool,
636-
* new mempool entries may have children in the mempool (which is generally
637-
* not the case when otherwise adding transactions).
638-
* UpdateTransactionsFromBlock() will find child transactions and update the
639-
* descendant state for each transaction in vHashesToUpdate (excluding any
640-
* child transactions present in vHashesToUpdate, which are already accounted
641-
* for). Note: vHashesToUpdate should be the set of transactions from the
642-
* disconnected block that have been accepted back into the mempool.
635+
/** UpdateTransactionsFromBlock is called when adding transactions from a
636+
* disconnected block back to the mempool, new mempool entries may have
637+
* children in the mempool (which is generally not the case when otherwise
638+
* adding transactions).
639+
* @post updated descendant state for descendants of each transaction in
640+
* vHashesToUpdate (excluding any child transactions present in
641+
* vHashesToUpdate, which are already accounted for). Updated state
642+
* includes add fee/size information for such descendants to the
643+
* parent and updated ancestor state to include the parent.
644+
*
645+
* @param[in] vHashesToUpdate The set of txids from the
646+
* disconnected block that have been accepted back into the mempool.
647+
* @param[in] ancestor_size_limit The maximum allowed size in virtual
648+
* bytes of an entry and its ancestors
649+
* @param[in] ancestor_count_limit The maximum allowed number of
650+
* transactions including the entry and its ancestors.
643651
*/
644-
void UpdateTransactionsFromBlock(const std::vector<uint256>& vHashesToUpdate) EXCLUSIVE_LOCKS_REQUIRED(cs, cs_main) LOCKS_EXCLUDED(m_epoch);
652+
void UpdateTransactionsFromBlock(const std::vector<uint256>& vHashesToUpdate,
653+
uint64_t ancestor_size_limit, uint64_t ancestor_count_limit) EXCLUSIVE_LOCKS_REQUIRED(cs, cs_main) LOCKS_EXCLUDED(m_epoch);
645654

646655
/** Try to calculate all in-mempool ancestors of entry.
647656
* (these are all calculated including the tx itself)
@@ -790,19 +799,38 @@ class CTxMemPool
790799
/** UpdateForDescendants is used by UpdateTransactionsFromBlock to update
791800
* the descendants for a single transaction that has been added to the
792801
* mempool but may have child transactions in the mempool, eg during a
793-
* chain reorg. setExclude is the set of descendant transactions in the
794-
* mempool that must not be accounted for (because any descendants in
795-
* setExclude were added to the mempool after the transaction being
796-
* updated and hence their state is already reflected in the parent
797-
* state).
802+
* chain reorg.
803+
*
804+
* @pre CTxMemPool::m_children is correct for the given tx and all
805+
* descendants.
806+
* @pre cachedDescendants is an accurate cache where each entry has all
807+
* descendants of the corresponding key, including those that should
808+
* be removed for violation of ancestor limits.
809+
* @post if updateIt has any non-excluded descendants, cachedDescendants has
810+
* a new cache line for updateIt.
811+
* @post descendants_to_remove has a new entry for any descendant which exceeded
812+
* ancestor limits relative to updateIt.
798813
*
799-
* cachedDescendants will be updated with the descendants of the transaction
800-
* being updated, so that future invocations don't need to walk the
801-
* same transaction again, if encountered in another transaction chain.
814+
* @param[in] updateIt the entry to update for its descendants
815+
* @param[in,out] cachedDescendants a cache where each line corresponds to all
816+
* descendants. It will be updated with the descendants of the transaction
817+
* being updated, so that future invocations don't need to walk the same
818+
* transaction again, if encountered in another transaction chain.
819+
* @param[in] setExclude the set of descendant transactions in the mempool
820+
* that must not be accounted for (because any descendants in setExclude
821+
* were added to the mempool after the transaction being updated and hence
822+
* their state is already reflected in the parent state).
823+
* @param[out] descendants_to_remove Populated with the txids of entries that
824+
* exceed ancestor limits. It's the responsibility of the caller to
825+
* removeRecursive them.
826+
* @param[in] ancestor_size_limit the max number of ancestral bytes allowed
827+
* for any descendant
828+
* @param[in] ancestor_count_limit the max number of ancestor transactions
829+
* allowed for any descendant
802830
*/
803-
void UpdateForDescendants(txiter updateIt,
804-
cacheMap &cachedDescendants,
805-
const std::set<uint256> &setExclude) EXCLUSIVE_LOCKS_REQUIRED(cs);
831+
void UpdateForDescendants(txiter updateIt, cacheMap& cachedDescendants,
832+
const std::set<uint256>& setExclude, std::set<uint256>& descendants_to_remove,
833+
uint64_t ancestor_size_limit, uint64_t ancestor_count_limit) EXCLUSIVE_LOCKS_REQUIRED(cs);
806834
/** Update ancestors of hash to add/remove it as a descendant transaction. */
807835
void UpdateAncestorsOf(bool add, txiter hash, setEntries &setAncestors) EXCLUSIVE_LOCKS_REQUIRED(cs);
808836
/** Set ancestor state for an entry */

src/validation.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,9 @@ void CChainState::MaybeUpdateMempoolForReorg(
348348
// previously-confirmed transactions back to the mempool.
349349
// UpdateTransactionsFromBlock finds descendants of any transactions in
350350
// the disconnectpool that were added back and cleans up the mempool state.
351-
m_mempool->UpdateTransactionsFromBlock(vHashUpdate);
351+
const uint64_t ancestor_count_limit = gArgs.GetIntArg("-limitancestorcount", DEFAULT_ANCESTOR_LIMIT);
352+
const uint64_t ancestor_size_limit = gArgs.GetIntArg("-limitancestorsize", DEFAULT_ANCESTOR_SIZE_LIMIT) * 1000;
353+
m_mempool->UpdateTransactionsFromBlock(vHashUpdate, ancestor_size_limit, ancestor_count_limit);
352354

353355
const auto check_final_and_mature = [this, flags=STANDARD_LOCKTIME_VERIFY_FLAGS](CTxMemPool::txiter it)
354356
EXCLUSIVE_LOCKS_REQUIRED(m_mempool->cs, ::cs_main) {

0 commit comments

Comments
 (0)