Skip to content

Commit

Permalink
[refactor] batch-add transactions to DisconnectedBlockTransactions
Browse files Browse the repository at this point in the history
No behavior change.
In a future commit, we can optimize by reserving vtx.size().
  • Loading branch information
glozow committed Sep 7, 2023
1 parent 5666966 commit 925bb72
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
14 changes: 11 additions & 3 deletions src/txmempool.h
Original file line number Diff line number Diff line change
Expand Up @@ -892,10 +892,18 @@ struct DisconnectedBlockTransactions {
return memusage::MallocUsage(sizeof(CTransactionRef) + 6 * sizeof(void*)) * queuedTx.size() + cachedInnerUsage;
}

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

// Remove entries based on txid_index, and update memory usage.
Expand Down
4 changes: 1 addition & 3 deletions src/validation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2721,9 +2721,7 @@ bool Chainstate::DisconnectTip(BlockValidationState& state, DisconnectedBlockTra

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

0 comments on commit 925bb72

Please sign in to comment.