0
0
Fork 0
mirror of https://github.com/bitcoin/bitcoin.git synced 2025-02-09 10:43:19 -05:00

Merge bitcoin/bitcoin#25023: Remove unused SetTip(nullptr) code

faab8dceb3 Remove unused SetTip(nullptr) code (MacroFake)

Pull request description:

  Now that this path is no longer used after commit b51e60f914, 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 faab8dceb3. Just moved an assert statement since last review

Tree-SHA512: 7dc273b11133d85d32ca2a69c0c7c07b39cdd338141ef5b51496e7de334a809864d5459eb95535497866c8b1e468aae84ed8f91b543041e6ee20130d5622874e
This commit is contained in:
fanquake 2022-08-04 16:45:03 +01:00
commit 36c83b40bd
No known key found for this signature in database
GPG key ID: 2EEB9F5CC09526C1
5 changed files with 15 additions and 16 deletions

View file

@ -18,11 +18,9 @@ std::string CBlockIndex::ToString() const
pprev, nHeight, hashMerkleRoot.ToString(), GetBlockHash().ToString()); pprev, nHeight, hashMerkleRoot.ToString(), GetBlockHash().ToString());
} }
void CChain::SetTip(CBlockIndex *pindex) { void CChain::SetTip(CBlockIndex& block)
if (pindex == nullptr) { {
vChain.clear(); CBlockIndex* pindex = █
return;
}
vChain.resize(pindex->nHeight + 1); vChain.resize(pindex->nHeight + 1);
while (pindex && vChain[pindex->nHeight] != pindex) { while (pindex && vChain[pindex->nHeight] != pindex) {
vChain[pindex->nHeight] = pindex; vChain[pindex->nHeight] = pindex;

View file

@ -465,7 +465,7 @@ public:
} }
/** Set/initialize a chain with a given tip. */ /** Set/initialize a chain with a given tip. */
void SetTip(CBlockIndex* pindex); void SetTip(CBlockIndex& block);
/** Return a CBlockLocator that refers to a block in this chain (by default the tip). */ /** Return a CBlockLocator that refers to a block in this chain (by default the tip). */
CBlockLocator GetLocator(const CBlockIndex* pindex = nullptr) const; CBlockLocator GetLocator(const CBlockIndex* pindex = nullptr) const;

View file

@ -325,7 +325,7 @@ void MinerTestingSetup::TestBasicMining(const CChainParams& chainparams, const C
next->pprev = prev; next->pprev = prev;
next->nHeight = prev->nHeight + 1; next->nHeight = prev->nHeight + 1;
next->BuildSkip(); next->BuildSkip();
m_node.chainman->ActiveChain().SetTip(next); m_node.chainman->ActiveChain().SetTip(*next);
} }
BOOST_CHECK(pblocktemplate = AssemblerForTest(chainparams).CreateNewBlock(scriptPubKey)); BOOST_CHECK(pblocktemplate = AssemblerForTest(chainparams).CreateNewBlock(scriptPubKey));
// Extend to a 210000-long block chain. // Extend to a 210000-long block chain.
@ -337,7 +337,7 @@ void MinerTestingSetup::TestBasicMining(const CChainParams& chainparams, const C
next->pprev = prev; next->pprev = prev;
next->nHeight = prev->nHeight + 1; next->nHeight = prev->nHeight + 1;
next->BuildSkip(); next->BuildSkip();
m_node.chainman->ActiveChain().SetTip(next); m_node.chainman->ActiveChain().SetTip(*next);
} }
BOOST_CHECK(pblocktemplate = AssemblerForTest(chainparams).CreateNewBlock(scriptPubKey)); BOOST_CHECK(pblocktemplate = AssemblerForTest(chainparams).CreateNewBlock(scriptPubKey));
@ -362,7 +362,7 @@ void MinerTestingSetup::TestBasicMining(const CChainParams& chainparams, const C
// Delete the dummy blocks again. // Delete the dummy blocks again.
while (m_node.chainman->ActiveChain().Tip()->nHeight > nHeight) { while (m_node.chainman->ActiveChain().Tip()->nHeight > nHeight) {
CBlockIndex* del = m_node.chainman->ActiveChain().Tip(); CBlockIndex* del = m_node.chainman->ActiveChain().Tip();
m_node.chainman->ActiveChain().SetTip(del->pprev); m_node.chainman->ActiveChain().SetTip(*Assert(del->pprev));
m_node.chainman->ActiveChainstate().CoinsTip().SetBestBlock(del->pprev->GetBlockHash()); m_node.chainman->ActiveChainstate().CoinsTip().SetBestBlock(del->pprev->GetBlockHash());
delete del->phashBlock; delete del->phashBlock;
delete del; delete del;

View file

@ -72,7 +72,7 @@ BOOST_AUTO_TEST_CASE(getlocator_test)
// Build a CChain for the main branch. // Build a CChain for the main branch.
CChain chain; CChain chain;
chain.SetTip(&vBlocksMain.back()); chain.SetTip(vBlocksMain.back());
// Test 100 random starting points for locators. // Test 100 random starting points for locators.
for (int n=0; n<100; n++) { for (int n=0; n<100; n++) {
@ -128,7 +128,7 @@ BOOST_AUTO_TEST_CASE(findearliestatleast_test)
// Build a CChain for the main branch. // Build a CChain for the main branch.
CChain chain; CChain chain;
chain.SetTip(&vBlocksMain.back()); chain.SetTip(vBlocksMain.back());
// Verify that FindEarliestAtLeast is correct. // Verify that FindEarliestAtLeast is correct.
for (unsigned int i=0; i<10000; ++i) { for (unsigned int i=0; i<10000; ++i) {
@ -155,7 +155,7 @@ BOOST_AUTO_TEST_CASE(findearliestatleast_edge_test)
} }
CChain chain; CChain chain;
chain.SetTip(&blocks.back()); chain.SetTip(blocks.back());
BOOST_CHECK_EQUAL(chain.FindEarliestAtLeast(50, 0)->nHeight, 0); BOOST_CHECK_EQUAL(chain.FindEarliestAtLeast(50, 0)->nHeight, 0);
BOOST_CHECK_EQUAL(chain.FindEarliestAtLeast(100, 0)->nHeight, 0); BOOST_CHECK_EQUAL(chain.FindEarliestAtLeast(100, 0)->nHeight, 0);

View file

@ -2583,6 +2583,7 @@ bool CChainState::DisconnectTip(BlockValidationState& state, DisconnectedBlockTr
CBlockIndex *pindexDelete = m_chain.Tip(); CBlockIndex *pindexDelete = m_chain.Tip();
assert(pindexDelete); assert(pindexDelete);
assert(pindexDelete->pprev);
// Read block from disk. // Read block from disk.
std::shared_ptr<CBlock> pblock = std::make_shared<CBlock>(); std::shared_ptr<CBlock> pblock = std::make_shared<CBlock>();
CBlock& block = *pblock; CBlock& block = *pblock;
@ -2630,7 +2631,7 @@ bool CChainState::DisconnectTip(BlockValidationState& state, DisconnectedBlockTr
} }
} }
m_chain.SetTip(pindexDelete->pprev); m_chain.SetTip(*pindexDelete->pprev);
UpdateTip(pindexDelete->pprev); UpdateTip(pindexDelete->pprev);
// Let wallets know transactions went from 1-confirmed to // Let wallets know transactions went from 1-confirmed to
@ -2744,7 +2745,7 @@ bool CChainState::ConnectTip(BlockValidationState& state, CBlockIndex* pindexNew
disconnectpool.removeForBlock(blockConnecting.vtx); disconnectpool.removeForBlock(blockConnecting.vtx);
} }
// Update m_chain & related variables. // Update m_chain & related variables.
m_chain.SetTip(pindexNew); m_chain.SetTip(*pindexNew);
UpdateTip(pindexNew); UpdateTip(pindexNew);
int64_t nTime6 = GetTimeMicros(); nTimePostConnect += nTime6 - nTime5; nTimeTotal += nTime6 - nTime1; int64_t nTime6 = GetTimeMicros(); nTimePostConnect += nTime6 - nTime5; nTimeTotal += nTime6 - nTime1;
@ -3892,7 +3893,7 @@ bool CChainState::LoadChainTip()
if (!pindex) { if (!pindex) {
return false; return false;
} }
m_chain.SetTip(pindex); m_chain.SetTip(*pindex);
PruneBlockIndexCandidates(); PruneBlockIndexCandidates();
tip = m_chain.Tip(); tip = m_chain.Tip();
@ -4969,7 +4970,7 @@ bool ChainstateManager::PopulateAndValidateSnapshot(
return false; return false;
} }
snapshot_chainstate.m_chain.SetTip(snapshot_start_block); snapshot_chainstate.m_chain.SetTip(*snapshot_start_block);
// The remainder of this function requires modifying data protected by cs_main. // The remainder of this function requires modifying data protected by cs_main.
LOCK(::cs_main); LOCK(::cs_main);