Skip to content

Commit 8e770d3

Browse files
committed
Merge 915ac8a into merged_master (Bitcoin PR #19413)
2 parents d164aab + 915ac8a commit 8e770d3

7 files changed

Lines changed: 37 additions & 42 deletions

File tree

src/init.cpp

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

16431643
// If the loaded chain has a wrong genesis, bail out immediately
16441644
// (we're likely using a testnet datadir, or the other way around).
1645-
if (!::BlockIndex().empty() &&
1645+
if (!chainman.BlockIndex().empty() &&
16461646
!LookupBlockIndex(chainparams.GetConsensus().hashGenesisBlock)) {
16471647
return InitError(_("Incorrect or no genesis block found. Wrong datadir for network?"));
16481648
}
@@ -1922,8 +1922,8 @@ bool AppInitMain(const util::Ref& context, NodeContext& node)
19221922
//// debug print
19231923
{
19241924
LOCK(cs_main);
1925-
LogPrintf("block tree size = %u\n", ::BlockIndex().size());
1926-
chain_active_height = ::ChainActive().Height();
1925+
LogPrintf("block tree size = %u\n", chainman.BlockIndex().size());
1926+
chain_active_height = chainman.ActiveChain().Height();
19271927
}
19281928
LogPrintf("nBestHeight = %d\n", chain_active_height);
19291929

src/rpc/blockchain.cpp

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1467,50 +1467,48 @@ static UniValue getchaintips(const JSONRPCRequest& request)
14671467
},
14681468
}.Check(request);
14691469

1470+
ChainstateManager& chainman = EnsureChainman(request.context);
14701471
LOCK(cs_main);
14711472

14721473
/*
1473-
* Idea: the set of chain tips is ::ChainActive().tip, plus orphan blocks which do not have another orphan building off of them.
1474+
* Idea: The set of chain tips is the active chain tip, plus orphan blocks which do not have another orphan building off of them.
14741475
* Algorithm:
14751476
* - Make one pass through BlockIndex(), picking out the orphan blocks, and also storing a set of the orphan block's pprev pointers.
14761477
* - Iterate through the orphan blocks. If the block isn't pointed to by another orphan, it is a chain tip.
1477-
* - add ::ChainActive().Tip()
1478+
* - Add the active chain tip
14781479
*/
14791480
std::set<const CBlockIndex*, CompareBlocksByHeight> setTips;
14801481
std::set<const CBlockIndex*> setOrphans;
14811482
std::set<const CBlockIndex*> setPrevs;
14821483

