Skip to content

Commit 36c83b4

Browse files
committed
Merge bitcoin/bitcoin#25023: Remove unused SetTip(nullptr) code
faab8dc Remove unused SetTip(nullptr) code (MacroFake) Pull request description: Now that this path is no longer used after commit b51e60f, we can remove it. Future code should reset `CChain` by simply discarding it and constructing a fresh one. ACKs for top commit: ryanofsky: Code review ACK faab8dc. Just moved an assert statement since last review Tree-SHA512: 7dc273b11133d85d32ca2a69c0c7c07b39cdd338141ef5b51496e7de334a809864d5459eb95535497866c8b1e468aae84ed8f91b543041e6ee20130d5622874e
2 parents 7312eff + faab8dc commit 36c83b4

File tree

5 files changed

+15
-16
lines changed

5 files changed

+15
-16
lines changed

src/chain.cpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,9 @@ std::string CBlockIndex::ToString() const
1818
pprev, nHeight, hashMerkleRoot.ToString(), GetBlockHash().ToString());
1919
}
2020

21-
void CChain::SetTip(CBlockIndex *pindex) {
22-
if (pindex == nullptr) {
23-
vChain.clear();
24-
return;
25-
}
21+
void CChain::SetTip(CBlockIndex& block)
22+
{
23+
CBlockIndex* pindex = █
2624
vChain.resize(pindex->nHeight + 1);
2725
while (pindex && vChain[pindex->nHeight] != pindex) {
2826
vChain[pindex->nHeight] = pindex;

src/chain.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -465,7 +465,7 @@ class CChain
465465
}
466466

467467
/** Set/initialize a chain with a given tip. */
468-
void SetTip(CBlockIndex* pindex);
468+
void SetTip(CBlockIndex& block);
469469

470470
/** Return a CBlockLocator that refers to a block in this chain (by default the tip). */
471471
CBlockLocator GetLocator(const CBlockIndex* pindex = nullptr) const;

src/test/miner_tests.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,7 @@ void MinerTestingSetup::TestBasicMining(const CChainParams& chainparams, const C
325325
next->pprev = prev;
326326
next->nHeight = prev->nHeight + 1;
327327
next->BuildSkip();
328-
m_node.chainman->ActiveChain().SetTip(next);
328+
m_node.chainman->ActiveChain().SetTip(*next);
329329
}
330330
BOOST_CHECK(pblocktemplate = AssemblerForTest(chainparams).CreateNewBlock(scriptPubKey));
331331
// Extend to a 210000-long block chain.
@@ -337,7 +337,7 @@ void MinerTestingSetup::TestBasicMining(const CChainParams& chainparams, const C
337337
next->pprev = prev;
338338
next->nHeight = prev->nHeight + 1;
339339
next->BuildSkip();
340-
m_node.chainman->ActiveChain().SetTip(next);
340+
m_node.chainman->ActiveChain().SetTip(*next);
341341
}
342342
BOOST_CHECK(pblocktemplate = AssemblerForTest(chainparams).CreateNewBlock(scriptPubKey));
343343

@@ -362,7 +362,7 @@ void MinerTestingSetup::TestBasicMining(const CChainParams& chainparams, const C
362362
// Delete the dummy blocks again.
363363
while (m_node.chainman->ActiveChain().Tip()->nHeight > nHeight) {
364364
CBlockIndex* del = m_node.chainman->ActiveChain().Tip();
365-
m_node.chainman->ActiveChain().SetTip(del->pprev);
365+
m_node.chainman->ActiveChain().SetTip(*Assert(del->pprev));
366366
m_node.chainman->ActiveChainstate().CoinsTip().SetBestBlock(del->pprev->GetBlockHash());
367367
delete del->phashBlock;
368368
delete del;

src/test/skiplist_tests.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ BOOST_AUTO_TEST_CASE(getlocator_test)
7272

7373
// Build a CChain for the main branch.
7474
CChain chain;
75-
chain.SetTip(&vBlocksMain.back());
75+
chain.SetTip(vBlocksMain.back());
7676

7777
// Test 100 random starting points for locators.
7878
for (int n=0; n<100; n++) {
@@ -128,7 +128,7 @@ BOOST_AUTO_TEST_CASE(findearliestatleast_test)
128128

129129
// Build a CChain for the main branch.
130130
CChain chain;
131-
chain.SetTip(&vBlocksMain.back());
131+
chain.SetTip(vBlocksMain.back());
132132

133133
// Verify that FindEarliestAtLeast is correct.
134134
for (unsigned int i=0; i<10000; ++i) {
@@ -155,7 +155,7 @@ BOOST_AUTO_TEST_CASE(findearliestatleast_edge_test)
155155
}
156156

157157
CChain chain;
158-
chain.SetTip(&blocks.back());
158+
chain.SetTip(blocks.back());
159159

160160
BOOST_CHECK_EQUAL(chain.FindEarliestAtLeast(50, 0)->nHeight, 0);
161161
BOOST_CHECK_EQUAL(chain.FindEarliestAtLeast(100, 0)->nHeight, 0);

src/validation.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2583,6 +2583,7 @@ bool CChainState::DisconnectTip(BlockValidationState& state, DisconnectedBlockTr
25832583

25842584
CBlockIndex *pindexDelete = m_chain.Tip();
25852585
assert(pindexDelete);
2586+
assert(pindexDelete->pprev);
25862587
// Read block from disk.
25872588
std::shared_ptr<CBlock> pblock = std::make_shared<CBlock>();
25882589
CBlock& block = *pblock;
@@ -2630,7 +2631,7 @@ bool CChainState::DisconnectTip(BlockValidationState& state, DisconnectedBlockTr
26302631
}
26312632
}
26322633

2633-
m_chain.SetTip(pindexDelete->pprev);
2634+
m_chain.SetTip(*pindexDelete->pprev);
26342635

26352636
UpdateTip(pindexDelete->pprev);
26362637
// Let wallets know transactions went from 1-confirmed to
@@ -2744,7 +2745,7 @@ bool CChainState::ConnectTip(BlockValidationState& state, CBlockIndex* pindexNew
27442745
disconnectpool.removeForBlock(blockConnecting.vtx);
27452746
}
27462747
// Update m_chain & related variables.
2747-
m_chain.SetTip(pindexNew);
2748+
m_chain.SetTip(*pindexNew);
27482749
UpdateTip(pindexNew);
27492750

27502751
int64_t nTime6 = GetTimeMicros(); nTimePostConnect += nTime6 - nTime5; nTimeTotal += nTime6 - nTime1;
@@ -3892,7 +3893,7 @@ bool CChainState::LoadChainTip()
38923893
if (!pindex) {
38933894
return false;
38943895
}
3895-
m_chain.SetTip(pindex);
3896+
m_chain.SetTip(*pindex);
38963897
PruneBlockIndexCandidates();
38973898

38983899
tip = m_chain.Tip();
@@ -4969,7 +4970,7 @@ bool ChainstateManager::PopulateAndValidateSnapshot(
49694970
return false;
49704971
}
49714972

4972-
snapshot_chainstate.m_chain.SetTip(snapshot_start_block);
4973+
snapshot_chainstate.m_chain.SetTip(*snapshot_start_block);
49734974

49744975
// The remainder of this function requires modifying data protected by cs_main.
49754976
LOCK(::cs_main);

0 commit comments

Comments
 (0)