From 60351125ebe68b1b28d3a6e6f59100142ec37f85 Mon Sep 17 00:00:00 2001 From: furszy Date: Fri, 14 Jan 2022 17:20:34 -0300 Subject: [PATCH] init: move tier two objects initializers and the Masternodes collateral transactions' output lock in wallet to the tiertwo/init.cpp file. --- src/init.cpp | 49 +++--------------------------------- src/tiertwo/init.cpp | 60 ++++++++++++++++++++++++++++++++++++++++++++ src/tiertwo/init.h | 12 +++++++++ 3 files changed, 76 insertions(+), 45 deletions(-) diff --git a/src/init.cpp b/src/init.cpp index d5f1097cb88e9..a6dab6e2a4403 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -21,13 +21,11 @@ #include "checkpoints.h" #include "compat/sanity.h" #include "consensus/upgrades.h" -#include "evo/deterministicmns.h" #include "evo/evonotificationinterface.h" #include "fs.h" #include "httpserver.h" #include "httprpc.h" #include "invalid.h" -#include "llmq/quorums_init.h" #include "key.h" #include "mapport.h" #include "masternodeconfig.h" @@ -44,7 +42,6 @@ #include "shutdown.h" #include "spork.h" #include "sporkdb.h" -#include "evo/evodb.h" #include "tiertwo/init.h" #include "txdb.h" #include "torcontrol.h" @@ -196,7 +193,7 @@ void Shutdown() StopREST(); StopRPC(); StopHTTPServer(); - llmq::StopLLMQSystem(); + StopTierTwoThreads(); #ifdef ENABLE_WALLET for (CWalletRef pwallet : vpwallets) { pwallet->Flush(false); @@ -268,9 +265,7 @@ void Shutdown() zerocoinDB.reset(); accumulatorCache.reset(); pSporkDB.reset(); - llmq::DestroyLLMQSystem(); - deterministicMNManager.reset(); - evoDb.reset(); + DeleteTierTwo(); } #ifdef ENABLE_WALLET for (CWalletRef pwallet : vpwallets) { @@ -1439,7 +1434,6 @@ bool AppInitMain() nCoinDBCache = std::min(nCoinDBCache, nMaxCoinsDBCache << 20); // cap total coins db cache nTotalCache -= nCoinDBCache; nCoinCacheUsage = nTotalCache; // the rest goes to in-memory cache - int64_t nEvoDbCache = 1024 * 1024 * 16; // TODO LogPrintf("Cache configuration:\n"); LogPrintf("* Using %.1fMiB for block index database\n", nBlockTreeDBCache * (1.0 / 1024 / 1024)); LogPrintf("* Using %.1fMiB for chain state database\n", nCoinDBCache * (1.0 / 1024 / 1024)); @@ -1468,10 +1462,7 @@ bool AppInitMain() pSporkDB.reset(new CSporkDB(0, false, false)); accumulatorCache.reset(new AccumulatorCache(zerocoinDB.get())); - deterministicMNManager.reset(); - evoDb.reset(); - evoDb.reset(new CEvoDB(nEvoDbCache, false, fReindex)); - deterministicMNManager.reset(new CDeterministicMNManager(*evoDb)); + InitTierTwoPreChainLoad(fReindex); if (fReset) { pblocktree->WriteReindexing(true); @@ -1542,8 +1533,7 @@ bool AppInitMain() // The on-disk coinsdb is now in a good state, create the cache pcoinsTip.reset(new CCoinsViewCache(pcoinscatcher.get())); - // Initialize LLMQ system - llmq::InitLLMQSystem(*evoDb); + InitTierTwoPostCoinsCacheLoad(); bool is_coinsview_empty = fReset || fReindexChainState || pcoinsTip->GetBestBlock().IsNull(); if (!is_coinsview_empty) { @@ -1732,34 +1722,6 @@ bool AppInitMain() // set the mode of budget voting for this node SetBudgetFinMode(gArgs.GetArg("-budgetvotemode", "auto")); -#ifdef ENABLE_WALLET - // !TODO: remove after complete transition to DMN - // use only the first wallet here. This section can be removed after transition to DMN - if (gArgs.GetBoolArg("-mnconflock", DEFAULT_MNCONFLOCK) && !vpwallets.empty() && vpwallets[0]) { - LOCK(vpwallets[0]->cs_wallet); - LogPrintf("Locking Masternodes collateral utxo:\n"); - uint256 mnTxHash; - for (const auto& mne : masternodeConfig.getEntries()) { - mnTxHash.SetHex(mne.getTxHash()); - COutPoint outpoint = COutPoint(mnTxHash, (unsigned int) std::stoul(mne.getOutputIndex())); - vpwallets[0]->LockCoin(outpoint); - LogPrintf("Locked collateral, MN: %s, tx hash: %s, output index: %s\n", - mne.getAlias(), mne.getTxHash(), mne.getOutputIndex()); - } - } - - // automatic lock for DMN - if (gArgs.GetBoolArg("-mnconflock", DEFAULT_MNCONFLOCK)) { - LogPrintf("Locking masternode collaterals...\n"); - const auto& mnList = deterministicMNManager->GetListAtChainTip(); - mnList.ForEachMN(false, [&](const CDeterministicMNCPtr& dmn) { - for (CWallet* pwallet : vpwallets) { - pwallet->LockOutpointIfMineWithMutex(nullptr, dmn->collateralOutpoint); - } - }); - } -#endif - // Start tier two threads and jobs StartTierTwoThreadsAndScheduleJobs(threadGroup, scheduler); @@ -1768,9 +1730,6 @@ bool AppInitMain() return false; } - // start LLMQ system - llmq::StartLLMQSystem(); - // ********************************************************* Step 11: start node if (!strErrors.str().empty()) diff --git a/src/tiertwo/init.cpp b/src/tiertwo/init.cpp index a6f9a2297c308..67a819a86a9ac 100644 --- a/src/tiertwo/init.cpp +++ b/src/tiertwo/init.cpp @@ -5,14 +5,17 @@ #include "tiertwo/init.h" #include "budget/budgetdb.h" +#include "evo/evodb.h" #include "flatdb.h" #include "guiinterface.h" #include "guiinterfaceutil.h" #include "masternodeman.h" #include "masternode-payments.h" #include "masternodeconfig.h" +#include "llmq/quorums_init.h" #include "tiertwo/masternode_meta_manager.h" #include "validation.h" +#include "wallet/wallet.h" #include @@ -29,6 +32,21 @@ std::string GetTierTwoHelpString(bool showDebug) return strUsage; } +void InitTierTwoPreChainLoad(bool fReindex) +{ + int64_t nEvoDbCache = 1024 * 1024 * 16; // TODO + deterministicMNManager.reset(); + evoDb.reset(); + evoDb.reset(new CEvoDB(nEvoDbCache, false, fReindex)); + deterministicMNManager.reset(new CDeterministicMNManager(*evoDb)); +} + +void InitTierTwoPostCoinsCacheLoad() +{ + // Initialize LLMQ system + llmq::InitLLMQSystem(*evoDb); +} + // Sets the last CACHED_BLOCK_HASHES hashes into masternode manager cache static void LoadBlockHashesCache(CMasternodeMan& man) { @@ -182,6 +200,34 @@ bool InitActiveMN() if (!res) { return UIError(res.getError()); } } } + +#ifdef ENABLE_WALLET + // !TODO: remove after complete transition to DMN + // use only the first wallet here. This section can be removed after transition to DMN + if (gArgs.GetBoolArg("-mnconflock", DEFAULT_MNCONFLOCK) && !vpwallets.empty() && vpwallets[0]) { + LOCK(vpwallets[0]->cs_wallet); + LogPrintf("Locking Masternodes collateral utxo:\n"); + uint256 mnTxHash; + for (const auto& mne : masternodeConfig.getEntries()) { + mnTxHash.SetHex(mne.getTxHash()); + COutPoint outpoint = COutPoint(mnTxHash, (unsigned int) std::stoul(mne.getOutputIndex())); + vpwallets[0]->LockCoin(outpoint); + LogPrintf("Locked collateral, MN: %s, tx hash: %s, output index: %s\n", + mne.getAlias(), mne.getTxHash(), mne.getOutputIndex()); + } + } + + // automatic lock for DMN + if (gArgs.GetBoolArg("-mnconflock", DEFAULT_MNCONFLOCK)) { + LogPrintf("Locking masternode collaterals...\n"); + const auto& mnList = deterministicMNManager->GetListAtChainTip(); + mnList.ForEachMN(false, [&](const CDeterministicMNCPtr& dmn) { + for (CWallet* pwallet : vpwallets) { + pwallet->LockOutpointIfMineWithMutex(nullptr, dmn->collateralOutpoint); + } + }); + } +#endif // All good return true; } @@ -189,4 +235,18 @@ bool InitActiveMN() void StartTierTwoThreadsAndScheduleJobs(boost::thread_group& threadGroup, CScheduler& scheduler) { threadGroup.create_thread(std::bind(&ThreadCheckMasternodes)); + // start LLMQ system + llmq::StartLLMQSystem(); +} + +void StopTierTwoThreads() +{ + llmq::StopLLMQSystem(); } + +void DeleteTierTwo() +{ + llmq::DestroyLLMQSystem(); + deterministicMNManager.reset(); + evoDb.reset(); +} \ No newline at end of file diff --git a/src/tiertwo/init.h b/src/tiertwo/init.h index bb20e86729c8a..c91bf735d1855 100644 --- a/src/tiertwo/init.h +++ b/src/tiertwo/init.h @@ -17,6 +17,12 @@ namespace boost { std::string GetTierTwoHelpString(bool showDebug); +/** Inits the tier two global objects */ +void InitTierTwoPreChainLoad(bool fReindex); + +/** Inits the tier two global objects that require access to the coins tip cache */ +void InitTierTwoPostCoinsCacheLoad(); + /** Loads from disk all the tier two related objects */ bool LoadTierTwo(int chain_active_height, bool fReindexChainState); @@ -34,5 +40,11 @@ bool InitActiveMN(); /** Starts tier two threads and jobs */ void StartTierTwoThreadsAndScheduleJobs(boost::thread_group& threadGroup, CScheduler& scheduler); +/** Stops tier two workers */ +void StopTierTwoThreads(); + +/** Cleans manager and worker objects pointers */ +void DeleteTierTwo(); + #endif //PIVX_TIERTWO_INIT_H