Skip to content

Commit 812ef53

Browse files
MacroFakePastaPastaPasta
authored andcommitted
Merge 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 bitcoin#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
1 parent 34e12fa commit 812ef53

File tree

4 files changed

+12
-13
lines changed

4 files changed

+12
-13
lines changed

src/node/interfaces.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -767,7 +767,7 @@ class ChainImpl : public Chain
767767
bool checkFinalTx(const CTransaction& tx) override
768768
{
769769
LOCK(cs_main);
770-
return CheckFinalTxAtTip(m_node.chainman->ActiveChain().Tip(), tx);
770+
return CheckFinalTxAtTip(*Assert(m_node.chainman->ActiveChain().Tip()), tx);
771771
}
772772
bool isInstantSendLockedTx(const uint256& hash) override
773773
{

src/test/miner_tests.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -448,7 +448,7 @@ BOOST_AUTO_TEST_CASE(CreateNewBlock_validity)
448448
tx.nLockTime = 0;
449449
hash = tx.GetHash();
450450
m_node.mempool->addUnchecked(entry.Fee(HIGHFEE).Time(GetTime()).SpendsCoinbase(true).FromTx(tx));
451-
BOOST_CHECK(CheckFinalTxAtTip(m_node.chainman->ActiveChain().Tip(), CTransaction{tx})); // Locktime passes
451+
BOOST_CHECK(CheckFinalTxAtTip(*Assert(m_node.chainman->ActiveChain().Tip()), CTransaction{tx})); // Locktime passes
452452
BOOST_CHECK(!TestSequenceLocks(CTransaction{tx})); // Sequence locks fail
453453

454454
{
@@ -462,7 +462,7 @@ BOOST_AUTO_TEST_CASE(CreateNewBlock_validity)
462462
prevheights[0] = baseheight + 2;
463463
hash = tx.GetHash();
464464
m_node.mempool->addUnchecked(entry.Time(GetTime()).FromTx(tx));
465-
BOOST_CHECK(CheckFinalTxAtTip(m_node.chainman->ActiveChain().Tip(), CTransaction{tx})); // Locktime passes
465+
BOOST_CHECK(CheckFinalTxAtTip(*Assert(m_node.chainman->ActiveChain().Tip()), CTransaction{tx})); // Locktime passes
466466
BOOST_CHECK(!TestSequenceLocks(CTransaction{tx})); // Sequence locks fail
467467

468468
for (int i = 0; i < CBlockIndex::nMedianTimeSpan; i++)
@@ -483,7 +483,7 @@ BOOST_AUTO_TEST_CASE(CreateNewBlock_validity)
483483
tx.nLockTime = m_node.chainman->ActiveChain().Tip()->nHeight + 1;
484484
hash = tx.GetHash();
485485
m_node.mempool->addUnchecked(entry.Time(GetTime()).FromTx(tx));
486-
BOOST_CHECK(!CheckFinalTxAtTip(m_node.chainman->ActiveChain().Tip(), CTransaction{tx})); // Locktime fails
486+
BOOST_CHECK(!CheckFinalTxAtTip(*Assert(m_node.chainman->ActiveChain().Tip()), CTransaction{tx})); // Locktime fails
487487
BOOST_CHECK(TestSequenceLocks(CTransaction{tx})); // Sequence locks pass
488488
BOOST_CHECK(IsFinalTx(CTransaction(tx), m_node.chainman->ActiveChain().Tip()->nHeight + 2, m_node.chainman->ActiveChain().Tip()->GetMedianTimePast())); // Locktime passes on 2nd block
489489

@@ -494,7 +494,7 @@ BOOST_AUTO_TEST_CASE(CreateNewBlock_validity)
494494
prevheights[0] = baseheight + 4;
495495
hash = tx.GetHash();
496496
m_node.mempool->addUnchecked(entry.Time(GetTime()).FromTx(tx));
497-
BOOST_CHECK(!CheckFinalTxAtTip(m_node.chainman->ActiveChain().Tip(), CTransaction{tx})); // Locktime fails
497+
BOOST_CHECK(!CheckFinalTxAtTip(*Assert(m_node.chainman->ActiveChain().Tip()), CTransaction{tx})); // Locktime fails
498498
BOOST_CHECK(TestSequenceLocks(CTransaction{tx})); // Sequence locks pass
499499
BOOST_CHECK(IsFinalTx(CTransaction(tx), m_node.chainman->ActiveChain().Tip()->nHeight + 2, m_node.chainman->ActiveChain().Tip()->GetMedianTimePast() + 1)); // Locktime passes 1 second later
500500

@@ -503,7 +503,7 @@ BOOST_AUTO_TEST_CASE(CreateNewBlock_validity)
503503
prevheights[0] = m_node.chainman->ActiveChain().Tip()->nHeight + 1;
504504
tx.nLockTime = 0;
505505
tx.vin[0].nSequence = 0;
506-
BOOST_CHECK(CheckFinalTxAtTip(m_node.chainman->ActiveChain().Tip(), CTransaction{tx})); // Locktime passes
506+
BOOST_CHECK(CheckFinalTxAtTip(*Assert(m_node.chainman->ActiveChain().Tip()), CTransaction{tx})); // Locktime passes
507507
BOOST_CHECK(TestSequenceLocks(CTransaction{tx})); // Sequence locks pass
508508
tx.vin[0].nSequence = 1;
509509
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
@@ -145,25 +145,24 @@ const CBlockIndex* CChainState::FindForkInGlobalIndex(const CBlockLocator& locat
145145

146146
bool CheckInputScripts(const CTransaction& tx, TxValidationState &state, const CCoinsViewCache &inputs, unsigned int flags, bool cacheSigStore, bool cacheFullScriptStore, PrecomputedTransactionData& txdata, std::vector<CScriptCheck> *pvChecks = nullptr);
147147

148-
bool CheckFinalTxAtTip(const CBlockIndex* active_chain_tip, const CTransaction& tx)
148+
bool CheckFinalTxAtTip(const CBlockIndex& active_chain_tip, const CTransaction& tx)
149149
{
150150
AssertLockHeld(cs_main);
151-
assert(active_chain_tip); // TODO: Make active_chain_tip a reference
152151

153152
// CheckFinalTxAtTip() uses active_chain_tip.Height()+1 to evaluate
154153
// nLockTime because when IsFinalTx() is called within
155154
// AcceptBlock(), the height of the block *being*
156155
// evaluated is what is used. Thus if we want to know if a
157156
// transaction can be part of the *next* block, we need to call
158157
// IsFinalTx() with one more than active_chain_tip.Height().
159-
const int nBlockHeight = active_chain_tip->nHeight + 1;
158+
const int nBlockHeight = active_chain_tip.nHeight + 1;
160159

161160
// BIP113 requires that time-locked transactions have nLockTime set to
162161
// less than the median time of the previous block they're contained in.
163162
// When the next block is created its previous block will be the current
164163
// chain tip, so we use that to calculate the median time passed to
165164
// IsFinalTx().
166-
const int64_t nBlockTime{active_chain_tip->GetMedianTimePast()};
165+
const int64_t nBlockTime{active_chain_tip.GetMedianTimePast()};
167166

168167
return IsFinalTx(tx, nBlockHeight, nBlockTime);
169168
}
@@ -380,7 +379,7 @@ void CChainState::MaybeUpdateMempoolForReorg(
380379
const CTransaction& tx = it->GetTx();
381380

382381
// The transaction must be final.
383-
if (!CheckFinalTxAtTip(m_chain.Tip(), tx)) return true;
382+
if (!CheckFinalTxAtTip(*Assert(m_chain.Tip()), tx)) return true;
384383
LockPoints lp = it->GetLockPoints();
385384
const bool validLP{TestLockPointValidity(m_chain, lp)};
386385
CCoinsViewMemPool view_mempool(&CoinsTip(), *m_mempool);
@@ -632,7 +631,7 @@ bool MemPoolAccept::PreChecks(ATMPArgs& args, Workspace& ws)
632631
// Only accept nLockTime-using transactions that can be mined in the next
633632
// block; we don't want our mempool filled up with transactions that can't
634633
// be mined yet.
635-
if (!CheckFinalTxAtTip(m_active_chainstate.m_chain.Tip(), tx)) {
634+
if (!CheckFinalTxAtTip(*Assert(m_active_chainstate.m_chain.Tip()), tx)) {
636635
return state.Invalid(TxValidationResult::TX_PREMATURE_SPEND, "non-final");
637636
}
638637

src/validation.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ int GetUTXOConfirmations(CChainState& active_chainstate, const COutPoint& outpoi
280280
/**
281281
* Check if transaction will be final in the next block to be created.
282282
*/
283-
bool CheckFinalTxAtTip(const CBlockIndex* active_chain_tip, const CTransaction& tx) EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
283+
bool CheckFinalTxAtTip(const CBlockIndex& active_chain_tip, const CTransaction& tx) EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
284284

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

0 commit comments

Comments
 (0)