Skip to content

Commit

Permalink
refactor: stop using ::ChainstateActive() in GetBlockHash
Browse files Browse the repository at this point in the history
  • Loading branch information
kwvg committed Jun 26, 2024
1 parent 6abf7f8 commit c48c0e7
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 14 deletions.
10 changes: 5 additions & 5 deletions src/llmq/blockprocessor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ bool CQuorumBlockProcessor::ProcessCommitment(int nHeight, const uint256& blockH
}
const auto& llmq_params = llmq_params_opt.value();

uint256 quorumHash = GetQuorumBlockHash(llmq_params, nHeight, qc.quorumIndex);
uint256 quorumHash = GetQuorumBlockHash(llmq_params, m_chainstate.m_chain, nHeight, qc.quorumIndex);

LogPrint(BCLog::LLMQ, "CQuorumBlockProcessor::%s height=%d, type=%d, quorumIndex=%d, quorumHash=%s, signers=%s, validMembers=%d, quorumPublicKey=%s fJustCheck[%d] processing commitment from block.\n", __func__,
nHeight, ToUnderlying(qc.llmqType), qc.quorumIndex, quorumHash.ToString(), qc.CountSigners(), qc.CountValidMembers(), qc.quorumPublicKey.ToString(), fJustCheck);
Expand Down Expand Up @@ -420,22 +420,22 @@ size_t CQuorumBlockProcessor::GetNumCommitmentsRequired(const Consensus::LLMQPar
size_t ret{0};

for (const auto quorumIndex : irange::range(quorums_num)) {
uint256 quorumHash = GetQuorumBlockHash(llmqParams, nHeight, quorumIndex);
uint256 quorumHash = GetQuorumBlockHash(llmqParams, m_chainstate.m_chain, nHeight, quorumIndex);
if (!quorumHash.IsNull() && !HasMinedCommitment(llmqParams.type, quorumHash)) ++ret;
}

return ret;
}

// WARNING: This method returns uint256() on the first block of the DKG interval (because the block hash is not known yet)
uint256 CQuorumBlockProcessor::GetQuorumBlockHash(const Consensus::LLMQParams& llmqParams, int nHeight, int quorumIndex)
uint256 CQuorumBlockProcessor::GetQuorumBlockHash(const Consensus::LLMQParams& llmqParams, const CChain& active_chain, int nHeight, int quorumIndex)
{
AssertLockHeld(cs_main);

int quorumStartHeight = nHeight - (nHeight % llmqParams.dkgInterval) + quorumIndex;

uint256 quorumBlockHash;
if (!GetBlockHash(quorumBlockHash, quorumStartHeight)) {
if (!GetBlockHash(active_chain, quorumBlockHash, quorumStartHeight)) {
LogPrint(BCLog::LLMQ, "[GetQuorumBlockHash] llmqType[%d] h[%d] qi[%d] quorumStartHeight[%d] quorumHash[EMPTY]\n", ToUnderlying(llmqParams.type), nHeight, quorumIndex, quorumStartHeight);
return {};
}
Expand Down Expand Up @@ -705,7 +705,7 @@ std::optional<std::vector<CFinalCommitment>> CQuorumBlockProcessor::GetMineableC
for (const auto quorumIndex : irange::range(quorums_num)) {
CFinalCommitment cf;

uint256 quorumHash = GetQuorumBlockHash(llmqParams, nHeight, quorumIndex);
uint256 quorumHash = GetQuorumBlockHash(llmqParams, m_chainstate.m_chain, nHeight, quorumIndex);
if (quorumHash.IsNull()) {
break;
}
Expand Down
3 changes: 2 additions & 1 deletion src/llmq/blockprocessor.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include <optional>

class BlockValidationState;
class CChain;
class CChainState;
class CConnman;
class CDataStream;
Expand Down Expand Up @@ -77,7 +78,7 @@ class CQuorumBlockProcessor
bool ProcessCommitment(int nHeight, const uint256& blockHash, const CFinalCommitment& qc, BlockValidationState& state, bool fJustCheck, bool fBLSChecks) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
static bool IsMiningPhase(const Consensus::LLMQParams& llmqParams, int nHeight) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
size_t GetNumCommitmentsRequired(const Consensus::LLMQParams& llmqParams, int nHeight) const EXCLUSIVE_LOCKS_REQUIRED(cs_main);
static uint256 GetQuorumBlockHash(const Consensus::LLMQParams& llmqParams, int nHeight, int quorumIndex) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
static uint256 GetQuorumBlockHash(const Consensus::LLMQParams& llmqParams, const CChain& active_chain, int nHeight, int quorumIndex) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
};
} // namespace llmq

Expand Down
12 changes: 7 additions & 5 deletions src/validation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1970,13 +1970,15 @@ void StopScriptCheckWorkerThreads()
scriptcheckqueue.StopWorkerThreads();
}

bool GetBlockHash(uint256& hashRet, int nBlockHeight)
bool GetBlockHash(const CChain& active_chain, uint256& hashRet, int nBlockHeight)
{
LOCK(cs_main);
if(::ChainActive().Tip() == nullptr) return false;
if(nBlockHeight < -1 || nBlockHeight > ::ChainActive().Height()) return false;
if(nBlockHeight == -1) nBlockHeight = ::ChainActive().Height();
hashRet = ::ChainActive()[nBlockHeight]->GetBlockHash();

if (active_chain.Tip() == nullptr) return false;
if (nBlockHeight < -1 || nBlockHeight > active_chain.Height()) return false;
if (nBlockHeight == -1) nBlockHeight = active_chain.Height();
hashRet = active_chain[nBlockHeight]->GetBlockHash();

return true;
}

Expand Down
6 changes: 3 additions & 3 deletions src/validation.h
Original file line number Diff line number Diff line change
Expand Up @@ -1143,10 +1143,10 @@ extern std::unique_ptr<CBlockTreeDB> pblocktree;


/**
* Return true if hash can be found in ::ChainActive() at nBlockHeight height.
* Fills hashRet with found hash, if no nBlockHeight is specified - ::ChainActive().Height() is used.
* Return true if hash can be found in active_chain at nBlockHeight height.
* Fills hashRet with found hash, if no nBlockHeight is specified - active_chain.Height() is used.
*/
bool GetBlockHash(uint256& hashRet, int nBlockHeight = -1);
bool GetBlockHash(const CChain& active_chain, uint256& hashRet, int nBlockHeight = -1);

/** Get block file info entry for one block file */
CBlockFileInfo* GetBlockFileInfo(size_t n);
Expand Down

0 comments on commit c48c0e7

Please sign in to comment.