diff options
author | glozow <gloriajzhao@gmail.com> | 2023-09-06 11:24:42 +0100 |
---|---|---|
committer | glozow <gloriajzhao@gmail.com> | 2023-09-07 18:55:44 +0100 |
commit | 925bb723ca71aa76380b769d8926c7c2ad9bbb7b (patch) | |
tree | d440f284dbf57a30b226de638a6f7c1dac6bb237 /src/txmempool.h | |
parent | 5666966dffeb620b4a628679d667346e02fd3b19 (diff) |
[refactor] batch-add transactions to DisconnectedBlockTransactions
No behavior change.
In a future commit, we can optimize by reserving vtx.size().
Diffstat (limited to 'src/txmempool.h')
-rw-r--r-- | src/txmempool.h | 14 |
1 files changed, 11 insertions, 3 deletions
diff --git a/src/txmempool.h b/src/txmempool.h index 869612a4a2..8b4ac2dce1 100644 --- a/src/txmempool.h +++ b/src/txmempool.h @@ -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. |