Skip to content

Commit

Permalink
Improve DisconnectTip performance
Browse files Browse the repository at this point in the history
Summary: This backport core's PR9208

Test Plan:
  make check
  ./test/functional/test_runner.py --extended

Reviewers: #bitcoin_abc, jasonbcox

Reviewed By: #bitcoin_abc, jasonbcox

Subscribers: jasonbcox

Differential Revision: https://reviews.bitcoinabc.org/D983
  • Loading branch information
sdaftuar authored and deadalnix committed Jan 19, 2018
1 parent 01150a9 commit f18009a
Show file tree
Hide file tree
Showing 6 changed files with 228 additions and 66 deletions.
5 changes: 5 additions & 0 deletions src/core_memusage.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,9 @@ static inline size_t RecursiveDynamicUsage(const CBlockLocator &locator) {
return memusage::DynamicUsage(locator.vHave);
}

template <typename X>
static inline size_t RecursiveDynamicUsage(const std::shared_ptr<X> &p) {
return p ? memusage::DynamicUsage(p) + RecursiveDynamicUsage(*p) : 0;
}

#endif // BITCOIN_CORE_MEMUSAGE_H
2 changes: 1 addition & 1 deletion src/txmempool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ CTxMemPoolEntry::CTxMemPoolEntry(const CTransactionRef &_tx, const Amount _nFee,
lockPoints(lp) {
nTxSize = GetTransactionSize(*tx);
nModSize = tx->CalculateModifiedSize(GetTxSize());
nUsageSize = RecursiveDynamicUsage(*tx) + memusage::DynamicUsage(tx);
nUsageSize = RecursiveDynamicUsage(tx);

nCountWithDescendants = 1;
nSizeWithDescendants = GetTxSize();
Expand Down
94 changes: 93 additions & 1 deletion src/txmempool.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@

#include <boost/multi_index/hashed_index.hpp>
#include <boost/multi_index/ordered_index.hpp>
#include <boost/multi_index/sequenced_index.hpp>
#include <boost/multi_index_container.hpp>

#include <boost/signals2/signal.hpp>

#include <map>
Expand Down Expand Up @@ -230,12 +232,16 @@ struct update_lock_points {
const LockPoints &lp;
};

// extracts a TxMemPoolEntry's transaction hash
// extracts a transaction hash from CTxMempoolEntry or CTransactionRef
struct mempoolentry_txid {
typedef uint256 result_type;
result_type operator()(const CTxMemPoolEntry &entry) const {
return entry.GetTx().GetId();
}

result_type operator()(const CTransactionRef &tx) const {
return tx->GetHash();
}
};

/** \class CompareTxMemPoolEntryByDescendantScore
Expand Down Expand Up @@ -813,4 +819,90 @@ struct TxCoinAgePriorityCompare {
}
};

/**
* DisconnectedBlockTransactions
* During the reorg, it's desirable to re-add previously confirmed transactions
* to the mempool, so that anything not re-confirmed in the new chain is
* available to be mined. However, it's more efficient to wait until the reorg
* is complete and process all still-unconfirmed transactions at that time,
* since we expect most confirmed transactions to (typically) still be
* confirmed in the new chain, and re-accepting to the memory pool is expensive
* (and therefore better to not do in the middle of reorg-processing).
* Instead, store the disconnected transactions (in order!) as we go, remove any
* that are included in blocks in the new chain, and then process the remaining
* still-unconfirmed transactions at the end.
*/

// multi_index tag names
struct txid_index {};
struct insertion_order {};

struct DisconnectedBlockTransactions {
typedef boost::multi_index_container<
CTransactionRef, boost::multi_index::indexed_by<
// sorted by txid
boost::multi_index::hashed_unique<
boost::multi_index::tag<txid_index>,
mempoolentry_txid, SaltedTxidHasher>,
// sorted by order in the blockchain
boost::multi_index::sequenced<
boost::multi_index::tag<insertion_order>>>>
indexed_disconnected_transactions;

// 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.
// For now, assert() that we've emptied out this object on destruction.
// This assert() can always be removed if the reorg-processing code were
// to be refactored such that this assumption is no longer true (for
// instance if there was some other way we cleaned up the mempool after a
// reorg, besides draining this object).
~DisconnectedBlockTransactions() { assert(queuedTx.empty()); }

indexed_disconnected_transactions queuedTx;
uint64_t cachedInnerUsage = 0;

// Estimate the overhead of queuedTx to be 6 pointers + an allocation, as
// no exact formula for boost::multi_index_contained is implemented.
size_t DynamicMemoryUsage() const {
return memusage::MallocUsage(sizeof(CTransactionRef) +
6 * sizeof(void *)) *
queuedTx.size() +
cachedInnerUsage;
}

void addTransaction(const CTransactionRef &tx) {
queuedTx.insert(tx);
cachedInnerUsage += RecursiveDynamicUsage(tx);
}

// Remove entries based on txid_index, and update memory usage.
void removeForBlock(const std::vector<CTransactionRef> &vtx) {
// Short-circuit in the common case of a block being added to the tip
if (queuedTx.empty()) {
return;
}
for (auto const &tx : vtx) {
auto it = queuedTx.find(tx->GetHash());
if (it != queuedTx.end()) {
cachedInnerUsage -= RecursiveDynamicUsage(*it);
queuedTx.erase(it);
}
}
}

// Remove an entry by insertion_order index, and update memory usage.
void removeEntry(indexed_disconnected_transactions::index<
insertion_order>::type::iterator entry) {
cachedInnerUsage -= RecursiveDynamicUsage(*entry);
queuedTx.get<insertion_order>().erase(entry);
}

void clear() {
cachedInnerUsage = 0;
queuedTx.clear();
}
};

#endif // BITCOIN_TXMEMPOOL_H
150 changes: 105 additions & 45 deletions src/validation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -593,6 +593,62 @@ bool IsDAAEnabled(const Config &config, const CBlockIndex *pindexPrev) {
return IsDAAEnabled(config, pindexPrev->nHeight);
}

/**
* Make mempool consistent after a reorg, by re-adding or recursively erasing
* disconnected block transactions from the mempool, and also removing any other
* transactions from the mempool that are no longer valid given the new
* tip/height.
*
* Note: we assume that disconnectpool only contains transactions that are NOT
* confirmed in the current chain nor already in the mempool (otherwise,
* in-mempool descendants of such transactions would be removed).
*
* Passing fAddToMempool=false will skip trying to add the transactions back,
* and instead just erase from the mempool as needed.
*/
void UpdateMempoolForReorg(const Config &config,
DisconnectedBlockTransactions &disconnectpool,
bool fAddToMempool) {
AssertLockHeld(cs_main);
std::vector<uint256> vHashUpdate;
// disconnectpool's insertion_order index sorts the entries from oldest to
// newest, but the oldest entry will be the last tx from the latest mined
// block that was disconnected.
// 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.get<insertion_order>().rbegin();
while (it != disconnectpool.queuedTx.get<insertion_order>().rend()) {
// ignore validation errors in resurrected transactions
CValidationState stateDummy;
if (!fAddToMempool || (*it)->IsCoinBase() ||
!AcceptToMemoryPool(config, mempool, stateDummy, *it, false,
nullptr, nullptr, true)) {
// If the transaction doesn't make it in to the mempool, remove any
// transactions that depend on it (which would now be orphans).
mempool.removeRecursive(**it, MemPoolRemovalReason::REORG);
} else if (mempool.exists((*it)->GetHash())) {
vHashUpdate.push_back((*it)->GetHash());
}
++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.
// UpdateTransactionsFromBlock finds descendants of any transactions in the
// disconnectpool that were added back and cleans up the mempool state.
mempool.UpdateTransactionsFromBlock(vHashUpdate);

// We also need to remove any now-immature transactions
mempool.removeForReorg(config, pcoinsTip, chainActive.Tip()->nHeight + 1,
STANDARD_LOCKTIME_VERIFY_FLAGS);
// Re-limit mempool size, in case we added any transactions
LimitMempoolSize(
mempool, GetArg("-maxmempool", DEFAULT_MAX_MEMPOOL_SIZE) * 1000000,
GetArg("-mempoolexpiry", DEFAULT_MEMPOOL_EXPIRY) * 60 * 60);
}

// Used to avoid mempool polluting consensus critical paths if CCoinsViewMempool
// were somehow broken and returning the wrong scriptPubKeys
static bool CheckInputsFromMempoolAndCache(const CTransaction &tx,
Expand Down Expand Up @@ -2381,12 +2437,18 @@ static void UpdateTip(const Config &config, CBlockIndex *pindexNew) {
}

/**
* Disconnect chainActive's tip. You probably want to call
* mempool.removeForReorg and manually re-limit mempool size after this, with
* cs_main held.
* Disconnect chainActive's tip.
* After calling, the mempool will be in an inconsistent state, with
* transactions from disconnected blocks being added to disconnectpool. You
* should make the mempool consistent again by calling UpdateMempoolForReorg.
* with cs_main held.
*
* If disconnectpool is nullptr, then no disconnected transactions are added to
* disconnectpool (note that the caller is responsible for mempool consistency
* in any case).
*/
static bool DisconnectTip(const Config &config, CValidationState &state,
bool fBare = false) {
DisconnectedBlockTransactions *disconnectpool) {
CBlockIndex *pindexDelete = chainActive.Tip();
assert(pindexDelete);

Expand Down Expand Up @@ -2418,27 +2480,19 @@ static bool DisconnectTip(const Config &config, CValidationState &state,
return false;
}

if (!fBare) {
// Resurrect mempool transactions from the disconnected block.
std::vector<uint256> vHashUpdate;
for (const auto &it : block.vtx) {
const CTransaction &tx = *it;
// ignore validation errors in resurrected transactions
CValidationState stateDummy;
if (tx.IsCoinBase() ||
!AcceptToMemoryPool(config, mempool, stateDummy, it, false,
nullptr, nullptr, true)) {
mempool.removeRecursive(tx, MemPoolRemovalReason::REORG);
} else if (mempool.exists(tx.GetId())) {
vHashUpdate.push_back(tx.GetId());
}
if (disconnectpool) {
// Save transactions to re-add to mempool at end of reorg
for (const auto &tx : block.vtx) {
disconnectpool->addTransaction(tx);
}
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();
mempool.removeRecursive(**it, MemPoolRemovalReason::REORG);
disconnectpool->removeEntry(it);
}
// 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.
// UpdateTransactionsFromBlock finds descendants of any transactions in
// this block that were added back and cleans up the mempool state.
mempool.UpdateTransactionsFromBlock(vHashUpdate);
}

// Update chainActive and related variables.
Expand Down Expand Up @@ -2538,8 +2592,8 @@ class ConnectTrace {
static bool ConnectTip(const Config &config, CValidationState &state,
CBlockIndex *pindexNew,
const std::shared_ptr<const CBlock> &pblock,
ConnectTrace &connectTrace) {
const CChainParams &chainparams = config.GetChainParams();
ConnectTrace &connectTrace,
DisconnectedBlockTransactions &disconnectpool) {
assert(pindexNew->pprev == chainActive.Tip());
// Read block from disk.
int64_t nTime1 = GetTimeMicros();
Expand All @@ -2565,7 +2619,7 @@ static bool ConnectTip(const Config &config, CValidationState &state,
{
CCoinsViewCache view(pcoinsTip);
bool rv = ConnectBlock(config, blockConnecting, state, pindexNew, view,
chainparams);
config.GetChainParams());
GetMainSignals().BlockChecked(blockConnecting, state);
if (!rv) {
if (state.IsInvalid()) {
Expand Down Expand Up @@ -2593,6 +2647,7 @@ static bool ConnectTip(const Config &config, CValidationState &state,
(nTime5 - nTime4) * 0.001, nTimeChainState * 0.000001);
// Remove conflicting transactions from the mempool.;
mempool.removeForBlock(blockConnecting.vtx, pindexNew->nHeight);
disconnectpool.removeForBlock(blockConnecting.vtx);
// Update chainActive & related variables.
UpdateTip(config, pindexNew);

Expand Down Expand Up @@ -2703,8 +2758,14 @@ static bool ActivateBestChainStep(const Config &config, CValidationState &state,

// Disconnect active blocks which are no longer in the best chain.
bool fBlocksDisconnected = false;
DisconnectedBlockTransactions disconnectpool;
while (chainActive.Tip() && chainActive.Tip() != pindexFork) {
if (!DisconnectTip(config, state)) return false;
if (!DisconnectTip(config, state, &disconnectpool)) {
// This is likely a fatal error, but keep the mempool consistent,
// just in case. Only remove from the mempool in this case.
UpdateMempoolForReorg(config, disconnectpool, false);
return false;
}
fBlocksDisconnected = true;
}

Expand Down Expand Up @@ -2732,7 +2793,7 @@ static bool ActivateBestChainStep(const Config &config, CValidationState &state,
pindexConnect == pindexMostWork
? pblock
: std::shared_ptr<const CBlock>(),
connectTrace)) {
connectTrace, disconnectpool)) {
if (state.IsInvalid()) {
// The block violates a consensus rule.
if (!state.CorruptionPossible())
Expand All @@ -2744,6 +2805,9 @@ static bool ActivateBestChainStep(const Config &config, CValidationState &state,
} else {
// A system error occurred (disk space, database error,
// ...).
// Make the mempool consistent with the current tip, just in
// case any observers try to use it before shutdown.
UpdateMempoolForReorg(config, disconnectpool, false);
return false;
}
} else {
Expand All @@ -2760,12 +2824,9 @@ static bool ActivateBestChainStep(const Config &config, CValidationState &state,
}

if (fBlocksDisconnected) {
mempool.removeForReorg(config, pcoinsTip,
chainActive.Tip()->nHeight + 1,
STANDARD_LOCKTIME_VERIFY_FLAGS);
LimitMempoolSize(
mempool, GetArg("-maxmempool", DEFAULT_MAX_MEMPOOL_SIZE) * 1000000,
GetArg("-mempoolexpiry", DEFAULT_MEMPOOL_EXPIRY) * 60 * 60);
// If any blocks were disconnected, disconnectpool may be non empty. Add
// any disconnected transactions back to the mempool.
UpdateMempoolForReorg(config, disconnectpool, true);
}
mempool.check(pcoinsTip);

Expand Down Expand Up @@ -2932,24 +2993,25 @@ bool InvalidateBlock(const Config &config, CValidationState &state,
setDirtyBlockIndex.insert(pindex);
setBlockIndexCandidates.erase(pindex);

DisconnectedBlockTransactions disconnectpool;
while (chainActive.Contains(pindex)) {
CBlockIndex *pindexWalk = chainActive.Tip();
pindexWalk->nStatus |= BLOCK_FAILED_CHILD;
setDirtyBlockIndex.insert(pindexWalk);
setBlockIndexCandidates.erase(pindexWalk);
// ActivateBestChain considers blocks already in chainActive
// unconditionally valid already, so force disconnect away from it.
if (!DisconnectTip(config, state)) {
mempool.removeForReorg(config, pcoinsTip,
chainActive.Tip()->nHeight + 1,
STANDARD_LOCKTIME_VERIFY_FLAGS);
if (!DisconnectTip(config, state, &disconnectpool)) {
// It's probably hopeless to try to make the mempool consistent
// here if DisconnectTip failed, but we can try.
UpdateMempoolForReorg(config, disconnectpool, false);
return false;
}
}

LimitMempoolSize(
mempool, GetArg("-maxmempool", DEFAULT_MAX_MEMPOOL_SIZE) * 1000000,
GetArg("-mempoolexpiry", DEFAULT_MEMPOOL_EXPIRY) * 60 * 60);
// DisconnectTip will add transactions to disconnectpool; try to add these
// back to the mempool.
UpdateMempoolForReorg(config, disconnectpool, true);

// The resulting new best tip may not be in setBlockIndexCandidates anymore,
// so add it again.
Expand All @@ -2965,8 +3027,6 @@ bool InvalidateBlock(const Config &config, CValidationState &state,
}

InvalidChainFound(pindex);
mempool.removeForReorg(config, pcoinsTip, chainActive.Tip()->nHeight + 1,
STANDARD_LOCKTIME_VERIFY_FLAGS);
uiInterface.NotifyBlockTip(IsInitialBlockDownload(), pindex->pprev);
return true;
}
Expand Down Expand Up @@ -4327,7 +4387,7 @@ bool RewindBlockIndex(const Config &config) {
// needless reindex/redownload of the blockchain).
break;
}
if (!DisconnectTip(config, state, true)) {
if (!DisconnectTip(config, state, nullptr)) {
return error(
"RewindBlockIndex: unable to disconnect block at height %i",
pindex->nHeight);
Expand Down
Loading

0 comments on commit f18009a

Please sign in to comment.