1483-
for (const std::pair<const uint256, CBlockIndex*>& item : ::BlockIndex())
1484-
{
1485-
if (!::ChainActive().Contains(item.second)) {
1484+
for (const std::pair<const uint256, CBlockIndex*>& item : chainman.BlockIndex()) {
1485+
if (!chainman.ActiveChain().Contains(item.second)) {
14861486
setOrphans.insert(item.second);
14871487
setPrevs.insert(item.second->pprev);
14881488
}
14891489
}
14901490

1491-
for (std::set<const CBlockIndex*>::iterator it = setOrphans.begin(); it != setOrphans.end(); ++it)
1492-
{
1491+
for (std::set<const CBlockIndex*>::iterator it = setOrphans.begin(); it != setOrphans.end(); ++it) {
14931492
if (setPrevs.erase(*it) == 0) {
14941493
setTips.insert(*it);
14951494
}
14961495
}
14971496

14981497
// Always report the currently active tip.
1499-
setTips.insert(::ChainActive().Tip());
1498+
setTips.insert(chainman.ActiveChain().Tip());
15001499

15011500
/* Construct the output array. */
15021501
UniValue res(UniValue::VARR);
1503-
for (const CBlockIndex* block : setTips)
1504-
{
1502+
for (const CBlockIndex* block : setTips) {
15051503
UniValue obj(UniValue::VOBJ);
15061504
obj.pushKV("height", block->nHeight);
15071505
obj.pushKV("hash", block->phashBlock->GetHex());
15081506

1509-
const int branchLen = block->nHeight - ::ChainActive().FindFork(block)->nHeight;
1507+
const int branchLen = block->nHeight - chainman.ActiveChain().FindFork(block)->nHeight;
15101508
obj.pushKV("branchlen", branchLen);
15111509

15121510
std::string status;
1513-
if (::ChainActive().Contains(block)) {
1511+
if (chainman.ActiveChain().Contains(block)) {
15141512
// This block is part of the currently active chain.
15151513
status = "active";
15161514
} else if (block->nStatus & BLOCK_FAILED_MASK) {

src/rpc/mining.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1641,11 +1641,12 @@ UniValue testproposedblock(const JSONRPCRequest& request)
16411641
if (!DecodeHexBlk(block, request.params[0].get_str()))
16421642
throw JSONRPCError(RPC_DESERIALIZATION_ERROR, "Block decode failed");
16431643

1644+
ChainstateManager& chainman = EnsureChainman(request.context);
16441645
LOCK(cs_main);
16451646

16461647
uint256 hash = block.GetHash();
1647-
BlockMap::iterator mi = ::BlockIndex().find(hash);
1648-
if (mi != ::BlockIndex().end())
1648+
BlockMap::iterator mi = chainman.BlockIndex().find(hash);
1649+
if (mi != chainman.BlockIndex().end())
16491650
throw JSONRPCError(RPC_VERIFY_ERROR, "already have block");
16501651

16511652
CBlockIndex* const pindexPrev = ::ChainActive().Tip();

src/validation.cpp

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1397,12 +1397,6 @@ bool CChainState::IsInitialBlockDownload() const
13971397

13981398
static CBlockIndex *pindexBestForkTip = nullptr, *pindexBestForkBase = nullptr;
13991399

1400-
BlockMap& BlockIndex()
1401-
{
1402-
LOCK(::cs_main);
1403-
return g_chainman.m_blockman.m_block_index;
1404-
}
1405-
14061400
static void AlertNotify(const std::string& strMessage)
14071401
{
14081402
uiInterface.NotifyAlertChanged();
@@ -5647,8 +5641,9 @@ bool MainchainRPCCheck(const bool init)
56475641
std::vector<uint256> vblocksToReconsiderAgain;
56485642
for(uint256& blockhash : vblocksToReconsider) {
56495643
LOCK(cs_main);
5650-
if (::BlockIndex().count(blockhash)) {
5651-
CBlockIndex* pblockindex = ::BlockIndex()[blockhash];
5644+
ChainstateManager& chainman = g_chainman;
5645+
if (chainman.BlockIndex().count(blockhash)) {
5646+
CBlockIndex* pblockindex = chainman.BlockIndex()[blockhash];
56525647
if ((pblockindex->nStatus & BLOCK_FAILED_MASK)) {
56535648
vblocksToReconsiderAgain.push_back(blockhash);
56545649
}
@@ -5723,9 +5718,10 @@ bool MainchainRPCCheck(const bool init)
57235718
for(const uint256& blockhash : vblocksToReconsider) {
57245719
{
57255720
LOCK(cs_main);
5726-
if (::BlockIndex().count(blockhash) == 0)
5721+
ChainstateManager& chainman = g_chainman;
5722+
if (chainman.BlockIndex().count(blockhash) == 0)
57275723
continue;
5728-
CBlockIndex* pblockindex = ::BlockIndex()[blockhash];
5724+
CBlockIndex* pblockindex = chainman.BlockIndex()[blockhash];
57295725
ResetBlockFailureFlags(pblockindex);
57305726
}
57315727
}
@@ -5740,8 +5736,9 @@ bool MainchainRPCCheck(const bool init)
57405736
//Now to clear out now-valid blocks
57415737
for(const uint256& blockhash : vblocksToReconsider) {
57425738
LOCK(cs_main);
5743-
if (::BlockIndex().count(blockhash)) {
5744-
CBlockIndex* pblockindex = ::BlockIndex()[blockhash];
5739+
ChainstateManager& chainman = g_chainman;
5740+
if (chainman.BlockIndex().count(blockhash)) {
5741+
CBlockIndex* pblockindex = chainman.BlockIndex()[blockhash];
57455742

57465743
//Marked as invalid still, put back into queue
57475744
if((pblockindex->nStatus & BLOCK_FAILED_MASK)) {

src/validation.h

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

893-
/** Please prefer the identical ChainstateManager::BlockIndex */
894-
BlockMap& BlockIndex();
895-
896893
/** Global variable that points to the active block tree (protected by cs_main) */
897894
extern std::unique_ptr<CBlockTreeDB> pblocktree;
898895

src/wallet/rpcwallet.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4869,6 +4869,7 @@ UniValue signblock(const JSONRPCRequest& request)
48694869
if (!DecodeHexBlk(block, request.params[0].get_str()))
48704870
throw JSONRPCError(RPC_DESERIALIZATION_ERROR, "Block decode failed");
48714871

4872+
ChainstateManager& chainman = g_chainman; // FIXME avoid using this global, see #19413
48724873
LOCK(cs_main);
48734874

48744875
LegacyScriptPubKeyMan* spk_man = pwallet->GetLegacyScriptPubKeyMan();
@@ -4877,8 +4878,8 @@ UniValue signblock(const JSONRPCRequest& request)
48774878
}
48784879

48794880
uint256 hash = block.GetHash();
4880-
BlockMap::iterator mi = ::BlockIndex().find(hash);
4881-
if (mi != ::BlockIndex().end())
4881+
BlockMap::iterator mi = chainman.BlockIndex().find(hash);
4882+
if (mi != chainman.BlockIndex().end())
48824883
throw JSONRPCError(RPC_VERIFY_ERROR, "already have block");
48834884

48844885
CBlockIndex* const pindexPrev = ::ChainActive().Tip();

src/wallet/test/wallet_tests.cpp

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -337,15 +337,16 @@ BOOST_FIXTURE_TEST_CASE(coin_mark_dirty_immature_credit, TestChain100Setup)
337337
BOOST_CHECK_EQUAL(wtx.GetImmatureCredit()[CAsset()], 50*COIN);
338338
}
339339

340-
static int64_t AddTx(CWallet& wallet, uint32_t lockTime, int64_t mockTime, int64_t blockTime)
340+
static int64_t AddTx(ChainstateManager& chainman, CWallet& wallet, uint32_t lockTime, int64_t mockTime, int64_t blockTime)
341341
{
342342
CMutableTransaction tx;
343343
CWalletTx::Confirmation confirm;
344344
tx.nLockTime = lockTime;
345345
SetMockTime(mockTime);
346346
CBlockIndex* block = nullptr;
347347
if (blockTime > 0) {
348-
auto inserted = ::BlockIndex().emplace(GetRandHash(), new CBlockIndex);
348+
LOCK(cs_main);
349+
auto inserted = chainman.BlockIndex().emplace(GetRandHash(), new CBlockIndex);
349350
assert(inserted.second);
350351
const uint256& hash = inserted.first->first;
351352
block = inserted.first->second;
@@ -367,24 +368,24 @@ static int64_t AddTx(CWallet& wallet, uint32_t lockTime, int64_t mockTime, int64
367368
BOOST_AUTO_TEST_CASE(ComputeTimeSmart)
368369
{
369370
// New transaction should use clock time if lower than block time.
370-
BOOST_CHECK_EQUAL(AddTx(m_wallet, 1, 100, 120), 100);
371+
BOOST_CHECK_EQUAL(AddTx(*m_node.chainman, m_wallet, 1, 100, 120), 100);
371372

372373
// Test that updating existing transaction does not change smart time.
373-
BOOST_CHECK_EQUAL(AddTx(m_wallet, 1, 200, 220), 100);
374+
BOOST_CHECK_EQUAL(AddTx(*m_node.chainman, m_wallet, 1, 200, 220), 100);
374375

375376
// New transaction should use clock time if there's no block time.
376-
BOOST_CHECK_EQUAL(AddTx(m_wallet, 2, 300, 0), 300);
377+
BOOST_CHECK_EQUAL(AddTx(*m_node.chainman, m_wallet, 2, 300, 0), 300);
377378

378379
// New transaction should use block time if lower than clock time.
379-
BOOST_CHECK_EQUAL(AddTx(m_wallet, 3, 420, 400), 400);
380+
BOOST_CHECK_EQUAL(AddTx(*m_node.chainman, m_wallet, 3, 420, 400), 400);
380381

381382
// New transaction should use latest entry time if higher than
382383
// min(block time, clock time).
383-
BOOST_CHECK_EQUAL(AddTx(m_wallet, 4, 500, 390), 400);
384+
BOOST_CHECK_EQUAL(AddTx(*m_node.chainman, m_wallet, 4, 500, 390), 400);
384385

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

389390
// Reset mock time for other tests.
390391
SetMockTime(0);

0 commit comments

Comments
 (0)