forked from bitcoin/bitcoin
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fully encapsulate DisconnectedBlockTransactions
- Loading branch information
Showing
2 changed files
with
23 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,11 +28,13 @@ | |
* still-unconfirmed transactions at the end. | ||
*/ | ||
struct DisconnectedBlockTransactions { | ||
private: | ||
uint64_t cachedInnerUsage = 0; | ||
std::list<CTransactionRef> queuedTx; | ||
using Queue = decltype(queuedTx); | ||
std::unordered_map<uint256, Queue::iterator, SaltedTxidHasher> iters_by_txid; | ||
|
||
public: | ||
// It's almost certainly a logic bug if we don't clear out queuedTx before | ||
// destruction, as we add to it while disconnecting blocks, and then we | ||
// need to re-process remaining transactions to ensure mempool consistency. | ||
|
@@ -75,11 +77,16 @@ struct DisconnectedBlockTransactions { | |
} | ||
|
||
// Remove the first entry and update memory usage. | ||
void remove_first() | ||
CTransactionRef take_first() | ||
{ | ||
cachedInnerUsage -= RecursiveDynamicUsage(queuedTx.front()); | ||
iters_by_txid.erase(queuedTx.front()->GetHash()); | ||
queuedTx.pop_front(); | ||
CTransactionRef ret; | ||
if (!queuedTx.empty()) { | ||
ret = queuedTx.front(); | ||
cachedInnerUsage -= RecursiveDynamicUsage(queuedTx.front()); | ||
This comment has been minimized.
Sorry, something went wrong. |
||
iters_by_txid.erase(queuedTx.front()->GetHash()); | ||
queuedTx.pop_front(); | ||
} | ||
return ret; | ||
} | ||
|
||
void clear() | ||
|
@@ -88,6 +95,11 @@ struct DisconnectedBlockTransactions { | |
iters_by_txid.clear(); | ||
queuedTx.clear(); | ||
} | ||
std::list<CTransactionRef> take() { | ||
std::list<CTransactionRef> ret = queuedTx; | ||
This comment has been minimized.
Sorry, something went wrong.
stickies-v
|
||
clear(); | ||
return ret; | ||
} | ||
}; | ||
|
||
#endif // BITCOIN_KERNEL_DISCONNECTED_TRANSACTIONS_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -302,8 +302,9 @@ void Chainstate::MaybeUpdateMempoolForReorg( | |
// Iterate disconnectpool in reverse, so that we add transactions | ||
// back to the mempool starting with the earliest transaction that had | ||
// been previously seen in a block. | ||
auto it = disconnectpool.queuedTx.rbegin(); | ||
while (it != disconnectpool.queuedTx.rend()) { | ||
std::list<CTransactionRef> queuedTx = disconnectpool.take(); | ||
auto it = queuedTx.rbegin(); | ||
while (it != queuedTx.rend()) { | ||
// ignore errors related to fees in resurrected transactions. | ||
if (!fAddToMempool || (*it)->IsCoinBase() || | ||
AcceptToMemoryPool(*this, *it, GetTime(), | ||
|
@@ -317,7 +318,6 @@ void Chainstate::MaybeUpdateMempoolForReorg( | |
} | ||
++it; | ||
} | ||
disconnectpool.queuedTx.clear(); | ||
// AcceptToMemoryPool/addUnchecked all assume that new mempool entries have | ||
// no in-mempool children, which is generally not true when adding | ||
// previously-confirmed transactions back to the mempool. | ||
|
@@ -2725,11 +2725,11 @@ bool Chainstate::DisconnectTip(BlockValidationState& state, DisconnectedBlockTra | |
for (auto it = block.vtx.rbegin(); it != block.vtx.rend(); ++it) { | ||
disconnectpool->addTransaction(*it); | ||
} | ||
while (!disconnectpool->queuedTx.empty() && disconnectpool->DynamicMemoryUsage() > MAX_DISCONNECTED_TX_POOL_SIZE * 1000) { | ||
while (disconnectpool->DynamicMemoryUsage() > MAX_DISCONNECTED_TX_POOL_SIZE * 1000) { | ||
This comment has been minimized.
Sorry, something went wrong.
theuni
Author
Owner
|
||
// Drop the earliest entry, and remove its children from the mempool. | ||
auto ptx = disconnectpool->queuedTx.front(); | ||
m_mempool->removeRecursive(*ptx, MemPoolRemovalReason::REORG); | ||
disconnectpool->remove_first(); | ||
if (auto ptx = disconnectpool->take_first()) { | ||
m_mempool->removeRecursive(*ptx, MemPoolRemovalReason::REORG); | ||
} | ||
} | ||
} | ||
|
||
|
nit: just using
ret
instead ofqueuedTx.front()
seems appropriate? (+ next line)nit:
first_tx
or something would probably be a better name instead ofret