Skip to content

Commit

Permalink
refactor: drop usage of chainstate globals in masternode logic
Browse files Browse the repository at this point in the history
  • Loading branch information
kwvg committed Jun 26, 2024
1 parent 303c6bb commit 208b1c0
Show file tree
Hide file tree
Showing 14 changed files with 61 additions and 50 deletions.
5 changes: 3 additions & 2 deletions src/evo/mnauth.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ void CMNAuth::PushMNAUTH(CNode& peer, CConnman& connman, const CActiveMasternode
}

PeerMsgRet CMNAuth::ProcessMessage(CNode& peer, CConnman& connman, CMasternodeMetaMan& mn_metaman, const CActiveMasternodeManager* const mn_activeman,
const CMasternodeSync& mn_sync, const CDeterministicMNList& tip_mn_list, std::string_view msg_type, CDataStream& vRecv)
const CChain& active_chain, const CMasternodeSync& mn_sync, const CDeterministicMNList& tip_mn_list,
std::string_view msg_type, CDataStream& vRecv)
{
assert(mn_metaman.IsValid());

Expand Down Expand Up @@ -105,7 +106,7 @@ PeerMsgRet CMNAuth::ProcessMessage(CNode& peer, CConnman& connman, CMasternodeMe
if (Params().NetworkIDString() != CBaseChainParams::MAIN && gArgs.IsArgSet("-pushversion")) {
nOurNodeVersion = gArgs.GetArg("-pushversion", PROTOCOL_VERSION);
}
const CBlockIndex* tip = ::ChainActive().Tip();
const CBlockIndex* tip = active_chain.Tip();
const bool is_basic_scheme_active{DeploymentActiveAfter(tip, Params().GetConsensus(), Consensus::DEPLOYMENT_V19)};
ConstCBLSPublicKeyVersionWrapper pubKey(dmn->pdmnState->pubKeyOperator.Get(), !is_basic_scheme_active);
// See comment in PushMNAUTH (fInbound is negated here as we're on the other side of the connection)
Expand Down
4 changes: 3 additions & 1 deletion src/evo/mnauth.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

class CActiveMasternodeManager;
class CBlockIndex;
class CChain;
class CConnman;
class CDataStream;
class CDeterministicMN;
Expand Down Expand Up @@ -59,7 +60,8 @@ class CMNAuth
* attempting to call this function regardless of sync state
*/
static PeerMsgRet ProcessMessage(CNode& peer, CConnman& connman, CMasternodeMetaMan& mn_metaman, const CActiveMasternodeManager* const mn_activeman,
const CMasternodeSync& mn_sync, const CDeterministicMNList& tip_mn_list, std::string_view msg_type, CDataStream& vRecv);
const CChain& active_chain, const CMasternodeSync& mn_sync, const CDeterministicMNList& tip_mn_list,
std::string_view msg_type, CDataStream& vRecv);
static void NotifyMasternodeListChanged(bool undo, const CDeterministicMNList& oldMNList, const CDeterministicMNListDiff& diff, CConnman& connman);
};

Expand Down
21 changes: 11 additions & 10 deletions src/evo/mnhftx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ bool MNHFTx::Verify(const llmq::CQuorumManager& qman, const uint256& quorumHash,
return true;
}

