Skip to content

Commit fa0dfdf

Browse files
author
MarcoFalke
committed
refactor: Remove confusing BlockIndex global
1 parent 8edfc17 commit fa0dfdf

File tree

5 files changed

+22
-32
lines changed

5 files changed

+22
-32
lines changed

src/init.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1589,7 +1589,7 @@ bool AppInitMain(const util::Ref& context, NodeContext& node)
15891589

15901590
// If the loaded chain has a wrong genesis, bail out immediately
15911591
// (we're likely using a testnet datadir, or the other way around).
1592-
if (!::BlockIndex().empty() &&
1592+
if (!chainman.BlockIndex().empty() &&
15931593
!LookupBlockIndex(chainparams.GetConsensus().hashGenesisBlock)) {
15941594
return InitError(_("Incorrect or no genesis block found. Wrong datadir for network?"));
15951595
}
@@ -1869,8 +1869,8 @@ bool AppInitMain(const util::Ref& context, NodeContext& node)
18691869
//// debug print
18701870
{
18711871
LOCK(cs_main);
1872-
LogPrintf("block tree size = %u\n", ::BlockIndex().size());
1873-
chain_active_height = ::ChainActive().Height();
1872+
LogPrintf("block tree size = %u\n", chainman.BlockIndex().size());
1873+
chain_active_height = chainman.ActiveChain().Height();
18741874
}
18751875
LogPrintf("nBestHeight = %d\n", chain_active_height);
18761876

src/rpc/blockchain.cpp

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1325,50 +1325,48 @@ static UniValue getchaintips(const JSONRPCRequest& request)
13251325
},
13261326
}.Check(request);
13271327

1328+
ChainstateManager& chainman = EnsureChainman(request.context);
13281329
LOCK(cs_main);
13291330

13301331
/*
1331-
* Idea: the set of chain tips is ::ChainActive().tip, plus orphan blocks which do not have another orphan building off of them.
1332+
* Idea: The set of chain tips is the active chain tip, plus orphan blocks which do not have another orphan building off of them.
13321333
* Algorithm:
13331334
* - Make one pass through BlockIndex(), picking out the orphan blocks, and also storing a set of the orphan block's pprev pointers.
13341335
* - Iterate through the orphan blocks. If the block isn't pointed to by another orphan, it is a chain tip.
1335-
* - add ::ChainActive().Tip()
1336+
* - Add the active chain tip
13361337
*/
13371338
std::set<const CBlockIndex*, CompareBlocksByHeight> setTips;
13381339
std::set<const CBlockIndex*> setOrphans;
13391340
std::set<const CBlockIndex*> setPrevs;
13401341

