Skip to content

Commit

Permalink
Merge #2723: Finish moving tier two initialization code from init.cpp…
Browse files Browse the repository at this point in the history
… to tiertwo/init.cpp and add disabledkg init flag

c445333 init: add flag to disable the DKG processes. (furszy)
6035112 init: move tier two objects initializers and the Masternodes collateral transactions' output lock in wallet to the tiertwo/init.cpp file. (furszy)
38b92b9 init: improve dmn collateral locking. (furszy)
918db4e init: move Masternode arguments inside tiertwo/init GetTierTwoHelpString (furszy)

Pull request description:

  Follow-up to #2684, built on top of #2647. Starting in 296e6fa.

  Focused on the following points:

  1) Move init arguments help messages to a new `GetTierTwoHelpString()`, the tier two objects initializers and the Masternodes collateral output locking process to the tiertwo/init files.
  2) Improve DMN collateral locking process:
     * Walking through the DMN list only once instead of one-time per wallet.
     * Removing the wallet dependency on `evo/deterministicmns.h`.
  3) Add `-disabledkg` init argument so the `p2p_quorum_connect.py` functional test does not get affected by the automatic DKG sessions processes (coming in #2722). The same flag will be used in other future tests that perform manual operations as well.

ACKs for top commit:
  random-zebra:
    utACK c445333
  Fuzzbawls:
    ACK c445333

Tree-SHA512: ed879a1b2ce840777bc9144297bfe76aa3184d6b3aede925c6bb2a8f6854a4b87b5acfa734d8f5da128cf5880a8927999f9c5798e17174f97b26f3a392e1b8cb
  • Loading branch information
furszy committed Jan 25, 2022
2 parents f2df53c + c445333 commit 1eaa10c
Show file tree
Hide file tree
Showing 7 changed files with 117 additions and 76 deletions.
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 @@ -91,7 +88,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 @@ -222,7 +218,7 @@ void Shutdown()
StopREST();
StopRPC();
StopHTTPServer();
llmq::StopLLMQSystem();
StopTierTwoThreads();
#ifdef ENABLE_WALLET
for (CWalletRef pwallet : vpwallets) {
pwallet->Flush(false);
Expand Down Expand Up @@ -294,9 +290,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 @@ -541,19 +535,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 @@ -1475,7 +1457,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 @@ -1506,10 +1487,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 @@ -1580,8 +1558,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 @@ -1766,31 +1743,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 @@ -1799,9 +1751,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 @@ -4110,6 +4110,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 @@ -4129,17 +4134,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

0 comments on commit 1eaa10c

Please sign in to comment.