Skip to content

Commit 925bb72

Browse files
committed
[refactor] batch-add transactions to DisconnectedBlockTransactions
No behavior change. In a future commit, we can optimize by reserving vtx.size().
1 parent 5666966 commit 925bb72

File tree

2 files changed

+12
-6
lines changed

2 files changed

+12
-6
lines changed

src/txmempool.h

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -892,10 +892,18 @@ struct DisconnectedBlockTransactions {
892892
return memusage::MallocUsage(sizeof(CTransactionRef) + 6 * sizeof(void*)) * queuedTx.size() + cachedInnerUsage;
893893
}
894894

895-
void addTransaction(const CTransactionRef& tx)
895+
/** Add transactions from the block, iterating through vtx in reverse order. Callers should call
896+
* this function for blocks in descending order by block height.
897+
* We assume that callers never pass multiple transactions with the same txid, otherwise things
898+
* can go very wrong in removeForBlock due to queuedTx containing an item without a
899+
* corresponding entry in iters_by_txid.
900+
*/
901+
void AddTransactionsFromBlock(const std::vector<CTransactionRef>& vtx)
896902
{
897-
queuedTx.insert(tx);
898-
cachedInnerUsage += RecursiveDynamicUsage(tx);
903+
for (auto block_it = vtx.rbegin(); block_it != vtx.rend(); ++block_it) {
904+
queuedTx.insert(*block_it);
905+
cachedInnerUsage += RecursiveDynamicUsage(*block_it);
906+
}
899907
}
900908

901909
// Remove entries based on txid_index, and update memory usage.

src/validation.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2721,9 +2721,7 @@ bool Chainstate::DisconnectTip(BlockValidationState& state, DisconnectedBlockTra
27212721

27222722
if (disconnectpool && m_mempool) {
27232723
// Save transactions to re-add to mempool at end of reorg
2724-
for (auto it = block.vtx.rbegin(); it != block.vtx.rend(); ++it) {
2725-
disconnectpool->addTransaction(*it);
2726-
}
2724+
disconnectpool->AddTransactionsFromBlock(block.vtx);
27272725
while (disconnectpool->DynamicMemoryUsage() > MAX_DISCONNECTED_TX_POOL_SIZE * 1000) {
27282726
// Drop the earliest entry, and remove its children from the mempool.
27292727
auto it = disconnectpool->queuedTx.get<insertion_order>().begin();

0 commit comments

Comments
 (0)