Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Finish moving tier two initialization code from init.cpp to tiertwo/init.cpp and add disabledkg init flag #2723

Merged
merged 4 commits into from
Jan 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 5 additions & 56 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 @@ -89,7 +86,6 @@ static const bool DEFAULT_PROXYRANDOMIZE = true;
static const bool DEFAULT_REST_ENABLE = false;
static const bool DEFAULT_DISABLE_SAFEMODE = false;
static const bool DEFAULT_STOPAFTERBLOCKIMPORT = false;
static const bool DEFAULT_MNCONFLOCK = true;

std::unique_ptr<CConnman> g_connman;
std::unique_ptr<PeerLogicValidation> peerLogic;
Expand Down Expand Up @@ -197,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 @@ -269,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 @@ -516,19 +510,7 @@ std::string HelpMessage(HelpMessageMode mode)
strUsage += HelpMessageOpt("-shrinkdebugfile", "Shrink debug.log file on client startup (default: 1 when no -debug)");
AppendParamsHelpMessages(strUsage, showDebug);

strUsage += HelpMessageGroup("Masternode options:");
strUsage += HelpMessageOpt("-masternode=<n>", strprintf("Enable the client to act as a masternode (0-1, default: %u)", DEFAULT_MASTERNODE));
strUsage += HelpMessageOpt("-mnconf=<file>", strprintf("Specify masternode configuration file (default: %s)", PIVX_MASTERNODE_CONF_FILENAME));
strUsage += HelpMessageOpt("-mnconflock=<n>", strprintf("Lock masternodes from masternode configuration file (default: %u)", DEFAULT_MNCONFLOCK));
strUsage += HelpMessageOpt("-masternodeprivkey=<n>", "Set the masternode private key");
strUsage += HelpMessageOpt("-masternodeaddr=<n>", strprintf("Set external address:port to get to this masternode (example: %s)", "128.127.106.235:51472"));
strUsage += HelpMessageOpt("-budgetvotemode=<mode>", "Change automatic finalized budget voting behavior. mode=auto: Vote for only exact finalized budget match to my generated budget. (string, default: auto)");
strUsage += HelpMessageOpt("-mnoperatorprivatekey=<WIF>", "Set the masternode operator private key. Only valid with -masternode=1. When set, the masternode acts as a deterministic masternode.");
if (showDebug) {
strUsage += HelpMessageOpt("-pushversion",
strprintf("Modifies the mnauth serialization if the version is lower than %d."
"testnet/regtest only; ", MNAUTH_NODE_VER_VERSION));
}
strUsage += GetTierTwoHelpString(showDebug);

strUsage += HelpMessageGroup("Node relay options:");
if (showDebug) {
Expand Down Expand Up @@ -1447,7 +1429,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 @@ -1476,10 +1457,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 @@ -1550,8 +1528,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 @@ -1740,31 +1717,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)) {
const auto& mnList = deterministicMNManager->GetListAtChainTip();
for (CWallet* pwallet : vpwallets) {
pwallet->ScanMasternodeCollateralsAndLock(mnList);
}
}
#endif

// Start tier two threads and jobs
StartTierTwoThreadsAndScheduleJobs(threadGroup, scheduler);

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

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

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

if (!strErrors.str().empty())
Expand Down
87 changes: 87 additions & 0 deletions src/tiertwo/init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,53 @@
#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>

std::string GetTierTwoHelpString(bool showDebug)
{
std::string strUsage = HelpMessageGroup("Masternode options:");
strUsage += HelpMessageOpt("-masternode=<n>", strprintf("Enable the client to act as a masternode (0-1, default: %u)", DEFAULT_MASTERNODE));
strUsage += HelpMessageOpt("-mnconf=<file>", strprintf("Specify masternode configuration file (default: %s)", PIVX_MASTERNODE_CONF_FILENAME));
strUsage += HelpMessageOpt("-mnconflock=<n>", strprintf("Lock masternodes from masternode configuration file (default: %u)", DEFAULT_MNCONFLOCK));
strUsage += HelpMessageOpt("-masternodeprivkey=<n>", "Set the masternode private key");
strUsage += HelpMessageOpt("-masternodeaddr=<n>", strprintf("Set external address:port to get to this masternode (example: %s)", "128.127.106.235:51472"));
strUsage += HelpMessageOpt("-budgetvotemode=<mode>", "Change automatic finalized budget voting behavior. mode=auto: Vote for only exact finalized budget match to my generated budget. (string, default: auto)");
strUsage += HelpMessageOpt("-mnoperatorprivatekey=<WIF>", "Set the masternode operator private key. Only valid with -masternode=1. When set, the masternode acts as a deterministic masternode.");
if (showDebug) {
strUsage += HelpMessageOpt("-pushversion", strprintf("Modifies the mnauth serialization if the version is lower than %d."
"testnet/regtest only; ", MNAUTH_NODE_VER_VERSION));
strUsage += HelpMessageOpt("-disabledkg", "Disable the DKG sessions process threads for the entire lifecycle. testnet/regtest only.");
}
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 @@ -169,11 +205,62 @@ 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
if (gArgs.GetBoolArg("-disabledkg", false)) {
if (Params().NetworkIDString() == CBaseChainParams::MAIN) {
throw std::runtime_error("DKG system can be disabled only on testnet/regtest");
} else {
LogPrintf("DKG system disabled.\n");
}
} else {
llmq::StartLLMQSystem();
}
}

