Skip to content

Commit

Permalink
Merge #2684: [Refactor] Move tier two managers initialization from in…
Browse files Browse the repository at this point in the history
…it.cpp to tiertwo/init.cpp

d1fffb5 [Move-only] Move Masternodes thread creation from init.cpp to tiertwo/init.cpp (furszy)
8f7868f Remove completely broken "fLiteMode" (furszy)
c78bcd7 [Move-only] Move active masternode initialization from init.cpp to tiertwo/init.cpp (furszy)
a04bf92 [Move-only] Move tier two managers dump from init.cpp to a separate "tiertwo/init.cpp" (furszy)
77d8dcb [Move-only] Move tier two managers initialization from init.cpp to a separate "tiertwo/init.cpp" (furszy)

Pull request description:

  It's primarily a move-only refactoring, breaking up a bit more the long monolithic `init.cpp`. Placing the tier two components initialization in a separate `tiertwo/init.cpp` file.

  This will make future inclusions easier, like #2647 introducing the new tier two MN metadata manager and the network sync requests manager.

  And, as an extra cleanup, removed the broken `litemode` flag. Because, as is now, it simply makes no sense.. it's a "turn this on to fork your node".

ACKs for top commit:
  random-zebra:
    diff utACK d1fffb5
  Fuzzbawls:
    ACK d1fffb5

Tree-SHA512: ad2edd1dbc7007562fa55e61b73a50552d56aa186180b56cba3598435f607fcde2721fe0f7edb575ab4bc4ac907bcebfdf0e30a74139f99a3427dd790b959275
  • Loading branch information
