Skip to content

Commit

Permalink
init: move tier two objects initializers and the Masternodes collater…
Browse files Browse the repository at this point in the history
…al transactions' output lock in wallet to the tiertwo/init.cpp file.
  • Loading branch information
furszy committed Jan 19, 2022
1 parent 38b92b9 commit 6035112
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 45 deletions.
49 changes: 4 additions & 45 deletions src/init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
Expand Down Expand Up @@ -196,7 +193,7 @@ void Shutdown()
StopREST();
StopRPC();
StopHTTPServer();
llmq::StopLLMQSystem();
StopTierTwoThreads();
#ifdef ENABLE_WALLET
for (CWalletRef pwallet : vpwallets) {
pwallet->Flush(false);
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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);

Expand All @@ -1768,9 +1730,6 @@ bool AppInitMain()
return false;
}

// start LLMQ system
llmq::StartLLMQSystem();

// ********************************************************* Step 11: start node

if (!strErrors.str().empty())
Expand Down
60 changes: 60 additions & 0 deletions src/tiertwo/init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 <boost/thread.hpp>

Expand All @@ -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)
{
Expand Down Expand Up @@ -182,11 +200,53 @@ 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;
}

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();
}
12 changes: 12 additions & 0 deletions src/tiertwo/init.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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

0 comments on commit 6035112

Please sign in to comment.