Skip to content

Commit

Permalink
refactor: drop usage of chainstate globals in llmq logic
Browse files Browse the repository at this point in the history
  • Loading branch information
kwvg committed Jun 26, 2024
1 parent fa20718 commit 303c6bb
Show file tree
Hide file tree
Showing 17 changed files with 57 additions and 45 deletions.
2 changes: 1 addition & 1 deletion src/evo/specialtxman.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ static bool CheckSpecialTxInner(CDeterministicMNManager& dmnman, const Chainstat
case TRANSACTION_COINBASE:
return CheckCbTx(tx, pindexPrev, state);
case TRANSACTION_QUORUM_COMMITMENT:
return llmq::CheckLLMQCommitment(dmnman, tx, pindexPrev, state);
return llmq::CheckLLMQCommitment(dmnman, chainman, tx, pindexPrev, state);
case TRANSACTION_MNHF_SIGNAL:
if (!DeploymentActiveAfter(pindexPrev, consensusParams, Consensus::DEPLOYMENT_V20)) {
return state.Invalid(TxValidationResult::TX_CONSENSUS, "mnhf-before-v20");
Expand Down
8 changes: 4 additions & 4 deletions src/llmq/blockprocessor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ bool CQuorumBlockProcessor::ProcessCommitment(int nHeight, const uint256& blockH
return state.Invalid(BlockValidationResult::BLOCK_CONSENSUS, "bad-qc-dup");
}

if (!IsMiningPhase(llmq_params, nHeight)) {
if (!IsMiningPhase(llmq_params, m_chainstate.m_chain, nHeight)) {
// should not happen as it's already handled in ProcessBlock
return state.Invalid(BlockValidationResult::BLOCK_CONSENSUS, "bad-qc-height");
}
Expand Down Expand Up @@ -385,12 +385,12 @@ bool CQuorumBlockProcessor::GetCommitmentsFromBlock(const CBlock& block, gsl::no
return true;
}

bool CQuorumBlockProcessor::IsMiningPhase(const Consensus::LLMQParams& llmqParams, int nHeight)
bool CQuorumBlockProcessor::IsMiningPhase(const Consensus::LLMQParams& llmqParams, const CChain& active_chain, int nHeight)
{
AssertLockHeld(cs_main);

// Note: This function can be called for new blocks
assert(nHeight <= ::ChainActive().Height() + 1);
assert(nHeight <= active_chain.Height() + 1);

int quorumCycleStartHeight = nHeight - (nHeight % llmqParams.dkgInterval);
int quorumCycleMiningStartHeight = quorumCycleStartHeight + llmqParams.dkgMiningWindowStart;
Expand All @@ -409,7 +409,7 @@ size_t CQuorumBlockProcessor::GetNumCommitmentsRequired(const Consensus::LLMQPar
{
AssertLockHeld(cs_main);

if (!IsMiningPhase(llmqParams, nHeight)) return 0;
if (!IsMiningPhase(llmqParams, m_chainstate.m_chain, nHeight)) return 0;

// Note: This function can be called for new blocks
assert(nHeight <= m_chainstate.m_chain.Height() + 1);
Expand Down
2 changes: 1 addition & 1 deletion src/llmq/blockprocessor.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ class CQuorumBlockProcessor
private:
static bool GetCommitmentsFromBlock(const CBlock& block, gsl::not_null<const CBlockIndex*> pindex, std::multimap<Consensus::LLMQType, CFinalCommitment>& ret, BlockValidationState& state) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
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);
static bool IsMiningPhase(const Consensus::LLMQParams& llmqParams, const CChain& active_chain, 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, const CChain& active_chain, int nHeight, int quorumIndex) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
};
Expand Down
2 changes: 1 addition & 1 deletion src/llmq/chainlocks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -555,7 +555,7 @@ bool CChainLocksHandler::VerifyChainLock(const CChainLockSig& clsig) const
{
const auto llmqType = Params().GetConsensus().llmqTypeChainLocks;
const uint256 nRequestId = ::SerializeHash(std::make_pair(llmq::CLSIG_REQUESTID_PREFIX, clsig.getHeight()));
return llmq::VerifyRecoveredSig(llmqType, qman, clsig.getHeight(), nRequestId, clsig.getBlockHash(), clsig.getSig());
return llmq::VerifyRecoveredSig(llmqType, m_chainstate.m_chain, qman, clsig.getHeight(), nRequestId, clsig.getBlockHash(), clsig.getSig());
}

bool CChainLocksHandler::InternalHasChainLock(int nHeight, const uint256& blockHash) const
Expand Down
4 changes: 2 additions & 2 deletions src/llmq/commitment.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ bool CFinalCommitment::VerifySizes(const Consensus::LLMQParams& params) const
return true;
}

bool CheckLLMQCommitment(CDeterministicMNManager& dmnman, const CTransaction& tx, gsl::not_null<const CBlockIndex*> pindexPrev, TxValidationState& state)
bool CheckLLMQCommitment(CDeterministicMNManager& dmnman, const ChainstateManager& chainman, const CTransaction& tx, gsl::not_null<const CBlockIndex*> pindexPrev, TxValidationState& state)
{
const auto opt_qcTx = GetTxPayload<CFinalCommitmentTxPayload>(tx);
if (!opt_qcTx) {
Expand Down Expand Up @@ -200,7 +200,7 @@ bool CheckLLMQCommitment(CDeterministicMNManager& dmnman, const CTransaction& tx
return state.Invalid(TxValidationResult::TX_CONSENSUS, "bad-qc-height");
}

const CBlockIndex* pQuorumBaseBlockIndex = WITH_LOCK(cs_main, return g_chainman.m_blockman.LookupBlockIndex(qcTx.commitment.quorumHash));
const CBlockIndex* pQuorumBaseBlockIndex = WITH_LOCK(cs_main, return chainman.m_blockman.LookupBlockIndex(qcTx.commitment.quorumHash));
if (pQuorumBaseBlockIndex == nullptr) {
return state.Invalid(TxValidationResult::TX_CONSENSUS, "bad-qc-quorum-hash");
}
Expand Down
3 changes: 2 additions & 1 deletion src/llmq/commitment.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

class CBlockIndex;
class CDeterministicMNManager;
class ChainstateManager;
class TxValidationState;

namespace llmq
Expand Down Expand Up @@ -172,7 +173,7 @@ class CFinalCommitmentTxPayload
}
};

bool CheckLLMQCommitment(CDeterministicMNManager& dmnman, const CTransaction& tx, gsl::not_null<const CBlockIndex*> pindexPrev, TxValidationState& state);
bool CheckLLMQCommitment(CDeterministicMNManager& dmnman, const ChainstateManager& chainman, const CTransaction& tx, gsl::not_null<const CBlockIndex*> pindexPrev, TxValidationState& state);

uint256 BuildCommitmentHash(Consensus::LLMQType llmqType, const uint256& blockHash, const std::vector<bool>& validMembers, const CBLSPublicKey& pubKey, const uint256& vvecHash);

Expand Down
2 changes: 1 addition & 1 deletion src/llmq/context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ LLMQContext::LLMQContext(CChainState& chainstate, CConnman& connman, CDeterminis
quorum_block_processor{std::make_unique<llmq::CQuorumBlockProcessor>(chainstate, dmnman, evo_db, peerman)},
qdkgsman{std::make_unique<llmq::CDKGSessionManager>(*bls_worker, chainstate, connman, dmnman, *dkg_debugman, mn_metaman, *quorum_block_processor, mn_activeman, sporkman, peerman, unit_tests, wipe)},
qman{std::make_unique<llmq::CQuorumManager>(*bls_worker, chainstate, connman, dmnman, *qdkgsman, evo_db, *quorum_block_processor, mn_activeman, mn_sync, sporkman)},
sigman{std::make_unique<llmq::CSigningManager>(connman, mn_activeman, *qman, peerman, unit_tests, wipe)},
sigman{std::make_unique<llmq::CSigningManager>(connman, mn_activeman, chainstate, *qman, peerman, unit_tests, wipe)},
shareman{std::make_unique<llmq::CSigSharesManager>(connman, *sigman, mn_activeman, *qman, sporkman, peerman)},
clhandler{[&]() -> llmq::CChainLocksHandler* const {
assert(llmq::chainLocksHandler == nullptr);
Expand Down
2 changes: 1 addition & 1 deletion src/llmq/ehf_signals.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ void CEHFSignalsHandler::trySignEHFSignal(int bit, const CBlockIndex* const pind
return;
}

const auto quorum = llmq::SelectQuorumForSigning(llmq_params_opt.value(), qman, requestId);
const auto quorum = llmq::SelectQuorumForSigning(llmq_params_opt.value(), chainstate.m_chain, qman, requestId);
if (!quorum) {
LogPrintf("CEHFSignalsHandler::trySignEHFSignal no quorum for id=%s\n", requestId.ToString());
return;
Expand Down
2 changes: 1 addition & 1 deletion src/llmq/instantsend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -920,7 +920,7 @@ std::unordered_set<uint256, StaticSaltedHasher> CInstantSendManager::ProcessPend
nSignHeight = blockIndex->nHeight + dkgInterval - 1;
}

auto quorum = llmq::SelectQuorumForSigning(llmq_params, qman, id, nSignHeight, signOffset);
auto quorum = llmq::SelectQuorumForSigning(llmq_params, m_chainstate.m_chain, qman, id, nSignHeight, signOffset);
if (!quorum) {
// should not happen, but if one fails to select, all others will also fail to select
return {};
Expand Down
19 changes: 11 additions & 8 deletions src/llmq/quorums.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1107,25 +1107,26 @@ void CQuorumManager::StartCleanupOldQuorumDataThread(const CBlockIndex* pIndex)
});
}

CQuorumCPtr SelectQuorumForSigning(const Consensus::LLMQParams& llmq_params, const CQuorumManager& quorum_manager, const uint256& selectionHash, int signHeight, int signOffset)
CQuorumCPtr SelectQuorumForSigning(const Consensus::LLMQParams& llmq_params, const CChain& active_chain, const CQuorumManager& qman,
const uint256& selectionHash, int signHeight, int signOffset)
{
size_t poolSize = llmq_params.signingActiveQuorumCount;

CBlockIndex* pindexStart;
{
LOCK(cs_main);
if (signHeight == -1) {
signHeight = ::ChainActive().Height();
signHeight = active_chain.Height();
}
int startBlockHeight = signHeight - signOffset;
if (startBlockHeight > ::ChainActive().Height() || startBlockHeight < 0) {
if (startBlockHeight > active_chain.Height() || startBlockHeight < 0) {
return {};
}
pindexStart = ::ChainActive()[startBlockHeight];
pindexStart = active_chain[startBlockHeight];
}

if (IsQuorumRotationEnabled(llmq_params, pindexStart)) {
auto quorums = quorum_manager.ScanQuorums(llmq_params.type, pindexStart, poolSize);
auto quorums = qman.ScanQuorums(llmq_params.type, pindexStart, poolSize);
if (quorums.empty()) {
return nullptr;
}
Expand All @@ -1149,7 +1150,7 @@ CQuorumCPtr SelectQuorumForSigning(const Consensus::LLMQParams& llmq_params, con
}
return *itQuorum;
} else {
auto quorums = quorum_manager.ScanQuorums(llmq_params.type, pindexStart, poolSize);
auto quorums = qman.ScanQuorums(llmq_params.type, pindexStart, poolSize);
if (quorums.empty()) {
return nullptr;
}
Expand All @@ -1168,11 +1169,13 @@ CQuorumCPtr SelectQuorumForSigning(const Consensus::LLMQParams& llmq_params, con
}
}

bool VerifyRecoveredSig(Consensus::LLMQType llmqType, const CQuorumManager& quorum_manager, int signedAtHeight, const uint256& id, const uint256& msgHash, const CBLSSignature& sig, const int signOffset)
bool VerifyRecoveredSig(Consensus::LLMQType llmqType, const CChain& active_chain, const CQuorumManager& qman,
int signedAtHeight, const uint256& id, const uint256& msgHash, const CBLSSignature& sig,
const int signOffset)
{
const auto& llmq_params_opt = Params().GetLLMQ(llmqType);
assert(llmq_params_opt.has_value());
auto quorum = SelectQuorumForSigning(llmq_params_opt.value(), quorum_manager, id, signedAtHeight, signOffset);
auto quorum = SelectQuorumForSigning(llmq_params_opt.value(), active_chain, qman, id, signedAtHeight, signOffset);
if (!quorum) {
return false;
}
Expand Down
8 changes: 5 additions & 3 deletions src/llmq/quorums.h
Original file line number Diff line number Diff line change
Expand Up @@ -294,11 +294,13 @@ class CQuorumManager
// which are not 100% at the chain tip.
static constexpr int SIGN_HEIGHT_OFFSET{8};

CQuorumCPtr SelectQuorumForSigning(const Consensus::LLMQParams& llmq_params, const CQuorumManager& quorum_manager, const uint256& selectionHash, int signHeight = -1 /*chain tip*/, int signOffset = SIGN_HEIGHT_OFFSET);
CQuorumCPtr SelectQuorumForSigning(const Consensus::LLMQParams& llmq_params, const CChain& active_chain, const CQuorumManager& qman,
const uint256& selectionHash, int signHeight = -1 /*chain tip*/, int signOffset = SIGN_HEIGHT_OFFSET);

// Verifies a recovered sig that was signed while the chain tip was at signedAtTip
bool VerifyRecoveredSig(Consensus::LLMQType llmqType, const CQuorumManager& quorum_manager, int signedAtHeight, const uint256& id, const uint256& msgHash, const CBLSSignature& sig, int signOffset = SIGN_HEIGHT_OFFSET);

bool VerifyRecoveredSig(Consensus::LLMQType llmqType, const CChain& active_chain, const CQuorumManager& qman,
int signedAtHeight, const uint256& id, const uint256& msgHash, const CBLSSignature& sig,
int signOffset = SIGN_HEIGHT_OFFSET);
} // namespace llmq

template<typename T> struct SaltedHasherImpl;
Expand Down
8 changes: 4 additions & 4 deletions src/llmq/signing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -534,9 +534,9 @@ void CRecoveredSigsDb::CleanupOldVotes(int64_t maxAge)

//////////////////

CSigningManager::CSigningManager(CConnman& _connman, const CActiveMasternodeManager* const mn_activeman, const CQuorumManager& _qman,
const std::unique_ptr<PeerManager>& peerman, bool fMemory, bool fWipe) :
db(fMemory, fWipe), connman(_connman), m_mn_activeman(mn_activeman), qman(_qman), m_peerman(peerman)
CSigningManager::CSigningManager(CConnman& _connman, const CActiveMasternodeManager* const mn_activeman, const CChainState& chainstate,
const CQuorumManager& _qman, const std::unique_ptr<PeerManager>& peerman, bool fMemory, bool fWipe) :
db(fMemory, fWipe), connman(_connman), m_mn_activeman(mn_activeman), m_chainstate(chainstate), qman(_qman), m_peerman(peerman)
{
}

Expand Down Expand Up @@ -890,7 +890,7 @@ bool CSigningManager::AsyncSignIfMember(Consensus::LLMQType llmqType, CSigShares
// TODO fix this by re-signing when the next block arrives, but only when that block results in a change of the quorum list and no recovered signature has been created in the mean time
const auto &llmq_params_opt = Params().GetLLMQ(llmqType);
assert(llmq_params_opt.has_value());
return SelectQuorumForSigning(llmq_params_opt.value(), qman, id);
return SelectQuorumForSigning(llmq_params_opt.value(), m_chainstate.m_chain, qman, id);
} else {
return qman.GetQuorum(llmqType, quorumHash);
}
Expand Down
6 changes: 4 additions & 2 deletions src/llmq/signing.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include <unordered_map>

class CActiveMasternodeManager;
class CChainState;
class CConnman;
class CDataStream;
class CDBBatch;
Expand Down Expand Up @@ -163,6 +164,7 @@ class CSigningManager
CRecoveredSigsDb db;
CConnman& connman;
const CActiveMasternodeManager* const m_mn_activeman;
const CChainState& m_chainstate;
const CQuorumManager& qman;
const std::unique_ptr<PeerManager>& m_peerman;

Expand All @@ -179,8 +181,8 @@ class CSigningManager
std::vector<CRecoveredSigsListener*> recoveredSigsListeners GUARDED_BY(cs_listeners);

public:
CSigningManager(CConnman& _connman, const CActiveMasternodeManager* const mn_activeman, const CQuorumManager& _qman,
const std::unique_ptr<PeerManager>& peerman, bool fMemory, bool fWipe);
CSigningManager(CConnman& _connman, const CActiveMasternodeManager* const mn_activeman, const CChainState& chainstate,
const CQuorumManager& _qman, const std::unique_ptr<PeerManager>& peerman, bool fMemory, bool fWipe);

bool AlreadyHave(const CInv& inv) const;
bool GetRecoveredSigForGetData(const uint256& hash, CRecoveredSig& ret) const;
Expand Down
12 changes: 6 additions & 6 deletions src/llmq/snapshot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,27 +86,27 @@ UniValue CQuorumRotationInfo::ToJson() const
}

bool BuildQuorumRotationInfo(const CGetQuorumRotationInfo& request, CQuorumRotationInfo& response,
CDeterministicMNManager& dmnman, const CQuorumManager& qman,
CDeterministicMNManager& dmnman, const ChainstateManager& chainman, const CQuorumManager& qman,
const CQuorumBlockProcessor& quorum_block_processor, std::string& errorRet)
{
AssertLockHeld(cs_main);

std::vector<const CBlockIndex*> baseBlockIndexes;
if (request.baseBlockHashes.size() == 0) {
const CBlockIndex* blockIndex = ::ChainActive().Genesis();
const CBlockIndex* blockIndex = chainman.ActiveChain().Genesis();
if (!blockIndex) {
errorRet = strprintf("genesis block not found");
return false;
}
baseBlockIndexes.push_back(blockIndex);
} else {
for (const auto& blockHash : request.baseBlockHashes) {
const CBlockIndex* blockIndex = g_chainman.m_blockman.LookupBlockIndex(blockHash);
const CBlockIndex* blockIndex = chainman.m_blockman.LookupBlockIndex(blockHash);
if (!blockIndex) {
errorRet = strprintf("block %s not found", blockHash.ToString());
return false;
}
if (!::ChainActive().Contains(blockIndex)) {
if (!chainman.ActiveChain().Contains(blockIndex)) {
errorRet = strprintf("block %s is not in the active chain", blockHash.ToString());
return false;
}
Expand All @@ -117,7 +117,7 @@ bool BuildQuorumRotationInfo(const CGetQuorumRotationInfo& request, CQuorumRotat
});
}

const CBlockIndex* tipBlockIndex = ::ChainActive().Tip();
const CBlockIndex* tipBlockIndex = chainman.ActiveChain().Tip();
if (!tipBlockIndex) {
errorRet = strprintf("tip block not found");
return false;
Expand All @@ -127,7 +127,7 @@ bool BuildQuorumRotationInfo(const CGetQuorumRotationInfo& request, CQuorumRotat
return false;
}

const CBlockIndex* blockIndex = g_chainman.m_blockman.LookupBlockIndex(request.blockRequestHash);
const CBlockIndex* blockIndex = chainman.m_blockman.LookupBlockIndex(request.blockRequestHash);
if (!blockIndex) {
errorRet = strprintf("block not found");
return false;
Expand Down
2 changes: 1 addition & 1 deletion src/llmq/snapshot.h
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ class CQuorumRotationInfo
};

bool BuildQuorumRotationInfo(const CGetQuorumRotationInfo& request, CQuorumRotationInfo& response,
CDeterministicMNManager& dmnman, const CQuorumManager& qman,
CDeterministicMNManager& dmnman, const ChainstateManager& chainman, const CQuorumManager& qman,
const CQuorumBlockProcessor& quorumBlockProcessor, std::string& errorRet);
uint256 GetLastBaseBlockHash(Span<const CBlockIndex*> baseBlockIndexes, const CBlockIndex* blockIndex);

Expand Down
2 changes: 1 addition & 1 deletion src/net_processing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4842,7 +4842,7 @@ void PeerManagerImpl::ProcessMessage(

llmq::CQuorumRotationInfo quorumRotationInfoRet;
std::string strError;
if (BuildQuorumRotationInfo(cmd, quorumRotationInfoRet, *m_dmnman, *m_llmq_ctx->qman, *m_llmq_ctx->quorum_block_processor, strError)) {
if (BuildQuorumRotationInfo(cmd, quorumRotationInfoRet, *m_dmnman, m_chainman, *m_llmq_ctx->qman, *m_llmq_ctx->quorum_block_processor, strError)) {
m_connman.PushMessage(&pfrom, msgMaker.Make(NetMsgType::QUORUMROTATIONINFO, quorumRotationInfoRet));
} else {
strError = strprintf("getquorumrotationinfo failed for size(baseBlockHashes)=%d, blockRequestHash=%s. error=%s", cmd.baseBlockHashes.size(), cmd.blockRequestHash.ToString(), strError);
Expand Down
Loading

0 comments on commit 303c6bb

Please sign in to comment.