Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Refactor] Add LookupBlockIndex #2715

Merged
merged 3 commits into from
Jan 19, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 4 additions & 7 deletions src/budget/budgetmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1677,13 +1677,10 @@ bool CheckCollateral(const uint256& nTxCollateralHash, const uint256& nExpectedH
int nProposalHeight = 0;
{
LOCK(cs_main);
BlockMap::iterator mi = mapBlockIndex.find(nBlockHash);
if (mi != mapBlockIndex.end() && (*mi).second) {
CBlockIndex* pindex = (*mi).second;
if (chainActive.Contains(pindex)) {
nProposalHeight = pindex->nHeight;
nTime = pindex->nTime;
}
CBlockIndex* pindex = LookupBlockIndex(nBlockHash);
if (pindex && chainActive.Contains(pindex)) {
nProposalHeight = pindex->nHeight;
nTime = pindex->nTime;
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/budget/finalizedbudget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -311,8 +311,8 @@ bool CFinalizedBudget::IsPaidAlready(const uint256& nProposalHash, const uint256
// -> reject transaction so it gets paid to a masternode instead
if (nBlockHash != nPaidBlockHash) {
LOCK(cs_main);
auto it = mapBlockIndex.find(nPaidBlockHash);
return it != mapBlockIndex.end() && chainActive.Contains(it->second);
CBlockIndex* pindex = LookupBlockIndex(nPaidBlockHash);
return pindex && chainActive.Contains(pindex);
}

// Re-checking same block. Not a double payment.
Expand Down
5 changes: 2 additions & 3 deletions src/evo/specialtx_validation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -442,11 +442,10 @@ bool VerifyLLMQCommitment(const llmq::CFinalCommitment& qfc, const CBlockIndex*

if (pindexPrev) {
// Get quorum index
auto it = mapBlockIndex.find(qfc.quorumHash);
if (it == mapBlockIndex.end()) {
CBlockIndex* pindexQuorum = LookupBlockIndex(qfc.quorumHash);
if (!pindexQuorum) {
return state.DoS(100, false, REJECT_INVALID, "bad-qc-quorum-hash-not-found");
}
const CBlockIndex* pindexQuorum = it->second;

// Check height
if (pindexQuorum->nHeight % params->dkgInterval != 0) {
Expand Down
3 changes: 2 additions & 1 deletion src/init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1503,8 +1503,9 @@ bool AppInitMain()

// If the loaded chain has a wrong genesis, bail out immediately
// (we're likely using a testnet datadir, or the other way around).
if (!mapBlockIndex.empty() && mapBlockIndex.count(consensus.hashGenesisBlock) == 0)
if (!mapBlockIndex.empty() && !LookupBlockIndex(consensus.hashGenesisBlock)) {
return UIError(_("Incorrect or no genesis block found. Wrong datadir for network?"));
}

// Check for changed -txindex state
if (fTxIndex != gArgs.GetBoolArg("-txindex", DEFAULT_TXINDEX)) {
Expand Down
43 changes: 21 additions & 22 deletions src/net_processing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -312,10 +312,10 @@ void ProcessBlockAvailability(NodeId nodeid)
assert(state != nullptr);

if (!state->hashLastUnknownBlock.IsNull()) {
BlockMap::iterator itOld = mapBlockIndex.find(state->hashLastUnknownBlock);
if (itOld != mapBlockIndex.end() && itOld->second->nChainWork > 0) {
if (state->pindexBestKnownBlock == NULL || itOld->second->nChainWork >= state->pindexBestKnownBlock->nChainWork)
state->pindexBestKnownBlock = itOld->second;
CBlockIndex* pindex = LookupBlockIndex(state->hashLastUnknownBlock);
if (pindex && pindex->nChainWork > 0) {
if (state->pindexBestKnownBlock == NULL || pindex->nChainWork >= state->pindexBestKnownBlock->nChainWork)
furszy marked this conversation as resolved.
Show resolved Hide resolved
state->pindexBestKnownBlock = pindex;
state->hashLastUnknownBlock.SetNull();
}
}
Expand All @@ -329,11 +329,11 @@ void UpdateBlockAvailability(NodeId nodeid, const uint256& hash)

ProcessBlockAvailability(nodeid);

BlockMap::iterator it = mapBlockIndex.find(hash);
if (it != mapBlockIndex.end() && it->second->nChainWork > 0) {
CBlockIndex* pindex = LookupBlockIndex(hash);
if (pindex && pindex->nChainWork > 0) {
// An actually better block was announced.
if (state->pindexBestKnownBlock == NULL || it->second->nChainWork >= state->pindexBestKnownBlock->nChainWork)
state->pindexBestKnownBlock = it->second;
if (state->pindexBestKnownBlock == NULL || pindex->nChainWork >= state->pindexBestKnownBlock->nChainWork)
furszy marked this conversation as resolved.
Show resolved Hide resolved
state->pindexBestKnownBlock = pindex;
} else {
// An unknown block was announced; just assume that the latest one is the best one.
state->hashLastUnknownBlock = hash;
Expand Down Expand Up @@ -625,9 +625,9 @@ static void CheckBlockSpam(NodeId nodeId, const uint256& hashBlock)
nodestate = State(nodeId);
if (!nodestate) { return; }

const auto it = mapBlockIndex.find(hashBlock);
if (it == mapBlockIndex.end()) { return; }
blockReceivedHeight = it->second->nHeight;
CBlockIndex* pindex = LookupBlockIndex(hashBlock);
if (!pindex) { return; }
blockReceivedHeight = pindex->nHeight;
}

nodestate->nodeBlocks.onBlockReceived(blockReceivedHeight);
Expand Down Expand Up @@ -766,7 +766,7 @@ bool static AlreadyHave(const CInv& inv) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
}

case MSG_BLOCK:
return mapBlockIndex.count(inv.hash);
return LookupBlockIndex(inv.hash) != nullptr;
case MSG_TXLOCK_REQUEST:
// deprecated
return true;
Expand Down Expand Up @@ -956,26 +956,26 @@ void static ProcessGetBlockData(CNode* pfrom, const CInv& inv, CConnman* connman
CNetMsgMaker msgMaker(pfrom->GetSendVersion());

bool send = false;
BlockMap::iterator mi = mapBlockIndex.find(inv.hash);
if (mi != mapBlockIndex.end()) {
if (chainActive.Contains(mi->second)) {
CBlockIndex* pindex = LookupBlockIndex(inv.hash);
if (pindex) {
if (chainActive.Contains(pindex)) {
send = true;
} else {
// To prevent fingerprinting attacks, only send blocks outside of the active
// chain if they are valid, and no more than a max reorg depth than the best header
// chain we know about.
send = mi->second->IsValid(BLOCK_VALID_SCRIPTS) && (pindexBestHeader != NULL) &&
(chainActive.Height() - mi->second->nHeight < gArgs.GetArg("-maxreorg", DEFAULT_MAX_REORG_DEPTH));
send = pindex->IsValid(BLOCK_VALID_SCRIPTS) && (pindexBestHeader != NULL) &&
furszy marked this conversation as resolved.
Show resolved Hide resolved
(chainActive.Height() - pindex->nHeight < gArgs.GetArg("-maxreorg", DEFAULT_MAX_REORG_DEPTH));
if (!send) {
LogPrint(BCLog::NET, "ProcessGetData(): ignoring request from peer=%i for old block that isn't in the main chain\n", pfrom->GetId());
}
}
}
// Don't send not-validated blocks
if (send && (mi->second->nStatus & BLOCK_HAVE_DATA)) {
if (send && (pindex->nStatus & BLOCK_HAVE_DATA)) {
// Send block from disk
CBlock block;
if (!ReadBlockFromDisk(block, (*mi).second))
if (!ReadBlockFromDisk(block, pindex))
assert(!"cannot load block from disk");
if (inv.type == MSG_BLOCK)
connman->PushMessage(pfrom, msgMaker.Make(NetMsgType::BLOCK, block));
Expand Down Expand Up @@ -1512,10 +1512,9 @@ bool static ProcessMessage(CNode* pfrom, std::string strCommand, CDataStream& vR
CBlockIndex* pindex = NULL;
if (locator.IsNull()) {
// If locator is null, return the hashStop block
BlockMap::iterator mi = mapBlockIndex.find(hashStop);
if (mi == mapBlockIndex.end())
CBlockIndex* pindex = LookupBlockIndex(hashStop);
if (!pindex)
return true;
pindex = (*mi).second;
} else {
// Find the last block the caller has in the main chain
pindex = FindForkInGlobalIndex(chainActive, locator);
Expand Down
3 changes: 1 addition & 2 deletions src/rest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,7 @@ static bool rest_headers(HTTPRequest* req,
headers.reserve(count);
{
LOCK(cs_main);
BlockMap::const_iterator it = mapBlockIndex.find(hash);
const CBlockIndex *pindex = (it != mapBlockIndex.end()) ? it->second : NULL;
CBlockIndex* pindex = LookupBlockIndex(hash);
while (pindex != NULL && chainActive.Contains(pindex)) {
headers.push_back(pindex);
if (headers.size() == (unsigned long)count)
Expand Down
5 changes: 2 additions & 3 deletions src/rpc/blockchain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -733,7 +733,7 @@ static bool GetUTXOStats(CCoinsView *view, CCoinsStats &stats)
stats.hashBlock = pcursor->GetBestBlock();
{
LOCK(cs_main);
stats.nHeight = mapBlockIndex.find(stats.hashBlock)->second->nHeight;
stats.nHeight = LookupBlockIndex(stats.hashBlock)->nHeight;
}
ss << stats.hashBlock;
uint256 prevkey;
Expand Down Expand Up @@ -863,8 +863,7 @@ UniValue gettxout(const JSONRPCRequest& request)
}
}

BlockMap::iterator it = mapBlockIndex.find(pcoinsTip->GetBestBlock());
CBlockIndex* pindex = it->second;
CBlockIndex* pindex = LookupBlockIndex(pcoinsTip->GetBestBlock());
assert(pindex != nullptr);
ret.pushKV("bestblock", pindex->GetBlockHash().GetHex());
if (coin.nHeight == MEMPOOL_HEIGHT) {
Expand Down
10 changes: 4 additions & 6 deletions src/rpc/mining.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -548,9 +548,8 @@ UniValue getblocktemplate(const JSONRPCRequest& request)
throw JSONRPCError(RPC_DESERIALIZATION_ERROR, "Block decode failed");

uint256 hash = block.GetHash();
BlockMap::iterator mi = mapBlockIndex.find(hash);
if (mi != mapBlockIndex.end()) {
CBlockIndex* pindex = mi->second;
CBlockIndex* pindex = LookupBlockIndex(hash);
if (pindex) {
if (pindex->IsValid(BLOCK_VALID_SCRIPTS))
return "duplicate";
if (pindex->nStatus & BLOCK_FAILED_MASK)
Expand Down Expand Up @@ -761,9 +760,8 @@ UniValue submitblock(const JSONRPCRequest& request)
bool fBlockPresent = false;
{
LOCK(cs_main);
BlockMap::iterator mi = mapBlockIndex.find(hash);
if (mi != mapBlockIndex.end()) {
CBlockIndex* pindex = mi->second;
CBlockIndex* pindex = LookupBlockIndex(hash);
if (pindex) {
if (pindex->IsValid(BLOCK_VALID_SCRIPTS))
return "duplicate";
if (pindex->nStatus & BLOCK_FAILED_MASK)
Expand Down
10 changes: 4 additions & 6 deletions src/rpc/rawtransaction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,8 @@ void TxToJSON(CWallet* const pwallet, const CTransaction& tx, const uint256 hash

if (!hashBlock.IsNull()) {
entry.pushKV("blockhash", hashBlock.GetHex());
BlockMap::iterator mi = mapBlockIndex.find(hashBlock);
if (mi != mapBlockIndex.end() && (*mi).second) {
CBlockIndex* pindex = (*mi).second;
CBlockIndex* pindex = LookupBlockIndex(hashBlock);
if (pindex) {
if (chainActive.Contains(pindex)) {
entry.pushKV("confirmations", 1 + chainActive.Height() - pindex->nHeight);
entry.pushKV("time", pindex->GetBlockTime());
Expand Down Expand Up @@ -236,11 +235,10 @@ UniValue getrawtransaction(const JSONRPCRequest& request)

if (!request.params[2].isNull()) {
uint256 blockhash = ParseHashV(request.params[2], "parameter 3");
BlockMap::iterator it = mapBlockIndex.find(blockhash);
if (it == mapBlockIndex.end()) {
blockindex = LookupBlockIndex(blockhash);
if (!blockindex) {
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Block hash not found");
}
blockindex = it->second;
in_active_chain = chainActive.Contains(blockindex);
}

Expand Down
Loading