Skip to content

Commit 8ab9929

Browse files
committed
merge bitcoin#22677: cut the validation <-> txmempool circular dependency
1 parent ee49383 commit 8ab9929

File tree

5 files changed

+73
-56
lines changed

5 files changed

+73
-56
lines changed

src/txmempool.cpp

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
#include <util/moneystr.h>
1919
#include <util/system.h>
2020
#include <util/time.h>
21-
#include <validation.h>
2221
#include <validationinterface.h>
2322

2423
#include <evo/specialtx.h>
@@ -82,6 +81,24 @@ struct update_lock_points
8281
const LockPoints& lp;
8382
};
8483

84+
bool TestLockPointValidity(CChain& active_chain, const LockPoints* lp)
85+
{
86+
AssertLockHeld(cs_main);
87+
assert(lp);
88+
// If there are relative lock times then the maxInputBlock will be set
89+
// If there are no relative lock times, the LockPoints don't depend on the chain
90+
if (lp->maxInputBlock) {
91+
// Check whether active_chain is an extension of the block at which the LockPoints
92+
// calculation was valid. If not LockPoints are no longer valid
93+
if (!active_chain.Contains(lp->maxInputBlock)) {
94+
return false;
95+
}
96+
}
97+
98+
// LockPoints still valid
99+
return true;
100+
}
101+
85102
CTxMemPoolEntry::CTxMemPoolEntry(const CTransactionRef& tx, CAmount fee,
86103
int64_t time, unsigned int entry_height,
87104
bool spends_coinbase, int64_t sigops_count, LockPoints lp)
@@ -859,44 +876,27 @@ void CTxMemPool::removeRecursive(const CTransaction &origTx, MemPoolRemovalReaso
859876
RemoveStaged(setAllRemoves, false, reason);
860877
}
861878

862-
void CTxMemPool::removeForReorg(CChainState& active_chainstate, int flags) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
879+
void CTxMemPool::removeForReorg(CChain& chain, std::function<bool(txiter)> check_final_and_mature) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
863880
{
864881
// Remove transactions spending a coinbase which are now immature and no-longer-final transactions
865882
AssertLockHeld(cs);
883+
AssertLockHeld(::cs_main);
884+
866885
setEntries txToRemove;
867886
for (indexed_transaction_set::const_iterator it = mapTx.begin(); it != mapTx.end(); it++) {
868-
const CTransaction& tx = it->GetTx();
869-
LockPoints lp = it->GetLockPoints();
870-
bool validLP = TestLockPointValidity(active_chainstate.m_chain, &lp);
871-
CCoinsViewMemPool view_mempool(&active_chainstate.CoinsTip(), *this);
872-
if (!CheckFinalTx(active_chainstate.m_chain.Tip(), tx, flags)
873-
|| !CheckSequenceLocks(active_chainstate.m_chain.Tip(), view_mempool, tx, flags, &lp, validLP)) {
874-
// Note if CheckSequenceLocks fails the LockPoints may still be invalid
875-
// So it's critical that we remove the tx and not depend on the LockPoints.
876-
txToRemove.insert(it);
877-
} else if (it->GetSpendsCoinbase()) {
878-
for (const CTxIn& txin : tx.vin) {
879-
indexed_transaction_set::const_iterator it2 = mapTx.find(txin.prevout.hash);
880-
if (it2 != mapTx.end())
881-
continue;
882-
const Coin &coin = active_chainstate.CoinsTip().AccessCoin(txin.prevout);
883-
if (m_check_ratio != 0) assert(!coin.IsSpent());
884-
unsigned int nMemPoolHeight = active_chainstate.m_chain.Tip()->nHeight + 1;
885-
if (coin.IsSpent() || (coin.IsCoinBase() && ((signed long)nMemPoolHeight) - coin.nHeight < COINBASE_MATURITY)) {
886-
txToRemove.insert(it);
887-
break;
888-
}
889-
}
890-
}
891-
if (!validLP) {
892-
mapTx.modify(it, update_lock_points(lp));
893-
}
887+
if (check_final_and_mature(it)) txToRemove.insert(it);
894888
}
895889
setEntries setAllRemoves;
896890
for (txiter it : txToRemove) {
897891
CalculateDescendants(it, setAllRemoves);
898892
}
899893
RemoveStaged(setAllRemoves, false, MemPoolRemovalReason::REORG);
894+
for (indexed_transaction_set::const_iterator it = mapTx.begin(); it != mapTx.end(); it++) {
895+
LockPoints lp = it->GetLockPoints();
896+
if (!TestLockPointValidity(chain, &lp)) {
897+
mapTx.modify(it, update_lock_points(lp));
898+
}
899+
}
900900
}
901901