1341-
for (const std::pair<const uint256, CBlockIndex*>& item : ::BlockIndex())
1342-
{
1343-
if (!::ChainActive().Contains(item.second)) {
1342+
for (const std::pair<const uint256, CBlockIndex*>& item : chainman.BlockIndex()) {
1343+
if (!chainman.ActiveChain().Contains(item.second)) {
13441344
setOrphans.insert(item.second);
13451345
setPrevs.insert(item.second->pprev);
13461346
}
13471347
}
13481348

1349-
for (std::set<const CBlockIndex*>::iterator it = setOrphans.begin(); it != setOrphans.end(); ++it)
1350-
{
1349+
for (std::set<const CBlockIndex*>::iterator it = setOrphans.begin(); it != setOrphans.end(); ++it) {
13511350
if (setPrevs.erase(*it) == 0) {
13521351
setTips.insert(*it);
13531352
}
13541353
}
13551354

13561355
// Always report the currently active tip.
1357-
setTips.insert(::ChainActive().Tip());
1356+
setTips.insert(chainman.ActiveChain().Tip());
13581357

13591358
/* Construct the output array. */
13601359
UniValue res(UniValue::VARR);
1361-
for (const CBlockIndex* block : setTips)
1362-
{
1360+
for (const CBlockIndex* block : setTips) {
13631361
UniValue obj(UniValue::VOBJ);
13641362
obj.pushKV("height", block->nHeight);
13651363
obj.pushKV("hash", block->phashBlock->GetHex());
13661364

1367-
const int branchLen = block->nHeight - ::ChainActive().FindFork(block)->nHeight;
1365+
const int branchLen = block->nHeight - chainman.ActiveChain().FindFork(block)->nHeight;
13681366
obj.pushKV("branchlen", branchLen);
13691367

13701368
std::string status;
1371-
if (::ChainActive().Contains(block)) {
1369+
if (chainman.ActiveChain().Contains(block)) {
13721370
// This block is part of the currently active chain.
13731371
status = "active";
13741372
} else if (block->nStatus & BLOCK_FAILED_MASK) {

src/validation.cpp

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1321,12 +1321,6 @@ bool CChainState::IsInitialBlockDownload() const
13211321

13221322
static CBlockIndex *pindexBestForkTip = nullptr, *pindexBestForkBase = nullptr;
13231323

1324-
BlockMap& BlockIndex()
1325-
{
1326-
LOCK(::cs_main);
1327-
return g_chainman.m_blockman.m_block_index;
1328-
}
1329-
13301324
static void AlertNotify(const std::string& strMessage)
13311325
{
13321326
uiInterface.NotifyAlertChanged();

src/validation.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -886,9 +886,6 @@ CChainState& ChainstateActive();
886886
/** Please prefer the identical ChainstateManager::ActiveChain */
887887
CChain& ChainActive();
888888

889-
/** Please prefer the identical ChainstateManager::BlockIndex */
890-
BlockMap& BlockIndex();
891-
892889
/** Global variable that points to the active block tree (protected by cs_main) */
893890
extern std::unique_ptr<CBlockTreeDB> pblocktree;
894891

src/wallet/test/wallet_tests.cpp

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -333,15 +333,16 @@ BOOST_FIXTURE_TEST_CASE(coin_mark_dirty_immature_credit, TestChain100Setup)
333333
BOOST_CHECK_EQUAL(wtx.GetImmatureCredit(), 50*COIN);
334334
}
335335

336-
static int64_t AddTx(CWallet& wallet, uint32_t lockTime, int64_t mockTime, int64_t blockTime)
336+
static int64_t AddTx(ChainstateManager& chainman, CWallet& wallet, uint32_t lockTime, int64_t mockTime, int64_t blockTime)
337337
{
338338
CMutableTransaction tx;
339339
CWalletTx::Confirmation confirm;
340340
tx.nLockTime = lockTime;
341341
SetMockTime(mockTime);
342342
CBlockIndex* block = nullptr;
343343
if (blockTime > 0) {
344-
auto inserted = ::BlockIndex().emplace(GetRandHash(), new CBlockIndex);
344+
LOCK(cs_main);
345+
auto inserted = chainman.BlockIndex().emplace(GetRandHash(), new CBlockIndex);
345346
assert(inserted.second);
346347
const uint256& hash = inserted.first->first;
347348
block = inserted.first->second;
@@ -363,24 +364,24 @@ static int64_t AddTx(CWallet& wallet, uint32_t lockTime, int64_t mockTime, int64
363364
BOOST_AUTO_TEST_CASE(ComputeTimeSmart)
364365
{
365366
// New transaction should use clock time if lower than block time.
366-
BOOST_CHECK_EQUAL(AddTx(m_wallet, 1, 100, 120), 100);
367+
BOOST_CHECK_EQUAL(AddTx(*m_node.chainman, m_wallet, 1, 100, 120), 100);
367368

368369
// Test that updating existing transaction does not change smart time.
369-
BOOST_CHECK_EQUAL(AddTx(m_wallet, 1, 200, 220), 100);
370+
BOOST_CHECK_EQUAL(AddTx(*m_node.chainman, m_wallet, 1, 200, 220), 100);
370371

371372
// New transaction should use clock time if there's no block time.
372-
BOOST_CHECK_EQUAL(AddTx(m_wallet, 2, 300, 0), 300);
373+
BOOST_CHECK_EQUAL(AddTx(*m_node.chainman, m_wallet, 2, 300, 0), 300);
373374

374375
// New transaction should use block time if lower than clock time.
375-
BOOST_CHECK_EQUAL(AddTx(m_wallet, 3, 420, 400), 400);
376+
BOOST_CHECK_EQUAL(AddTx(*m_node.chainman, m_wallet, 3, 420, 400), 400);
376377

377378
// New transaction should use latest entry time if higher than
378379
// min(block time, clock time).
379-
BOOST_CHECK_EQUAL(AddTx(m_wallet, 4, 500, 390), 400);
380+
BOOST_CHECK_EQUAL(AddTx(*m_node.chainman, m_wallet, 4, 500, 390), 400);
380381

381382
// If there are future entries, new transaction should use time of the
382383
// newest entry that is no more than 300 seconds ahead of the clock time.
383-
BOOST_CHECK_EQUAL(AddTx(m_wallet, 5, 50, 600), 300);
384+
BOOST_CHECK_EQUAL(AddTx(*m_node.chainman, m_wallet, 5, 50, 600), 300);
384385

385386
// Reset mock time for other tests.
386387
SetMockTime(0);

0 commit comments

Comments
 (0)