Skip to content

Commit 27724c2

Browse files
author
MacroFake
committed
Merge bitcoin/bitcoin#25677: refactor: make active_chain_tip a reference
9376a6d refactor: make active_chain_tip a reference (Aurèle Oulès) Pull request description: This PR fixes a TODO introduced in #21055. Makes `active_chain_tip` argument in `CheckFinalTxAtTip` function a reference instead of a pointer. ACKs for top commit: dongcarl: ACK 9376a6d Tree-SHA512: c36d1769e0b9598b7f79334704b26b73e958d54caa3bd7e4eff954f3964fcf3f5e3a44a5a760497afad51b76e1614c86314fe035e4083c855e3574a620de7f4d
2 parents 5d294bc + 9376a6d commit 27724c2

File tree

3 files changed

+11
-12
lines changed

3 files changed

+11
-12
lines changed

src/test/miner_tests.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -389,7 +389,7 @@ void MinerTestingSetup::TestBasicMining(const CChainParams& chainparams, const C
389389
tx.nLockTime = 0;
390390
hash = tx.GetHash();
391391
m_node.mempool->addUnchecked(entry.Fee(HIGHFEE).Time(GetTime()).SpendsCoinbase(true).FromTx(tx));
392-
BOOST_CHECK(CheckFinalTxAtTip(m_node.chainman->ActiveChain().Tip(), CTransaction{tx})); // Locktime passes
392+
BOOST_CHECK(CheckFinalTxAtTip(*Assert(m_node.chainman->ActiveChain().Tip()), CTransaction{tx})); // Locktime passes
393393
BOOST_CHECK(!TestSequenceLocks(CTransaction{tx})); // Sequence locks fail
394394

395395
{
@@ -403,7 +403,7 @@ void MinerTestingSetup::TestBasicMining(const CChainParams& chainparams, const C
403403
prevheights[0] = baseheight + 2;
404404
hash = tx.GetHash();
405405
m_node.mempool->addUnchecked(entry.Time(GetTime()).FromTx(tx));
406-
BOOST_CHECK(CheckFinalTxAtTip(m_node.chainman->ActiveChain().Tip(), CTransaction{tx})); // Locktime passes
406+
BOOST_CHECK(CheckFinalTxAtTip(*Assert(m_node.chainman->ActiveChain().Tip()), CTransaction{tx})); // Locktime passes
407407
BOOST_CHECK(!TestSequenceLocks(CTransaction{tx})); // Sequence locks fail
408408

409409
const int SEQUENCE_LOCK_TIME = 512; // Sequence locks pass 512 seconds later
@@ -426,7 +426,7 @@ void MinerTestingSetup::TestBasicMining(const CChainParams& chainparams, const C
426426
tx.nLockTime = m_node.chainman->ActiveChain().Tip()->nHeight + 1;
427427
hash = tx.GetHash();
428428
m_node.mempool->addUnchecked(entry.Time(GetTime()).FromTx(tx));
429-
BOOST_CHECK(!CheckFinalTxAtTip(m_node.chainman->ActiveChain().Tip(), CTransaction{tx})); // Locktime fails
429+
BOOST_CHECK(!CheckFinalTxAtTip(*Assert(m_node.chainman->ActiveChain().Tip()), CTransaction{tx})); // Locktime fails
430430
BOOST_CHECK(TestSequenceLocks(CTransaction{tx})); // Sequence locks pass
431431
BOOST_CHECK(IsFinalTx(CTransaction(tx), m_node.chainman->ActiveChain().Tip()->nHeight + 2, m_node.chainman->ActiveChain().Tip()->GetMedianTimePast())); // Locktime passes on 2nd block
432432

@@ -437,7 +437,7 @@ void MinerTestingSetup::TestBasicMining(const CChainParams& chainparams, const C
437437
prevheights[0] = baseheight + 4;
438438
hash = tx.GetHash();
439439
m_node.mempool->addUnchecked(entry.Time(GetTime()).FromTx(tx));
440-
BOOST_CHECK(!CheckFinalTxAtTip(m_node.chainman->ActiveChain().Tip(), CTransaction{tx})); // Locktime fails
440+
BOOST_CHECK(!CheckFinalTxAtTip(*Assert(m_node.chainman->ActiveChain().Tip()), CTransaction{tx})); // Locktime fails
441441
BOOST_CHECK(TestSequenceLocks(CTransaction{tx})); // Sequence locks pass
442442
BOOST_CHECK(IsFinalTx(CTransaction(tx), m_node.chainman->ActiveChain().Tip()->nHeight + 2, m_node.chainman->ActiveChain().Tip()->GetMedianTimePast() + 1)); // Locktime passes 1 second later
443443

@@ -446,7 +446,7 @@ void MinerTestingSetup::TestBasicMining(const CChainParams& chainparams, const C
446446
prevheights[0] = m_node.chainman->ActiveChain().Tip()->nHeight + 1;
447447
tx.nLockTime = 0;
448448
tx.vin[0].nSequence = 0;
449-
BOOST_CHECK(CheckFinalTxAtTip(m_node.chainman->ActiveChain().Tip(), CTransaction{tx})); // Locktime passes
449+
BOOST_CHECK(CheckFinalTxAtTip(*Assert(m_node.chainman->ActiveChain().Tip()), CTransaction{tx})); // Locktime passes
450450
BOOST_CHECK(TestSequenceLocks(CTransaction{tx})); // Sequence locks pass
451451
tx.vin[0].nSequence = 1;
452452
BOOST_CHECK(!TestSequenceLocks(CTransaction{tx})); // Sequence locks fail

src/validation.cpp

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -157,25 +157,24 @@ bool CheckInputScripts(const CTransaction& tx, TxValidationState& state,
157157
std::vector<CScriptCheck>* pvChecks = nullptr)
158158
EXCLUSIVE_LOCKS_REQUIRED(cs_main);
159159

160-
bool CheckFinalTxAtTip(const CBlockIndex* active_chain_tip, const CTransaction& tx)
160+
bool CheckFinalTxAtTip(const CBlockIndex& active_chain_tip, const CTransaction& tx)
161161
{
162162
AssertLockHeld(cs_main);
163-
assert(active_chain_tip); // TODO: Make active_chain_tip a reference
164163

165164
// CheckFinalTxAtTip() uses active_chain_tip.Height()+1 to evaluate
166165
// nLockTime because when IsFinalTx() is called within
167166
// AcceptBlock(), the height of the block *being*
168167
// evaluated is what is used. Thus if we want to know if a
169168
// transaction can be part of the *next* block, we need to call
170169
// IsFinalTx() with one more than active_chain_tip.Height().
171-
const int nBlockHeight = active_chain_tip->nHeight + 1;
170+
const int nBlockHeight = active_chain_tip.nHeight + 1;
172171

173172
// BIP113 requires that time-locked transactions have nLockTime set to
174173
// less than the median time of the previous block they're contained in.
175174
// When the next block is created its previous block will be the current
176175
// chain tip, so we use that to calculate the median time passed to
177176
// IsFinalTx().
178-
const int64_t nBlockTime{active_chain_tip->GetMedianTimePast()};
177+
const int64_t nBlockTime{active_chain_tip.GetMedianTimePast()};
179178

180179
return IsFinalTx(tx, nBlockHeight, nBlockTime);
181180
}
@@ -337,7 +336,7 @@ void CChainState::MaybeUpdateMempoolForReorg(
337336
const CTransaction& tx = it->GetTx();
338337

339338
// The transaction must be final.
340-
if (!CheckFinalTxAtTip(m_chain.Tip(), tx)) return true;
339+
if (!CheckFinalTxAtTip(*Assert(m_chain.Tip()), tx)) return true;
341340
LockPoints lp = it->GetLockPoints();
342341
const bool validLP{TestLockPointValidity(m_chain, lp)};
343342
CCoinsViewMemPool view_mempool(&CoinsTip(), *m_mempool);
@@ -714,7 +713,7 @@ bool MemPoolAccept::PreChecks(ATMPArgs& args, Workspace& ws)
714713
// Only accept nLockTime-using transactions that can be mined in the next
715714
// block; we don't want our mempool filled up with transactions that can't
716715
// be mined yet.
717-
if (!CheckFinalTxAtTip(m_active_chainstate.m_chain.Tip(), tx)) {
716+
if (!CheckFinalTxAtTip(*Assert(m_active_chainstate.m_chain.Tip()), tx)) {
718717
return state.Invalid(TxValidationResult::TX_PREMATURE_SPEND, "non-final");
719718
}
720719

src/validation.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ PackageMempoolAcceptResult ProcessNewPackage(CChainState& active_chainstate, CTx
261261
/**
262262
* Check if transaction will be final in the next block to be created.
263263
*/
264-
bool CheckFinalTxAtTip(const CBlockIndex* active_chain_tip, const CTransaction& tx) EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
264+
bool CheckFinalTxAtTip(const CBlockIndex& active_chain_tip, const CTransaction& tx) EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
265265

266266
/**
267267
* Check if transaction will be BIP68 final in the next block to be created on top of tip.

0 commit comments

Comments
 (0)