void StopTierTwoThreads()
{
llmq::StopLLMQSystem();
}

void DeleteTierTwo()
{
llmq::DestroyLLMQSystem();
deterministicMNManager.reset();
evoDb.reset();
}
15 changes: 15 additions & 0 deletions src/tiertwo/init.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,21 @@
#include <string>

static const bool DEFAULT_MASTERNODE = false;
static const bool DEFAULT_MNCONFLOCK = true;

class CScheduler;
namespace boost {
class thread_group;
}

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 @@ -31,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
18 changes: 6 additions & 12 deletions src/wallet/wallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

#include "checkpoints.h"
#include "coincontrol.h"
#include "evo/deterministicmns.h"
#include "evo/providertx.h"
#include "guiinterfaceutil.h"
#include "masternode.h"
#include "policy/policy.h"
Expand Down Expand Up @@ -4121,6 +4121,11 @@ void CWallet::AutoCombineDust(CConnman* connman)
}
}

void CWallet::LockOutpointIfMineWithMutex(const CTransactionRef& ptx, const COutPoint& c)
{
WITH_LOCK(cs_wallet, LockOutpointIfMine(ptx, c));
}

void CWallet::LockOutpointIfMine(const CTransactionRef& ptx, const COutPoint& c)
{
AssertLockHeld(cs_wallet);
Expand All @@ -4140,17 +4145,6 @@ void CWallet::LockOutpointIfMine(const CTransactionRef& ptx, const COutPoint& c)
}
}

// Called during Init
void CWallet::ScanMasternodeCollateralsAndLock(const CDeterministicMNList& mnList)
{
LOCK(cs_wallet);

LogPrintf("Locking masternode collaterals...\n");
mnList.ForEachMN(false, [&](const CDeterministicMNCPtr& dmn) {
LockOutpointIfMine(nullptr, dmn->collateralOutpoint);
});
}

// Called from AddToWalletIfInvolvingMe
void CWallet::LockIfMyCollateral(const CTransactionRef& ptx)
{
Expand Down
9 changes: 3 additions & 6 deletions src/wallet/wallet.h
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,6 @@ class ScriptPubKeyMan;
class SaplingScriptPubKeyMan;
class SaplingNoteData;
struct SaplingNoteEntry;
class CDeterministicMNList;

/** (client) version numbers for particular wallet features */
enum WalletFeature {
Expand Down Expand Up @@ -886,13 +885,11 @@ class CWallet : public CCryptoKeyStore, public CValidationInterface
* -- If ptx is null, c is the output of a transaction in mapWallet
*/
void LockOutpointIfMine(const CTransactionRef& ptx, const COutPoint& c);

/*
* Locks cs_wallet
* Called during Init. If a DMN collateral is found in the wallet,
* lock the corresponding coin, to prevent accidental spending.
* Same functionality as above but locking the cs_wallet mutex internally.
* future: add capability to lock the mutex from outside of this class without exposing it.
*/
void ScanMasternodeCollateralsAndLock(const CDeterministicMNList& mnList);
void LockOutpointIfMineWithMutex(const CTransactionRef& ptx, const COutPoint& c);

/*
* Requires cs_wallet lock.
Expand Down
2 changes: 1 addition & 1 deletion test/functional/p2p_quorum_connect.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def set_test_params(self):
self.minerPos = 0
self.controllerPos = 1
self.setup_clean_chain = True
self.extra_args = [["-nuparams=v5_shield:1", "-nuparams=v6_evo:101"]] * self.num_nodes
self.extra_args = [["-nuparams=v5_shield:1", "-nuparams=v6_evo:101", "-disabledkg"]] * self.num_nodes
self.extra_args[0].append("-sporkkey=932HEevBSujW2ud7RfB1YF91AFygbBRQj3de3LyaCRqNzKKgWXi")

def add_new_dmn(self, mns, strType, op_keys=None, from_out=None):
Expand Down
1 change: 0 additions & 1 deletion test/lint/lint-circular-dependencies.sh
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ EXPECTED_CIRCULAR_DEPENDENCIES=(
"chain -> legacy/stakemodifier -> stakeinput -> chain"
"chain -> legacy/stakemodifier -> validation -> chain"
"chainparamsbase -> util/system -> logging -> chainparamsbase"
"evo/deterministicmns -> masternode -> wallet/wallet -> evo/deterministicmns"
"kernel -> stakeinput -> wallet/wallet -> kernel"
"legacy/validation_zerocoin_legacy -> wallet/wallet -> validation -> legacy/validation_zerocoin_legacy"
"qt/askpassphrasedialog -> qt/pivx/pivxgui -> qt/pivx/topbar -> qt/askpassphrasedialog"
Expand Down