902902
void CTxMemPool::removeConflicts(const CTransaction &tx)

src/txmempool.h

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include <addressindex.h>
1818
#include <spentindex.h>
1919
#include <amount.h>
20+
#include <chain.h>
2021
#include <coins.h>
2122
#include <gsl/pointers.h>
2223
#include <indirectmap.h>
@@ -59,6 +60,11 @@ struct LockPoints {
5960
CBlockIndex* maxInputBlock{nullptr};
6061
};
6162

63+
/**
64+
* Test whether the LockPoints height and time are still valid on the current chain
65+
*/
66+
bool TestLockPointValidity(CChain& active_chain, const LockPoints* lp) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
67+
6268
struct CompareIteratorByHash {
6369
// SFINAE for T where T is either a pointer type (e.g., a txiter) or a reference_wrapper<T>
6470
// (e.g. a wrapped CTxMemPoolEntry&)
@@ -615,7 +621,10 @@ class CTxMemPool
615621
bool removeSpentIndex(const uint256 txhash);
616622

617623
void removeRecursive(const CTransaction& tx, MemPoolRemovalReason reason) EXCLUSIVE_LOCKS_REQUIRED(cs);
618-
void removeForReorg(CChainState& active_chainstate, int flags) EXCLUSIVE_LOCKS_REQUIRED(cs, cs_main);
624+
/** After reorg, check if mempool entries are now non-final, premature coinbase spends, or have
625+
* invalid lockpoints. Update lockpoints and remove entries (and descendants of entries) that
626+
* are no longer valid. */
627+
void removeForReorg(CChain& chain, std::function<bool(txiter)> check_final_and_mature) EXCLUSIVE_LOCKS_REQUIRED(cs, cs_main);
619628
void removeConflicts(const CTransaction& tx) EXCLUSIVE_LOCKS_REQUIRED(cs);
620629
void removeProTxPubKeyConflicts(const CTransaction &tx, const CKeyID &keyId) EXCLUSIVE_LOCKS_REQUIRED(cs);
621630
void removeProTxPubKeyConflicts(const CTransaction &tx, const CBLSLazyPublicKey &pubKey) EXCLUSIVE_LOCKS_REQUIRED(cs);

src/validation.cpp

Lines changed: 32 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -172,24 +172,6 @@ bool CheckFinalTx(const CBlockIndex* active_chain_tip, const CTransaction &tx, i
172172
return IsFinalTx(tx, nBlockHeight, nBlockTime);
173173
}
174174

175-
bool TestLockPointValidity(CChain& active_chain, const LockPoints* lp)
176-
{
177-
AssertLockHeld(cs_main);
178-
assert(lp);
179-
// If there are relative lock times then the maxInputBlock will be set
180-
// If there are no relative lock times, the LockPoints don't depend on the chain
181-
if (lp->maxInputBlock) {
182-
// Check whether ::ChainActive() is an extension of the block at which the LockPoints
183-
// calculation was valid. If not LockPoints are no longer valid
184-
if (!active_chain.Contains(lp->maxInputBlock)) {
185-
return false;
186-
}
187-
}
188-
189-
// LockPoints still valid
190-
return true;
191-
}
192-
193175
bool CheckSequenceLocks(CBlockIndex* tip,
194176
const CCoinsView& coins_view,
195177
const CTransaction& tx,
@@ -389,8 +371,39 @@ void CChainState::MaybeUpdateMempoolForReorg(
389371
// the disconnectpool that were added back and cleans up the mempool state.
390372
m_mempool->UpdateTransactionsFromBlock(vHashUpdate);
391373

374+
const auto check_final_and_mature = [this, flags=STANDARD_LOCKTIME_VERIFY_FLAGS](CTxMemPool::txiter it)
375+
EXCLUSIVE_LOCKS_REQUIRED(m_mempool->cs, ::cs_main) {
376+
bool should_remove = false;
377+
AssertLockHeld(m_mempool->cs);
378+
AssertLockHeld(::cs_main);
379+
const CTransaction& tx = it->GetTx();
380+
LockPoints lp = it->GetLockPoints();
381+
bool validLP = TestLockPointValidity(m_chain, &lp);
382+
CCoinsViewMemPool view_mempool(&CoinsTip(), *m_mempool);
383+
if (!CheckFinalTx(m_chain.Tip(), tx, flags)
384+
|| !CheckSequenceLocks(m_chain.Tip(), view_mempool, tx, flags, &lp, validLP)) {
385+
// Note if CheckSequenceLocks fails the LockPoints may still be invalid
386+
// So it's critical that we remove the tx and not depend on the LockPoints.
387+
should_remove = true;
388+
} else if (it->GetSpendsCoinbase()) {
389+
for (const CTxIn& txin : tx.vin) {
390+
auto it2 = m_mempool->mapTx.find(txin.prevout.hash);
391+
if (it2 != m_mempool->mapTx.end())
392+
continue;
393+
const Coin &coin = CoinsTip().AccessCoin(txin.prevout);
394+
assert(!coin.IsSpent());
395+
unsigned int nMemPoolHeight = m_chain.Tip()->nHeight + 1;
396+
if (coin.IsSpent() || (coin.IsCoinBase() && ((signed long)nMemPoolHeight) - coin.nHeight < COINBASE_MATURITY)) {
397+
should_remove = true;
398+
break;
399+
}
400+
}
401+
}
402+
return should_remove;
403+
};
404+
392405
// We also need to remove any now-immature transactions
393-
m_mempool->removeForReorg(*this, STANDARD_LOCKTIME_VERIFY_FLAGS);
406+
m_mempool->removeForReorg(m_chain, check_final_and_mature);
394407
// Re-limit mempool size, in case we added any transactions
395408
LimitMempoolSize(
396409
*m_mempool,

src/validation.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -284,11 +284,6 @@ int GetUTXOConfirmations(CChainState& active_chainstate, const COutPoint& outpoi
284284
*/
285285
bool CheckFinalTx(const CBlockIndex* active_chain_tip, const CTransaction &tx, int flags = -1) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
286286

287-
/**
288-
* Test whether the LockPoints height and time are still valid on the current chain
289-
*/
290-
bool TestLockPointValidity(CChain& active_chain, const LockPoints* lp) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
291-
292287
/**
293288
* Check if transaction will be BIP68 final in the next block to be created on top of tip.
294289
* @param[in] tip Chain tip to check tx sequence locks against. For example,

test/lint/lint-circular-dependencies.sh

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ EXPECTED_CIRCULAR_DEPENDENCIES=(
1818
"qt/addresstablemodel -> qt/walletmodel -> qt/addresstablemodel"
1919
"qt/recentrequeststablemodel -> qt/walletmodel -> qt/recentrequeststablemodel"
2020
"qt/transactiontablemodel -> qt/walletmodel -> qt/transactiontablemodel"
21-
"txmempool -> validation -> txmempool"
2221
"wallet/fees -> wallet/wallet -> wallet/fees"
2322
"wallet/wallet -> wallet/walletdb -> wallet/wallet"
2423
"node/coinstats -> validation -> node/coinstats"
@@ -63,12 +62,13 @@ EXPECTED_CIRCULAR_DEPENDENCIES=(
6362

6463
"llmq/chainlocks -> validation -> llmq/chainlocks"
6564
"coinjoin/coinjoin -> llmq/chainlocks -> net -> coinjoin/coinjoin"
65+
"evo/assetlocktx -> validation -> txmempool -> evo/assetlocktx"
6666
"evo/deterministicmns -> llmq/utils -> llmq/snapshot -> evo/simplifiedmns -> evo/deterministicmns"
6767
"evo/deterministicmns -> llmq/utils -> net -> evo/deterministicmns"
68+
"evo/deterministicmns -> validation -> txmempool -> evo/deterministicmns"
6869
"policy/policy -> policy/settings -> policy/policy"
6970
"consensus/tx_verify -> evo/assetlocktx -> validation -> consensus/tx_verify"
70-
"consensus/tx_verify -> evo/assetlocktx -> llmq/signing -> net_processing -> txmempool -> consensus/tx_verify"
71-
"evo/assetlocktx -> llmq/signing -> net_processing -> txmempool -> evo/assetlocktx"
71+
"consensus/tx_verify -> evo/assetlocktx -> validation -> txmempool -> consensus/tx_verify"
7272

7373
"evo/simplifiedmns -> llmq/blockprocessor -> llmq/utils -> llmq/snapshot -> evo/simplifiedmns"
7474
"llmq/blockprocessor -> llmq/utils -> llmq/snapshot -> llmq/blockprocessor"

0 commit comments

Comments
 (0)