Skip to content

Commit

Permalink
refactor: pass llmq::CQuorumSnapshotManager by argument
Browse files Browse the repository at this point in the history
  • Loading branch information
kwvg committed Jan 6, 2025
1 parent dda0cc6 commit 7125330
Show file tree
Hide file tree
Showing 33 changed files with 249 additions and 153 deletions.
11 changes: 6 additions & 5 deletions src/evo/cbtx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ bool CheckCbTx(const CTransaction& tx, const CBlockIndex* pindexPrev, TxValidati

// This can only be done after the block has been fully processed, as otherwise we won't have the finished MN list
bool CheckCbTxMerkleRoots(const CBlock& block, const CBlockIndex* pindex, CDeterministicMNManager& dmnman,
const llmq::CQuorumBlockProcessor& quorum_block_processor, BlockValidationState& state,
const CCoinsViewCache& view)
llmq::CQuorumSnapshotManager& qsnapman, const llmq::CQuorumBlockProcessor& quorum_block_processor,
BlockValidationState& state, const CCoinsViewCache& view)
{
if (block.vtx[0]->nType != TRANSACTION_COINBASE) {
return true;
Expand All @@ -87,7 +87,7 @@ bool CheckCbTxMerkleRoots(const CBlock& block, const CBlockIndex* pindex, CDeter
static int64_t nTimeMerkleQuorum = 0;

uint256 calculatedMerkleRoot;
if (!CalcCbTxMerkleRootMNList(block, pindex->pprev, calculatedMerkleRoot, dmnman, state, view)) {
if (!CalcCbTxMerkleRootMNList(block, pindex->pprev, calculatedMerkleRoot, state, dmnman, qsnapman, view)) {
// pass the state returned by the function above
return false;
}
Expand Down Expand Up @@ -117,7 +117,8 @@ bool CheckCbTxMerkleRoots(const CBlock& block, const CBlockIndex* pindex, CDeter
}

bool CalcCbTxMerkleRootMNList(const CBlock& block, const CBlockIndex* pindexPrev, uint256& merkleRootRet,
CDeterministicMNManager& dmnman, BlockValidationState& state, const CCoinsViewCache& view)
BlockValidationState& state, CDeterministicMNManager& dmnman,
llmq::CQuorumSnapshotManager& qsnapman, const CCoinsViewCache& view)
{
try {
static std::atomic<int64_t> nTimeDMN = 0;
Expand All @@ -127,7 +128,7 @@ bool CalcCbTxMerkleRootMNList(const CBlock& block, const CBlockIndex* pindexPrev
int64_t nTime1 = GetTimeMicros();

CDeterministicMNList tmpMNList;
if (!dmnman.BuildNewListFromBlock(block, pindexPrev, state, view, tmpMNList, false)) {
if (!dmnman.BuildNewListFromBlock(block, pindexPrev, state, view, tmpMNList, qsnapman, false)) {
// pass the state returned by the function above
return false;
}
Expand Down
10 changes: 6 additions & 4 deletions src/evo/cbtx.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ class CDeterministicMNManager;
class TxValidationState;

namespace llmq {
class CQuorumBlockProcessor;
class CChainLocksHandler;
class CQuorumBlockProcessor;
class CQuorumSnapshotManager;
}// namespace llmq

// Forward declaration from core_io to get rid of circular dependency
Expand Down Expand Up @@ -87,10 +88,11 @@ template<> struct is_serializable_enum<CCbTx::Version> : std::true_type {};
bool CheckCbTx(const CTransaction& tx, const CBlockIndex* pindexPrev, TxValidationState& state);

bool CheckCbTxMerkleRoots(const CBlock& block, const CBlockIndex* pindex, CDeterministicMNManager& dmnman,
const llmq::CQuorumBlockProcessor& quorum_block_processor, BlockValidationState& state,
const CCoinsViewCache& view);
llmq::CQuorumSnapshotManager& qsnapman, const llmq::CQuorumBlockProcessor& quorum_block_processor,
BlockValidationState& state, const CCoinsViewCache& view);
bool CalcCbTxMerkleRootMNList(const CBlock& block, const CBlockIndex* pindexPrev, uint256& merkleRootRet,
CDeterministicMNManager& dmnman, BlockValidationState& state, const CCoinsViewCache& view);
BlockValidationState& state, CDeterministicMNManager& dmnman,
llmq::CQuorumSnapshotManager& qsnapman, const CCoinsViewCache& view);
bool CalcCbTxMerkleRootQuorums(const CBlock& block, const CBlockIndex* pindexPrev,
const llmq::CQuorumBlockProcessor& quorum_block_processor, uint256& merkleRootRet,
BlockValidationState& state);
Expand Down
8 changes: 4 additions & 4 deletions src/evo/chainhelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,16 @@
#include <masternode/payments.h>

CChainstateHelper::CChainstateHelper(CCreditPoolManager& cpoolman, CDeterministicMNManager& dmnman,
CMNHFManager& mnhfman, CGovernanceManager& govman,
llmq::CInstantSendManager& isman, llmq::CQuorumBlockProcessor& qblockman,
CMNHFManager& mnhfman, CGovernanceManager& govman, llmq::CInstantSendManager& isman,
llmq::CQuorumBlockProcessor& qblockman, llmq::CQuorumSnapshotManager& qsnapman,
const ChainstateManager& chainman, const Consensus::Params& consensus_params,
const CMasternodeSync& mn_sync, const CSporkManager& sporkman,
const llmq::CChainLocksHandler& clhandler, const llmq::CQuorumManager& qman) :
isman{isman},
clhandler{clhandler},
mn_payments{std::make_unique<CMNPaymentsProcessor>(dmnman, govman, chainman, consensus_params, mn_sync, sporkman)},
special_tx{std::make_unique<CSpecialTxProcessor>(cpoolman, dmnman, mnhfman, qblockman, chainman, consensus_params,
clhandler, qman)}
special_tx{std::make_unique<CSpecialTxProcessor>(cpoolman, dmnman, mnhfman, qblockman, qsnapman, chainman,
consensus_params, clhandler, qman)}
{}

CChainstateHelper::~CChainstateHelper() = default;
Expand Down
9 changes: 5 additions & 4 deletions src/evo/chainhelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class CChainLocksHandler;
class CInstantSendManager;
class CQuorumBlockProcessor;
class CQuorumManager;
class CQuorumSnapshotManager;
}

class CChainstateHelper
Expand All @@ -37,10 +38,10 @@ class CChainstateHelper
public:
explicit CChainstateHelper(CCreditPoolManager& cpoolman, CDeterministicMNManager& dmnman, CMNHFManager& mnhfman,
CGovernanceManager& govman, llmq::CInstantSendManager& isman,
llmq::CQuorumBlockProcessor& qblockman, const ChainstateManager& chainman,
const Consensus::Params& consensus_params, const CMasternodeSync& mn_sync,
const CSporkManager& sporkman, const llmq::CChainLocksHandler& clhandler,
const llmq::CQuorumManager& qman);
llmq::CQuorumBlockProcessor& qblockman, llmq::CQuorumSnapshotManager& qsnapman,
const ChainstateManager& chainman, const Consensus::Params& consensus_params,
const CMasternodeSync& mn_sync, const CSporkManager& sporkman,
const llmq::CChainLocksHandler& clhandler, const llmq::CQuorumManager& qman);
~CChainstateHelper();

CChainstateHelper() = delete;
Expand Down
21 changes: 15 additions & 6 deletions src/evo/deterministicmns.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -595,7 +595,10 @@ void CDeterministicMNList::RemoveMN(const uint256& proTxHash)
mnInternalIdMap = mnInternalIdMap.erase(dmn->GetInternalId());
}

bool CDeterministicMNManager::ProcessBlock(const CBlock& block, gsl::not_null<const CBlockIndex*> pindex, BlockValidationState& state, const CCoinsViewCache& view, bool fJustCheck, std::optional<MNListUpdates>& updatesRet)
bool CDeterministicMNManager::ProcessBlock(const CBlock& block, gsl::not_null<const CBlockIndex*> pindex,
BlockValidationState& state, const CCoinsViewCache& view,
llmq::CQuorumSnapshotManager& qsnapman, bool fJustCheck,
std::optional<MNListUpdates>& updatesRet)
{
AssertLockHeld(cs_main);

Expand All @@ -610,7 +613,7 @@ bool CDeterministicMNManager::ProcessBlock(const CBlock& block, gsl::not_null<co
int nHeight = pindex->nHeight;

try {
if (!BuildNewListFromBlock(block, pindex->pprev, state, view, newList, true)) {
if (!BuildNewListFromBlock(block, pindex->pprev, state, view, newList, qsnapman, true)) {
// pass the state returned by the function above
return false;
}
Expand Down Expand Up @@ -700,7 +703,10 @@ void CDeterministicMNManager::UpdatedBlockTip(gsl::not_null<const CBlockIndex*>
tipIndex = pindex;
}

bool CDeterministicMNManager::BuildNewListFromBlock(const CBlock& block, gsl::not_null<const CBlockIndex*> pindexPrev, BlockValidationState& state, const CCoinsViewCache& view, CDeterministicMNList& mnListRet, bool debugLogs)
bool CDeterministicMNManager::BuildNewListFromBlock(const CBlock& block, gsl::not_null<const CBlockIndex*> pindexPrev,
BlockValidationState& state, const CCoinsViewCache& view,
CDeterministicMNList& mnListRet,
llmq::CQuorumSnapshotManager& qsnapman, bool debugLogs)
{
int nHeight = pindexPrev->nHeight + 1;

Expand Down Expand Up @@ -923,7 +929,7 @@ bool CDeterministicMNManager::BuildNewListFromBlock(const CBlock& block, gsl::no
return state.Invalid(BlockValidationResult::BLOCK_CONSENSUS, "bad-qc-quorum-hash");
}

HandleQuorumCommitment(opt_qc->commitment, pQuorumBaseBlockIndex, newList, debugLogs);
HandleQuorumCommitment(opt_qc->commitment, pQuorumBaseBlockIndex, newList, qsnapman, debugLogs);
}
}
}
Expand Down Expand Up @@ -995,11 +1001,14 @@ bool CDeterministicMNManager::BuildNewListFromBlock(const CBlock& block, gsl::no
return true;
}

void CDeterministicMNManager::HandleQuorumCommitment(const llmq::CFinalCommitment& qc, gsl::not_null<const CBlockIndex*> pQuorumBaseBlockIndex, CDeterministicMNList& mnList, bool debugLogs)
void CDeterministicMNManager::HandleQuorumCommitment(const llmq::CFinalCommitment& qc,
gsl::not_null<const CBlockIndex*> pQuorumBaseBlockIndex,
CDeterministicMNList& mnList,
llmq::CQuorumSnapshotManager& qsnapman, bool debugLogs)
{
// The commitment has already been validated at this point, so it's safe to use members of it

auto members = llmq::utils::GetAllQuorumMembers(qc.llmqType, *this, pQuorumBaseBlockIndex);
auto members = llmq::utils::GetAllQuorumMembers(qc.llmqType, *this, qsnapman, pQuorumBaseBlockIndex);

for (size_t i = 0; i < members.size(); i++) {
if (!mnList.HasMN(members[i]->proTxHash)) {
Expand Down
17 changes: 10 additions & 7 deletions src/evo/deterministicmns.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ class TxValidationState;

extern RecursiveMutex cs_main;

namespace llmq
{
class CFinalCommitment;
namespace llmq {
class CFinalCommitment;
class CQuorumSnapshotManager;
} // namespace llmq

class CDeterministicMN
Expand Down Expand Up @@ -598,15 +598,18 @@ class CDeterministicMNManager
~CDeterministicMNManager() = default;

bool ProcessBlock(const CBlock& block, gsl::not_null<const CBlockIndex*> pindex, BlockValidationState& state,
const CCoinsViewCache& view, bool fJustCheck, std::optional<MNListUpdates>& updatesRet) EXCLUSIVE_LOCKS_REQUIRED(!cs, cs_main);
const CCoinsViewCache& view, llmq::CQuorumSnapshotManager& qsnapman, bool fJustCheck,
std::optional<MNListUpdates>& updatesRet) EXCLUSIVE_LOCKS_REQUIRED(!cs, cs_main);
bool UndoBlock(gsl::not_null<const CBlockIndex*> pindex, std::optional<MNListUpdates>& updatesRet) EXCLUSIVE_LOCKS_REQUIRED(!cs);

void UpdatedBlockTip(gsl::not_null<const CBlockIndex*> pindex) EXCLUSIVE_LOCKS_REQUIRED(!cs);

// the returned list will not contain the correct block hash (we can't know it yet as the coinbase TX is not updated yet)
bool BuildNewListFromBlock(const CBlock& block, gsl::not_null<const CBlockIndex*> pindexPrev, BlockValidationState& state, const CCoinsViewCache& view,
CDeterministicMNList& mnListRet, bool debugLogs) EXCLUSIVE_LOCKS_REQUIRED(!cs);
void HandleQuorumCommitment(const llmq::CFinalCommitment& qc, gsl::not_null<const CBlockIndex*> pQuorumBaseBlockIndex, CDeterministicMNList& mnList, bool debugLogs);
bool BuildNewListFromBlock(const CBlock& block, gsl::not_null<const CBlockIndex*> pindexPrev,
BlockValidationState& state, const CCoinsViewCache& view, CDeterministicMNList& mnListRet,
llmq::CQuorumSnapshotManager& qsnapman, bool debugLogs) EXCLUSIVE_LOCKS_REQUIRED(!cs);
void HandleQuorumCommitment(const llmq::CFinalCommitment& qc, gsl::not_null<const CBlockIndex*> pQuorumBaseBlockIndex,
CDeterministicMNList& mnList, llmq::CQuorumSnapshotManager& qsnapman, bool debugLogs);

CDeterministicMNList GetListForBlock(gsl::not_null<const CBlockIndex*> pindex) EXCLUSIVE_LOCKS_REQUIRED(!cs) {
LOCK(cs);
Expand Down
21 changes: 12 additions & 9 deletions src/evo/specialtxman.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,11 @@
#include <primitives/block.h>
#include <validation.h>

static bool CheckSpecialTxInner(CDeterministicMNManager& dmnman, const ChainstateManager& chainman,
const llmq::CQuorumManager& qman, const CTransaction& tx, const CBlockIndex* pindexPrev,
const CCoinsViewCache& view, const std::optional<CRangesSet>& indexes, bool check_sigs,
TxValidationState& state) EXCLUSIVE_LOCKS_REQUIRED(::cs_main)
static bool CheckSpecialTxInner(CDeterministicMNManager& dmnman, llmq::CQuorumSnapshotManager& qsnapman,
const ChainstateManager& chainman, const llmq::CQuorumManager& qman,
const CTransaction& tx, const CBlockIndex* pindexPrev, const CCoinsViewCache& view,
const std::optional<CRangesSet>& indexes, bool check_sigs, TxValidationState& state)
EXCLUSIVE_LOCKS_REQUIRED(::cs_main)
{
AssertLockHeld(cs_main);

Expand All @@ -47,7 +48,7 @@ static bool CheckSpecialTxInner(CDeterministicMNManager& dmnman, const Chainstat
case TRANSACTION_COINBASE:
return CheckCbTx(tx, pindexPrev, state);
case TRANSACTION_QUORUM_COMMITMENT:
return llmq::CheckLLMQCommitment(dmnman, chainman, tx, pindexPrev, state);
return llmq::CheckLLMQCommitment(dmnman, qsnapman, chainman, tx, pindexPrev, state);
case TRANSACTION_MNHF_SIGNAL:
return CheckMNHFTx(chainman, qman, tx, pindexPrev, state);
case TRANSACTION_ASSET_LOCK:
Expand All @@ -66,7 +67,8 @@ static bool CheckSpecialTxInner(CDeterministicMNManager& dmnman, const Chainstat
bool CSpecialTxProcessor::CheckSpecialTx(const CTransaction& tx, const CBlockIndex* pindexPrev, const CCoinsViewCache& view, bool check_sigs, TxValidationState& state)
{
AssertLockHeld(cs_main);
return CheckSpecialTxInner(m_dmnman, m_chainman, m_qman, tx, pindexPrev, view, std::nullopt, check_sigs, state);
return CheckSpecialTxInner(m_dmnman, m_qsnapman, m_chainman, m_qman, tx, pindexPrev, view, std::nullopt, check_sigs,
state);
}

[[nodiscard]] bool CSpecialTxProcessor::ProcessSpecialTx(const CTransaction& tx, const CBlockIndex* pindex, TxValidationState& state)
Expand Down Expand Up @@ -145,7 +147,8 @@ bool CSpecialTxProcessor::ProcessSpecialTxsInBlock(const CBlock& block, const CB
TxValidationState tx_state;
// At this moment CheckSpecialTx() and ProcessSpecialTx() may fail by 2 possible ways:
// consensus failures and "TX_BAD_SPECIAL"
if (!CheckSpecialTxInner(m_dmnman, m_chainman, m_qman, *ptr_tx, pindex->pprev, view, creditPool.indexes, fCheckCbTxMerkleRoots, tx_state)) {
if (!CheckSpecialTxInner(m_dmnman, m_qsnapman, m_chainman, m_qman, *ptr_tx, pindex->pprev, view,
creditPool.indexes, fCheckCbTxMerkleRoots, tx_state)) {
assert(tx_state.GetResult() == TxValidationResult::TX_CONSENSUS || tx_state.GetResult() == TxValidationResult::TX_BAD_SPECIAL);
return state.Invalid(BlockValidationResult::BLOCK_CONSENSUS, tx_state.GetRejectReason(),
strprintf("Special Transaction check failed (tx hash %s) %s", ptr_tx->GetHash().ToString(), tx_state.GetDebugMessage()));
Expand All @@ -170,7 +173,7 @@ bool CSpecialTxProcessor::ProcessSpecialTxsInBlock(const CBlock& block, const CB
nTimeQuorum += nTime3 - nTime2;
LogPrint(BCLog::BENCHMARK, " - m_qblockman: %.2fms [%.2fs]\n", 0.001 * (nTime3 - nTime2), nTimeQuorum * 0.000001);

if (!m_dmnman.ProcessBlock(block, pindex, state, view, fJustCheck, updatesRet)) {
if (!m_dmnman.ProcessBlock(block, pindex, state, view, m_qsnapman, fJustCheck, updatesRet)) {
// pass the state returned by the function above
return false;
}
Expand All @@ -179,7 +182,7 @@ bool CSpecialTxProcessor::ProcessSpecialTxsInBlock(const CBlock& block, const CB
nTimeDMN += nTime4 - nTime3;
LogPrint(BCLog::BENCHMARK, " - m_dmnman: %.2fms [%.2fs]\n", 0.001 * (nTime4 - nTime3), nTimeDMN * 0.000001);

if (fCheckCbTxMerkleRoots && !CheckCbTxMerkleRoots(block, pindex, m_dmnman, m_qblockman, state, view)) {
if (fCheckCbTxMerkleRoots && !CheckCbTxMerkleRoots(block, pindex, m_dmnman, m_qsnapman, m_qblockman, state, view)) {
// pass the state returned by the function above
return false;
}
Expand Down
20 changes: 16 additions & 4 deletions src/evo/specialtxman.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,10 @@ struct MNListUpdates;

namespace Consensus { struct Params; }
namespace llmq {
class CChainLocksHandler;
class CQuorumBlockProcessor;
class CQuorumManager;
class CChainLocksHandler;
class CQuorumSnapshotManager;
} // namespace llmq

extern RecursiveMutex cs_main;
Expand All @@ -39,6 +40,7 @@ class CSpecialTxProcessor
CDeterministicMNManager& m_dmnman;
CMNHFManager& m_mnhfman;
llmq::CQuorumBlockProcessor& m_qblockman;
llmq::CQuorumSnapshotManager& m_qsnapman;
const ChainstateManager& m_chainman;
const Consensus::Params& m_consensus_params;
const llmq::CChainLocksHandler& m_clhandler;
Expand All @@ -50,10 +52,20 @@ class CSpecialTxProcessor

public:
explicit CSpecialTxProcessor(CCreditPoolManager& cpoolman, CDeterministicMNManager& dmnman, CMNHFManager& mnhfman,
llmq::CQuorumBlockProcessor& qblockman, const ChainstateManager& chainman, const Consensus::Params& consensus_params,
llmq::CQuorumBlockProcessor& qblockman, llmq::CQuorumSnapshotManager& qsnapman,
const ChainstateManager& chainman, const Consensus::Params& consensus_params,
const llmq::CChainLocksHandler& clhandler, const llmq::CQuorumManager& qman) :
m_cpoolman(cpoolman), m_dmnman{dmnman}, m_mnhfman{mnhfman}, m_qblockman{qblockman}, m_chainman(chainman), m_consensus_params{consensus_params},
m_clhandler{clhandler}, m_qman{qman} {}
m_cpoolman(cpoolman),
m_dmnman{dmnman},
m_mnhfman{mnhfman},
m_qblockman{qblockman},
m_qsnapman{qsnapman},
m_chainman(chainman),
m_consensus_params{consensus_params},
m_clhandler{clhandler},
m_qman{qman}
{
}

bool CheckSpecialTx(const CTransaction& tx, const CBlockIndex* pindexPrev, const CCoinsViewCache& view, bool check_sigs, TxValidationState& state)
EXCLUSIVE_LOCKS_REQUIRED(cs_main);
Expand Down
Loading

0 comments on commit 7125330

Please sign in to comment.