diff --git a/src/interfaces/chain.cpp b/src/interfaces/chain.cpp --- a/src/interfaces/chain.cpp +++ b/src/interfaces/chain.cpp @@ -216,7 +216,7 @@ const CTransaction &tx, TxValidationState &state) override { LOCK(cs_main); return ContextualCheckTransactionForCurrentBlock( - m_params.GetConsensus(), tx, state); + ::ChainActive().Tip(), m_params.GetConsensus(), tx, state); } std::optional findLocatorFork(const CBlockLocator &locator) override { diff --git a/src/test/miner_tests.cpp b/src/test/miner_tests.cpp --- a/src/test/miner_tests.cpp +++ b/src/test/miner_tests.cpp @@ -508,7 +508,7 @@ // Locktime passes. TxValidationState state; BOOST_CHECK(ContextualCheckTransactionForCurrentBlock( - params, CTransaction(tx), state, flags)); + ::ChainActive().Tip(), params, CTransaction(tx), state, flags)); } // Sequence locks fail. @@ -534,7 +534,7 @@ // Locktime passes. TxValidationState state; BOOST_CHECK(ContextualCheckTransactionForCurrentBlock( - params, CTransaction(tx), state, flags)); + ::ChainActive().Tip(), params, CTransaction(tx), state, flags)); } // Sequence locks fail. @@ -571,7 +571,7 @@ // Locktime fails. TxValidationState state; BOOST_CHECK(!ContextualCheckTransactionForCurrentBlock( - params, CTransaction(tx), state, flags)); + ::ChainActive().Tip(), params, CTransaction(tx), state, flags)); BOOST_CHECK_EQUAL(state.GetRejectReason(), "bad-txns-nonfinal"); } @@ -599,7 +599,7 @@ // Locktime fails. TxValidationState state; BOOST_CHECK(!ContextualCheckTransactionForCurrentBlock( - params, CTransaction(tx), state, flags)); + ::ChainActive().Tip(), params, CTransaction(tx), state, flags)); BOOST_CHECK_EQUAL(state.GetRejectReason(), "bad-txns-nonfinal"); } @@ -626,7 +626,7 @@ // Locktime passes. TxValidationState state; BOOST_CHECK(ContextualCheckTransactionForCurrentBlock( - params, CTransaction(tx), state, flags)); + ::ChainActive().Tip(), params, CTransaction(tx), state, flags)); } // Sequence locks pass. diff --git a/src/txmempool.cpp b/src/txmempool.cpp --- a/src/txmempool.cpp +++ b/src/txmempool.cpp @@ -585,7 +585,8 @@ TxValidationState state; if (!ContextualCheckTransactionForCurrentBlock( - config.GetChainParams().GetConsensus(), tx, state, flags) || + ::ChainActive().Tip(), config.GetChainParams().GetConsensus(), + tx, state, flags) || !CheckSequenceLocks(*this, tx, flags, &lp, validLP)) { // Note if CheckSequenceLocks fails the LockPoints may still be // invalid. So it's critical that we remove the tx and not depend on diff --git a/src/validation.h b/src/validation.h --- a/src/validation.h +++ b/src/validation.h @@ -518,10 +518,9 @@ * * See consensus/consensus.h for flag definitions. */ -bool ContextualCheckTransactionForCurrentBlock(const Consensus::Params ¶ms, - const CTransaction &tx, - TxValidationState &state, - int flags = -1) +bool ContextualCheckTransactionForCurrentBlock( + const CBlockIndex *active_chain_tip, const Consensus::Params ¶ms, + const CTransaction &tx, TxValidationState &state, int flags = -1) EXCLUSIVE_LOCKS_REQUIRED(cs_main); /** diff --git a/src/validation.cpp b/src/validation.cpp --- a/src/validation.cpp +++ b/src/validation.cpp @@ -469,6 +469,7 @@ // be mined yet. TxValidationState ctxState; if (!ContextualCheckTransactionForCurrentBlock( + ::ChainActive().Tip(), args.m_config.GetChainParams().GetConsensus(), tx, ctxState, STANDARD_LOCKTIME_VERIFY_FLAGS)) { // We copy the state from a dummy to ensure we don't increase the @@ -3940,11 +3941,12 @@ return true; } -bool ContextualCheckTransactionForCurrentBlock(const Consensus::Params ¶ms, - const CTransaction &tx, - TxValidationState &state, - int flags) { +bool ContextualCheckTransactionForCurrentBlock( + const CBlockIndex *active_chain_tip, const Consensus::Params ¶ms, + const CTransaction &tx, TxValidationState &state, int flags) { AssertLockHeld(cs_main); + assert(std::addressof(*::ChainActive().Tip()) == + std::addressof(*active_chain_tip)); // By convention a negative value for flags indicates that the current // network-enforced consensus rules should be used. In a future soft-fork @@ -3959,7 +3961,7 @@ // evaluated is what is used. Thus if we want to know if a transaction can // be part of the *next* block, we need to call ContextualCheckTransaction() // with one more than ::ChainActive().Height(). - const int nBlockHeight = ::ChainActive().Height() + 1; + const int nBlockHeight = active_chain_tip->nHeight + 1; // BIP113 will require that time-locked transactions have nLockTime set to // less than the median time of the previous block they're contained in. @@ -3967,9 +3969,7 @@ // chain tip, so we use that to calculate the median time passed to // ContextualCheckTransaction() if LOCKTIME_MEDIAN_TIME_PAST is set. const int64_t nMedianTimePast = - ::ChainActive().Tip() == nullptr - ? 0 - : ::ChainActive().Tip()->GetMedianTimePast(); + active_chain_tip == nullptr ? 0 : active_chain_tip->GetMedianTimePast(); const int64_t nLockTimeCutoff = (flags & LOCKTIME_MEDIAN_TIME_PAST) ? nMedianTimePast : GetAdjustedTime();