bool CheckMNHFTx(const llmq::CQuorumManager& qman, const CTransaction& tx, const CBlockIndex* pindexPrev, TxValidationState& state)
bool CheckMNHFTx(const ChainstateManager& chainman, const llmq::CQuorumManager& qman, const CTransaction& tx, const CBlockIndex* pindexPrev, TxValidationState& state)
{
if (!tx.IsSpecialTxVersion() || tx.nType != TRANSACTION_MNHF_SIGNAL) {
return state.Invalid(TxValidationResult::TX_CONSENSUS, "bad-mnhf-type");
Expand All @@ -116,7 +116,7 @@ bool CheckMNHFTx(const llmq::CQuorumManager& qman, const CTransaction& tx, const
return state.Invalid(TxValidationResult::TX_CONSENSUS, "bad-mnhf-version");
}

const CBlockIndex* pindexQuorum = WITH_LOCK(::cs_main, return g_chainman.m_blockman.LookupBlockIndex(mnhfTx.signal.quorumHash));
const CBlockIndex* pindexQuorum = WITH_LOCK(::cs_main, return chainman.m_blockman.LookupBlockIndex(mnhfTx.signal.quorumHash));
if (!pindexQuorum) {
return state.Invalid(TxValidationResult::TX_CONSENSUS, "bad-mnhf-quorum-hash");
}
Expand Down Expand Up @@ -160,7 +160,7 @@ std::optional<uint8_t> extractEHFSignal(const CTransaction& tx)
return opt_mnhfTx->signal.versionBit;
}

static bool extractSignals(const llmq::CQuorumManager& qman, const CBlock& block, const CBlockIndex* const pindex, std::vector<uint8_t>& new_signals, BlockValidationState& state)
static bool extractSignals(const ChainstateManager& chainman, const llmq::CQuorumManager& qman, const CBlock& block, const CBlockIndex* const pindex, std::vector<uint8_t>& new_signals, BlockValidationState& state)
{
// we skip the coinbase
for (size_t i = 1; i < block.vtx.size(); ++i) {
Expand All @@ -172,7 +172,7 @@ static bool extractSignals(const llmq::CQuorumManager& qman, const CBlock& block
}

TxValidationState tx_state;
if (!CheckMNHFTx(qman, tx, pindex, tx_state)) {
if (!CheckMNHFTx(chainman, qman, tx, pindex, tx_state)) {
return state.Invalid(BlockValidationResult::BLOCK_CONSENSUS, tx_state.GetRejectReason(), tx_state.GetDebugMessage());
}

Expand All @@ -192,11 +192,11 @@ static bool extractSignals(const llmq::CQuorumManager& qman, const CBlock& block

std::optional<CMNHFManager::Signals> CMNHFManager::ProcessBlock(const CBlock& block, const CBlockIndex* const pindex, bool fJustCheck, BlockValidationState& state)
{
assert(m_qman);
assert(m_chainman && m_qman);

try {
std::vector<uint8_t> new_signals;
if (!extractSignals(*m_qman, block, pindex, new_signals, state)) {
if (!extractSignals(*m_chainman, *m_qman, block, pindex, new_signals, state)) {
// state is set inside extractSignals
return std::nullopt;
}
Expand Down Expand Up @@ -246,11 +246,11 @@ std::optional<CMNHFManager::Signals> CMNHFManager::ProcessBlock(const CBlock& bl

bool CMNHFManager::UndoBlock(const CBlock& block, const CBlockIndex* const pindex)
{
assert(m_qman);
assert(m_chainman && m_qman);

std::vector<uint8_t> excluded_signals;
BlockValidationState state;
if (!extractSignals(*m_qman, block, pindex, excluded_signals, state)) {
if (!extractSignals(*m_chainman, *m_qman, block, pindex, excluded_signals, state)) {
LogPrintf("CMNHFManager::%s: failed to extract signals\n", __func__);
return false;
}
Expand Down Expand Up @@ -354,10 +354,11 @@ void CMNHFManager::AddSignal(const CBlockIndex* const pindex, int bit)
AddToCache(signals, pindex);
}

void CMNHFManager::ConnectManagers(gsl::not_null<llmq::CQuorumManager*> qman)
void CMNHFManager::ConnectManagers(gsl::not_null<ChainstateManager*> chainman, gsl::not_null<llmq::CQuorumManager*> qman)
{
// Do not allow double-initialization
assert(m_qman == nullptr);
assert(m_chainman == nullptr && m_qman == nullptr);
m_chainman = chainman;
m_qman = qman;
}

Expand Down
8 changes: 5 additions & 3 deletions src/evo/mnhftx.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class BlockValidationState;
class CBlock;
class CBlockIndex;
class CEvoDB;
class ChainstateManager;
class TxValidationState;
namespace llmq {
class CQuorumManager;
Expand Down Expand Up @@ -100,6 +101,7 @@ class CMNHFManager : public AbstractEHFManager
{
private:
CEvoDB& m_evoDb;
ChainstateManager* m_chainman{nullptr};
llmq::CQuorumManager* m_qman{nullptr};

static constexpr size_t MNHFCacheSize = 1000;
Expand Down Expand Up @@ -147,14 +149,14 @@ class CMNHFManager : public AbstractEHFManager
* Separated from constructor to allow LLMQContext to use CMNHFManager in read-only capacity.
* Required to mutate state.
*/
void ConnectManagers(gsl::not_null<llmq::CQuorumManager*> qman);
void ConnectManagers(gsl::not_null<ChainstateManager*> chainman, gsl::not_null<llmq::CQuorumManager*> qman);

/**
* Reset llmq::CQuorumManager pointer.
*
* @pre Must be called before LLMQContext (containing llmq::CQuorumManager) is destroyed.
*/
void DisconnectManagers() { m_qman = nullptr; };
void DisconnectManagers() { m_chainman = nullptr; m_qman = nullptr; };

private:
void AddToCache(const Signals& signals, const CBlockIndex* const pindex);
Expand All @@ -175,6 +177,6 @@ class CMNHFManager : public AbstractEHFManager
};

std::optional<uint8_t> extractEHFSignal(const CTransaction& tx);
bool CheckMNHFTx(const llmq::CQuorumManager& qman, const CTransaction& tx, const CBlockIndex* pindexPrev, TxValidationState& state);
bool CheckMNHFTx(const ChainstateManager& chainman, const llmq::CQuorumManager& qman, const CTransaction& tx, const CBlockIndex* pindexPrev, TxValidationState& state);

#endif // BITCOIN_EVO_MNHFTX_H
16 changes: 8 additions & 8 deletions src/evo/simplifiedmns.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -316,29 +316,29 @@ CSimplifiedMNListDiff BuildSimplifiedDiff(const CDeterministicMNList& from, cons
return diffRet;
}

bool BuildSimplifiedMNListDiff(const uint256& baseBlockHash, const uint256& blockHash, CSimplifiedMNListDiff& mnListDiffRet,
CDeterministicMNManager& dmnman, const llmq::CQuorumBlockProcessor& quorum_block_processor, const llmq::CQuorumManager& qman,
std::string& errorRet, bool extended)
bool BuildSimplifiedMNListDiff(CDeterministicMNManager& dmnman, const ChainstateManager& chainman, const llmq::CQuorumBlockProcessor& qblockman,
const llmq::CQuorumManager& qman, const uint256& baseBlockHash, const uint256& blockHash,
CSimplifiedMNListDiff& mnListDiffRet, std::string& errorRet, bool extended)
{
AssertLockHeld(cs_main);
mnListDiffRet = CSimplifiedMNListDiff();

const CBlockIndex* baseBlockIndex = ::ChainActive().Genesis();
const CBlockIndex* baseBlockIndex = chainman.ActiveChain().Genesis();
if (!baseBlockHash.IsNull()) {
baseBlockIndex = g_chainman.m_blockman.LookupBlockIndex(baseBlockHash);
baseBlockIndex = chainman.m_blockman.LookupBlockIndex(baseBlockHash);
if (!baseBlockIndex) {
errorRet = strprintf("block %s not found", baseBlockHash.ToString());
return false;
}
}

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(baseBlockIndex) || !::ChainActive().Contains(blockIndex)) {
if (!chainman.ActiveChain().Contains(baseBlockIndex) || !chainman.ActiveChain().Contains(blockIndex)) {
errorRet = strprintf("block %s and %s are not in the same chain", baseBlockHash.ToString(), blockHash.ToString());
return false;
}
Expand All @@ -356,7 +356,7 @@ bool BuildSimplifiedMNListDiff(const uint256& baseBlockHash, const uint256& bloc
// null block hash was provided to get the diff from the genesis block.
mnListDiffRet.baseBlockHash = baseBlockHash;

if (!mnListDiffRet.BuildQuorumsDiff(baseBlockIndex, blockIndex, quorum_block_processor)) {
if (!mnListDiffRet.BuildQuorumsDiff(baseBlockIndex, blockIndex, qblockman)) {
errorRet = strprintf("failed to build quorums diff");
return false;
}
Expand Down
7 changes: 4 additions & 3 deletions src/evo/simplifiedmns.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class UniValue;
class CBlockIndex;
class CDeterministicMNList;
class CDeterministicMN;
class ChainstateManager;

namespace llmq {
class CFinalCommitment;
Expand Down Expand Up @@ -170,8 +171,8 @@ class CSimplifiedMNListDiff
[[nodiscard]] UniValue ToJson(bool extended = false) const;
};

bool BuildSimplifiedMNListDiff(const uint256& baseBlockHash, const uint256& blockHash, CSimplifiedMNListDiff& mnListDiffRet,
CDeterministicMNManager& dmnman, const llmq::CQuorumBlockProcessor& quorum_block_processor, const llmq::CQuorumManager& qman,
std::string& errorRet, bool extended = false);
bool BuildSimplifiedMNListDiff(CDeterministicMNManager& dmnman, const ChainstateManager& chainman, const llmq::CQuorumBlockProcessor& qblockman,
const llmq::CQuorumManager& qman, const uint256& baseBlockHash, const uint256& blockHash,
CSimplifiedMNListDiff& mnListDiffRet, std::string& errorRet, bool extended = false);

#endif // BITCOIN_EVO_SIMPLIFIEDMNS_H
2 changes: 1 addition & 1 deletion src/evo/specialtxman.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ static bool CheckSpecialTxInner(CDeterministicMNManager& dmnman, const Chainstat
if (!DeploymentActiveAfter(pindexPrev, consensusParams, Consensus::DEPLOYMENT_V20)) {
return state.Invalid(TxValidationResult::TX_CONSENSUS, "mnhf-before-v20");
}
return CheckMNHFTx(qman, tx, pindexPrev, state);
return CheckMNHFTx(chainman, qman, tx, pindexPrev, state);
case TRANSACTION_ASSET_LOCK:
if (!DeploymentActiveAfter(pindexPrev, consensusParams, Consensus::DEPLOYMENT_V20)) {
return state.Invalid(TxValidationResult::TX_CONSENSUS, "assetlocks-before-v20");
Expand Down
2 changes: 1 addition & 1 deletion src/init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2016,7 +2016,7 @@ bool AppInitMain(const CoreContext& context, NodeContext& node, interfaces::Bloc
node.llmq_ctx = std::make_unique<LLMQContext>(chainman.ActiveChainstate(), *node.connman, *node.dmnman, *node.evodb, *node.mn_metaman, *node.mnhf_manager, *node.sporkman,
*node.mempool, node.mn_activeman.get(), *node.mn_sync, node.peerman, /* unit_tests = */ false, /* wipe = */ fReset || fReindexChainState);
// Enable CMNHFManager::{Process, Undo}Block
node.mnhf_manager->ConnectManagers(node.llmq_ctx->qman.get());
node.mnhf_manager->ConnectManagers(node.chainman, node.llmq_ctx->qman.get());
// Have to start it early to let VerifyDB check ChainLock signatures in coinbase
node.llmq_ctx->Start();

Expand Down
24 changes: 12 additions & 12 deletions src/llmq/snapshot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,9 @@ UniValue CQuorumRotationInfo::ToJson() const
return obj;
}

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

Expand Down Expand Up @@ -123,7 +123,7 @@ bool BuildQuorumRotationInfo(const CGetQuorumRotationInfo& request, CQuorumRotat
return false;
}
//Build MN list Diff always with highest baseblock
if (!BuildSimplifiedMNListDiff(baseBlockIndexes.back()->GetBlockHash(), tipBlockIndex->GetBlockHash(), response.mnListDiffTip, dmnman, quorum_block_processor, qman, errorRet)) {
if (!BuildSimplifiedMNListDiff(dmnman, chainman, qblockman, qman, baseBlockIndexes.back()->GetBlockHash(), tipBlockIndex->GetBlockHash(), response.mnListDiffTip, errorRet)) {
return false;
}

Expand Down Expand Up @@ -156,7 +156,7 @@ bool BuildQuorumRotationInfo(const CGetQuorumRotationInfo& request, CQuorumRotat
}

//Build MN list Diff always with highest baseblock
if (!BuildSimplifiedMNListDiff(GetLastBaseBlockHash(baseBlockIndexes, pWorkBlockIndex), pWorkBlockIndex->GetBlockHash(), response.mnListDiffH, dmnman, quorum_block_processor, qman, errorRet)) {
if (!BuildSimplifiedMNListDiff(dmnman, chainman, qblockman, qman, GetLastBaseBlockHash(baseBlockIndexes, pWorkBlockIndex), pWorkBlockIndex->GetBlockHash(), response.mnListDiffH, errorRet)) {
return false;
}

Expand Down Expand Up @@ -202,7 +202,7 @@ bool BuildQuorumRotationInfo(const CGetQuorumRotationInfo& request, CQuorumRotat
const CBlockIndex* pWorkBlockHMinus4CIndex = pBlockHMinus4CIndex->GetAncestor(pBlockHMinus4CIndex->nHeight - workDiff);
//Checked later if extraShare is on

if (!BuildSimplifiedMNListDiff(GetLastBaseBlockHash(baseBlockIndexes, pWorkBlockHMinusCIndex), pWorkBlockHMinusCIndex->GetBlockHash(), response.mnListDiffAtHMinusC, dmnman, quorum_block_processor, qman, errorRet)) {
if (!BuildSimplifiedMNListDiff(dmnman, chainman, qblockman, qman, GetLastBaseBlockHash(baseBlockIndexes, pWorkBlockHMinusCIndex), pWorkBlockHMinusCIndex->GetBlockHash(), response.mnListDiffAtHMinusC, errorRet)) {
return false;
}

Expand All @@ -214,7 +214,7 @@ bool BuildQuorumRotationInfo(const CGetQuorumRotationInfo& request, CQuorumRotat
response.quorumSnapshotAtHMinusC = std::move(snapshotHMinusC.value());
}

if (!BuildSimplifiedMNListDiff(GetLastBaseBlockHash(baseBlockIndexes, pWorkBlockHMinus2CIndex), pWorkBlockHMinus2CIndex->GetBlockHash(), response.mnListDiffAtHMinus2C, dmnman, quorum_block_processor, qman, errorRet)) {
if (!BuildSimplifiedMNListDiff(dmnman, chainman, qblockman, qman, GetLastBaseBlockHash(baseBlockIndexes, pWorkBlockHMinus2CIndex), pWorkBlockHMinus2CIndex->GetBlockHash(), response.mnListDiffAtHMinus2C, errorRet)) {
return false;
}

Expand All @@ -226,7 +226,7 @@ bool BuildQuorumRotationInfo(const CGetQuorumRotationInfo& request, CQuorumRotat
response.quorumSnapshotAtHMinus2C = std::move(snapshotHMinus2C.value());
}

if (!BuildSimplifiedMNListDiff(GetLastBaseBlockHash(baseBlockIndexes, pWorkBlockHMinus3CIndex), pWorkBlockHMinus3CIndex->GetBlockHash(), response.mnListDiffAtHMinus3C, dmnman, quorum_block_processor, qman, errorRet)) {
if (!BuildSimplifiedMNListDiff(dmnman, chainman, qblockman, qman, GetLastBaseBlockHash(baseBlockIndexes, pWorkBlockHMinus3CIndex), pWorkBlockHMinus3CIndex->GetBlockHash(), response.mnListDiffAtHMinus3C, errorRet)) {
return false;
}

Expand Down Expand Up @@ -255,7 +255,7 @@ bool BuildQuorumRotationInfo(const CGetQuorumRotationInfo& request, CQuorumRotat
}

CSimplifiedMNListDiff mn4c;
if (!BuildSimplifiedMNListDiff(GetLastBaseBlockHash(baseBlockIndexes, pWorkBlockHMinus4CIndex), pWorkBlockHMinus4CIndex->GetBlockHash(), mn4c, dmnman, quorum_block_processor, qman, errorRet)) {
if (!BuildSimplifiedMNListDiff(dmnman, chainman, qblockman, qman, GetLastBaseBlockHash(baseBlockIndexes, pWorkBlockHMinus4CIndex), pWorkBlockHMinus4CIndex->GetBlockHash(), mn4c, errorRet)) {
return false;
}

Expand All @@ -268,11 +268,11 @@ bool BuildQuorumRotationInfo(const CGetQuorumRotationInfo& request, CQuorumRotat

std::set<int> snapshotHeightsNeeded;

std::vector<std::pair<int, const CBlockIndex*>> qdata = quorum_block_processor.GetLastMinedCommitmentsPerQuorumIndexUntilBlock(llmqType, blockIndex, 0);
std::vector<std::pair<int, const CBlockIndex*>> qdata = qblockman.GetLastMinedCommitmentsPerQuorumIndexUntilBlock(llmqType, blockIndex, 0);

for (const auto& obj : qdata) {
uint256 minedBlockHash;
llmq::CFinalCommitmentPtr qc = quorum_block_processor.GetMinedCommitment(llmqType, obj.second->GetBlockHash(), minedBlockHash);
llmq::CFinalCommitmentPtr qc = qblockman.GetMinedCommitment(llmqType, obj.second->GetBlockHash(), minedBlockHash);
if (qc == nullptr) {
return false;
}
Expand Down Expand Up @@ -311,7 +311,7 @@ bool BuildQuorumRotationInfo(const CGetQuorumRotationInfo& request, CQuorumRotat
}

CSimplifiedMNListDiff mnhneeded;
if (!BuildSimplifiedMNListDiff(GetLastBaseBlockHash(baseBlockIndexes, pNeededWorkBlockIndex), pNeededWorkBlockIndex->GetBlockHash(), mnhneeded, dmnman, quorum_block_processor, qman, errorRet)) {
if (!BuildSimplifiedMNListDiff(dmnman, chainman, qblockman, qman, GetLastBaseBlockHash(baseBlockIndexes, pNeededWorkBlockIndex), pNeededWorkBlockIndex->GetBlockHash(), mnhneeded, errorRet)) {
return false;
}

Expand Down
6 changes: 3 additions & 3 deletions src/llmq/snapshot.h
Original file line number Diff line number Diff line change
Expand Up @@ -207,9 +207,9 @@ class CQuorumRotationInfo
[[nodiscard]] UniValue ToJson() const;
};

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

class CQuorumSnapshotManager
Expand Down
Loading

0 comments on commit 208b1c0

Please sign in to comment.