furszy committed Dec 29, 2021
2 parents 060a0a5 + d1fffb5 commit ab30cd2
Show file tree
Hide file tree
Showing 13 changed files with 206 additions and 134 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,7 @@ set(SERVER_SOURCES
./src/httpserver.cpp
./src/indirectmap.h
./src/init.cpp
./src/tiertwo/init.cpp
./src/interfaces/handler.cpp
./src/interfaces/wallet.cpp
./src/dbwrapper.cpp
Expand Down
4 changes: 4 additions & 0 deletions src/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@ BITCOIN_CORE_H = \
httpserver.h \
indirectmap.h \
init.h \
tiertwo/init.h \
interfaces/handler.h \
interfaces/wallet.h \
invalid.h \
Expand Down Expand Up @@ -358,6 +359,7 @@ libbitcoin_server_a_SOURCES = \
httprpc.cpp \
httpserver.cpp \
init.cpp \
tiertwo/init.cpp \
dbwrapper.cpp \
legacy/validation_zerocoin_legacy.cpp \
sapling/sapling_validation.cpp \
Expand Down Expand Up @@ -737,6 +739,7 @@ CLEANFILES = $(EXTRA_LIBRARIES)

CLEANFILES += *.gcda *.gcno
CLEANFILES += budget/*.gcda budget/*.gcno
CLEANFILES += bls/*.gcda bls/*.gcno
CLEANFILES += compat/*.gcda compat/*.gcno
CLEANFILES += consensus/*.gcda consensus/*.gcno
CLEANFILES += crc32c/src/*.gcda crc32c/src/*.gcno
Expand All @@ -752,6 +755,7 @@ CLEANFILES += sapling/*.gcda sapling/*.gcno
CLEANFILES += script/*.gcda script/*.gcno
CLEANFILES += support/*.gcda support/*.gcno
CLEANFILES += test/librust/*.gcda test/librust/*.gcno
CLEANFILES += tiertwo/*.gcda tiertwo/*.gcno
CLEANFILES += univalue/*.gcda univalue/*.gcno
CLEANFILES += util/*.gcda util/*.gcno
CLEANFILES += wallet/*.gcda wallet/*.gcno
Expand Down
2 changes: 0 additions & 2 deletions src/budget/budgetmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1308,8 +1308,6 @@ bool CBudgetManager::ProcessMessage(CNode* pfrom, std::string& strCommand, CData

int CBudgetManager::ProcessMessageInner(CNode* pfrom, std::string& strCommand, CDataStream& vRecv)
{
// lite mode is not supported
if (fLiteMode) return 0;
if (!masternodeSync.IsBlockchainSynced()) return 0;

if (strCommand == NetMsgType::BUDGETVOTESYNC) {
Expand Down
125 changes: 9 additions & 116 deletions src/init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@
#include "addrman.h"
#include "amount.h"
#include "bls/bls_wrapper.h"
#include "budget/budgetdb.h"
#include "budget/budgetmanager.h"
#include "checkpoints.h"
#include "compat/sanity.h"
#include "consensus/upgrades.h"
Expand All @@ -32,9 +30,7 @@
#include "llmq/quorums_init.h"
#include "key.h"
#include "mapport.h"
#include "masternode-payments.h"
#include "masternodeconfig.h"
#include "masternodeman.h"
#include "miner.h"
#include "netbase.h"
#include "net_processing.h"
Expand All @@ -49,6 +45,7 @@
#include "spork.h"
#include "sporkdb.h"
#include "evo/evodb.h"
#include "tiertwo/init.h"
#include "txdb.h"
#include "torcontrol.h"
#include "guiinterface.h"
Expand Down Expand Up @@ -93,7 +90,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_MASTERNODE = false;
static const bool DEFAULT_MNCONFLOCK = true;

std::unique_ptr<CConnman> g_connman;
Expand Down Expand Up @@ -229,9 +225,7 @@ void Shutdown()
g_connman.reset();
peerLogic.reset();

DumpMasternodes();
DumpBudgets(g_budgetman);
DumpMasternodePayments();
DumpTierTwo();
if (::mempool.IsLoaded() && gArgs.GetBoolArg("-persistmempool", DEFAULT_PERSIST_MEMPOOL)) {
DumpMempool(::mempool);
}
Expand Down Expand Up @@ -522,7 +516,6 @@ std::string HelpMessage(HelpMessageMode mode)
}
strUsage += HelpMessageOpt("-shrinkdebugfile", "Shrink debug.log file on client startup (default: 1 when no -debug)");
AppendParamsHelpMessages(strUsage, showDebug);
strUsage += HelpMessageOpt("-litemode=<n>", strprintf("Disable all PIVX specific functionality (Masternodes, Budgeting) (0-1, default: %u)", 0));

strUsage += HelpMessageGroup("Masternode options:");
strUsage += HelpMessageOpt("-masternode=<n>", strprintf("Enable the client to act as a masternode (0-1, default: %u)", DEFAULT_MASTERNODE));
Expand Down Expand Up @@ -950,19 +943,6 @@ static std::string ResolveErrMsg(const char * const optname, const std::string&
return strprintf(_("Cannot resolve -%s address: '%s'"), optname, strBind);
}

// Sets the last CACHED_BLOCK_HASHES hashes into masternode manager cache
static void LoadBlockHashesCache(CMasternodeMan& man)
{
LOCK(cs_main);
const CBlockIndex* pindex = chainActive.Tip();
unsigned int inserted = 0;
while (pindex && inserted < CACHED_BLOCK_HASHES) {
man.CacheBlockHash(pindex);
pindex = pindex->pprev;
++inserted;
}
}

void InitLogging()
{
g_logger->m_print_to_file = !gArgs.IsArgNegated("-debuglogfile");
Expand Down Expand Up @@ -1748,91 +1728,12 @@ bool AppInitMain()

// ********************************************************* Step 10: setup layer 2 data

uiInterface.InitMessage(_("Loading masternode cache..."));

mnodeman.SetBestHeight(chain_active_height);
LoadBlockHashesCache(mnodeman);
CMasternodeDB mndb;
CMasternodeDB::ReadResult readResult = mndb.Read(mnodeman);
if (readResult == CMasternodeDB::FileError)
LogPrintf("Missing masternode cache file - mncache.dat, will try to recreate\n");
else if (readResult != CMasternodeDB::Ok) {
LogPrintf("Error reading mncache.dat - cached data discarded\n");
}

uiInterface.InitMessage(_("Loading budget cache..."));

CBudgetDB budgetdb;
const bool fDryRun = (chain_active_height <= 0);
if (!fDryRun) g_budgetman.SetBestHeight(chain_active_height);
CBudgetDB::ReadResult readResult2 = budgetdb.Read(g_budgetman, fDryRun);

if (readResult2 == CBudgetDB::FileError)
LogPrintf("Missing budget cache - budget.dat, will try to recreate\n");
else if (readResult2 != CBudgetDB::Ok) {
LogPrintf("Error reading budget.dat - cached data discarded\n");
}

//flag our cached items so we send them to our peers
g_budgetman.ResetSync();
g_budgetman.ReloadMapSeen();

RegisterValidationInterface(&g_budgetman);

uiInterface.InitMessage(_("Loading masternode payment cache..."));

CMasternodePaymentDB mnpayments;
CMasternodePaymentDB::ReadResult readResult3 = mnpayments.Read(masternodePayments);

RegisterValidationInterface(&masternodePayments);
LoadTierTwo(chain_active_height);
if (!InitActiveMN()) return false;
RegisterTierTwoValidationInterface();

if (readResult3 == CMasternodePaymentDB::FileError)
LogPrintf("Missing masternode payment cache - mnpayments.dat, will try to recreate\n");
else if (readResult3 != CMasternodePaymentDB::Ok) {
LogPrintf("Error reading mnpayments.dat - cached data discarded\n");
}

fMasterNode = gArgs.GetBoolArg("-masternode", DEFAULT_MASTERNODE);

if ((fMasterNode || masternodeConfig.getCount() > -1) && fTxIndex == false) {
return UIError(strprintf(_("Enabling Masternode support requires turning on transaction indexing."
"Please add %s to your configuration and start with %s"), "txindex=1", "-reindex"));
}

if (fMasterNode) {
const std::string& mnoperatorkeyStr = gArgs.GetArg("-mnoperatorprivatekey", "");
const bool fDeterministic = !mnoperatorkeyStr.empty();
LogPrintf("IS %sMASTERNODE\n", (fDeterministic ? "DETERMINISTIC " : ""));

if (fDeterministic) {
// Check enforcement
if (!deterministicMNManager->IsDIP3Enforced()) {
const std::string strError = strprintf(_("Cannot start deterministic masternode before enforcement. Remove %s to start as legacy masternode"), "-mnoperatorprivatekey");
LogPrintf("-- ERROR: %s\n", strError);
return UIError(strError);
}
// Create and register activeMasternodeManager
activeMasternodeManager = new CActiveDeterministicMasternodeManager();
auto res = activeMasternodeManager->SetOperatorKey(mnoperatorkeyStr);
if (!res) { return UIError(res.getError()); }
RegisterValidationInterface(activeMasternodeManager);
// Init active masternode
const CBlockIndex* pindexTip = WITH_LOCK(cs_main, return chainActive.Tip(); );
activeMasternodeManager->Init(pindexTip);
} else {
// Check enforcement
if (deterministicMNManager->LegacyMNObsolete()) {
const std::string strError = strprintf(_("Legacy masternode system disabled. Use %s to start as deterministic masternode"), "-mnoperatorprivatekey");
LogPrintf("-- ERROR: %s\n", strError);
return UIError(strError);
}
auto res = initMasternode(gArgs.GetArg("-masternodeprivkey", ""), gArgs.GetArg("-masternodeaddr", ""), true);
if (!res) { return UIError(res.getError()); }
}
}

//get the mode of budget voting for this masternode
g_budgetman.strBudgetMode = gArgs.GetArg("-budgetvotemode", "auto");
// set the mode of budget voting for this node
SetBudgetFinMode(gArgs.GetArg("-budgetvotemode", "auto"));

#ifdef ENABLE_WALLET
// !TODO: remove after complete transition to DMN
Expand All @@ -1859,16 +1760,8 @@ bool AppInitMain()
}
#endif

// lite mode disables all Masternode related functionality
fLiteMode = gArgs.GetBoolArg("-litemode", false);
if (fMasterNode && fLiteMode) {
return UIError(_("You can not start a masternode in litemode"));
}

LogPrintf("fLiteMode %d\n", fLiteMode);
LogPrintf("Budget Mode %s\n", g_budgetman.strBudgetMode.c_str());

threadGroup.create_thread(std::bind(&ThreadCheckMasternodes));
// Start tier two threads and jobs
StartTierTwoThreadsAndScheduleJobs(threadGroup, scheduler);

if (ShutdownRequested()) {
LogPrintf("Shutdown requested. Exiting.\n");
Expand Down
5 changes: 2 additions & 3 deletions src/masternode-payments.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -399,15 +399,14 @@ bool CMasternodePayments::ProcessMessageMasternodePayments(CNode* pfrom, std::st
{
if (!masternodeSync.IsBlockchainSynced()) return true;

if (fLiteMode) return true; //disable all Masternode related functionality

// Skip after legacy obsolete. !TODO: remove when transition to DMN is complete
if (deterministicMNManager->LegacyMNObsolete()) {
LogPrint(BCLog::MASTERNODE, "mnw - skip obsolete message %s\n", strCommand);
return true;
}

if (strCommand == NetMsgType::GETMNWINNERS) { //Masternode Payments Request Sync
if (strCommand == NetMsgType::GETMNWINNERS) {
//Masternode Payments Request Sync
int nCountNeeded;
vRecv >> nCountNeeded;

Expand Down
3 changes: 0 additions & 3 deletions src/masternodeman.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -950,7 +950,6 @@ bool CMasternodeMan::ProcessMessage(CNode* pfrom, std::string& strCommand, CData

int CMasternodeMan::ProcessMessageInner(CNode* pfrom, std::string& strCommand, CDataStream& vRecv)
{
if (fLiteMode) return 0; //disable all Masternode related functionality
if (!masternodeSync.IsBlockchainSynced()) return 0;

// Skip after legacy obsolete. !TODO: remove when transition to DMN is complete
Expand Down Expand Up @@ -1156,8 +1155,6 @@ bool CMasternodeMan::IsWithinDepth(const uint256& nHash, int depth) const

void ThreadCheckMasternodes()
{
if (fLiteMode) return; //disable all Masternode related functionality

// Make this thread recognisable as the wallet flushing thread
util::ThreadRename("pivx-masternodeman");
LogPrintf("Masternodes thread started\n");
Expand Down
2 changes: 0 additions & 2 deletions src/spork.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,6 @@ void CSporkManager::LoadSporksFromDB()

bool CSporkManager::ProcessSpork(CNode* pfrom, std::string& strCommand, CDataStream& vRecv, int& dosScore)
{
if (fLiteMode) return true; // disable all masternode related functionality

if (strCommand == NetMsgType::SPORK) {
dosScore = ProcessSporkMsg(vRecv);
return dosScore == 0;
Expand Down

0 comments on commit ab30cd2

Please sign in to comment.