Skip to content

Commit eae54e6

Browse files
committedJan 28, 2021
scripted-diff: Use BlockManager::LookupBlockIndex
[META] In a previous commit, we moved ::LookupBlockIndex to become a member function of BlockManager. This commit is split out from that one since it can be expressed nicely as a scripted-diff. -BEGIN VERIFY SCRIPT- find_regex='LookupBlockIndex' \ && git grep -l -E "$find_regex" -- src \ | grep -v '^src/validation\.\(cpp\|h\)$' \ | xargs sed -i -E "s@${find_regex}@g_chainman.m_blockman.LookupBlockIndex@g" -END VERIFY SCRIPT-
1 parent 15d20f4 commit eae54e6

12 files changed

+55
-55
lines changed
 

‎src/index/base.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ void BaseIndex::ChainStateFlushed(const CBlockLocator& locator)
239239
const CBlockIndex* locator_tip_index;
240240
{
241241
LOCK(cs_main);
242-
locator_tip_index = LookupBlockIndex(locator_tip_hash);
242+
locator_tip_index = g_chainman.m_blockman.LookupBlockIndex(locator_tip_hash);
243243
}
244244

245245
if (!locator_tip_index) {

‎src/init.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -1611,7 +1611,7 @@ bool AppInitMain(const util::Ref& context, NodeContext& node, interfaces::BlockA
16111611
// If the loaded chain has a wrong genesis, bail out immediately
16121612
// (we're likely using a testnet datadir, or the other way around).
16131613
if (!chainman.BlockIndex().empty() &&
1614-
!LookupBlockIndex(chainparams.GetConsensus().hashGenesisBlock)) {
1614+
!g_chainman.m_blockman.LookupBlockIndex(chainparams.GetConsensus().hashGenesisBlock)) {
16151615
return InitError(_("Incorrect or no genesis block found. Wrong datadir for network?"));
16161616
}
16171617

‎src/miner.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ void RegenerateCommitments(CBlock& block)
4545
tx.vout.erase(tx.vout.begin() + GetWitnessCommitmentIndex(block));
4646
block.vtx.at(0) = MakeTransactionRef(tx);
4747

48-
GenerateCoinbaseCommitment(block, WITH_LOCK(cs_main, return LookupBlockIndex(block.hashPrevBlock)), Params().GetConsensus());
48+
GenerateCoinbaseCommitment(block, WITH_LOCK(cs_main, return g_chainman.m_blockman.LookupBlockIndex(block.hashPrevBlock)), Params().GetConsensus());
4949

5050
block.hashMerkleRoot = BlockMerkleRoot(block);
5151
}

‎src/net_processing.cpp

+14-14
Original file line numberDiff line numberDiff line change
@@ -676,7 +676,7 @@ static void ProcessBlockAvailability(NodeId nodeid) EXCLUSIVE_LOCKS_REQUIRED(cs_
676676
assert(state != nullptr);
677677

678678
if (!state->hashLastUnknownBlock.IsNull()) {
679-
const CBlockIndex* pindex = LookupBlockIndex(state->hashLastUnknownBlock);
679+
const CBlockIndex* pindex = g_chainman.m_blockman.LookupBlockIndex(state->hashLastUnknownBlock);
680680
if (pindex && pindex->nChainWork > 0) {
681681
if (state->pindexBestKnownBlock == nullptr || pindex->nChainWork >= state->pindexBestKnownBlock->nChainWork) {
682682
state->pindexBestKnownBlock = pindex;
@@ -693,7 +693,7 @@ static void UpdateBlockAvailability(NodeId nodeid, const uint256 &hash) EXCLUSIV
693693

694694
ProcessBlockAvailability(nodeid);
695695

696-
const CBlockIndex* pindex = LookupBlockIndex(hash);
696+
const CBlockIndex* pindex = g_chainman.m_blockman.LookupBlockIndex(hash);
697697
if (pindex && pindex->nChainWork > 0) {
698698
// An actually better block was announced.
699699
if (state->pindexBestKnownBlock == nullptr || pindex->nChainWork >= state->pindexBestKnownBlock->nChainWork) {
@@ -1595,7 +1595,7 @@ bool static AlreadyHaveTx(const GenTxid& gtxid, const CTxMemPool& mempool) EXCLU
15951595

15961596
bool static AlreadyHaveBlock(const uint256& block_hash) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
15971597
{
1598-
return LookupBlockIndex(block_hash) != nullptr;
1598+
return g_chainman.m_blockman.LookupBlockIndex(block_hash) != nullptr;
15991599
}
16001600

16011601
void RelayTransaction(const uint256& txid, const uint256& wtxid, const CConnman& connman)
@@ -1685,7 +1685,7 @@ void static ProcessGetBlockData(CNode& pfrom, Peer& peer, const CChainParams& ch
16851685
bool need_activate_chain = false;
16861686
{
16871687
LOCK(cs_main);
1688-
const CBlockIndex* pindex = LookupBlockIndex(inv.hash);
1688+
const CBlockIndex* pindex = g_chainman.m_blockman.LookupBlockIndex(inv.hash);
16891689
if (pindex) {
16901690
if (pindex->HaveTxsDownloaded() && !pindex->IsValid(BLOCK_VALID_SCRIPTS) &&
16911691
pindex->IsValid(BLOCK_VALID_TREE)) {
@@ -1706,7 +1706,7 @@ void static ProcessGetBlockData(CNode& pfrom, Peer& peer, const CChainParams& ch
17061706
}
17071707

17081708
LOCK(cs_main);
1709-
const CBlockIndex* pindex = LookupBlockIndex(inv.hash);
1709+
const CBlockIndex* pindex = g_chainman.m_blockman.LookupBlockIndex(inv.hash);
17101710
if (pindex) {
17111711
send = BlockRequestAllowed(pindex, consensusParams);
17121712
if (!send) {
@@ -1996,7 +1996,7 @@ void PeerManagerImpl::ProcessHeadersMessage(CNode& pfrom, const Peer& peer,
19961996
// don't connect before giving DoS points
19971997
// - Once a headers message is received that is valid and does connect,
19981998
// nUnconnectingHeaders gets reset back to 0.
1999-
if (!LookupBlockIndex(headers[0].hashPrevBlock) && nCount < MAX_BLOCKS_TO_ANNOUNCE) {
1999+
if (!g_chainman.m_blockman.LookupBlockIndex(headers[0].hashPrevBlock) && nCount < MAX_BLOCKS_TO_ANNOUNCE) {
20002000
nodestate->nUnconnectingHeaders++;
20012001
m_connman.PushMessage(&pfrom, msgMaker.Make(NetMsgType::GETHEADERS, ::ChainActive().GetLocator(pindexBestHeader), uint256()));
20022002
LogPrint(BCLog::NET, "received header %s: missing prev block %s, sending getheaders (%d) to end (peer=%d, nUnconnectingHeaders=%d)\n",
@@ -2026,7 +2026,7 @@ void PeerManagerImpl::ProcessHeadersMessage(CNode& pfrom, const Peer& peer,
20262026

20272027
// If we don't have the last header, then they'll have given us
20282028
// something new (if these headers are valid).
2029-
if (!LookupBlockIndex(hashLastBlock)) {
2029+
if (!g_chainman.m_blockman.LookupBlockIndex(hashLastBlock)) {
20302030
received_new_header = true;
20312031
}
20322032
}
@@ -2278,7 +2278,7 @@ static bool PrepareBlockFilterRequest(CNode& peer, const CChainParams& chain_par
22782278

22792279
{
22802280
LOCK(cs_main);
2281-
stop_index = LookupBlockIndex(stop_hash);
2281+
stop_index = g_chainman.m_blockman.LookupBlockIndex(stop_hash);
22822282

22832283
// Check that the stop block exists and the peer would be allowed to fetch it.
22842284
if (!stop_index || !BlockRequestAllowed(stop_index, chain_params.GetConsensus())) {
@@ -3021,7 +3021,7 @@ void PeerManagerImpl::ProcessMessage(CNode& pfrom, const std::string& msg_type,
30213021
{
30223022
LOCK(cs_main);
30233023

3024-
const CBlockIndex* pindex = LookupBlockIndex(req.blockhash);
3024+
const CBlockIndex* pindex = g_chainman.m_blockman.LookupBlockIndex(req.blockhash);
30253025
if (!pindex || !(pindex->nStatus & BLOCK_HAVE_DATA)) {
30263026
LogPrint(BCLog::NET, "Peer %d sent us a getblocktxn for a block we don't have\n", pfrom.GetId());
30273027
return;
@@ -3075,7 +3075,7 @@ void PeerManagerImpl::ProcessMessage(CNode& pfrom, const std::string& msg_type,
30753075
if (locator.IsNull())
30763076
{
30773077
// If locator is null, return the hashStop block
3078-
pindex = LookupBlockIndex(hashStop);
3078+
pindex = g_chainman.m_blockman.LookupBlockIndex(hashStop);
30793079
if (!pindex) {
30803080
return;
30813081
}
@@ -3352,14 +3352,14 @@ void PeerManagerImpl::ProcessMessage(CNode& pfrom, const std::string& msg_type,
33523352
{
33533353
LOCK(cs_main);
33543354

3355-
if (!LookupBlockIndex(cmpctblock.header.hashPrevBlock)) {
3355+
if (!g_chainman.m_blockman.LookupBlockIndex(cmpctblock.header.hashPrevBlock)) {
33563356
// Doesn't connect (or is genesis), instead of DoSing in AcceptBlockHeader, request deeper headers
33573357
if (!::ChainstateActive().IsInitialBlockDownload())
33583358
m_connman.PushMessage(&pfrom, msgMaker.Make(NetMsgType::GETHEADERS, ::ChainActive().GetLocator(pindexBestHeader), uint256()));
33593359
return;
33603360
}
33613361

3362-
if (!LookupBlockIndex(cmpctblock.header.GetHash())) {
3362+
if (!g_chainman.m_blockman.LookupBlockIndex(cmpctblock.header.GetHash())) {
33633363
received_new_header = true;
33643364
}
33653365
}
@@ -4425,7 +4425,7 @@ bool PeerManagerImpl::SendMessages(CNode* pto)
44254425
// then send all headers past that one. If we come across any
44264426
// headers that aren't on ::ChainActive(), give up.
44274427
for (const uint256& hash : peer->m_blocks_for_headers_relay) {
4428-
const CBlockIndex* pindex = LookupBlockIndex(hash);
4428+
const CBlockIndex* pindex = g_chainman.m_blockman.LookupBlockIndex(hash);
44294429
assert(pindex);
44304430
if (::ChainActive()[pindex->nHeight] != pindex) {
44314431
// Bail out if we reorged away from this block
@@ -4517,7 +4517,7 @@ bool PeerManagerImpl::SendMessages(CNode* pto)
45174517
// in the past.
45184518
if (!peer->m_blocks_for_headers_relay.empty()) {
45194519
const uint256& hashToAnnounce = peer->m_blocks_for_headers_relay.back();
4520-
const CBlockIndex* pindex = LookupBlockIndex(hashToAnnounce);
4520+
const CBlockIndex* pindex = g_chainman.m_blockman.LookupBlockIndex(hashToAnnounce);
45214521
assert(pindex);
45224522

45234523
// Warn if we're announcing a block that is not on the main chain.

‎src/node/coinstats.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ static bool GetUTXOStats(CCoinsView* view, CCoinsStats& stats, T hash_obj, const
6363
stats.hashBlock = pcursor->GetBestBlock();
6464
{
6565
LOCK(cs_main);
66-
stats.nHeight = LookupBlockIndex(stats.hashBlock)->nHeight;
66+
stats.nHeight = g_chainman.m_blockman.LookupBlockIndex(stats.hashBlock)->nHeight;
6767
}
6868

6969
PrepareHash(hash_obj, stats);

‎src/node/interfaces.cpp

+8-8
Original file line numberDiff line numberDiff line change
@@ -456,7 +456,7 @@ class ChainImpl : public Chain
456456
{
457457
WAIT_LOCK(cs_main, lock);
458458
const CChain& active = Assert(m_node.chainman)->ActiveChain();
459-
return FillBlock(LookupBlockIndex(hash), block, lock, active);
459+
return FillBlock(g_chainman.m_blockman.LookupBlockIndex(hash), block, lock, active);
460460
}
461461
bool findFirstBlockWithTimeAndHeight(int64_t min_time, int min_height, const FoundBlock& block) override
462462
{
@@ -468,7 +468,7 @@ class ChainImpl : public Chain
468468
{
469469
WAIT_LOCK(cs_main, lock);
470470
const CChain& active = Assert(m_node.chainman)->ActiveChain();
471-
if (const CBlockIndex* block = LookupBlockIndex(block_hash)) {
471+
if (const CBlockIndex* block = g_chainman.m_blockman.LookupBlockIndex(block_hash)) {
472472
if (const CBlockIndex* ancestor = block->GetAncestor(ancestor_height)) {
473473
return FillBlock(ancestor, ancestor_out, lock, active);
474474
}
@@ -479,17 +479,17 @@ class ChainImpl : public Chain
479479
{
480480
WAIT_LOCK(cs_main, lock);
481481
const CChain& active = Assert(m_node.chainman)->ActiveChain();
482-
const CBlockIndex* block = LookupBlockIndex(block_hash);
483-
const CBlockIndex* ancestor = LookupBlockIndex(ancestor_hash);
482+
const CBlockIndex* block = g_chainman.m_blockman.LookupBlockIndex(block_hash);
483+
const CBlockIndex* ancestor = g_chainman.m_blockman.LookupBlockIndex(ancestor_hash);
484484
if (block && ancestor && block->GetAncestor(ancestor->nHeight) != ancestor) ancestor = nullptr;
485485
return FillBlock(ancestor, ancestor_out, lock, active);
486486
}
487487
bool findCommonAncestor(const uint256& block_hash1, const uint256& block_hash2, const FoundBlock& ancestor_out, const FoundBlock& block1_out, const FoundBlock& block2_out) override
488488
{
489489
WAIT_LOCK(cs_main, lock);
490490
const CChain& active = Assert(m_node.chainman)->ActiveChain();
491-
const CBlockIndex* block1 = LookupBlockIndex(block_hash1);
492-
const CBlockIndex* block2 = LookupBlockIndex(block_hash2);
491+
const CBlockIndex* block1 = g_chainman.m_blockman.LookupBlockIndex(block_hash1);
492+
const CBlockIndex* block2 = g_chainman.m_blockman.LookupBlockIndex(block_hash2);
493493
const CBlockIndex* ancestor = block1 && block2 ? LastCommonAncestor(block1, block2) : nullptr;
494494
// Using & instead of && below to avoid short circuiting and leaving
495495
// output uninitialized.
@@ -499,7 +499,7 @@ class ChainImpl : public Chain
499499
double guessVerificationProgress(const uint256& block_hash) override
500500
{
501501
LOCK(cs_main);
502-
return GuessVerificationProgress(Params().TxData(), LookupBlockIndex(block_hash));
502+
return GuessVerificationProgress(Params().TxData(), g_chainman.m_blockman.LookupBlockIndex(block_hash));
503503
}
504504
bool hasBlocks(const uint256& block_hash, int min_height, Optional<int> max_height) override
505505
{
@@ -511,7 +511,7 @@ class ChainImpl : public Chain
511511
// used to limit the range, and passing min_height that's too low or
512512
// max_height that's too high will not crash or change the result.
513513
LOCK(::cs_main);
514-
if (CBlockIndex* block = LookupBlockIndex(block_hash)) {
514+
if (CBlockIndex* block = g_chainman.m_blockman.LookupBlockIndex(block_hash)) {
515515
if (max_height && block->nHeight >= *max_height) block = block->GetAncestor(*max_height);
516516
for (; block->nStatus & BLOCK_HAVE_DATA; block = block->pprev) {
517517
// Check pprev to not segfault if min_height is too low

‎src/rest.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ static bool rest_headers(const util::Ref& context,
179179
{
180180
LOCK(cs_main);
181181
tip = ::ChainActive().Tip();
182-
const CBlockIndex* pindex = LookupBlockIndex(hash);
182+
const CBlockIndex* pindex = g_chainman.m_blockman.LookupBlockIndex(hash);
183183
while (pindex != nullptr && ::ChainActive().Contains(pindex)) {
184184
headers.push_back(pindex);
185185
if (headers.size() == (unsigned long)count)
@@ -247,7 +247,7 @@ static bool rest_block(HTTPRequest* req,
247247
{
248248
LOCK(cs_main);
249249
tip = ::ChainActive().Tip();
250-
pblockindex = LookupBlockIndex(hash);
250+
pblockindex = g_chainman.m_blockman.LookupBlockIndex(hash);
251251
if (!pblockindex) {
252252
return RESTERR(req, HTTP_NOT_FOUND, hashStr + " not found");
253253
}

‎src/rpc/blockchain.cpp

+10-10
Original file line numberDiff line numberDiff line change
@@ -843,7 +843,7 @@ static RPCHelpMan getblockheader()
843843
const CBlockIndex* tip;
844844
{
845845
LOCK(cs_main);
846-
pblockindex = LookupBlockIndex(hash);
846+
pblockindex = g_chainman.m_blockman.LookupBlockIndex(hash);
847847
tip = ::ChainActive().Tip();
848848
}
849849

@@ -967,7 +967,7 @@ static RPCHelpMan getblock()
967967
const CBlockIndex* tip;
968968
{
969969
LOCK(cs_main);
970-
pblockindex = LookupBlockIndex(hash);
970+
pblockindex = g_chainman.m_blockman.LookupBlockIndex(hash);
971971
tip = ::ChainActive().Tip();
972972

973973
if (!pblockindex) {
@@ -1164,7 +1164,7 @@ static RPCHelpMan gettxout()
11641164
}
11651165
}
11661166

1167-
const CBlockIndex* pindex = LookupBlockIndex(coins_view->GetBestBlock());
1167+
const CBlockIndex* pindex = g_chainman.m_blockman.LookupBlockIndex(coins_view->GetBestBlock());
11681168
ret.pushKV("bestblock", pindex->GetBlockHash().GetHex());
11691169
if (coin.nHeight == MEMPOOL_HEIGHT) {
11701170
ret.pushKV("confirmations", 0);
@@ -1557,7 +1557,7 @@ static RPCHelpMan preciousblock()
15571557

15581558
{
15591559
LOCK(cs_main);
1560-
pblockindex = LookupBlockIndex(hash);
1560+
pblockindex = g_chainman.m_blockman.LookupBlockIndex(hash);
15611561
if (!pblockindex) {
15621562
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Block not found");
15631563
}
@@ -1595,7 +1595,7 @@ static RPCHelpMan invalidateblock()
15951595
CBlockIndex* pblockindex;
15961596
{
15971597
LOCK(cs_main);
1598-
pblockindex = LookupBlockIndex(hash);
1598+
pblockindex = g_chainman.m_blockman.LookupBlockIndex(hash);
15991599
if (!pblockindex) {
16001600
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Block not found");
16011601
}
@@ -1634,7 +1634,7 @@ static RPCHelpMan reconsiderblock()
16341634

16351635
{
16361636
LOCK(cs_main);
1637-
CBlockIndex* pblockindex = LookupBlockIndex(hash);
1637+
CBlockIndex* pblockindex = g_chainman.m_blockman.LookupBlockIndex(hash);
16381638
if (!pblockindex) {
16391639
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Block not found");
16401640
}
@@ -1689,7 +1689,7 @@ static RPCHelpMan getchaintxstats()
16891689
} else {
16901690
uint256 hash(ParseHashV(request.params[1], "blockhash"));
16911691
LOCK(cs_main);
1692-
pindex = LookupBlockIndex(hash);
1692+
pindex = g_chainman.m_blockman.LookupBlockIndex(hash);
16931693
if (!pindex) {
16941694
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Block not found");
16951695
}
@@ -1867,7 +1867,7 @@ static RPCHelpMan getblockstats()
18671867
pindex = ::ChainActive()[height];
18681868
} else {
18691869
const uint256 hash(ParseHashV(request.params[0], "hash_or_height"));
1870-
pindex = LookupBlockIndex(hash);
1870+
pindex = g_chainman.m_blockman.LookupBlockIndex(hash);
18711871
if (!pindex) {
18721872
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Block not found");
18731873
}
@@ -2330,7 +2330,7 @@ static RPCHelpMan getblockfilter()
23302330
bool block_was_connected;
23312331
{
23322332
LOCK(cs_main);
2333-
block_index = LookupBlockIndex(block_hash);
2333+
block_index = g_chainman.m_blockman.LookupBlockIndex(block_hash);
23342334
if (!block_index) {
23352335
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Block not found");
23362336
}
@@ -2440,7 +2440,7 @@ static RPCHelpMan dumptxoutset()
24402440
}
24412441

24422442
pcursor = std::unique_ptr<CCoinsViewCursor>(::ChainstateActive().CoinsDB().Cursor());
2443-
tip = LookupBlockIndex(stats.hashBlock);
2443+
tip = g_chainman.m_blockman.LookupBlockIndex(stats.hashBlock);
24442444
CHECK_NONFATAL(tip);
24452445
}
24462446

‎src/rpc/mining.cpp

+5-5
Original file line numberDiff line numberDiff line change
@@ -375,7 +375,7 @@ static RPCHelpMan generateblock()
375375
LOCK(cs_main);
376376

377377
BlockValidationState state;
378-
if (!TestBlockValidity(state, chainparams, block, LookupBlockIndex(block.hashPrevBlock), false, false)) {
378+
if (!TestBlockValidity(state, chainparams, block, g_chainman.m_blockman.LookupBlockIndex(block.hashPrevBlock), false, false)) {
379379
throw JSONRPCError(RPC_VERIFY_ERROR, strprintf("TestBlockValidity failed: %s", state.ToString()));
380380
}
381381
}
@@ -618,7 +618,7 @@ static RPCHelpMan getblocktemplate()
618618
throw JSONRPCError(RPC_DESERIALIZATION_ERROR, "Block decode failed");
619619

620620
uint256 hash = block.GetHash();
621-
const CBlockIndex* pindex = LookupBlockIndex(hash);
621+
const CBlockIndex* pindex = g_chainman.m_blockman.LookupBlockIndex(hash);
622622
if (pindex) {
623623
if (pindex->IsValid(BLOCK_VALID_SCRIPTS))
624624
return "duplicate";
@@ -966,7 +966,7 @@ static RPCHelpMan submitblock()
966966
uint256 hash = block.GetHash();
967967
{
968968
LOCK(cs_main);
969-
const CBlockIndex* pindex = LookupBlockIndex(hash);
969+
const CBlockIndex* pindex = g_chainman.m_blockman.LookupBlockIndex(hash);
970970
if (pindex) {
971971
if (pindex->IsValid(BLOCK_VALID_SCRIPTS)) {
972972
return "duplicate";
@@ -979,7 +979,7 @@ static RPCHelpMan submitblock()
979979

980980
{
981981
LOCK(cs_main);
982-
const CBlockIndex* pindex = LookupBlockIndex(block.hashPrevBlock);
982+
const CBlockIndex* pindex = g_chainman.m_blockman.LookupBlockIndex(block.hashPrevBlock);
983983
if (pindex) {
984984
UpdateUncommittedBlockStructures(block, pindex, Params().GetConsensus());
985985
}
@@ -1023,7 +1023,7 @@ static RPCHelpMan submitheader()
10231023
}
10241024
{
10251025
LOCK(cs_main);
1026-
if (!LookupBlockIndex(h.hashPrevBlock)) {
1026+
if (!g_chainman.m_blockman.LookupBlockIndex(h.hashPrevBlock)) {
10271027
throw JSONRPCError(RPC_VERIFY_ERROR, "Must submit previous header (" + h.hashPrevBlock.GetHex() + ") first");
10281028
}
10291029
}

0 commit comments

Comments
 (0)
Please sign in to comment.